1
0

practice.php 6.1 KB

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