tests_admin_list.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var string $page_title
  4. */
  5. // Batch Action
  6. $submit = http::value("submit");
  7. if($submit) {
  8. try {
  9. Core::validateCSRFToken($submit);
  10. $test_ids = http::value("test_ids", "int");
  11. $batch_action = http::value("batch_action");
  12. $tests = Test::load($test_ids);
  13. $test_count = count($tests);
  14. switch($batch_action) {
  15. case "unassign":
  16. $test_unassigned_count = 0;
  17. foreach($tests as $test) {
  18. $success = $test->unassignTester();
  19. $test_unassigned_count += $success;
  20. }
  21. Core::addAlert("success", "$test_unassigned_count of $test_count test(s) unassigned successfully.");
  22. break;
  23. case "mark_tester_invoice_paid":
  24. $actioned_count = 0;
  25. foreach($tests as $test) {
  26. $actioned_count += ($test->markTesterInvoicePaid()) ? 1 : 0;
  27. }
  28. Core::addAlert("success", "{$actioned_count} of {$test_count} test(s) marked as paid successfully.");
  29. break;
  30. case "reject_tester_invoice":
  31. $actioned_count = 0;
  32. foreach($tests as $test) {
  33. $actioned_count += ($test->rejectTesterInvoice()) ? 1 : 0;
  34. }
  35. Core::addAlert("success", "{$actioned_count} of {$test_count} invoices rejected successfully.");
  36. break;
  37. default:
  38. throw new Exception("Invalid action.");
  39. }
  40. HTTP::redirect($_SERVER["REQUEST_URI"]);
  41. }
  42. catch(Exception $e) {
  43. Core::addAlert("danger", $e->getMessage());
  44. }
  45. }
  46. // Batch Action End
  47. $status = http::value("status", "int");
  48. $status_tester_invoice = http::value("status_tester_invoice");
  49. $customer_id = http::value("customer_id", "int");
  50. $project_id = http::value("project_id", "int");
  51. $location_id = http::value("location_id", "int");
  52. $test_id = http::value("test_id", "int");
  53. $tester_id = http::value("tester_id", "int");
  54. $location_internal_id = http::value("location_internal_id", "int");
  55. $pass = http::value("pass", "int");
  56. $custom = http::value("custom");
  57. $filter_count_offset = 0;
  58. $nvp = [];
  59. // Status
  60. if(strlen($status) > 0) $nvp["status"] = $status;
  61. else {
  62. $nvp["status"] = (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED);
  63. $filter_count_offset--;
  64. }
  65. // Status Tester Invoice
  66. if(strlen($status_tester_invoice)) {
  67. $status_tester_invoice = (int) $status_tester_invoice;
  68. $nvp["status_tester_invoice"] = $status_tester_invoice;
  69. }
  70. // Project
  71. if($project_id) $nvp["project_id"] = $project_id;
  72. else if($customer_id) {
  73. $projects = Project::query(["customer_id" => $customer_id]);
  74. $nvp["project_id"] = array_column($projects, "id");
  75. }
  76. // Location
  77. if($location_id) $nvp["location_id"] = $location_id;
  78. else if($location_internal_id) {
  79. $locations = Location::query(["internal_id" => $location_internal_id]);
  80. if($locations) {
  81. $nvp["location_id"] = array_column($locations, "id");
  82. }
  83. }
  84. // Test ID
  85. if($test_id) $nvp["id"] = $test_id;
  86. // Tester ID
  87. if($tester_id) $nvp["tester_id"] = $tester_id;
  88. // Pass
  89. if(strlen($pass) > 0) $nvp["pass"] = $pass;
  90. $filter_count = count($nvp);
  91. $sort = http::value("sort");
  92. switch($sort) {
  93. case "updated":
  94. $query_options = ["sort" => "dts_updated", "order" => "desc"];
  95. break;
  96. case "due":
  97. $query_options = ["sort" => "dts_due", "order" => "asc"];
  98. break;
  99. default:
  100. $sort = "status";
  101. $query_options = ["sort" => "status", "order" => "asc"];
  102. break;
  103. }
  104. $tests = Test::query($nvp, $query_options);
  105. $projects = Project::load(array_column($tests, "project_id"));
  106. switch($custom) {
  107. case "overdue_project":
  108. $tests = array_filter($tests, function($test) {
  109. if($test->status == Test::STATUS_CANCELLED) return false;
  110. $project = $test->getProject();
  111. $unix_deadline = Util::getUnixTimeFromUTC($project->dts_deadline);
  112. return ($test->status != Test::STATUS_COMPLETED && time() > $unix_deadline);
  113. });
  114. $filter_count++;
  115. break;
  116. case "overdue_test":
  117. $tests = array_filter($tests, function($test) {
  118. if($test->status == Test::STATUS_CANCELLED) return false;
  119. if(is_null($test->dts_due)) return false;
  120. $unix_due = Util::getUnixTimeFromUTC($test->dts_due);
  121. return ($test->status < Test::STATUS_STARTED && time() > $unix_due);
  122. });
  123. $filter_count++;
  124. break;
  125. }
  126. $filter_count += $filter_count_offset;
  127. if(!$filter_count) $filter_count = "";
  128. $pagination_object = Util::getPaginationObject($tests);
  129. $tests = array_slice($tests, $pagination_object->offset, $pagination_object->pageSize);
  130. $testers = User::load(array_column($tests, "tester_id"));
  131. $customers = Customer::query([], ["sort" => "name"]);
  132. usort($projects, function($a, $b) {
  133. return $a->name <=> $b->name;
  134. });
  135. $sorts = [
  136. "status" => "Status",
  137. "updated" => "Update",
  138. "due" => "Due",
  139. ];
  140. Core::addScript("public/js/app-test-list.js");
  141. View::includeOnce("header", compact("page_title"));
  142. 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"));