Przeglądaj źródła

Add latest test rules to locations page

Alan Lam 3 lat temu
rodzic
commit
4ec3a8eda4

+ 4 - 2
controllers/locations.php

@@ -139,7 +139,7 @@ $location_ids_csv = implode(", ", $location_ids);
 $sql = "SELECT `location`.*, COUNT(`test`.`id`) AS `test_count`, SUM(IF(`test`.`retest` = 1, 1, 0)) AS `retest_count` 
 	FROM `location` 
     LEFT JOIN `test` ON `test`.`location_id`=`location`.`id` AND `dts_start` BETWEEN '$date_start' AND '$date_end' 
-	WHERE `customer_id`='$customer->id' AND `location`.`id` IN ($location_ids_csv) GROUP BY `location`.`id`
+	WHERE `location`.`id` IN ($location_ids_csv) GROUP BY `location`.`id`
 	ORDER BY $query_sort $order
 ";
 
@@ -169,7 +169,9 @@ if(!empty($action)) {
 	}
 }
 
+$latest_tests = $search->getLatestTests();
+
 View::includeOnce("header", compact("page_title"));
-View::include("locations", compact("customer", "include_archived", "include_closed", "hide_tested", "filter_count", "sorts", "sort", "locations"));
+View::include("locations", compact("customer", "include_archived", "include_closed", "hide_tested", "filter_count", "sorts", "sort", "locations", "latest_tests"));
 View::include("modal_add_locations_to_project", compact("customer"));
 View::includeOnce("footer");

+ 28 - 5
engine/searches/location_search.php

@@ -21,6 +21,9 @@ class LocationSearch {
 	private ?string $minTestDate = null;
 	private ?string $maxTestDate = null;
 	private bool $includeTested = true;
+	private ?string $filter = null;
+
+	private array $latestTests = [];
 
 	public function __construct(Customer $customer) {
 		$this->customer = $customer;
@@ -106,6 +109,14 @@ class LocationSearch {
 		$this->includeTested = $includeTested;
 	}
 
+	public function getFilter(): ?string {
+		return $this->filter;
+	}
+
+	public function setFilter(?string $filter): void {
+		$this->filter = $filter;
+	}
+
 	/**
 	 * @return Location[]
 	 * @throws \Exception
@@ -127,11 +138,13 @@ class LocationSearch {
 
 		$locations = Location::query($nvp, ["sort" => "name", "order" => "asc"]);
 
+		$base_test_nvp = [
+			"status" => Constraint::notEquals(Test::STATUS_CANCELLED),
+			"location_id" => array_column($locations, "id")
+		];
+
 		if(!$this->includeTested) {
-			$test_nvp = [
-				"status" => Constraint::notEquals(Test::STATUS_CANCELLED),
-				"location_id" => array_column($locations, "id")
-			];
+			$test_nvp = $base_test_nvp;
 
 			if($this->minTestDate && $this->maxTestDate) {
 				$test_nvp["dts_start"] = Constraint::between($this->minTestDate, $this->maxTestDate);
@@ -142,18 +155,28 @@ class LocationSearch {
 			}
 
 			$tests = Test::query($test_nvp);
-
 			$location_test_flags = [];
 			foreach($tests as $test) {
 				$location_test_flags[$test->location_id] = true;
 			}
+			unset($tests);
 
 			$locations = array_filter($locations, static function(Location $location) use ($location_test_flags) {
 				return !isset($location_test_flags[$location->id]);
 			});
 		}
 
+		$all_tests = Test::query($base_test_nvp, ["sort" => "dts_start", "order" => "asc"]);
+
+		foreach($all_tests as $test) {
+			$this->latestTests[$test->location_id] = $test;
+		}
+
 		return $locations;
 	}
 
+	public function getLatestTests(): array {
+		return $this->latestTests;
+	}
+
 }

+ 16 - 0
engine/user.php

@@ -800,4 +800,20 @@ class User extends Model {
 		return $test ? $test->getDateTimeStart() : null;
 	}
 
+	public function formatDateTime(?DateTimeInterface $dt): string {
+		if(!$dt) {
+			return "N/A";
+		}
+		$carbon = CarbonImmutable::parse($dt);
+		return $carbon->setTimezone(TIMEZONE)->format("d/m/Y H:i");
+	}
+
+	public function formatDate(?DateTimeInterface $dt): string {
+		if(!$dt) {
+			return "(null)";
+		}
+		$carbon = CarbonImmutable::parse($dt);
+		return $carbon->setTimezone(TIMEZONE)->format("d/m/Y");
+	}
+
 }

+ 44 - 2
views/location_list.php

@@ -1,11 +1,24 @@
 <?php
 /**
  * @var Location[] $locations
+ * @var Test[] $latest_tests
  */
 
+$user = Core::$user;
+
 ?>
 <div class="table-responsive limit-height-2">
 	<table class="table table-sm table-sm-text table-hover">
+		<colgroup>
+			<col style="width: 1%"/>
+			<col style="width: 30%"/>
+			<col style="width: 30%"/>
+			<col style="width: 10%"/>
+			<col style="width: 5%"/>
+			<col style="width: 5%"/>
+			<col style="width: 20%"/>
+			<col style="width: 1%"/>
+		</colgroup>
 		<thead>
 			<tr>
 				<th class="text-right pr-3">ID</th>
@@ -14,7 +27,8 @@
 				<th>Internal ID</th>
 				<th>Tests</th>
 				<th>Retests</th>
-				<th></th>
+				<th>Latest Test Result</th>
+				<th>Actions</th>
 			</tr>
 		</thead>
 		<tbody>
@@ -23,6 +37,8 @@
 			$class = "";
 			if($location->isArchived()) $class = "text-muted";
 
+			$test = $latest_tests[$location->id] ?? null;
+
 			?>
 			<tr class="<?=$class?>">
 				<td class="text-right pr-3">
@@ -57,7 +73,33 @@
 				<td class="text-right">
 					<?=htmlspecialchars(number_format($location->retest_count))?>
 				</td>
-				<td class="text-right align-middle">
+				<td>
+					<?php
+					if($test) {
+						if($test->isPass()) {
+							?>
+							<i class="fa fa-fw fa-check-circle text-success"></i> Passed on <?=$user->formatDate($test->getDateTimeStart())?>
+							<?php
+						}
+						elseif($test->isVoid()) {
+							?>
+							<i class="fa fa-fw fa-minus-circle text-secondary"></i> Void on <?=$user->formatDate($test->getDateTimeStart())?>
+							<?php
+						}
+						else {
+							?>
+							<i class="fa fa-fw fa-times-circle text-danger"></i> Failed on <?=$user->formatDate($test->getDateTimeStart())?>
+							<?php
+						}
+					}
+					else {
+						?>
+						None
+						<?php
+					}
+					?>
+				</td>
+				<td class="text-right text-nowrap align-middle">
 					<a class="btn btn-sm btn-outline-primary" href="/account/locations/<?=$location->id?>/edit">Edit</a>
 					<button type="button" class="btn btn-sm btn-outline-primary btn-map" data-location-id="<?=$location->id?>">Map</button>
 				</td>

+ 2 - 1
views/locations.php

@@ -9,6 +9,7 @@
  * @var string[] $sorts
  * @var string $sort
  * @var Location[] $locations
+ * @var Test[] $latest_tests
  */
 
 $query = $_GET;
@@ -216,7 +217,7 @@ var customerId = <?=$customer->id?>;
 	</div>
 
 	<?php
-	View::include("location_list", compact("customer", "locations"));
+	View::include("location_list", compact("customer", "locations", "latest_tests"));
 	?>
 </div>