test.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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->json);
  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("The uploaded file for $label is not an image.");
  86. list($width, $height, $type) = $size;
  87. if($type != IMAGETYPE_JPEG) throw new Exception("The uploaded file for $label is not a JPEG.");
  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("$label is required.");
  102. if($values && !in_array($value, $values)) $form_exception->addError("Value for $label is invalid.");
  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("Failed to save value for $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 <= 2) ? 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->json);
  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. if(($accept || $reject) && $test->status != Test::STATUS_PENDING_VERIFICATION) {
  193. throw new Exception("This test is no longer pending verification.");
  194. }
  195. if(empty($test->verifier_id)) {
  196. $test->verifier_id = Core::$user->id;
  197. $test->dts_verified = Util::getDateTime();
  198. }
  199. $test->verifier_notes = http::value("verifier_notes", "trim");
  200. // Modify test values
  201. // Overrides
  202. $unix_start = strtotime($test->dts_start . " UTC");
  203. $unix_end = strtotime($test->dts_end . " UTC");
  204. $pass_override = http::value("pass_override");
  205. if(strlen($pass_override) > 0) {
  206. $test->pass = $pass_override;
  207. }
  208. $start_override = http::getDateTimeFromFields("start_override");
  209. if($start_override) {
  210. $unix_start = strtotime($start_override);
  211. }
  212. $end_override = http::getDateTimeFromFields("end_override");
  213. if($end_override) {
  214. $unix_end = strtotime($end_override);
  215. }
  216. if($unix_start > $unix_end) {
  217. $form_exception->addError("Start time cannot be after end time.");
  218. }
  219. else {
  220. $test->dts_start = Util::getDateTime($unix_start);
  221. $test->dts_end = Util::getDateTime($unix_end);
  222. }
  223. $test->test_fee = null;
  224. $tester_fee = http::value("tester_fee", "float");
  225. if($tester_fee != $project->default_tester_fee) {
  226. $test->test_fee = $tester_fee;
  227. }
  228. // Test forms
  229. foreach($fields as $field) {
  230. if($field->type == "header") continue;
  231. if($field->type == "file") continue;
  232. if(empty($field->name)) continue;
  233. $value = http::value($field->name, "trim");
  234. if(in_array($field->name, Test::TEST_FIELDS)) $test->{$field->name} = $value;
  235. else {
  236. $test_value = TestValue::get($id, $field->name);
  237. if(!$test_value) {
  238. $test_value = new TestValue;
  239. $test_value->test_id = $id;
  240. $test_value->name = $field->name;
  241. }
  242. $test_value->value = $value;
  243. $test_value->commit();
  244. }
  245. }
  246. // Accept test as completed
  247. if($accept) {
  248. $test->status = Test::STATUS_COMPLETED;
  249. }
  250. // Reject test
  251. if($reject) {
  252. $test->status = Test::STATUS_CANCELLED;
  253. }
  254. if($form_exception->hasErrors()) throw $form_exception;
  255. $test->retest = http::value("retest", "int");
  256. $test->dts_updated = Util::getDateTime();
  257. $success = $test->commit();
  258. if(!$success) throw new Exception("Failed to update the test. Please try again later.");
  259. // If rejected, recreate empty test for the location
  260. if($reject) {
  261. $new_test = new Test;
  262. $new_test->project_id = $test->project_id;
  263. $new_test->location_id = $test->location_id;
  264. $new_test->status = Test::STATUS_UNASSIGNED;
  265. $new_test->dts_created = Util::getDateTime();
  266. $success = $new_test->commit();
  267. if(!$success) throw new Exception("Failed to create a new test. Please try again later.");
  268. }
  269. if($accept) {
  270. $tester->score++;
  271. $tester->commit();
  272. // Save PDF
  273. $pdf = $test->getPDF();
  274. $pdf_string = $pdf->Output("S");
  275. $pdf_path = $directory . "/pdf.pdf";
  276. $success = file_put_contents($pdf_path, $pdf_string);
  277. $email_customer = http::value("email_customer");
  278. $email_location = http::value("email_location");
  279. $email_location_area_manager = http::value("email_location_area_manager");
  280. if($email_customer && $customer->email_after_test && $customer->email) {
  281. $test->sendResultEmail($customer->email);
  282. }
  283. if($email_location && $customer->email_locations_after_test && $location->email) {
  284. $test->sendResultEmail($location->email);
  285. }
  286. if($email_location_area_manager && $customer->email_locations_after_test && $location->area_manager_email) {
  287. $test->sendResultEmail($location->area_manager_email);
  288. }
  289. if(EMAIL_TEST_RESULT) {
  290. $test->sendResultEmail(EMAIL_TEST_RESULT);
  291. }
  292. if(EMAIL_TEST_RESULT_COPY) {
  293. $test->sendResultEmail(EMAIL_TEST_RESULT_COPY);
  294. }
  295. }
  296. Core::addAlert("success", "Test verified successfully.");
  297. HTTP::redirect("/account/tests/$id");
  298. }
  299. catch(FormException $e) {
  300. $e->generateAlerts();
  301. }
  302. catch(Exception $e) {
  303. Core::addAlert("danger", $e->getMessage());
  304. }
  305. }
  306. View::includeOnce("header", compact("page_title"));
  307. View::include("test_verify_form", compact("test", "has_pdf", "tester", "location", "project", "customer", "template", "fields", "values", "tester_message"));
  308. View::includeOnce("footer");
  309. return;
  310. }
  311. // View test results
  312. else if(Core::$user->level >= User::LEVEL_VENUE) {
  313. HTTP::redirect("/account/tests/$id/result");
  314. }
  315. else {
  316. throw new Exception("Invalid request.");
  317. }
  318. ?>