1
0

test.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. $success = $test->commit();
  102. if(!$success) throw new Exception("Failed to save test. Please try again later.");
  103. HTTP::redirect("/account/tests/$id");
  104. }
  105. // During the test
  106. if($test->status == Test::STATUS_STARTED) {
  107. $test->status = Test::STATUS_PENDING_VERIFICATION;
  108. $unix_start = strtotime($test->dts_start);
  109. $unix_end = $unix_start + floor($duration / 1000); // $duration is milliseconds
  110. $test->dts_end = date("Y-m-d H:i:s", $unix_end);
  111. $test->pass = ($test->challenge_stage <= 2) ? 1 : 0; // Pass = Challenged during browsing or before
  112. $success = $test->commit();
  113. if(!$success) throw new Exception("Failed to save test. Please try again later.");
  114. JSON::success();
  115. }
  116. }
  117. catch(FormException $e) {
  118. $e->generateAlerts();
  119. }
  120. catch(Exception $e) {
  121. Core::addAlert("danger", $e->getMessage());
  122. }
  123. date_default_timezone_set($previous_timezone);
  124. }
  125. View::includeOnce("header", compact("page_title"));
  126. View::include("test_form", compact("test", "location", "project", "template"));
  127. View::includeOnce("footer");
  128. }
  129. // Verify test
  130. else if(Core::$user->level >= User::LEVEL_ADMIN) {
  131. $json = json_decode($template->json);
  132. $fields = array_merge($json->beforeTest, $json->duringTest);
  133. $values = $test->getValues();
  134. $names = array_map(function($value) { return $value->name; }, $values);
  135. $values = array_combine(
  136. array_map(function($value) { return $value->name; }, $values),
  137. array_map(function($value) { return $value->value; }, $values)
  138. );
  139. $submit = http::value("submit");
  140. $tester_message = http::value("tester_message", "trim");
  141. if($submit) {
  142. try {
  143. Core::validateCSRFToken($submit);
  144. $form_exception = new FormException;
  145. $accept = http::value("accept");
  146. $reject = http::value("reject");
  147. if(empty($test->verifier_id)) {
  148. $test->verifier_id = Core::$user->id;
  149. $test->dts_verified = Util::getDateTime();
  150. }
  151. $test->verifier_notes = http::value("verifier_notes", "trim");
  152. // Modify test values
  153. // Overrides
  154. $unix_start = strtotime($test->dts_start . " UTC");
  155. $unix_end = strtotime($test->dts_end . " UTC");
  156. $pass_override = http::value("pass_override");
  157. if(strlen($pass_override) > 0) {
  158. $test->pass = $pass_override;
  159. }
  160. $start_override = http::value("start_override");
  161. if($start_override) {
  162. $unix_start = strtotime($start_override);
  163. }
  164. $end_override = http::value("end_override");
  165. if($end_override) {
  166. $unix_end = strtotime($end_override);
  167. }
  168. if($unix_start > $unix_end) {
  169. $form_exception->addError("Start time cannot be after end time.");
  170. }
  171. else {
  172. $test->dts_start = Util::getDateTime($unix_start);
  173. $test->dts_end = Util::getDateTime($unix_end);
  174. }
  175. $test->test_fee = null;
  176. $tester_fee = http::value("tester_fee", "int");
  177. if($tester_fee != $project->default_tester_fee) {
  178. $test->test_fee = $tester_fee;
  179. }
  180. // Test forms
  181. foreach($fields as $field) {
  182. if($field->type == "header") continue;
  183. if($field->type == "file") continue;
  184. if(empty($field->name)) continue;
  185. $value = http::value($field->name, "trim");
  186. if(in_array($field->name, $fillable_fields)) $test->{$field->name} = $value;
  187. else {
  188. $test_value = TestValue::get($id, $field->name);
  189. if(!$test_value) {
  190. $test_value = new TestValue;
  191. $test_value->test_id = $id;
  192. $test_value->name = $field->name;
  193. }
  194. $test_value->value = $value;
  195. $test_value->commit();
  196. }
  197. }
  198. // Accept test as completed
  199. if($accept) {
  200. $test->status = Test::STATUS_COMPLETED;
  201. // TODO: Send email to venue/HQ/tester
  202. }
  203. // Reject test
  204. if($reject) {
  205. $test->status = Test::STATUS_CANCELLED;
  206. }
  207. if($form_exception->hasErrors()) throw $form_exception;
  208. $success = $test->commit();
  209. if(!$success) throw new Exception("Failed to update the test. Please try again later.");
  210. // If rejected, recreate empty test for the location
  211. if($reject) {
  212. $new_test = new Test;
  213. $new_test->project_id = $test->project_id;
  214. $new_test->location_id = $test->location_id;
  215. $new_test->status = Test::STATUS_UNASSIGNED;
  216. $new_test->dts_created = Util::getDateTime();
  217. $success = $new_test->commit();
  218. if(!$success) throw new Exception("Failed to create a new test. Please try again later.");
  219. }
  220. if($accept) {
  221. $tester->score++;
  222. $tester->commit();
  223. }
  224. Core::addAlert("success", "Test verified successfully.");
  225. HTTP::redirect("/account/tests/$id");
  226. }
  227. catch(FormException $e) {
  228. $e->generateAlerts();
  229. }
  230. catch(Exception $e) {
  231. Core::addAlert("danger", $e->getMessage());
  232. }
  233. }
  234. View::includeOnce("header", compact("page_title"));
  235. View::include("test_verify_form", compact("test", "tester", "location", "project", "template", "fields", "values", "tester_message"));
  236. View::includeOnce("footer");
  237. return;
  238. }
  239. // View test results
  240. else if(Core::$user->level >= User::LEVEL_VENUE) {
  241. HTTP::redirect("/account/tests/$id/result");
  242. }
  243. else {
  244. throw new Exception("Invalid request.");
  245. }
  246. ?>