1
0

tests_admin_list.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. default:
  28. throw new Exception("Invalid action.");
  29. }
  30. HTTP::redirect($_SERVER["REQUEST_URI"]);
  31. }
  32. catch(Exception $e) {
  33. Core::addAlert("danger", $e->getMessage());
  34. }
  35. }
  36. // Batch Action End
  37. $status = http::value("status", "int");
  38. $status_tester_invoice = http::value("status_tester_invoice");
  39. $customer_id = http::value("customer_id", "int");
  40. $project_id = http::value("project_id", "int");
  41. $location_id = http::value("location_id", "int");
  42. $test_id = http::value("test_id", "int");
  43. $tester_id = http::value("tester_id", "int");
  44. $location_internal_id = http::value("location_internal_id", "int");
  45. $pass = http::value("pass", "int");
  46. $custom = http::value("custom");
  47. $filter_count_offset = 0;
  48. $nvp = [];
  49. // Status
  50. if(strlen($status) > 0) $nvp["status"] = $status;
  51. else {
  52. $nvp["status"] = (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED);
  53. $filter_count_offset--;
  54. }
  55. // Status Tester Invoice
  56. if(strlen($status_tester_invoice)) {
  57. $status_tester_invoice = (int) $status_tester_invoice;
  58. $nvp["status_tester_invoice"] = $status_tester_invoice;
  59. }
  60. // Project
  61. if($project_id) $nvp["project_id"] = $project_id;
  62. else if($customer_id) {
  63. $projects = Project::query(["customer_id" => $customer_id]);
  64. $nvp["project_id"] = array_column($projects, "id");
  65. }
  66. // Location
  67. if($location_id) $nvp["location_id"] = $location_id;
  68. else if($location_internal_id) {
  69. $locations = Location::query(["internal_id" => $location_internal_id]);
  70. if($locations) {
  71. $nvp["location_id"] = array_column($locations, "id");
  72. }
  73. }
  74. // Test ID
  75. if($test_id) $nvp["id"] = $test_id;
  76. // Tester ID
  77. if($tester_id) $nvp["tester_id"] = $tester_id;
  78. // Pass
  79. if(strlen($pass) > 0) $nvp["pass"] = $pass;
  80. $filter_count = count($nvp);
  81. $sort = http::value("sort");
  82. switch($sort) {
  83. case "updated":
  84. $query_options = ["sort" => "dts_updated", "order" => "desc"];
  85. break;
  86. case "due":
  87. $query_options = ["sort" => "dts_due", "order" => "asc"];
  88. break;
  89. default:
  90. $sort = "status";
  91. $query_options = ["sort" => "status", "order" => "asc"];
  92. break;
  93. }
  94. $tests = Test::query($nvp, $query_options);
  95. $projects = Project::load(array_column($tests, "project_id"));
  96. switch($custom) {
  97. case "overdue_project":
  98. $tests = array_filter($tests, function($test) {
  99. if($test->status == Test::STATUS_CANCELLED) return false;
  100. $project = $test->getProject();
  101. $unix_deadline = Util::getUnixTimeFromUTC($project->dts_deadline);
  102. return ($test->status != Test::STATUS_COMPLETED && time() > $unix_deadline);
  103. });
  104. $filter_count++;
  105. break;
  106. case "overdue_test":
  107. $tests = array_filter($tests, function($test) {
  108. if($test->status == Test::STATUS_CANCELLED) return false;
  109. if(is_null($test->dts_due)) return false;
  110. $unix_due = Util::getUnixTimeFromUTC($test->dts_due);
  111. return ($test->status < Test::STATUS_STARTED && time() > $unix_due);
  112. });
  113. $filter_count++;
  114. break;
  115. }
  116. $filter_count += $filter_count_offset;
  117. if(!$filter_count) $filter_count = "";
  118. $pagination_object = Util::getPaginationObject($tests);
  119. $tests = array_slice($tests, $pagination_object->offset, $pagination_object->pageSize);
  120. $testers = User::load(array_column($tests, "tester_id"));
  121. $customers = Customer::query([], ["sort" => "name"]);
  122. usort($projects, function($a, $b) {
  123. return $a->name <=> $b->name;
  124. });
  125. $sorts = [
  126. "status" => "Status",
  127. "updated" => "Update",
  128. "due" => "Due",
  129. ];
  130. View::includeOnce("header", compact("page_title"));
  131. 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"));