tests_admin_list.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. $actioned_count = 0;
  15. switch($batch_action) {
  16. case "unassign":
  17. foreach($tests as $test) {
  18. $success = $test->unassignTester();
  19. $actioned_count += $success;
  20. }
  21. Core::addAlert("success", "$actioned_count of $test_count test(s) unassigned successfully.");
  22. break;
  23. case "mark_tester_invoice_paid":
  24. foreach($tests as $test) {
  25. $actioned_count += ($test->markTesterInvoicePaid()) ? 1 : 0;
  26. }
  27. Core::addAlert("success", "{$actioned_count} of {$test_count} test(s) marked as paid successfully.");
  28. break;
  29. case "reject_tester_invoice":
  30. foreach($tests as $test) {
  31. $actioned_count += ($test->rejectTesterInvoice()) ? 1 : 0;
  32. }
  33. Core::addAlert("success", "{$actioned_count} of {$test_count} invoices rejected successfully.");
  34. break;
  35. case "send_test_assigned_email":
  36. foreach($tests as $test) {
  37. $tester = $test->getTester();
  38. if($tester) {
  39. $test->sendAssignEmail("");
  40. $actioned_count++;
  41. }
  42. }
  43. Core::addAlert("success", "{$actioned_count} of {$test_count} emails sent successfully.");
  44. break;
  45. default:
  46. throw new RuntimeException("Invalid action.");
  47. }
  48. HTTP::redirect($_SERVER["REQUEST_URI"]);
  49. }
  50. catch(Exception $e) {
  51. Core::addAlert("danger", $e->getMessage());
  52. }
  53. }
  54. // Batch Action End
  55. $status = http::value("status", "int");
  56. $status_tester_invoice = http::value("status_tester_invoice");
  57. $customer_id = http::value("customer_id", "int");
  58. $project_id = http::value("project_id", "int");
  59. $location_id = http::value("location_id", "int");
  60. $test_id = http::value("test_id", "int");
  61. $tester_id = http::value("tester_id", "int");
  62. $location_internal_id = http::value("location_internal_id", "int");
  63. $pass = http::value("pass", "int");
  64. $min_days_overdue = http::value("min_days_overdue", "int");
  65. $custom = http::value("custom");
  66. $filter_count_offset = 0;
  67. $nvp = [];
  68. // Status
  69. if(strlen($status) > 0) $nvp["status"] = $status;
  70. else {
  71. $nvp["status"] = Constraint::notEquals(Test::STATUS_CANCELLED);
  72. $filter_count_offset--;
  73. }
  74. // Status Tester Invoice
  75. if(strlen($status_tester_invoice)) {
  76. $status_tester_invoice = (int) $status_tester_invoice;
  77. $nvp["status_tester_invoice"] = $status_tester_invoice;
  78. }
  79. // Project
  80. if($project_id) $nvp["project_id"] = $project_id;
  81. else if($customer_id) {
  82. $projects = Project::query(["customer_id" => $customer_id]);
  83. $nvp["project_id"] = array_column($projects, "id");
  84. }
  85. // Location
  86. if($location_id) $nvp["location_id"] = $location_id;
  87. else if($location_internal_id) {
  88. $locations = Location::query(["internal_id" => $location_internal_id]);
  89. if($locations) {
  90. $nvp["location_id"] = array_column($locations, "id");
  91. }
  92. }
  93. // Test ID
  94. if($test_id) $nvp["id"] = $test_id;
  95. // Tester ID
  96. if($tester_id) $nvp["tester_id"] = $tester_id;
  97. // Pass
  98. if(strlen($pass) > 0) $nvp["pass"] = $pass;
  99. $filter_count = count($nvp);
  100. $sort = http::value("sort");
  101. switch($sort) {
  102. case "updated":
  103. $query_options = ["sort" => "dts_updated", "order" => "desc"];
  104. break;
  105. case "due":
  106. $query_options = ["sort" => "dts_due", "order" => "asc"];
  107. break;
  108. default:
  109. $sort = "status";
  110. $query_options = ["sort" => "status", "order" => "asc"];
  111. break;
  112. }
  113. $tests = Test::query($nvp, $query_options);
  114. $projects = Project::load(array_column($tests, "project_id"));
  115. switch($custom) {
  116. case "overdue_project":
  117. $tests = array_filter($tests, static function(Test $test) use ($min_days_overdue) {
  118. if($test->isCancelled()) {
  119. return false;
  120. }
  121. if($test->isCompleted()) {
  122. return false;
  123. }
  124. $project = $test->getProject();
  125. $dt_deadline = $project->getDateTimeDeadline();
  126. if(!$dt_deadline) {
  127. return false;
  128. }
  129. if(!$project->isOverdue()) {
  130. return false;
  131. }
  132. $days_overdue = $dt_deadline->diffInDays(null, true);
  133. if($min_days_overdue > 0 && $days_overdue > $min_days_overdue) {
  134. return false;
  135. }
  136. return true;
  137. });
  138. $filter_count++;
  139. break;
  140. case "overdue_test":
  141. $tests = array_filter($tests, static function(Test $test) use ($min_days_overdue) {
  142. if($test->isCancelled()) {
  143. return false;
  144. }
  145. if($test->hasDueDateTime() === false) {
  146. return false;
  147. }
  148. $dt_due = $test->getDateTimeDue();
  149. if(!$dt_due) {
  150. return false;
  151. }
  152. if(!($test->hasNotStarted() && $test->isOverdue())) {
  153. return false;
  154. }
  155. $days_overdue = $dt_due->diffInDays(null, true);
  156. if($min_days_overdue > 0 && $days_overdue < $min_days_overdue) {
  157. return false;
  158. }
  159. return true;
  160. });
  161. $filter_count++;
  162. break;
  163. }
  164. $filter_count += $filter_count_offset;
  165. if(!$filter_count) $filter_count = "";
  166. $pagination_object = Util::getPaginationObject($tests);
  167. $tests = array_slice($tests, $pagination_object->offset, $pagination_object->pageSize);
  168. $testers = User::load(array_column($tests, "tester_id"));
  169. $customers = Customer::query([], ["sort" => "name"]);
  170. usort($projects, static function(Project $a, Project $b): int {
  171. return $a->name <=> $b->name;
  172. });
  173. $sorts = [
  174. "status" => "Status",
  175. "updated" => "Update",
  176. "due" => "Due",
  177. ];
  178. Core::addScript("public/js/app-test-list.js");
  179. View::includeOnce("header", compact("page_title"));
  180. 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"));