1
0

tests_admin_list.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. case "end":
  116. $query_options = ["sort" => "dts_end", "order" => "desc"];
  117. break;
  118. default:
  119. $sort = "status";
  120. $query_options = ["sort" => "status", "order" => "asc"];
  121. break;
  122. }
  123. $tests = Test::query($nvp, $query_options);
  124. $projects = Project::load(array_column($tests, "project_id"));
  125. switch($custom) {
  126. case "overdue_project":
  127. $tests = array_filter($tests, static function(Test $test) use ($min_days_overdue) {
  128. if($test->isCancelled()) {
  129. return false;
  130. }
  131. if($test->isCompleted()) {
  132. return false;
  133. }
  134. $project = $test->getProject();
  135. $dt_deadline = $project->getDateTimeDeadline();
  136. if(!$dt_deadline) {
  137. return false;
  138. }
  139. if(!$project->isOverdue()) {
  140. return false;
  141. }
  142. $days_overdue = $dt_deadline->diffInDays(null, true);
  143. if($min_days_overdue > 0 && $days_overdue > $min_days_overdue) {
  144. return false;
  145. }
  146. return true;
  147. });
  148. $filter_count++;
  149. break;
  150. case "overdue_test":
  151. $tests = array_filter($tests, static function(Test $test) use ($min_days_overdue) {
  152. if($test->isCancelled()) {
  153. return false;
  154. }
  155. if($test->hasDueDateTime() === false) {
  156. return false;
  157. }
  158. $dt_due = $test->getDateTimeDue();
  159. if(!$dt_due) {
  160. return false;
  161. }
  162. if(!($test->hasNotStarted() && $test->isOverdue())) {
  163. return false;
  164. }
  165. $days_overdue = $dt_due->diffInDays(null, true);
  166. if($min_days_overdue > 0 && $days_overdue < $min_days_overdue) {
  167. return false;
  168. }
  169. return true;
  170. });
  171. $filter_count++;
  172. break;
  173. }
  174. $filter_count += $filter_count_offset;
  175. if(!$filter_count) $filter_count = "";
  176. $pagination_object = Util::getPaginationObject($tests);
  177. $tests = array_slice($tests, $pagination_object->offset, $pagination_object->pageSize);
  178. $testers = User::load(array_column($tests, "tester_id"));
  179. $customers = Customer::query([], ["sort" => "name"]);
  180. usort($projects, static function(Project $a, Project $b): int {
  181. return $a->name <=> $b->name;
  182. });
  183. $sorts = [
  184. "status" => "Status",
  185. "updated" => "Recently updated",
  186. "end" => "Test date",
  187. "due" => "Test due date",
  188. ];
  189. Core::addScript("public/js/app-test-list.js");
  190. View::includeOnce("header", compact("page_title"));
  191. 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"));