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

Add "Min. Overdue Days" filter to admin test list

Alan Lam 4 лет назад
Родитель
Сommit
c61590c2be
4 измененных файлов с 128 добавлено и 28 удалено
  1. 53 17
      controllers/tests_admin_list.php
  2. 13 0
      engine/project.php
  3. 29 0
      engine/test.php
  4. 33 11
      views/test_list.php

+ 53 - 17
controllers/tests_admin_list.php

@@ -53,12 +53,10 @@ if($submit) {
 				break;
 
 			default:
-				throw new Exception("Invalid action.");
+				throw new RuntimeException("Invalid action.");
 		}
 
 		HTTP::redirect($_SERVER["REQUEST_URI"]);
-
-
 	}
 	catch(Exception $e) {
 		Core::addAlert("danger", $e->getMessage());
@@ -75,6 +73,7 @@ $test_id = http::value("test_id", "int");
 $tester_id = http::value("tester_id", "int");
 $location_internal_id = http::value("location_internal_id", "int");
 $pass = http::value("pass", "int");
+$min_days_overdue = http::value("min_days_overdue", "int");
 $custom = http::value("custom");
 
 $filter_count_offset = 0;
@@ -84,7 +83,7 @@ $nvp = [];
 // Status
 if(strlen($status) > 0) $nvp["status"] = $status;
 else {
-	$nvp["status"] = (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED);
+	$nvp["status"] = Constraint::notEquals(Test::STATUS_CANCELLED);
 	$filter_count_offset--;
 }
 
@@ -140,26 +139,63 @@ $projects = Project::load(array_column($tests, "project_id"));
 
 switch($custom) {
 	case "overdue_project":
-		$tests = array_filter($tests, function($test) {
-			if($test->status == Test::STATUS_CANCELLED) return false;
+		$tests = array_filter($tests, static function(Test $test) use ($min_days_overdue) {
+			if($test->isCancelled()) {
+				return false;
+			}
+
+			if($test->isCompleted()) {
+				return false;
+			}
+
 			$project = $test->getProject();
 
-			$unix_deadline = Util::getUnixTimeFromUTC($project->dts_deadline);
+			$dt_deadline = $project->getDateTimeDeadline();
+			if(!$dt_deadline) {
+				return false;
+			}
+
+			if(!$project->isOverdue()) {
+				return false;
+			}
 
-			return ($test->status != Test::STATUS_COMPLETED && time() > $unix_deadline);
+			$days_overdue = $dt_deadline->diffInDays(null, true);
+			if($min_days_overdue > 0 && $days_overdue > $min_days_overdue) {
+				return false;
+			}
+
+			return true;
 		});
 
 		$filter_count++;
 
 		break;
-	case "overdue_test":
-		$tests = array_filter($tests, function($test) {
-			if($test->status == Test::STATUS_CANCELLED) return false;
-			if(is_null($test->dts_due)) return false;
 
-			$unix_due = Util::getUnixTimeFromUTC($test->dts_due);
-
-			return ($test->status < Test::STATUS_STARTED && time() > $unix_due);
+	case "overdue_test":
+		$tests = array_filter($tests, static function(Test $test) use ($min_days_overdue) {
+			if($test->isCancelled()) {
+				return false;
+			}
+
+			if($test->hasDueDateTime() === false) {
+				return false;
+			}
+
+			$dt_due = $test->getDateTimeDue();
+			if(!$dt_due) {
+				return false;
+			}
+
+			if(!($test->hasNotStarted() && $test->isOverdue())) {
+				return false;
+			}
+
+			$days_overdue = $dt_due->diffInDays(null, true);
+			if($min_days_overdue > 0 && $days_overdue < $min_days_overdue) {
+				return false;
+			}
+
+			return true;
 		});
 
 		$filter_count++;
@@ -176,7 +212,7 @@ $tests = array_slice($tests, $pagination_object->offset, $pagination_object->pag
 $testers = User::load(array_column($tests, "tester_id"));
 $customers = Customer::query([], ["sort" => "name"]);
 
-usort($projects, function($a, $b) {
+usort($projects, static function(Project $a, Project $b): int {
 	return $a->name <=> $b->name;
 });
 
@@ -189,4 +225,4 @@ $sorts = [
 Core::addScript("public/js/app-test-list.js");
 
 View::includeOnce("header", compact("page_title"));
-View::include("test_list", compact("tests", "pagination_object", "sort", "sorts", "projects", "customers", "customer_id", "project_id", "location_id", "location_internal_id", "pass", "custom", "test_id", "tester_id", "filter_count", "status", "status_tester_invoice"));
+View::include("test_list", compact("tests", "pagination_object", "sort", "sorts", "projects", "customers", "customer_id", "project_id", "location_id", "location_internal_id", "pass", "min_days_overdue", "custom", "test_id", "tester_id", "filter_count", "status", "status_tester_invoice"));

+ 13 - 0
engine/project.php

@@ -579,4 +579,17 @@ class Project extends Model {
 		]);
 	}
 
+	public function getTimeDeadline(): int {
+		if($this->dts_deadline === null) {
+			return 0;
+		}
+
+		return $this->getDateTimeDeadline()->getTimestamp();
+	}
+
+	public function isOverdue(): bool {
+		return time() > $this->getTimeDeadline();
+	}
+
+
 }

+ 29 - 0
engine/test.php

@@ -1,6 +1,7 @@
 <?php /** @noinspection HtmlDeprecatedTag */
 /** @noinspection HtmlDeprecatedAttribute */
 
+use Carbon\CarbonImmutable;
 use Mpdf\Mpdf;
 
 /**
@@ -828,4 +829,32 @@ class Test extends Model {
 		return $this->getStatus() === self::STATUS_CANCELLED;
 	}
 
+	public function hasDueDateTime(): bool {
+		return $this->dts_due !== null;
+	}
+
+	public function getTimeDue(): int {
+		if(!$this->dts_due) {
+			return 0;
+		}
+
+		return $this->getDateTimeDue()->getTimestamp();
+	}
+
+	public function hasNotStarted(): bool {
+		return $this->status < self::STATUS_STARTED;
+	}
+
+	public function isOverdue(): bool {
+		return time() > $this->getTimeDue();
+	}
+
+	public function getDateTimeDue(): ?CarbonImmutable {
+		if(!$this->dts_due) {
+			return null;
+		}
+
+		return CarbonImmutable::parse($this->dts_due, "UTC");
+	}
+
 }

+ 33 - 11
views/test_list.php

@@ -15,6 +15,7 @@
  * @var int $tester_id
  * @var int $location_id
  * @var int $location_internal_id
+ * @var int $min_days_overdue
  * @var int $custom
  * @var object $pagination_object
  */
@@ -208,6 +209,14 @@ $custom_filters = [
 									<input class="form-control" type="text" name="location_internal_id" id="location-internal-id" placeholder="Enter location internal ID" value="<?=$location_internal_id?>" />
 								</div>
 							</div>
+							<div class="row mb-1">
+								<label class="col-sm-4 col-form-label" for="min-days-overdue">
+									<?=htmlspecialchars(__("Min. Days Overdue"))?>
+								</label>
+								<div class="col-sm">
+									<input class="form-control" type="number" name="min_days_overdue" id="min-days-overdue" placeholder="<?=htmlspecialchars(__("Enter days"))?>" value="<?=$min_days_overdue?>" />
+								</div>
+							</div>
 							<div class="row mb-1">
 								<label class="col-sm-4 col-form-label" for="custom">Custom Filter</label>
 								<div class="col-sm">
@@ -296,6 +305,12 @@ $custom_filters = [
 
 							$unix_start = strtotime($test->dts_start . " UTC");
 							$unix_end = strtotime($test->dts_end . " UTC");
+
+							$dt_due = $test->getDateTimeDue();
+							$is_overdue = $test->isOverdue();
+							$due_class = $is_overdue ? "text-danger" : "";
+							$days_overdue = $dt_due->diffInDays();
+
 							?>
 
 							<tr>
@@ -347,19 +362,26 @@ $custom_filters = [
 								}
 								?>
 								</td>
-								<td class="text-center align-middle">
-								<?php
-								if(is_null($test->dts_due)) {
-									?>
-									<small class="text-muted">N/A</small>
+								<td class="align-middle <?=$due_class?>">
 									<?php
-								}
-								else {
+									if($dt_due) {
+										?>
+										<strong><?=$dt_due->format("Y-m-d")?></strong>
+										<?php
+										if($is_overdue) {
+											?><br />
+											<small>
+												<?=htmlspecialchars(n__("%d day overdue", "%d days overdue", $days_overdue, $days_overdue))?>
+											</small>
+											<?php
+										}
+									}
+									else {
+										?>
+										<small class="text-muted">N/A</small>
+										<?php
+									}
 									?>
-									<?=explode(" ", $test->dts_due)[0]?>
-									<?php
-								}
-								?>
 								</td>
 								<td class="text-center align-middle">
 									<span class="badge badge-<?=$test->getStatusClass()?>"><?=$test->getStatusText()?></span>