test_verify_form.php 7.3 KB

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