1
0

practice.php 5.5 KB

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