Jelajahi Sumber

Add validation of entry/exit times

Alan Lam 2 tahun lalu
induk
melakukan
6f25168e02

+ 20 - 17
controllers/practice.php

@@ -4,6 +4,8 @@
  * @var int $id
  */
 
+use Carbon\CarbonImmutable;
+
 Core::addScript("public/js/app-practice-test.js");
 Core::addScript("public/js/app-test-requirements.js");
 
@@ -79,18 +81,18 @@ if($submit) {
 				$end_override = HTTP::value("end_override");
 
 				if($start_override && $end_override) {
-					$dt_start = new DateTime($start_override, new DateTimeZone(TIMEZONE));
-					$dt_end = new DateTime($end_override, new DateTimeZone(TIMEZONE));
-
-					$unix_start = $dt_start->format("U");
-					$unix_end = $dt_end->format("U");
+					$dt_start = CarbonImmutable::parse($start_override, TIMEZONE);
+					$dt_end = CarbonImmutable::parse($end_override, TIMEZONE);
 
-					if($unix_start > $unix_end) $form_exception->addError(__("Entry Time must not be after Exit Time."));
-					if($unix_start === $unix_end) $form_exception->addError(__("Entry Time must not equal the Exit Time."));
-					if(!$unix_start && $unix_end && $test->dts_start) {
-						$recorded_start_unix = Util::getUnixTimeFromUTC($test->dts_start);
-						if($unix_end < $recorded_start_unix) $form_exception->addError(__("Exit Time cannot be later than the recorded Entry Time."));
+					if($dt_start > $dt_end) {
+						$form_exception->addError(__("Entry Time must not be after Exit Time."));
+					}
+					if($dt_start === $dt_end) {
+						$form_exception->addError(__("Entry Time must not equal the Exit Time."));
 					}
+
+					$test->dts_start = $dt_start->utc()->toDateTimeString();
+					$test->dts_end = $dt_end->utc()->toDateTimeString();
 				}
 				else {
 					$form_exception->addError(__("Entry Time and Exit Time must be filled in."));
@@ -190,14 +192,20 @@ if($submit) {
 			}
 		}
 
-		if($form_exception->hasErrors()) throw $form_exception;
+		$validate_file = $template->getProcessingDirectoryPath() . "/test_validate.php";
+		if(file_exists($validate_file)) {
+			require $validate_file;
+		}
+
+		if($form_exception->hasErrors()) {
+			throw $form_exception;
+		}
 
 		date_default_timezone_set("UTC");
 
 		// Before the Test
 		if($is_confirmed) {
 			$test->status = Test::STATUS_STARTED;
-			$test->dts_start = Util::getDateTime();
 			$test->dts_updated = Util::getDateTime();
 			$tests[$id] = $test;
 			$_SESSION["practice_tests"] = $tests;
@@ -208,11 +216,6 @@ if($submit) {
 		if($is_started || $is_unverified) {
 			$test->status = Test::STATUS_PENDING_VERIFICATION;
 
-			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
 			$test->dts_updated = Util::getDateTime();
 

+ 24 - 32
controllers/test_tester_form.php

@@ -10,6 +10,8 @@
  * @var string[] $template_keys
  */
 
+use Carbon\CarbonImmutable;
+
 Core::$show_footer_content = false;
 
 if((int) Core::$user->getId() !== $test->getTesterId()) throw new Exception(__("You do not have permission to view this page."));
@@ -30,13 +32,10 @@ if($submit) {
 
 	$previous_timezone = date_default_timezone_get();
 
-	$unix_start = null;
-	$unix_end = null;
-
 	try {
 		Core::validateCSRFToken($submit);
 
-		$form_exception = new FormException;
+		$form_exception = new FormException();
 
 		// Parse before the test form
 		if($test->isConfirmed()) {
@@ -56,18 +55,18 @@ if($submit) {
 				$end_override = HTTP::value("end_override");
 
 				if($start_override && $end_override) {
-					$dt_start = new DateTime($start_override, new DateTimeZone(TIMEZONE));
-					$dt_end = new DateTime($end_override, new DateTimeZone(TIMEZONE));
+					$dt_start = CarbonImmutable::parse($start_override, TIMEZONE);
+					$dt_end = CarbonImmutable::parse($end_override, TIMEZONE);
 
-					$unix_start = $dt_start->format("U");
-					$unix_end = $dt_end->format("U");
-
-					if($unix_start > $unix_end) $form_exception->addError(__("Entry Time must not be after Exit Time."));
-					if($unix_start === $unix_end) $form_exception->addError(__("Entry Time must not equal the Exit Time."));
-					if(!$unix_start && $unix_end && $test->dts_start) {
-						$recorded_start_unix = Util::getUnixTimeFromUTC($test->dts_start);
-						if($unix_end < $recorded_start_unix) $form_exception->addError(__("Exit Time cannot be later than the recorded Entry Time."));
+					if($dt_start > $dt_end) {
+						$form_exception->addError(__("Entry Time must not be after Exit Time."));
+					}
+					if($dt_start === $dt_end) {
+						$form_exception->addError(__("Entry Time must not equal the Exit Time."));
 					}
+
+					$test->dts_start = $dt_start->utc()->toDateTimeString();
+					$test->dts_end = $dt_end->utc()->toDateTimeString();
 				}
 				else {
 					$form_exception->addError(__("Entry Time and Exit Time must be filled in."));
@@ -75,7 +74,9 @@ if($submit) {
 			}
 		}
 
-		if(empty($fields)) throw new LogicException(__("No fields can be shown."));
+		if(empty($fields)) {
+			throw new LogicException(__("No fields can be shown."));
+		}
 
 		foreach($fields as $field) {
 			$name = $field->name ?? false;
@@ -164,7 +165,14 @@ if($submit) {
 			}
 		}
 
-		if($form_exception->hasErrors()) throw $form_exception;
+		$validate_file = $template->getProcessingDirectoryPath() . "/test_validate.php";
+		if(file_exists($validate_file)) {
+			require $validate_file;
+		}
+
+		if($form_exception->hasErrors()) {
+			throw $form_exception;
+		}
 
 		date_default_timezone_set("UTC");
 
@@ -187,22 +195,6 @@ if($submit) {
 
 			$test->status = Test::STATUS_PENDING_VERIFICATION;
 
-			if($has_started) {
-
-				/**
-				 * @var int $unix_start
-				 * @var int $unix_end
-				 */
-
-				if($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);
-				}
-				else if($unix_end) {
-					$test->dts_end = date("Y-m-d H:i:s", $unix_end);
-				}
-			}
-
 			$test->pass = 1;
 			if($template->hasPassFail()) {
 				$test->pass = ($test->challenge_stage <= $template->getPassStage()) ? 1 : 0; // Pass = Challenged during browsing or before

+ 14 - 4
engine/template.php

@@ -101,21 +101,31 @@ class Template extends Model {
 		return Customer::get($this->customer_id);
 	}
 
+	public function getProcessingDirectoryPath(): string {
+		return ROOT_DIR . "/template_processing/" . $this->folder;
+	}
+
 	/**
 	 * @param $file
 	 * @return string
 	 * @throws Exception
 	 */
 	public function getProcessingFilePath($file): string {
-		if(!$this->folder) throw new Exception("Template processing folder not set.");
+		if(!$this->folder) {
+			throw new RuntimeException("Template processing folder not set.");
+		}
 
-		$folder_path = ROOT_DIR . "/template_processing/" . $this->folder;
-		if(!file_exists($folder_path)) throw new Exception("Template processing folder does not exist.");
+		$folder_path = $this->getProcessingDirectoryPath();
+		if(!file_exists($folder_path)) {
+			throw new RuntimeException("Template processing folder does not exist.");
+		}
 
 		$file_path = $folder_path . "/" . $file;
 		if(!file_exists($file_path)) {
 			$file_path = ROOT_DIR . "/template_processing/_common/{$file}";
-			if(!file_exists($file_path)) throw new Exception("Template processing file \"$file\" does not exist.");
+			if(!file_exists($file_path)) {
+				throw new RuntimeException("Template processing file \"$file\" does not exist.");
+			}
 		}
 
 		return $file_path;

+ 36 - 0
template_processing/age_verification_greene_king/test_validate.php

@@ -0,0 +1,36 @@
+<?php
+
+/**
+ * @var Test $test
+ * @var FormException $form_exception
+ */
+
+if($test->dts_start && $test->dts_end) {
+	$dt_start = $test->getDateTimeStart();
+	$dt_end = $test->getDateTimeEnd();
+
+	$duration = $dt_end->diffInMinutes($dt_start);
+
+	$values = $test->getValues(true);
+
+	$line_of_sight = (bool) ($values["play_machine_line_of_sight"] ?? false);
+	$play_minutes = (int) ($values["play_minutes"] ?? 0);
+
+	// Calculate minimum duration
+	$min_duration = 0;
+
+	if($test->challenge_stage >= 2) {
+		if($line_of_sight) {
+			$min_duration += 1;
+		}
+		else {
+			$min_duration += 2;
+		}
+	}
+
+	$min_duration += $play_minutes;
+
+	if($duration < $min_duration) {
+		$form_exception->addError("Please check the entry and exit times. Based on the information you have given, the minimum test duration is $min_duration minute(s).");
+	}
+}