1
0

test.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. $location = $test->getLocation();
  6. $project = $test->getProject();
  7. $tester = $test->getTester();
  8. $template = $project->getTemplate();
  9. $page_title = $location->name;
  10. Core::addBreadcrumb("/account", "Account");
  11. Core::addBreadcrumb("/account/tests", "Tests");
  12. Core::addBreadcrumb("/account/tests/$id", $page_title);
  13. $fillable_fields = ["shop_left", "shop_right", "attire", "challenge_stage", "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact", "additional_notes"];
  14. // Do test
  15. if(Core::$user->level == User::LEVEL_TESTER) {
  16. Core::$show_footer_content = false;
  17. if(Core::$user->id != $test->tester_id) throw new Exception("You do not have permission to view this page.");
  18. 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.");
  19. // Show completed message
  20. if($test->status >= Test::STATUS_PENDING_VERIFICATION) {
  21. View::includeOnce("header", compact("page_title"));
  22. View::include("test_form", compact("test", "location", "project", "template"));
  23. View::includeOnce("footer");
  24. return;
  25. }
  26. $json = json_decode($template->json);
  27. $submit = http::value("submit");
  28. if($submit) {
  29. $previous_timezone = date_default_timezone_get();
  30. date_default_timezone_set("UTC");
  31. try {
  32. Core::validateCSRFToken($submit);
  33. $form_exception = new FormException;
  34. if($test->status == Test::STATUS_CONFIRMED) {
  35. // Parse before the test form
  36. $fields = $json->beforeTest;
  37. }
  38. if($test->status == Test::STATUS_STARTED) {
  39. // Parse after the test form
  40. $fields = $json->duringTest;
  41. $duration = http::value("duration", "int");
  42. if(!$duration) $form_exception->addError("Duration was not submitted.");
  43. }
  44. foreach($fields as $field) {
  45. $name = $field->name ?? false;
  46. if(empty($name)) continue; // Not a field
  47. $label = $field->label ?? "";
  48. $type = $field->type;
  49. $required = $field->required ?? false;
  50. $values = $field->values ?? [];
  51. if($type == "file") {
  52. $file = (object) $_FILES[$name];
  53. try {
  54. if(!$required && $file->error == UPLOAD_ERR_NO_FILE) continue;
  55. if($required && $file->error == UPLOAD_ERR_NO_FILE) {
  56. throw new Exception("$label is required.");
  57. }
  58. // Detect upload error
  59. if($file->error != UPLOAD_ERR_OK) throw new Exception(HTTP::getUploadErrorString($file->error));
  60. // Get image details
  61. $size = @getimagesize($file->tmp_name);
  62. if(!$size) throw new Exception("The uploaded file for $label is not an image.");
  63. list($width, $height, $type) = $size;
  64. if($type != IMAGETYPE_JPEG) throw new Exception("The uploaded file for $label is not a JPEG.");
  65. // Resize image
  66. $output_image = Image::normalise($file->tmp_name);
  67. // Write image to disk
  68. $output_path = $test->getDirectory() . "/" . $name . ".jpg";
  69. imagejpeg($output_image, $output_path, 60);
  70. imagedestroy($output_image);
  71. }
  72. catch(Exception $e) {
  73. $form_exception->addError($e->getMessage());
  74. }
  75. }
  76. else {
  77. $value = http::value($name, "trim");
  78. if($required && empty($value)) $form_exception->addError("$label is required.");
  79. if($values && !in_array($value, $values)) $form_exception->addError("Value for $label is invalid.");
  80. if(in_array($name, $fillable_fields)) {
  81. $test->$name = $value;
  82. }
  83. else {
  84. $test_value = TestValue::get($test->id, $name);
  85. if(!$test_value) {
  86. $test_value = new TestValue;
  87. $test_value->test_id = $test->id;
  88. $test_value->name = $name;
  89. }
  90. $test_value->value = $value;
  91. $success = $test_value->commit();
  92. if(!$success) $form_exception->addError("Failed to save value for $label.");
  93. }
  94. }
  95. }
  96. if($form_exception->hasErrors()) throw $form_exception;
  97. // Before the Test
  98. if($test->status == Test::STATUS_CONFIRMED) {
  99. $test->status = Test::STATUS_STARTED;
  100. $test->dts_start = Util::getDateTime();
  101. $test->dts_updated = Util::getDateTime();
  102. $success = $test->commit();
  103. if(!$success) throw new Exception("Failed to save test. Please try again later.");
  104. HTTP::redirect("/account/tests/$id");
  105. }
  106. // During the test
  107. if($test->status == Test::STATUS_STARTED) {
  108. $test->status = Test::STATUS_PENDING_VERIFICATION;
  109. $unix_start = strtotime($test->dts_start);
  110. $unix_end = $unix_start + floor($duration / 1000); // $duration is milliseconds
  111. $test->dts_end = date("Y-m-d H:i:s", $unix_end);
  112. $test->pass = ($test->challenge_stage <= 2) ? 1 : 0; // Pass = Challenged during browsing or before
  113. $test->dts_updated = Util::getDateTime();
  114. $success = $test->commit();
  115. if(!$success) throw new Exception("Failed to save test. Please try again later.");
  116. JSON::success();
  117. }
  118. }
  119. catch(FormException $e) {
  120. $e->generateAlerts();
  121. }
  122. catch(Exception $e) {
  123. Core::addAlert("danger", $e->getMessage());
  124. }
  125. date_default_timezone_set($previous_timezone);
  126. }
  127. View::includeOnce("header", compact("page_title"));
  128. View::include("test_form", compact("test", "location", "project", "template"));
  129. View::includeOnce("footer");
  130. }
  131. // Verify test
  132. else if(Core::$user->level >= User::LEVEL_ADMIN) {
  133. $json = json_decode($template->json);
  134. $fields = array_merge($json->beforeTest, $json->duringTest);
  135. $values = $test->getValues();
  136. $names = array_map(function($value) { return $value->name; }, $values);
  137. $values = array_combine(
  138. array_map(function($value) { return $value->name; }, $values),
  139. array_map(function($value) { return $value->value; }, $values)
  140. );
  141. $submit = http::value("submit");
  142. $tester_message = http::value("tester_message", "trim");
  143. if($submit) {
  144. try {
  145. Core::validateCSRFToken($submit);
  146. $form_exception = new FormException;
  147. $accept = http::value("accept");
  148. $reject = http::value("reject");
  149. if(empty($test->verifier_id)) {
  150. $test->verifier_id = Core::$user->id;
  151. $test->dts_verified = Util::getDateTime();
  152. }
  153. $test->verifier_notes = http::value("verifier_notes", "trim");
  154. // Modify test values
  155. // Overrides
  156. $unix_start = strtotime($test->dts_start . " UTC");
  157. $unix_end = strtotime($test->dts_end . " UTC");
  158. $pass_override = http::value("pass_override");
  159. if(strlen($pass_override) > 0) {
  160. $test->pass = $pass_override;
  161. }
  162. $start_override = http::value("start_override");
  163. if($start_override) {
  164. $unix_start = strtotime($start_override);
  165. }
  166. $end_override = http::value("end_override");
  167. if($end_override) {
  168. $unix_end = strtotime($end_override);
  169. }
  170. if($unix_start > $unix_end) {
  171. $form_exception->addError("Start time cannot be after end time.");
  172. }
  173. else {
  174. $test->dts_start = Util::getDateTime($unix_start);
  175. $test->dts_end = Util::getDateTime($unix_end);
  176. }
  177. $test->test_fee = null;
  178. $tester_fee = http::value("tester_fee", "int");
  179. if($tester_fee != $project->default_tester_fee) {
  180. $test->test_fee = $tester_fee;
  181. }
  182. // Test forms
  183. foreach($fields as $field) {
  184. if($field->type == "header") continue;
  185. if($field->type == "file") continue;
  186. if(empty($field->name)) continue;
  187. $value = http::value($field->name, "trim");
  188. if(in_array($field->name, $fillable_fields)) $test->{$field->name} = $value;
  189. else {
  190. $test_value = TestValue::get($id, $field->name);
  191. if(!$test_value) {
  192. $test_value = new TestValue;
  193. $test_value->test_id = $id;
  194. $test_value->name = $field->name;
  195. }
  196. $test_value->value = $value;
  197. $test_value->commit();
  198. }
  199. }
  200. // Accept test as completed
  201. if($accept) {
  202. $test->status = Test::STATUS_COMPLETED;
  203. // TODO: Send email to venue/HQ/tester
  204. }
  205. // Reject test
  206. if($reject) {
  207. $test->status = Test::STATUS_CANCELLED;
  208. }
  209. if($form_exception->hasErrors()) throw $form_exception;
  210. $test->dts_updated = Util::getDateTime();
  211. $success = $test->commit();
  212. if(!$success) throw new Exception("Failed to update the test. Please try again later.");
  213. // If rejected, recreate empty test for the location
  214. if($reject) {
  215. $new_test = new Test;
  216. $new_test->project_id = $test->project_id;
  217. $new_test->location_id = $test->location_id;
  218. $new_test->status = Test::STATUS_UNASSIGNED;
  219. $new_test->dts_created = Util::getDateTime();
  220. $success = $new_test->commit();
  221. if(!$success) throw new Exception("Failed to create a new test. Please try again later.");
  222. }
  223. if($accept) {
  224. $tester->score++;
  225. $tester->commit();
  226. }
  227. Core::addAlert("success", "Test verified successfully.");
  228. HTTP::redirect("/account/tests/$id");
  229. }
  230. catch(FormException $e) {
  231. $e->generateAlerts();
  232. }
  233. catch(Exception $e) {
  234. Core::addAlert("danger", $e->getMessage());
  235. }
  236. }
  237. View::includeOnce("header", compact("page_title"));
  238. View::include("test_verify_form", compact("test", "tester", "location", "project", "template", "fields", "values", "tester_message"));
  239. View::includeOnce("footer");
  240. return;
  241. }
  242. // View test results
  243. else if(Core::$user->level >= User::LEVEL_VENUE) {
  244. HTTP::redirect("/account/tests/$id/result");
  245. }
  246. else {
  247. throw new Exception("Invalid request.");
  248. }
  249. ?>