1
0

test_verify_form.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. foreach($fields as $field) {
  80. if($field->type === "header") continue;
  81. if($field->type === "file") continue;
  82. if(empty($field->name)) continue;
  83. $required_key = $field->requiredKey ?? null;
  84. if($required_key && !in_array($required_key, $template_keys)) continue;
  85. $value = http::value($field->name, "trim");
  86. if($value === "") {
  87. $value = null;
  88. }
  89. if(in_array($field->name, Test::TEST_FIELDS, true)) {
  90. $test->{$field->name} = $value;
  91. }
  92. else {
  93. $test_value = TestValue::get($id, $field->name);
  94. if(!$test_value) {
  95. $test_value = new TestValue;
  96. $test_value->test_id = $id;
  97. $test_value->name = $field->name;
  98. }
  99. $test_value->value = $value;
  100. $test_value->commit();
  101. }
  102. }
  103. $test->void = (int) HTTP::value("void", "int");
  104. $test->void_notes = HTTP::value("void_notes", "trim");
  105. // Accept test as completed
  106. if($accept) {
  107. $test->status = Test::STATUS_COMPLETED;
  108. }
  109. // Reject test
  110. if($reject) {
  111. $test->status = Test::STATUS_CANCELLED;
  112. }
  113. if($form_exception->hasErrors()) throw $form_exception;
  114. $test->retest = (int) http::value("retest", "int");
  115. $test->dts_updated = Util::getDateTime();
  116. $success = $test->commit();
  117. if(!$success) throw new Exception("Failed to update the test. Please try again later.");
  118. if($new_test_project) {
  119. $new_test = new Test;
  120. $new_test->project_id = $new_test_project->id;
  121. $new_test->location_id = $location->id;
  122. $new_test->status = Test::STATUS_UNASSIGNED;
  123. $new_test->dts_created = Util::getDateTime();
  124. if($new_test_is_retest) {
  125. $new_test->retest = 1;
  126. }
  127. $success = $new_test->commit();
  128. }
  129. // If rejected, recreate empty test for the location
  130. if($reject) {
  131. $new_test = new Test;
  132. $new_test->project_id = $test->project_id;
  133. $new_test->location_id = $test->location_id;
  134. $new_test->status = Test::STATUS_UNASSIGNED;
  135. $new_test->dts_created = Util::getDateTime();
  136. $success = $new_test->commit();
  137. if(!$success) throw new Exception("Failed to create a new test. Please try again later.");
  138. }
  139. if($accept) {
  140. $tester->score++;
  141. $tester->commit();
  142. if($test->isVoid() === false) {
  143. $mpdf = $test->getMPDF();
  144. $pdf_string = $mpdf->Output("", "S");
  145. $pdf_path = $directory . "/pdf.pdf";
  146. $success = file_put_contents($pdf_path, $pdf_string);
  147. $email_customer = http::value("email_customer");
  148. $email_location = http::value("email_location");
  149. $email_location_area_manager = http::value("email_location_area_manager");
  150. $include_previous_test = (bool) http::value("include_previous_test");
  151. $email_addresses = [];
  152. if($email_customer && $customer->email_after_test && $customer->email) {
  153. $email_addresses[] = $customer->email;
  154. }
  155. if($email_location && $customer->email_locations_after_test && $location->email) {
  156. $email_addresses[] = $location->email;
  157. }
  158. if($email_location_area_manager && $customer->email_locations_after_test && $location->area_manager_email) {
  159. $email_addresses[] = $location->area_manager_email;
  160. }
  161. if(EMAIL_TEST_RESULT) {
  162. $email_addresses[] = EMAIL_TEST_RESULT;
  163. }
  164. if(EMAIL_TEST_RESULT_COPY) {
  165. $email_addresses[] = EMAIL_TEST_RESULT_COPY;
  166. }
  167. foreach($email_addresses as $email_address) {
  168. $test->sendResultEmail($email_address, $include_previous_test);
  169. }
  170. }
  171. }
  172. if($test->getTesterInvoiceStatus() === Test::STATUS_TESTER_INVOICE_PENDING) {
  173. if($accept_tester_invoice) {
  174. $success = $test->markTesterInvoicePaid();
  175. if(!$success) throw new Exception("Failed to mark tester invoice as paid.");
  176. }
  177. if($reject_tester_invoice) {
  178. $success = $test->rejectTesterInvoice();
  179. if(!$success) throw new Exception("Failed to reject tester invoice.");
  180. }
  181. }
  182. Core::addAlert("success", "Test updated successfully.");
  183. HTTP::redirect("/account/tests/$id");
  184. }
  185. catch(FormException $e) {
  186. $e->generateAlerts();
  187. }
  188. catch(Exception $e) {
  189. Core::addAlert("danger", $e->getMessage());
  190. }
  191. }
  192. $projects = $customer->getProjects();
  193. $future_projects = array_filter($projects, static function(Project $p) use ($project) {
  194. if($p->id == $project->id) return false;
  195. if($p->template_id != $project->template_id) return false;
  196. $unix_deadline = Util::getUnixTimeFromUTC($p->dts_deadline);
  197. return ($unix_deadline > time());
  198. });
  199. View::includeOnce("header", compact("page_title"));
  200. View::include("test_verify_form", compact("test", "tester", "location", "project", "future_projects", "customer", "template", "fields", "tester_message"));
  201. View::includeOnce("footer");