test.php 11 KB

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