practice.php 6.1 KB

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