1
0

test.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. /**
  3. * @var Test $test
  4. */
  5. Core::addScript("public/js/app-test.js");
  6. $test = Test::get($id);
  7. if(!$test) throw new Exception(__("Test cannot be found."));
  8. $is_confirmed = $test->status == Test::STATUS_CONFIRMED;
  9. $is_started = $test->status == Test::STATUS_STARTED;
  10. $is_unverified = $test->status == Test::STATUS_PENDING_VERIFICATION;
  11. $location = $test->getLocation();
  12. $project = $test->getProject();
  13. $customer = $project->getCustomer();
  14. $tester = $test->getTester();
  15. $template = $project->getTemplate();
  16. $template_overrides = $project->getTemplateOverrides();
  17. if($template_overrides) {
  18. $template->setOverrides($template_overrides);
  19. }
  20. $page_title = $location->name;
  21. Core::addBreadcrumb("/account", "Account");
  22. Core::addBreadcrumb("/account/tests", "Tests");
  23. Core::addBreadcrumb("/account/tests/$id", $page_title);
  24. // Do test
  25. if(Core::$user->level == User::LEVEL_TESTER) {
  26. Core::$show_footer_content = false;
  27. if(Core::$user->id != $test->tester_id) throw new Exception(__("You do not have permission to view this page."));
  28. if($test->status == Test::STATUS_ASSIGNED) throw new Exception(__("You do not have permission to view this page as you have not accepted the test yet."));
  29. // Show completed message
  30. if($test->status > Test::STATUS_PENDING_VERIFICATION) {
  31. View::includeOnce("header", compact("page_title"));
  32. View::include("test_form", compact("test", "location", "project", "template"));
  33. View::includeOnce("footer");
  34. return;
  35. }
  36. $json = json_decode($template->getJSON());
  37. $submit = http::value("submit");
  38. if($submit) {
  39. $previous_timezone = date_default_timezone_get();
  40. try {
  41. Core::validateCSRFToken($submit);
  42. $form_exception = new FormException;
  43. if($is_confirmed) {
  44. // Parse before the test form
  45. $fields = $json->beforeTest;
  46. }
  47. if($is_started || $is_unverified) {
  48. // Parse after the test form
  49. $fields = $json->duringTest;
  50. // Start/End times
  51. if($is_started) {
  52. $start_override = http::getDateTimeFromFields("start_override");
  53. $end_override = http::getDateTimeFromFields("end_override");
  54. $start_unix = null;
  55. $end_unix = null;
  56. if($start_override && $end_override) {
  57. $start_unix = strtotime($start_override);
  58. $end_unix = strtotime($end_override);
  59. if($start_unix > $end_unix) $form_exception->addError(__("Entry Time must not be after Exit Time."));
  60. if(!$start_unix && $end_unix && $test->dts_start) {
  61. $recorded_start_unix = Util::getUnixTimeFromUTC($test->dts_start);
  62. if($end_unix < $recorded_start_unix) $form_exception->addError(__("Exit Time cannot be later than the recorded Entry Time."));
  63. }
  64. }
  65. else {
  66. $form_exception->addError(__("Entry Time and Exit Time must be filled in."));
  67. }
  68. }
  69. }
  70. foreach($fields as $field) {
  71. $name = $field->name ?? false;
  72. if(empty($name)) continue; // Not a field
  73. $label = $field->label ?? "";
  74. $type = $field->type;
  75. $required = $field->required ?? false;
  76. $values = $field->values ?? [];
  77. if($type == "file") {
  78. $file = (object) $_FILES[$name];
  79. try {
  80. if(!$required && $file->error == UPLOAD_ERR_NO_FILE) continue;
  81. if($required && $file->error == UPLOAD_ERR_NO_FILE) {
  82. throw new Exception("$label is required.");
  83. }
  84. // Detect upload error
  85. if($file->error != UPLOAD_ERR_OK) throw new Exception(HTTP::getUploadErrorString($file->error));
  86. // Get image details
  87. $size = @getimagesize($file->tmp_name);
  88. if(!$size) throw new Exception(sprintf(__("The uploaded file for %s is not an image."), $label));
  89. list($width, $height, $type) = $size;
  90. if($type != IMAGETYPE_JPEG) throw new Exception(sprintf(__("The uploaded file for %s is not a JPEG."), $label));
  91. // Resize image
  92. $output_image = Image::normalise($file->tmp_name);
  93. // Write image to disk
  94. $output_path = $test->getDirectory() . "/" . $name . ".jpg";
  95. imagejpeg($output_image, $output_path, 60);
  96. imagedestroy($output_image);
  97. }
  98. catch(Exception $e) {
  99. $form_exception->addError($e->getMessage());
  100. }
  101. }
  102. else {
  103. $value = http::value($name, "trim");
  104. if($required && empty($value)) $form_exception->addError(sprintf(__("%s is required."), $label));
  105. if($values && !in_array($value, $values)) $form_exception->addError(sprintf(__("Value for %s is invalid."), $label));
  106. if(in_array($name, Test::TEST_FIELDS)) {
  107. $test->$name = $value;
  108. }
  109. else {
  110. $test_value = TestValue::get($test->id, $name);
  111. if(!$test_value) {
  112. $test_value = new TestValue;
  113. $test_value->test_id = $test->id;
  114. $test_value->name = $name;
  115. }
  116. $test_value->value = $value;
  117. $success = $test_value->commit();
  118. if(!$success) $form_exception->addError(sprintf(__("Failed to save value for %s."), $label));
  119. }
  120. }
  121. }
  122. if($form_exception->hasErrors()) throw $form_exception;
  123. date_default_timezone_set("UTC");
  124. // Before the Test
  125. if($is_confirmed) {
  126. $test->status = Test::STATUS_STARTED;
  127. $test->dts_start = Util::getDateTime();
  128. $test->dts_updated = Util::getDateTime();
  129. $success = $test->commit();
  130. if(!$success) throw new Exception(__("Failed to save test. Please try again later."));
  131. Core::addAlert("success", __("Photos uploaded successfully."));
  132. HTTP::redirect("/account/tests/$id");
  133. }
  134. // During the test
  135. if($is_started || $is_unverified) {
  136. $test->status = Test::STATUS_PENDING_VERIFICATION;
  137. if($is_started) {
  138. if($start_unix && $end_unix) {
  139. $test->dts_start = date("Y-m-d H:i:s", $start_unix);
  140. $test->dts_end = date("Y-m-d H:i:s", $end_unix);
  141. }
  142. else if($end_unix) {
  143. $test->dts_end = date("Y-m-d H:i:s", $end_unix);
  144. }
  145. }
  146. $test->pass = 1;
  147. if($template->hasPassFail()) {
  148. $test->pass = ($test->challenge_stage <= $template->getPassStage()) ? 1 : 0; // Pass = Challenged during browsing or before
  149. }
  150. $test->dts_updated = Util::getDateTime();
  151. $success = $test->commit();
  152. if(!$success) throw new Exception(__("Failed to save test. Please try again later."));
  153. JSON::success();
  154. }
  155. }
  156. catch(FormException $e) {
  157. if(HTTP::isAJAX()) {
  158. $e->generateJSONError();
  159. }
  160. else {
  161. $e->generateAlerts();
  162. }
  163. }
  164. catch(Exception $e) {
  165. if(HTTP::isAJAX()) {
  166. JSON::fail($e->getMessage());
  167. }
  168. else {
  169. Core::addAlert("danger", $e->getMessage());
  170. }
  171. }
  172. date_default_timezone_set($previous_timezone);
  173. }
  174. View::includeOnce("header", compact("page_title"));
  175. View::include("test_form", compact("test", "location", "project", "template"));
  176. View::includeOnce("footer");
  177. }
  178. // Verify test
  179. else if(Core::$user->level >= User::LEVEL_ADMIN) {
  180. $json = json_decode($template->getJSON());
  181. $fields = array_merge($json->beforeTest, $json->duringTest);
  182. $values = $test->getValues();
  183. $names = array_map(function($value) { return $value->name; }, $values);
  184. $values = array_combine(
  185. array_map(function($value) { return $value->name; }, $values),
  186. array_map(function($value) { return $value->value; }, $values)
  187. );
  188. $directory = $test->getDirectory();
  189. $submit = http::value("submit");
  190. $tester_message = http::value("tester_message", "trim");
  191. if($submit) {
  192. try {
  193. Core::validateCSRFToken($submit);
  194. $form_exception = new FormException;
  195. $accept = http::value("accept");
  196. $reject = http::value("reject");
  197. $accept_tester_invoice = http::value("accept_tester_invoice");
  198. $reject_tester_invoice = http::value("reject_tester_invoice");
  199. if(($accept || $reject) && $test->status != Test::STATUS_PENDING_VERIFICATION) {
  200. throw new Exception("This test is no longer pending verification.");
  201. }
  202. if(empty($test->verifier_id)) {
  203. $test->verifier_id = Core::$user->id;
  204. $test->dts_verified = Util::getDateTime();
  205. }
  206. $test->verifier_notes = http::value("verifier_notes", "trim");
  207. // Check selected retest project
  208. $retest_project = false;
  209. $retest_project_id = http::value("retest_project_id", "int");
  210. if($retest_project_id) {
  211. $retest_project = Project::get($retest_project_id);
  212. if(!$retest_project || $retest_project->customer_id != $customer->id) throw new Exception("The retest project selected is invalid.");
  213. }
  214. // Modify test values
  215. // Overrides
  216. $unix_start = strtotime($test->dts_start . " UTC");
  217. $unix_end = strtotime($test->dts_end . " UTC");
  218. $pass_override = http::value("pass_override");
  219. if(strlen($pass_override) > 0) {
  220. $test->pass = $pass_override;
  221. }
  222. $start_override = http::getDateTimeFromFields("start_override");
  223. if($start_override) {
  224. $unix_start = strtotime($start_override);
  225. }
  226. $end_override = http::getDateTimeFromFields("end_override");
  227. if($end_override) {
  228. $unix_end = strtotime($end_override);
  229. }
  230. if($unix_start > $unix_end) {
  231. $form_exception->addError("Start time cannot be after end time.");
  232. }
  233. else {
  234. $test->dts_start = Util::getDateTime($unix_start);
  235. $test->dts_end = Util::getDateTime($unix_end);
  236. }
  237. $test->customer_fee = null;
  238. $customer_fee = http::value("customer_fee", "float");
  239. if($customer_fee !== false && $customer_fee != $project->default_customer_fee) {
  240. $test->customer_fee = $customer_fee;
  241. }
  242. $test->test_fee = null;
  243. $tester_fee = http::value("tester_fee", "float");
  244. if($tester_fee !== false && $tester_fee != $project->default_tester_fee) {
  245. $test->test_fee = $tester_fee;
  246. }
  247. // Test forms
  248. foreach($fields as $field) {
  249. if($field->type == "header") continue;
  250. if($field->type == "file") continue;
  251. if(empty($field->name)) continue;
  252. $value = http::value($field->name, "trim");
  253. if(in_array($field->name, Test::TEST_FIELDS)) $test->{$field->name} = $value;
  254. else {
  255. $test_value = TestValue::get($id, $field->name);
  256. if(!$test_value) {
  257. $test_value = new TestValue;
  258. $test_value->test_id = $id;
  259. $test_value->name = $field->name;
  260. }
  261. $test_value->value = $value;
  262. $test_value->commit();
  263. }
  264. }
  265. // Accept test as completed
  266. if($accept) {
  267. $test->status = Test::STATUS_COMPLETED;
  268. }
  269. // Reject test
  270. if($reject) {
  271. $test->status = Test::STATUS_CANCELLED;
  272. }
  273. if($form_exception->hasErrors()) throw $form_exception;
  274. $test->retest = http::value("retest", "int");
  275. $test->dts_updated = Util::getDateTime();
  276. $success = $test->commit();
  277. if(!$success) throw new Exception("Failed to update the test. Please try again later.");
  278. // If rejected, recreate empty test for the location
  279. if($reject) {
  280. $new_test = new Test;
  281. $new_test->project_id = $test->project_id;
  282. $new_test->location_id = $test->location_id;
  283. $new_test->status = Test::STATUS_UNASSIGNED;
  284. $new_test->dts_created = Util::getDateTime();
  285. $success = $new_test->commit();
  286. if(!$success) throw new Exception("Failed to create a new test. Please try again later.");
  287. }
  288. if($accept) {
  289. $tester->score++;
  290. $tester->commit();
  291. if(!$test->pass && $retest_project) {
  292. $retest = new Test;
  293. $retest->project_id = $retest_project->id;
  294. $retest->location_id = $location->id;
  295. $retest->status = Test::STATUS_UNASSIGNED;
  296. $retest->dts_created = Util::getDateTime();
  297. $retest->retest = 1;
  298. $success = $retest->commit();
  299. }
  300. // Save PDF
  301. $mpdf = $test->getMPDF();
  302. $pdf_string = $mpdf->Output("", "S");
  303. $pdf_path = $directory . "/pdf.pdf";
  304. $success = file_put_contents($pdf_path, $pdf_string);
  305. $email_customer = http::value("email_customer");
  306. $email_location = http::value("email_location");
  307. $email_location_area_manager = http::value("email_location_area_manager");
  308. if($email_customer && $customer->email_after_test && $customer->email) {
  309. $test->sendResultEmail($customer->email);
  310. }
  311. if($email_location && $customer->email_locations_after_test && $location->email) {
  312. $test->sendResultEmail($location->email);
  313. }
  314. if($email_location_area_manager && $customer->email_locations_after_test && $location->area_manager_email) {
  315. $test->sendResultEmail($location->area_manager_email);
  316. }
  317. if(EMAIL_TEST_RESULT) {
  318. $test->sendResultEmail(EMAIL_TEST_RESULT);
  319. }
  320. if(EMAIL_TEST_RESULT_COPY) {
  321. $test->sendResultEmail(EMAIL_TEST_RESULT_COPY);
  322. }
  323. }
  324. if($test->status_tester_invoice == Test::STATUS_TESTER_INVOICE_PENDING) {
  325. if($accept_tester_invoice) {
  326. $success = $test->markTesterInvoicePaid();
  327. if(!$success) throw new Exception("Failed to mark tester invoice as paid.");
  328. }
  329. if($reject_tester_invoice) {
  330. $success = $test->rejectTesterInvoice();
  331. if(!$success) throw new Exception("Failed to reject tester invoice.");
  332. }
  333. }
  334. Core::addAlert("success", "Test updated successfully.");
  335. HTTP::redirect("/account/tests/$id");
  336. }
  337. catch(FormException $e) {
  338. $e->generateAlerts();
  339. }
  340. catch(Exception $e) {
  341. Core::addAlert("danger", $e->getMessage());
  342. }
  343. }
  344. $projects = $customer->getProjects();
  345. $future_projects = array_filter($projects, function($p) use ($project) {
  346. if($p->id == $project->id) return false;
  347. if($p->template_id != $project->template_id) return false;
  348. $unix_deadline = Util::getUnixTimeFromUTC($p->dts_deadline);
  349. return ($unix_deadline > time());
  350. });
  351. View::includeOnce("header", compact("page_title"));
  352. View::include("test_verify_form", compact("test", "tester", "location", "project", "future_projects", "customer", "template", "fields", "values", "tester_message"));
  353. View::includeOnce("footer");
  354. return;
  355. }
  356. // View test results
  357. else if(Core::$user->level >= User::LEVEL_VENUE) {
  358. HTTP::redirect("/account/tests/$id/result");
  359. }
  360. else {
  361. throw new Exception(__("Invalid request."));
  362. }
  363. ?>