test.php 11 KB

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