tests_admin_list.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. $location_name = http::value("location_name", "trim");
  61. $test_id = http::value("test_id", "int");
  62. $tester_id = http::value("tester_id", "int");
  63. $location_internal_id = http::value("location_internal_id", "int");
  64. $pass = http::value("pass", "int");
  65. $min_days_overdue = http::value("min_days_overdue", "int");
  66. $custom = http::value("custom");
  67. $filter_count_offset = 0;
  68. $nvp = [];
  69. // Status
  70. if(strlen($status) > 0) $nvp["status"] = $status;
  71. else {
  72. $nvp["status"] = Constraint::notEquals(Test::STATUS_CANCELLED);
  73. $filter_count_offset--;
  74. }
  75. // Status Tester Invoice
  76. if(strlen($status_tester_invoice)) {
  77. $status_tester_invoice = (int) $status_tester_invoice;
  78. $nvp["status_tester_invoice"] = $status_tester_invoice;
  79. }
  80. // Project
  81. if($project_id) $nvp["project_id"] = $project_id;
  82. else if($customer_id) {
  83. $projects = Project::query(["customer_id" => $customer_id]);
  84. $nvp["project_id"] = array_column($projects, "id");
  85. }
  86. // Location
  87. if($location_id) {
  88. $nvp["location_id"] = $location_id;
  89. }
  90. else if($location_internal_id) {
  91. $locations = Location::query(["internal_id" => $location_internal_id]);
  92. $nvp["location_id"] = $locations ? array_column($locations, "id") : "0";
  93. }
  94. else if($location_name) {
  95. $locations = Location::query([
  96. "name" => Constraint::like("%" . DB::escape($location_name) . "%")
  97. ]);
  98. $nvp["location_id"] = $locations ? array_column($locations, "id") : "0";
  99. }
  100. // Test ID
  101. if($test_id) $nvp["id"] = $test_id;
  102. // Tester ID
  103. if($tester_id) $nvp["tester_id"] = $tester_id;
  104. // Pass
  105. if(strlen($pass) > 0) $nvp["pass"] = $pass;
  106. $filter_count = count($nvp);
  107. $sort = http::value("sort");
  108. switch($sort) {
  109. case "updated":
  110. $query_options = ["sort" => "dts_updated", "order" => "desc"];
  111. break;
  112. case "due":
  113. $query_options = ["sort" => "dts_due", "order" => "asc"];
  114. break;
  115. default:
  116. $sort = "status";
  117. $query_options = ["sort" => "status", "order" => "asc"];
  118. break;
  119. }
  120. $tests = Test::query($nvp, $query_options);
  121. $projects = Project::load(array_column($tests, "project_id"));
  122. switch($custom) {
  123. case "overdue_project":
  124. $tests = array_filter($tests, static function(Test $test) use ($min_days_overdue) {
  125. if($test->isCancelled()) {
  126. return false;
  127. }
  128. if($test->isCompleted()) {
  129. return false;
  130. }
  131. $project = $test->getProject();
  132. $dt_deadline = $project->getDateTimeDeadline();
  133. if(!$dt_deadline) {
  134. return false;
  135. }
  136. if(!$project->isOverdue()) {
  137. return false;
  138. }
  139. $days_overdue = $dt_deadline->diffInDays(null, true);
  140. if($min_days_overdue > 0 && $days_overdue > $min_days_overdue) {
  141. return false;
  142. }
  143. return true;
  144. });
  145. $filter_count++;
  146. break;
  147. case "overdue_test":
  148. $tests = array_filter($tests, static function(Test $test) use ($min_days_overdue) {
  149. if($test->isCancelled()) {
  150. return false;
  151. }
  152. if($test->hasDueDateTime() === false) {
  153. return false;
  154. }
  155. $dt_due = $test->getDateTimeDue();
  156. if(!$dt_due) {
  157. return false;
  158. }
  159. if(!($test->hasNotStarted() && $test->isOverdue())) {
  160. return false;
  161. }
  162. $days_overdue = $dt_due->diffInDays(null, true);
  163. if($min_days_overdue > 0 && $days_overdue < $min_days_overdue) {
  164. return false;
  165. }
  166. return true;
  167. });
  168. $filter_count++;
  169. break;
  170. }
  171. $filter_count += $filter_count_offset;
  172. if(!$filter_count) $filter_count = "";
  173. $pagination_object = Util::getPaginationObject($tests);
  174. $tests = array_slice($tests, $pagination_object->offset, $pagination_object->pageSize);
  175. $testers = User::load(array_column($tests, "tester_id"));
  176. $customers = Customer::query([], ["sort" => "name"]);
  177. usort($projects, static function(Project $a, Project $b): int {
  178. return $a->name <=> $b->name;
  179. });
  180. $sorts = [
  181. "status" => "Status",
  182. "updated" => "Update",
  183. "due" => "Due",
  184. ];
  185. Core::addScript("public/js/app-test-list.js");
  186. View::includeOnce("header", compact("page_title"));
  187. View::include("test_list", compact("tests", "pagination_object", "sort", "sorts", "projects", "customers", "customer_id", "project_id", "location_id", "location_internal_id", "location_name", "pass", "min_days_overdue", "custom", "test_id", "tester_id", "filter_count", "status", "status_tester_invoice"));