1
0

practice.php 5.3 KB

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