test_verify_form.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var int $id
  4. * @var Test $test
  5. * @var string $page_title
  6. * @var Location $location
  7. * @var Project $project
  8. * @var Template $template
  9. * @var string[] $template_keys
  10. */
  11. $customer = $project->getCustomer();
  12. $tester = $test->getTester();
  13. $fields = $test->getFieldsForVerifyForm();
  14. $directory = $test->getDirectory();
  15. $submit = http::value("submit");
  16. $tester_message = http::value("tester_message", "trim");
  17. if($submit) {
  18. try {
  19. Core::validateCSRFToken($submit);
  20. $form_exception = new FormException;
  21. $accept = http::value("accept");
  22. $reject = http::value("reject");
  23. $accept_tester_invoice = http::value("accept_tester_invoice");
  24. $reject_tester_invoice = http::value("reject_tester_invoice");
  25. if(($accept || $reject) && $test->getStatus() !== Test::STATUS_PENDING_VERIFICATION) {
  26. throw new Exception("This test is no longer pending verification.");
  27. }
  28. if(empty($test->verifier_id)) {
  29. $test->verifier_id = Core::$user->id;
  30. $test->dts_verified = Util::getDateTime();
  31. }
  32. $test->verifier_notes = http::value("verifier_notes", "trim");
  33. // Check selected retest project
  34. $new_test_project = false;
  35. $new_test_project_id = http::value("new_test_project_id", "int");
  36. $new_test_is_retest = http::value("new_test_is_retest", "int");
  37. if($new_test_project_id) {
  38. $new_test_project = Project::get($new_test_project_id);
  39. if(!$new_test_project || $new_test_project->customer_id != $customer->id) throw new Exception("The new test project selected is invalid.");
  40. }
  41. // Modify test values
  42. // Overrides
  43. $unix_start = strtotime($test->dts_start . " UTC");
  44. $unix_end = strtotime($test->dts_end . " UTC");
  45. $pass_override = http::value("pass_override");
  46. if(strlen($pass_override) > 0) {
  47. $test->pass = $pass_override;
  48. }
  49. $start_override = HTTP::value("start_override");
  50. if($start_override) {
  51. $dt_start = new DateTime($start_override, new DateTimeZone(TIMEZONE));
  52. $unix_start = $dt_start->format("U");
  53. }
  54. $end_override = HTTP::value("end_override");
  55. if($end_override) {
  56. $dt_end = new DateTime($end_override, new DateTimeZone(TIMEZONE));
  57. $unix_end = $dt_end->format("U");
  58. }
  59. if($unix_start > $unix_end) {
  60. $form_exception->addError("Start time cannot be after end time.");
  61. }
  62. else {
  63. $test->dts_start = Util::getDateTime($unix_start);
  64. $test->dts_end = Util::getDateTime($unix_end);
  65. }
  66. $test->customer_fee = null;
  67. $customer_fee = http::value("customer_fee", "float");
  68. if($customer_fee !== null && $customer_fee != $project->default_customer_fee) {
  69. $test->customer_fee = $customer_fee;
  70. }
  71. $test->test_fee = null;
  72. $tester_fee = http::value("tester_fee", "float");
  73. if($tester_fee !== null && $tester_fee != $project->default_tester_fee) {
  74. $test->test_fee = $tester_fee;
  75. }
  76. $test->test_travel_cost = HTTP::value("travel_cost", "float") ?? 0;
  77. if($test->test_travel_cost < 0 || $test->test_travel_cost > 999.99) $form_exception->addError("Travel costs must be between 0 and 999.99.");
  78. // Test forms
  79. if($test->isVoid() === false) {
  80. foreach($fields as $field) {
  81. if($field->type === "header") continue;
  82. if($field->type === "file") continue;
  83. if(empty($field->name)) continue;
  84. $required_key = $field->requiredKey ?? null;
  85. if($required_key && !in_array($required_key, $template_keys)) continue;
  86. $value = http::value($field->name, "trim");
  87. if(in_array($field->name, Test::TEST_FIELDS, true)) {
  88. $test->{$field->name} = $value;
  89. }
  90. else {
  91. $test_value = TestValue::get($id, $field->name);
  92. if(!$test_value) {
  93. $test_value = new TestValue;
  94. $test_value->test_id = $id;
  95. $test_value->name = $field->name;
  96. }
  97. $test_value->value = $value;
  98. $test_value->commit();
  99. }
  100. }
  101. }
  102. else {
  103. $test->void = (int) HTTP::value("void", "int");
  104. $test->void_notes = HTTP::value("void_notes", "trim");
  105. }
  106. // Accept test as completed
  107. if($accept) {
  108. $test->status = Test::STATUS_COMPLETED;
  109. }
  110. // Reject test
  111. if($reject) {
  112. $test->status = Test::STATUS_CANCELLED;
  113. }
  114. if($form_exception->hasErrors()) throw $form_exception;
  115. $test->retest = (int) http::value("retest", "int");
  116. $test->dts_updated = Util::getDateTime();
  117. $success = $test->commit();
  118. if(!$success) throw new Exception("Failed to update the test. Please try again later.");
  119. if($new_test_project) {
  120. $new_test = new Test;
  121. $new_test->project_id = $new_test_project->id;
  122. $new_test->location_id = $location->id;
  123. $new_test->status = Test::STATUS_UNASSIGNED;
  124. $new_test->dts_created = Util::getDateTime();
  125. if($new_test_is_retest) {
  126. $new_test->retest = 1;
  127. }
  128. $success = $new_test->commit();
  129. }
  130. // If rejected, recreate empty test for the location
  131. if($reject) {
  132. $new_test = new Test;
  133. $new_test->project_id = $test->project_id;
  134. $new_test->location_id = $test->location_id;
  135. $new_test->status = Test::STATUS_UNASSIGNED;
  136. $new_test->dts_created = Util::getDateTime();
  137. $success = $new_test->commit();
  138. if(!$success) throw new Exception("Failed to create a new test. Please try again later.");
  139. }
  140. if($accept) {
  141. $tester->score++;
  142. $tester->commit();
  143. if($test->isVoid() === false) {
  144. $mpdf = $test->getMPDF();
  145. $pdf_string = $mpdf->Output("", "S");
  146. $pdf_path = $directory . "/pdf.pdf";
  147. $success = file_put_contents($pdf_path, $pdf_string);
  148. $email_customer = http::value("email_customer");
  149. $email_location = http::value("email_location");
  150. $email_location_area_manager = http::value("email_location_area_manager");
  151. $include_previous_test = (bool) http::value("include_previous_test");
  152. $email_addresses = [];
  153. if($email_customer && $customer->email_after_test && $customer->email) {
  154. $email_addresses[] = $customer->email;
  155. }
  156. if($email_location && $customer->email_locations_after_test && $location->email) {
  157. $email_addresses[] = $location->email;
  158. }
  159. if($email_location_area_manager && $customer->email_locations_after_test && $location->area_manager_email) {
  160. $email_addresses[] = $location->area_manager_email;
  161. }
  162. if(EMAIL_TEST_RESULT) {
  163. $email_addresses[] = EMAIL_TEST_RESULT;
  164. }
  165. if(EMAIL_TEST_RESULT_COPY) {
  166. $email_addresses[] = EMAIL_TEST_RESULT_COPY;
  167. }
  168. foreach($email_addresses as $email_address) {
  169. $test->sendResultEmail($email_address, $include_previous_test);
  170. }
  171. }
  172. }
  173. if($test->getTesterInvoiceStatus() === Test::STATUS_TESTER_INVOICE_PENDING) {
  174. if($accept_tester_invoice) {
  175. $success = $test->markTesterInvoicePaid();
  176. if(!$success) throw new Exception("Failed to mark tester invoice as paid.");
  177. }
  178. if($reject_tester_invoice) {
  179. $success = $test->rejectTesterInvoice();
  180. if(!$success) throw new Exception("Failed to reject tester invoice.");
  181. }
  182. }
  183. Core::addAlert("success", "Test updated successfully.");
  184. HTTP::redirect("/account/tests/$id");
  185. }
  186. catch(FormException $e) {
  187. $e->generateAlerts();
  188. }
  189. catch(Exception $e) {
  190. Core::addAlert("danger", $e->getMessage());
  191. }
  192. }
  193. $projects = $customer->getProjects();
  194. $future_projects = array_filter($projects, static function(Project $p) use ($project) {
  195. if($p->id == $project->id) return false;
  196. if($p->template_id != $project->template_id) return false;
  197. $unix_deadline = Util::getUnixTimeFromUTC($p->dts_deadline);
  198. return ($unix_deadline > time());
  199. });
  200. View::includeOnce("header", compact("page_title"));
  201. View::include("test_verify_form", compact("test", "tester", "location", "project", "future_projects", "customer", "template", "fields", "tester_message"));
  202. View::includeOnce("footer");