Procházet zdrojové kódy

Fix practice controller

Alan Lam před 5 roky
rodič
revize
9323364f32
2 změnil soubory, kde provedl 42 přidání a 34 odebrání
  1. 39 31
      controllers/practice.php
  2. 3 3
      controllers/test_tester_form.php

+ 39 - 31
controllers/practice.php

@@ -1,4 +1,8 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+
+/**
+ * @var int $id
+ */
 
 Core::addScript("public/js/app-practice-test.js");
 
@@ -34,15 +38,20 @@ if(!$test || $reset) {
 
 }
 
-$is_confirmed = $test->status == Test::STATUS_CONFIRMED;
-$is_started = $test->status == Test::STATUS_STARTED;
-$is_unverified = $test->status == Test::STATUS_PENDING_VERIFICATION;
+$is_confirmed = $test->getStatus() === Test::STATUS_CONFIRMED;
+$is_started = $test->getStatus() === Test::STATUS_STARTED;
+$is_unverified = $test->getStatus() === Test::STATUS_PENDING_VERIFICATION;
+
+$fields = [];
 
 $submit = http::value("submit");
 if($submit) {
 
 	$previous_timezone = date_default_timezone_get();
 
+	$unix_start = null;
+	$unix_end = null;
+
 	try {
 		Core::validateCSRFToken($submit);
 
@@ -60,29 +69,30 @@ if($submit) {
 
 			// Start/End times
 			if($is_started) {
-				$start_override = http::getDateTimeFromFields("start_override");
-				$end_override = http::getDateTimeFromFields("end_override");
-
-				$start_unix = null;
-				$end_unix = null;
+				$start_override = HTTP::value("start_override");
+				$end_override = HTTP::value("end_override");
 
 				if($start_override && $end_override) {
-					$start_unix = strtotime($start_override);
-					$end_unix = strtotime($end_override);
+					$dt_start = new DateTime($start_override, new DateTimeZone(TIMEZONE));
+					$dt_end = new DateTime($start_override, new DateTimeZone(TIMEZONE));
+
+					$unix_start = $dt_start->format("U");
+					$unix_end = $dt_end->format("U");
 
-					if($start_unix > $end_unix) $form_exception->addError(__("Entry Time must not be after Exit Time."));
-					if(!$start_unix && $end_unix && $test->dts_start) {
+					if($unix_start > $unix_end) $form_exception->addError(__("Entry Time must not be after Exit Time."));
+					if(!$unix_start && $unix_end && $test->dts_start) {
 						$recorded_start_unix = Util::getUnixTimeFromUTC($test->dts_start);
-						if($end_unix < $recorded_start_unix) $form_exception->addError(__("Exit Time cannot be later than the recorded Entry Time."));
+						if($unix_end < $recorded_start_unix) $form_exception->addError(__("Exit Time cannot be later than the recorded Entry Time."));
 					}
 				}
 				else {
 					$form_exception->addError(__("Entry Time and Exit Time must be filled in."));
 				}
-
 			}
 		}
 
+		if(empty($fields)) throw new LogicException(__("No fields can be shown."));
+
 		foreach($fields as $field) {
 
 			$name = $field->name ?? false;
@@ -96,23 +106,23 @@ if($submit) {
 			$required = $field->required ?? false;
 			$values = $field->values ?? [];
 
-			if($type == "file") {
+			if($type === "file") {
 				$file = (object) $_FILES[$name];
 
 				try {
-					if(!$required && $file->error == UPLOAD_ERR_NO_FILE) continue;
-					if($required && $file->error == UPLOAD_ERR_NO_FILE) {
-						throw new Exception(sprintf(__("%s is required."), $label));
+					if(!$required && (int) $file->error === UPLOAD_ERR_NO_FILE) continue;
+					if($required && (int) $file->error === UPLOAD_ERR_NO_FILE) {
+						throw new Exception(__("%s is required.", $label));
 					}
 
 					// Detect upload error
-					if($file->error != UPLOAD_ERR_OK) throw new Exception(HTTP::getUploadErrorString($file->error));
+					if((int) $file->error !== UPLOAD_ERR_OK) throw new Exception(HTTP::getUploadErrorString($file->error));
 
 					// Get image details
 					$size = @getimagesize($file->tmp_name);
-					if(!$size) throw new Exception(sprintf(__("The uploaded file for %s is not an image.")), $label);
-					list($width, $height, $type) = $size;
-					if($type != IMAGETYPE_JPEG) throw new Exception(sprintf(__("The uploaded file for %s is not a JPEG."), $label));
+					if(!$size) throw new Exception(__("The uploaded file for %s is not an image.", $label));
+					[$width, $height, $type] = $size;
+					if($type !== IMAGETYPE_JPEG) throw new Exception(__("The uploaded file for %s is not a JPEG.", $label));
 
 					// Resize image
 					$output_image = Image::normalise($file->tmp_name);
@@ -130,10 +140,10 @@ if($submit) {
 			else {
 				$value = http::value($name, "trim");
 
-				if($required && $value === null) $form_exception->addError(sprintf(__("%s is required."), $label));
-				if($values && !in_array($value, $values)) $form_exception->addError(sprintf(__("Value for %s is invalid."), $label));
+				if($required && $value === null) $form_exception->addError(__("%s is required.", $label));
+				if($values && !in_array($value, $values, false)) $form_exception->addError(__("Value for %s is invalid.", $label));
 
-				if(in_array($name, Test::TEST_FIELDS)) {
+				if(in_array($name, Test::TEST_FIELDS, true)) {
 					$test->$name = $value;
 				}
 				else {
@@ -160,11 +170,9 @@ if($submit) {
 		if($is_started || $is_unverified) {
 			$test->status = Test::STATUS_PENDING_VERIFICATION;
 
-			if($is_started) {
-				if($start_unix && $end_unix) {
-					$test->dts_start = date("Y-m-d H:i:s", $start_unix);
-					$test->dts_end = date("Y-m-d H:i:s", $end_unix);
-				}
+			if($is_started && $unix_start && $unix_end) {
+				$test->dts_start = date("Y-m-d H:i:s", $unix_start);
+				$test->dts_end = date("Y-m-d H:i:s", $unix_end);
 			}
 
 			$test->pass = ($test->challenge_stage <= 2) ? 1 : 0; // Pass = Challenged during browsing or before

+ 3 - 3
controllers/test_tester_form.php

@@ -30,6 +30,9 @@ if($submit) {
 
 	$previous_timezone = date_default_timezone_get();
 
+	$unix_start = null;
+	$unix_end = null;
+
 	try {
 		Core::validateCSRFToken($submit);
 
@@ -58,9 +61,6 @@ if($submit) {
 				$start_override = HTTP::value("start_override");
 				$end_override = HTTP::value("end_override");
 
-				$unix_start = null;
-				$unix_end = null;
-
 				if($start_override && $end_override) {
 					$dt_start = new DateTime($start_override, new DateTimeZone(TIMEZONE));
 					$dt_end = new DateTime($start_override, new DateTimeZone(TIMEZONE));