test_verify_form.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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_primary = (int) HTTP::value("void_primary", "int");
  105. $test->void_secondary = (int) HTTP::value("void_secondary", "int");
  106. $test->void_notes = HTTP::value("void_notes", "trim");
  107. if($test->void || $test->void_primary || $test->void_secondary) {
  108. if(!$test->void_notes) {
  109. throw new RuntimeException(__("Void notes are required if any part of the test was void."));
  110. }
  111. }
  112. // Accept test as completed
  113. if($accept) {
  114. $test->status = Test::STATUS_COMPLETED;
  115. }
  116. // Reject test
  117. if($reject) {
  118. $test->status = Test::STATUS_CANCELLED;
  119. }
  120. if($form_exception->hasErrors()) throw $form_exception;
  121. $test->retest = (int) http::value("retest", "int");
  122. $test->dts_updated = Util::getDateTime();
  123. $success = $test->commit();
  124. if(!$success) throw new Exception("Failed to update the test. Please try again later.");
  125. if($new_test_project) {
  126. $new_test = new Test;
  127. $new_test->project_id = $new_test_project->id;
  128. $new_test->location_id = $location->id;
  129. $new_test->status = Test::STATUS_UNASSIGNED;
  130. $new_test->dts_created = Util::getDateTime();
  131. if($new_test_is_retest) {
  132. $new_test->retest = 1;
  133. }
  134. $success = $new_test->commit();
  135. }
  136. // If rejected, recreate empty test for the location
  137. if($reject) {
  138. $new_test = new Test;
  139. $new_test->project_id = $test->project_id;
  140. $new_test->location_id = $test->location_id;
  141. $new_test->status = Test::STATUS_UNASSIGNED;
  142. $new_test->dts_created = Util::getDateTime();
  143. $success = $new_test->commit();
  144. if(!$success) throw new Exception("Failed to create a new test. Please try again later.");
  145. }
  146. if($accept) {
  147. $tester->score++;
  148. $tester->commit();
  149. if($test->isVoid() === false) {
  150. $mpdf = $test->getMPDF();
  151. $pdf_string = $mpdf->Output("", "S");
  152. $pdf_path = $directory . "/pdf.pdf";
  153. $success = file_put_contents($pdf_path, $pdf_string);
  154. $email_customer = http::value("email_customer");
  155. $email_location = http::value("email_location");
  156. $email_location_area_manager = http::value("email_location_area_manager");
  157. $email_location_operations_manager = http::value("email_location_operations_manager");
  158. $email_location_gaming_manager = http::value("email_location_gaming_manager");
  159. $email_location_senior_gaming_manager = http::value("email_location_senior_gaming_manager");
  160. $include_previous_test = (bool) http::value("include_previous_test");
  161. $email_addresses = [];
  162. if($email_customer && $customer->email_after_test && $customer->email) {
  163. $email_addresses[] = $customer->email;
  164. foreach($customer->getCcEmailAddresses() as $cc_email_address) {
  165. $email_addresses[] = $cc_email_address;
  166. }
  167. }
  168. if($email_location && $customer->email_locations_after_test && $location->email) {
  169. $email_addresses[] = $location->email;
  170. }
  171. if($email_location_area_manager && $customer->email_locations_after_test && $location->area_manager_email) {
  172. $email_addresses[] = $location->area_manager_email;
  173. }
  174. if($email_location_operations_manager && $customer->email_locations_after_test && $location->operations_manager_email) {
  175. $email_addresses[] = $location->operations_manager_email;
  176. }
  177. if($email_location_gaming_manager && $customer->email_locations_after_test && $location->gaming_manager_email) {
  178. $email_addresses[] = $location->gaming_manager_email;
  179. }
  180. if($email_location_senior_gaming_manager && $customer->email_locations_after_test && $location->senior_gaming_manager_email) {
  181. $email_addresses[] = $location->senior_gaming_manager_email;
  182. }
  183. if(EMAIL_TEST_RESULT) {
  184. $email_addresses[] = EMAIL_TEST_RESULT;
  185. }
  186. if(EMAIL_TEST_RESULT_COPY) {
  187. $email_addresses[] = EMAIL_TEST_RESULT_COPY;
  188. }
  189. foreach($email_addresses as $email_address) {
  190. $test->sendResultEmail($email_address, $include_previous_test);
  191. }
  192. }
  193. }
  194. if($test->getTesterInvoiceStatus() === Test::STATUS_TESTER_INVOICE_PENDING) {
  195. if($accept_tester_invoice) {
  196. $success = $test->markTesterInvoicePaid();
  197. if(!$success) throw new Exception("Failed to mark tester invoice as paid.");
  198. }
  199. if($reject_tester_invoice) {
  200. $success = $test->rejectTesterInvoice();
  201. if(!$success) throw new Exception("Failed to reject tester invoice.");
  202. }
  203. }
  204. Core::addAlert("success", "Test updated successfully.");
  205. HTTP::redirect("/account/tests/$id");
  206. }
  207. catch(FormException $e) {
  208. $e->generateAlerts();
  209. }
  210. catch(Exception $e) {
  211. Core::addAlert("danger", $e->getMessage());
  212. }
  213. }
  214. $projects = $customer->getProjects();
  215. $future_projects = array_filter($projects, static function(Project $p) use ($project) {
  216. if($p->id == $project->id) return false;
  217. if($p->template_id != $project->template_id) return false;
  218. $unix_deadline = Util::getUnixTimeFromUTC($p->dts_deadline);
  219. return ($unix_deadline > time());
  220. });
  221. View::includeOnce("header", compact("page_title"));
  222. View::include("test_verify_form", compact("test", "tester", "location", "project", "future_projects", "customer", "template", "fields", "tester_message"));
  223. View::includeOnce("footer");