practice.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var int $id
  4. */
  5. Core::addScript("public/js/app-practice-test.js");
  6. Core::addScript("public/js/app-test-requirements.js");
  7. $template = Template::get($id);
  8. if(!$template) {
  9. throw new RuntimeException(__("Practice form not found."));
  10. }
  11. if(!$template->practice) {
  12. throw new RuntimeException(__("This practice form is no longer available."));
  13. }
  14. $page_title = __("Practice");
  15. Core::addBreadcrumb("/account", __("Account"));
  16. Core::addBreadcrumb("/account/practice", $page_title);
  17. Core::addBreadcrumb("/account/practice/$id", __("Form"));
  18. $json = json_decode($template->getJSON());
  19. // Reset
  20. $reset = http::value("reset");
  21. $tests = $_SESSION["practice_tests"] ?? [];
  22. $test = $tests[$id] ?? false;
  23. if(!$test || $reset) {
  24. $test = new PracticeTest;
  25. $test->tester_id = Core::$user->id;
  26. $test->status = Test::STATUS_CONFIRMED;
  27. $test->setTemplateId($template->id);
  28. $tests[$id] = $test;
  29. $_SESSION["practice_tests"] = $tests;
  30. if($reset) {
  31. // Force redirect if resetting
  32. http::redirect("/account/practice/$id");
  33. }
  34. }
  35. $is_confirmed = $test->getStatus() === Test::STATUS_CONFIRMED;
  36. $is_started = $test->getStatus() === Test::STATUS_STARTED;
  37. $is_unverified = $test->getStatus() === Test::STATUS_PENDING_VERIFICATION;
  38. $elements = [];
  39. $fields = [];
  40. $submit = http::value("submit");
  41. if($submit) {
  42. $previous_timezone = date_default_timezone_get();
  43. $unix_start = null;
  44. $unix_end = null;
  45. try {
  46. Core::validateCSRFToken($submit);
  47. $form_exception = new FormException;
  48. if($is_confirmed) {
  49. // Parse before the test form
  50. $elements = $json->beforeTest;
  51. }
  52. if($is_started || $is_unverified) {
  53. // Parse after the test form
  54. $elements = $json->duringTest;
  55. // Start/End times
  56. if($is_started) {
  57. $start_override = HTTP::value("start_override");
  58. $end_override = HTTP::value("end_override");
  59. if($start_override && $end_override) {
  60. $dt_start = new DateTime($start_override, new DateTimeZone(TIMEZONE));
  61. $dt_end = new DateTime($end_override, new DateTimeZone(TIMEZONE));
  62. $unix_start = $dt_start->format("U");
  63. $unix_end = $dt_end->format("U");
  64. if($unix_start > $unix_end) $form_exception->addError(__("Entry Time must not be after Exit Time."));
  65. if($unix_start === $unix_end) $form_exception->addError(__("Entry Time must not equal the Exit Time."));
  66. if(!$unix_start && $unix_end && $test->dts_start) {
  67. $recorded_start_unix = Util::getUnixTimeFromUTC($test->dts_start);
  68. if($unix_end < $recorded_start_unix) $form_exception->addError(__("Exit Time cannot be later than the recorded Entry Time."));
  69. }
  70. }
  71. else {
  72. $form_exception->addError(__("Entry Time and Exit Time must be filled in."));
  73. }
  74. }
  75. }
  76. foreach($elements as $element) {
  77. if(isset($element->name)) {
  78. $fields[] = $element;
  79. }
  80. }
  81. if(empty($fields)) {
  82. throw new LogicException(__("No fields can be shown."));
  83. }
  84. foreach($fields as $field) {
  85. $name = $field->name ?? false;
  86. if(empty($name)) continue; // Not a field
  87. $required_key = $field->requiredKey ?? null;
  88. if($required_key) continue;
  89. $label = $field->label ?? "";
  90. $type = $field->type;
  91. $required = $field->required ?? false;
  92. $values = $field->values ?? [];
  93. if($type === "file") {
  94. /** @var array $file */
  95. $file = $_FILES[$name] ?? null;
  96. if($file) {
  97. $file_error = $file["error"];
  98. }
  99. else {
  100. $file_error = UPLOAD_ERR_NO_FILE;
  101. }
  102. try {
  103. if(!$required && (int) $file_error === UPLOAD_ERR_NO_FILE) {
  104. continue;
  105. }
  106. if($required && (int) $file_error === UPLOAD_ERR_NO_FILE) {
  107. throw new Exception(__("%s is required.", $label));
  108. }
  109. // Detect upload error
  110. if((int) $file_error !== UPLOAD_ERR_OK) {
  111. throw new Exception(HTTP::getUploadErrorString($file_error));
  112. }
  113. // Get image details
  114. $size = @getimagesize($file["tmp_name"]);
  115. if(!$size) {
  116. throw new Exception(__("The uploaded file for %s is not an image.", $label));
  117. }
  118. [$width, $height, $type] = $size;
  119. if($type !== IMAGETYPE_JPEG) {
  120. throw new Exception(__("The uploaded file for %s is not a JPEG.", $label));
  121. }
  122. // Resize image
  123. $output_image = Image::normalise($file["tmp_name"]);
  124. // Write image to disk
  125. $output_path = $test->getDirectory() . "/" . $name . ".jpg";
  126. $success = imagejpeg($output_image, $output_path, 60);
  127. imagedestroy($output_image);
  128. if(!$success) throw new Exception(__("Failed to save %s.", $label));
  129. }
  130. catch(Exception $e) {
  131. $form_exception->addError($e->getMessage());
  132. }
  133. }
  134. else {
  135. $value = http::value($name, "trim");
  136. if($required && $value === null) {
  137. $form_exception->addError(__("%s is required.", $label));
  138. }
  139. if($values && !in_array($value, $values, false)) {
  140. $form_exception->addError(__("Value for %s is invalid.", $label));
  141. }
  142. if(in_array($name, Test::TEST_FIELDS, true)) {
  143. $test->$name = $value;
  144. }
  145. else {
  146. $test->setValue($name, $value);
  147. }
  148. }
  149. }
  150. if($form_exception->hasErrors()) throw $form_exception;
  151. date_default_timezone_set("UTC");
  152. // Before the Test
  153. if($is_confirmed) {
  154. $test->status = Test::STATUS_STARTED;
  155. $test->dts_start = Util::getDateTime();
  156. $test->dts_updated = Util::getDateTime();
  157. $tests[$id] = $test;
  158. $_SESSION["practice_tests"] = $tests;
  159. HTTP::redirect("/account/practice/$id");
  160. }
  161. // During the test
  162. if($is_started || $is_unverified) {
  163. $test->status = Test::STATUS_PENDING_VERIFICATION;
  164. if($is_started && $unix_start && $unix_end) {
  165. $test->dts_start = date("Y-m-d H:i:s", $unix_start);
  166. $test->dts_end = date("Y-m-d H:i:s", $unix_end);
  167. }
  168. $test->pass = ($test->challenge_stage <= 2) ? 1 : 0; // Pass = Challenged during browsing or before
  169. $test->dts_updated = Util::getDateTime();
  170. $tests[$id] = $test;
  171. $_SESSION["practice_tests"] = $tests;
  172. JSON::header();
  173. JSON::success();
  174. }
  175. }
  176. catch(FormException $e) {
  177. if(HTTP::isAJAX()) {
  178. JSON::header();
  179. $e->generateJSONError();
  180. }
  181. else {
  182. $e->generateAlerts();
  183. }
  184. }
  185. catch(Exception $e) {
  186. Core::addAlert("danger", $e->getMessage());
  187. }
  188. date_default_timezone_set($previous_timezone);
  189. }
  190. if($is_unverified) {
  191. $fields = array_merge($json->beforeTest, $json->duringTest);
  192. }
  193. View::includeOnce("header", compact("page_title"));
  194. View::include("practice_form", compact("test", "template", "fields"));
  195. View::includeOnce("footer");