test.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. $tester = $test->getTester();
  11. $template = $project->getTemplate();
  12. $page_title = $location->name;
  13. Core::addBreadcrumb("/account", "Account");
  14. Core::addBreadcrumb("/account/tests", "Tests");
  15. Core::addBreadcrumb("/account/tests/$id", $page_title);
  16. $fillable_fields = ["shop_left", "shop_right", "attire", "challenge_stage", "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact", "additional_notes"];
  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, $fillable_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. $submit = http::value("submit");
  149. $tester_message = http::value("tester_message", "trim");
  150. if($submit) {
  151. try {
  152. Core::validateCSRFToken($submit);
  153. $form_exception = new FormException;
  154. $accept = http::value("accept");
  155. $reject = http::value("reject");
  156. if(empty($test->verifier_id)) {
  157. $test->verifier_id = Core::$user->id;
  158. $test->dts_verified = Util::getDateTime();
  159. }
  160. $test->verifier_notes = http::value("verifier_notes", "trim");
  161. // Modify test values
  162. // Overrides
  163. $unix_start = strtotime($test->dts_start . " UTC");
  164. $unix_end = strtotime($test->dts_end . " UTC");
  165. $pass_override = http::value("pass_override");
  166. if(strlen($pass_override) > 0) {
  167. $test->pass = $pass_override;
  168. }
  169. $start_override = http::getDateTimeFromFields("start_override");
  170. if($start_override) {
  171. $unix_start = strtotime($start_override);
  172. }
  173. $end_override = http::getDateTimeFromFields("end_override");
  174. if($end_override) {
  175. $unix_end = strtotime($end_override);
  176. }
  177. if($unix_start > $unix_end) {
  178. $form_exception->addError("Start time cannot be after end time.");
  179. }
  180. else {
  181. $test->dts_start = Util::getDateTime($unix_start);
  182. $test->dts_end = Util::getDateTime($unix_end);
  183. }
  184. $test->test_fee = null;
  185. $tester_fee = http::value("tester_fee", "float");
  186. if($tester_fee != $project->default_tester_fee) {
  187. $test->test_fee = $tester_fee;
  188. }
  189. // Test forms
  190. foreach($fields as $field) {
  191. if($field->type == "header") continue;
  192. if($field->type == "file") continue;
  193. if(empty($field->name)) continue;
  194. $value = http::value($field->name, "trim");
  195. if(in_array($field->name, $fillable_fields)) $test->{$field->name} = $value;
  196. else {
  197. $test_value = TestValue::get($id, $field->name);
  198. if(!$test_value) {
  199. $test_value = new TestValue;
  200. $test_value->test_id = $id;
  201. $test_value->name = $field->name;
  202. }
  203. $test_value->value = $value;
  204. $test_value->commit();
  205. }
  206. }
  207. // Accept test as completed
  208. if($accept) {
  209. $test->status = Test::STATUS_COMPLETED;
  210. // TODO: Send email to venue/HQ/tester
  211. }
  212. // Reject test
  213. if($reject) {
  214. $test->status = Test::STATUS_CANCELLED;
  215. }
  216. if($form_exception->hasErrors()) throw $form_exception;
  217. $test->dts_updated = Util::getDateTime();
  218. $success = $test->commit();
  219. if(!$success) throw new Exception("Failed to update the test. Please try again later.");
  220. // If rejected, recreate empty test for the location
  221. if($reject) {
  222. $new_test = new Test;
  223. $new_test->project_id = $test->project_id;
  224. $new_test->location_id = $test->location_id;
  225. $new_test->status = Test::STATUS_UNASSIGNED;
  226. $new_test->dts_created = Util::getDateTime();
  227. $success = $new_test->commit();
  228. if(!$success) throw new Exception("Failed to create a new test. Please try again later.");
  229. }
  230. if($accept) {
  231. $tester->score++;
  232. $tester->commit();
  233. }
  234. Core::addAlert("success", "Test verified successfully.");
  235. HTTP::redirect("/account/tests/$id");
  236. }
  237. catch(FormException $e) {
  238. $e->generateAlerts();
  239. }
  240. catch(Exception $e) {
  241. Core::addAlert("danger", $e->getMessage());
  242. }
  243. }
  244. View::includeOnce("header", compact("page_title"));
  245. View::include("test_verify_form", compact("test", "tester", "location", "project", "template", "fields", "values", "tester_message"));
  246. View::includeOnce("footer");
  247. return;
  248. }
  249. // View test results
  250. else if(Core::$user->level >= User::LEVEL_VENUE) {
  251. HTTP::redirect("/account/tests/$id/result");
  252. }
  253. else {
  254. throw new Exception("Invalid request.");
  255. }
  256. ?>