1
0

practice.php 5.4 KB

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