tests_admin_list.php 4.8 KB

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