test.php 13 KB

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