test.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. if(HTTP::isAJAX()) {
  148. $e->generateJSONError();
  149. }
  150. else {
  151. $e->generateAlerts();
  152. }
  153. }
  154. catch(Exception $e) {
  155. if(HTTP::isAJAX()) {
  156. JSON::fail($e->getMessage());
  157. }
  158. else {
  159. Core::addAlert("danger", $e->getMessage());
  160. }
  161. }
  162. date_default_timezone_set($previous_timezone);
  163. }
  164. View::includeOnce("header", compact("page_title"));
  165. View::include("test_form", compact("test", "location", "project", "template"));
  166. View::includeOnce("footer");
  167. }
  168. // Verify test
  169. else if(Core::$user->level >= User::LEVEL_ADMIN) {
  170. $json = json_decode($template->json);
  171. $fields = array_merge($json->beforeTest, $json->duringTest);
  172. $values = $test->getValues();
  173. $names = array_map(function($value) { return $value->name; }, $values);
  174. $values = array_combine(
  175. array_map(function($value) { return $value->name; }, $values),
  176. array_map(function($value) { return $value->value; }, $values)
  177. );
  178. $directory = $test->getDirectory();
  179. $has_pdf = file_exists($directory . "/pdf.pdf");
  180. $submit = http::value("submit");
  181. $tester_message = http::value("tester_message", "trim");
  182. if($submit) {
  183. try {
  184. Core::validateCSRFToken($submit);
  185. $form_exception = new FormException;
  186. $accept = http::value("accept");
  187. $reject = http::value("reject");
  188. if(($accept || $reject) && $test->status != Test::STATUS_PENDING_VERIFICATION) {
  189. throw new Exception("This test is no longer pending verification.");
  190. }
  191. if(empty($test->verifier_id)) {
  192. $test->verifier_id = Core::$user->id;
  193. $test->dts_verified = Util::getDateTime();
  194. }
  195. $test->verifier_notes = http::value("verifier_notes", "trim");
  196. // Modify test values
  197. // Overrides
  198. $unix_start = strtotime($test->dts_start . " UTC");
  199. $unix_end = strtotime($test->dts_end . " UTC");
  200. $pass_override = http::value("pass_override");
  201. if(strlen($pass_override) > 0) {
  202. $test->pass = $pass_override;
  203. }
  204. $start_override = http::getDateTimeFromFields("start_override");
  205. if($start_override) {
  206. $unix_start = strtotime($start_override);
  207. }
  208. $end_override = http::getDateTimeFromFields("end_override");
  209. if($end_override) {
  210. $unix_end = strtotime($end_override);
  211. }
  212. if($unix_start > $unix_end) {
  213. $form_exception->addError("Start time cannot be after end time.");
  214. }
  215. else {
  216. $test->dts_start = Util::getDateTime($unix_start);
  217. $test->dts_end = Util::getDateTime($unix_end);
  218. }
  219. $test->test_fee = null;
  220. $tester_fee = http::value("tester_fee", "float");
  221. if($tester_fee != $project->default_tester_fee) {
  222. $test->test_fee = $tester_fee;
  223. }
  224. // Test forms
  225. foreach($fields as $field) {
  226. if($field->type == "header") continue;
  227. if($field->type == "file") continue;
  228. if(empty($field->name)) continue;
  229. $value = http::value($field->name, "trim");
  230. if(in_array($field->name, Test::TEST_FIELDS)) $test->{$field->name} = $value;
  231. else {
  232. $test_value = TestValue::get($id, $field->name);
  233. if(!$test_value) {
  234. $test_value = new TestValue;
  235. $test_value->test_id = $id;
  236. $test_value->name = $field->name;
  237. }
  238. $test_value->value = $value;
  239. $test_value->commit();
  240. }
  241. }
  242. // Accept test as completed
  243. if($accept) {
  244. $test->status = Test::STATUS_COMPLETED;
  245. }
  246. // Reject test
  247. if($reject) {
  248. $test->status = Test::STATUS_CANCELLED;
  249. }
  250. if($form_exception->hasErrors()) throw $form_exception;
  251. $test->dts_updated = Util::getDateTime();
  252. $success = $test->commit();
  253. if(!$success) throw new Exception("Failed to update the test. Please try again later.");
  254. // If rejected, recreate empty test for the location
  255. if($reject) {
  256. $new_test = new Test;
  257. $new_test->project_id = $test->project_id;
  258. $new_test->location_id = $test->location_id;
  259. $new_test->status = Test::STATUS_UNASSIGNED;
  260. $new_test->dts_created = Util::getDateTime();
  261. $success = $new_test->commit();
  262. if(!$success) throw new Exception("Failed to create a new test. Please try again later.");
  263. }
  264. if($accept) {
  265. $tester->score++;
  266. $tester->commit();
  267. // Save PDF
  268. $pdf = $test->getPDF();
  269. $pdf_string = $pdf->Output("S");
  270. $pdf_path = $directory . "/pdf.pdf";
  271. $success = file_put_contents($pdf_path, $pdf_string);
  272. $email_customer = http::value("email_customer");
  273. $email_location = http::value("email_location");
  274. $email_location_area_manager = http::value("email_location_area_manager");
  275. if($email_customer && $customer->email_after_test && $customer->email) {
  276. $test->sendResultEmail($customer->email);
  277. }
  278. if($email_location && $customer->email_locations_after_test && $location->email) {
  279. $test->sendResultEmail($location->email);
  280. }
  281. if($email_location_area_manager && $customer->email_locations_after_test && $location->area_manager_email) {
  282. $test->sendResultEmail($location->area_manager_email);
  283. }
  284. if(EMAIL_TEST_RESULT) {
  285. $test->sendResultEmail(EMAIL_TEST_RESULT);
  286. }
  287. if(EMAIL_TEST_RESULT_COPY) {
  288. $test->sendResultEmail(EMAIL_TEST_RESULT_COPY);
  289. }
  290. }
  291. Core::addAlert("success", "Test verified successfully.");
  292. HTTP::redirect("/account/tests/$id");
  293. }
  294. catch(FormException $e) {
  295. $e->generateAlerts();
  296. }
  297. catch(Exception $e) {
  298. Core::addAlert("danger", $e->getMessage());
  299. }
  300. }
  301. View::includeOnce("header", compact("page_title"));
  302. View::include("test_verify_form", compact("test", "has_pdf", "tester", "location", "project", "customer", "template", "fields", "values", "tester_message"));
  303. View::includeOnce("footer");
  304. return;
  305. }
  306. // View test results
  307. else if(Core::$user->level >= User::LEVEL_VENUE) {
  308. HTTP::redirect("/account/tests/$id/result");
  309. }
  310. else {
  311. throw new Exception("Invalid request.");
  312. }
  313. ?>