Просмотр исходного кода

Record and add links to next and previous tests

Alan Lam 2 лет назад
Родитель
Сommit
75a2eb8f70

+ 1 - 0
controllers/test_verify_form.php

@@ -182,6 +182,7 @@ if($submit) {
 			$new_test = new Test();
 			$new_test->project_id = $new_test_project->id;
 			$new_test->location_id = $location->id;
+			$new_test->previous_test_id = $test->id;
 			$new_test->status = Test::STATUS_UNASSIGNED;
 			$new_test->dts_created = Util::getDateTime();
 			if($new_test_is_retest) {

+ 12 - 0
db/migrations/20241030193414_add_previous_test_id_field_to_test_table.php

@@ -0,0 +1,12 @@
+<?php
+
+use Phinx\Migration\AbstractMigration;
+
+class AddPreviousTestIdFieldToTestTable extends AbstractMigration {
+	public function change() {
+		$this->table("test")
+			->addColumn("previous_test_id", "integer", ["signed" => false, "null" => true, "after" => "tester_id"])
+			->addForeignKey("previous_test_id", "test", "id", ["update" => "CASCADE", "delete" => "SET NULL"])
+			->update();
+	}
+}

+ 6 - 0
engine/constraint.php

@@ -71,6 +71,12 @@ class Constraint {
 		return $constraint;
 	}
 
+	public static function moreThan(string $value): Constraint {
+		$constraint = new Constraint("MORE_THAN");
+		$constraint->setValues($value);
+		return $constraint;
+	}
+
 	/**
 	 * Constraint constructor.
 	 * @param string $type

+ 35 - 1
engine/test.php

@@ -9,6 +9,7 @@ use Mpdf\Mpdf;
  * @property int $project_id
  * @property int $location_id
  * @property int $tester_id
+ * @property int $previous_test_id
  * @property int $stripe_invoice_id
  * @property int $status
  * @property int $status_tester_invoice
@@ -631,9 +632,16 @@ class Test extends Model {
 	 * @throws Exception
 	 */
 	public function getPreviousTest(): ?Test {
+		if($this->previous_test_id) {
+			return self::get($this->previous_test_id);
+		}
+
+		if($this->dts_start === null) {
+			return null;
+		}
 
 		$nvp = [
-			"dts_start" => (new Constraint("LESS_THAN"))->setValues($this->dts_start),
+			"dts_start" => Constraint::lessThan($this->dts_start),
 			"location_id" => $this->location_id,
 			"status" => self::STATUS_COMPLETED,
 		];
@@ -644,7 +652,33 @@ class Test extends Model {
 		]);
 
 		return $tests ? $tests[0] : null;
+	}
+
+	/**
+	 * @return Test|null
+	 * @throws Exception
+	 */
+	public function getNextTest(): ?Test {
+		$test = self::queryOne(["previous_test_id" => $this->id]);
+		if($test) {
+			return $test;
+		}
 
+		if($this->dts_start === null) {
+			return null;
+		}
+
+		$nvp = [
+			"dts_start" => Constraint::moreThan($this->dts),
+			"location_id" => $this->location_id,
+		];
+
+		$tests = self::query($nvp, [
+			"sort" => "dts_start",
+			"order" => "asc",
+		]);
+
+		return $tests ? $tests[0] : null;
 	}
 
 	/**

+ 14 - 1
views/test_verify_form.php

@@ -17,10 +17,12 @@
 use Carbon\CarbonImmutable;
 use Helpers\StripeHelper;
 
+$user = Core::$user;
+
 $config = $template->getConfiguration();
 $values = $test->getValues(true);
 
-$get_test_details = static function(Test $test) use ($project, $location): array {
+$get_test_details = static function(Test $test) use ($project, $location, $user): array {
 	$test_details = [];
 
 	$test_details["Project"] = sprintf('<a href="/account/projects/%d">%s</a>', $test->project_id, $project->getNameHtml());
@@ -44,6 +46,17 @@ $get_test_details = static function(Test $test) use ($project, $location): array
 		$test_details["Stripe Invoice"] = sprintf("<a href=\"%s\" target=\"_blank\">View</a>", htmlspecialchars($stripe_invoice->hosted_invoice_url));
 	}
 
+	$previous_test = $test->getPreviousTest();
+	$next_test = $test->getNextTest();
+
+	if($previous_test) {
+		$test_details["Previous Test"] = sprintf('<a href="/account/tests/%d">%s</a>', $previous_test->id, $previous_test->getProject()->getNameHtml());
+	}
+
+	if($next_test) {
+		$test_details["Next Test"] = sprintf('<a href="/account/tests/%d">%s</a>', $next_test->id, $next_test->getProject()->getNameHtml());
+	}
+
 	if($test->dts_end) {
 		$test_details = array_merge($test_details, [
 			"Duration" => $test->getDisplayDuration(),