test.php 8.8 KB

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