practice.php 5.4 KB

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