1
0

test.php 10 KB

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