test.php 9.1 KB

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