1
0

practice.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. $file = (object) $_FILES[$name];
  82. try {
  83. if(!$required && (int) $file->error === UPLOAD_ERR_NO_FILE) continue;
  84. if($required && (int) $file->error === UPLOAD_ERR_NO_FILE) {
  85. throw new Exception(__("%s is required.", $label));
  86. }
  87. // Detect upload error
  88. if((int) $file->error !== UPLOAD_ERR_OK) throw new Exception(HTTP::getUploadErrorString($file->error));
  89. // Get image details
  90. $size = @getimagesize($file->tmp_name);
  91. if(!$size) throw new Exception(__("The uploaded file for %s is not an image.", $label));
  92. [$width, $height, $type] = $size;
  93. if($type !== IMAGETYPE_JPEG) throw new Exception(__("The uploaded file for %s is not a JPEG.", $label));
  94. // Resize image
  95. $output_image = Image::normalise($file->tmp_name);
  96. // Write image to disk
  97. $output_path = $test->getDirectory() . "/" . $name . ".jpg";
  98. $success = imagejpeg($output_image, $output_path, 60);
  99. imagedestroy($output_image);
  100. if(!$success) throw new Exception(__("Failed to save %s.", $label));
  101. }
  102. catch(Exception $e) {
  103. $form_exception->addError($e->getMessage());
  104. }
  105. }
  106. else {
  107. $value = http::value($name, "trim");
  108. if($required && $value === null) $form_exception->addError(__("%s is required.", $label));
  109. if($values && !in_array($value, $values, false)) $form_exception->addError(__("Value for %s is invalid.", $label));
  110. if(in_array($name, Test::TEST_FIELDS, true)) {
  111. $test->$name = $value;
  112. }
  113. else {
  114. $test->setValue($name, $value);
  115. }
  116. }
  117. }
  118. if($form_exception->hasErrors()) throw $form_exception;
  119. date_default_timezone_set("UTC");
  120. // Before the Test
  121. if($is_confirmed) {
  122. $test->status = Test::STATUS_STARTED;
  123. $test->dts_start = Util::getDateTime();
  124. $test->dts_updated = Util::getDateTime();
  125. $tests[$id] = $test;
  126. $_SESSION["practice_tests"] = $tests;
  127. HTTP::redirect("/account/practice/$id");
  128. }
  129. // During the test
  130. if($is_started || $is_unverified) {
  131. $test->status = Test::STATUS_PENDING_VERIFICATION;
  132. if($is_started && $unix_start && $unix_end) {
  133. $test->dts_start = date("Y-m-d H:i:s", $unix_start);
  134. $test->dts_end = date("Y-m-d H:i:s", $unix_end);
  135. }
  136. $test->pass = ($test->challenge_stage <= 2) ? 1 : 0; // Pass = Challenged during browsing or before
  137. $test->dts_updated = Util::getDateTime();
  138. $tests[$id] = $test;
  139. $_SESSION["practice_tests"] = $tests;
  140. JSON::header();
  141. JSON::success();
  142. }
  143. }
  144. catch(FormException $e) {
  145. if(HTTP::isAJAX()) {
  146. JSON::header();
  147. $e->generateJSONError();
  148. }
  149. else {
  150. $e->generateAlerts();
  151. }
  152. }
  153. catch(Exception $e) {
  154. Core::addAlert("danger", $e->getMessage());
  155. }
  156. date_default_timezone_set($previous_timezone);
  157. }
  158. if($is_unverified) {
  159. $fields = array_merge($json->beforeTest, $json->duringTest);
  160. }
  161. View::includeOnce("header", compact("page_title"));
  162. View::include("practice_form", compact("test", "template", "fields"));
  163. View::includeOnce("footer");