test.php 11 KB

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