practice.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/practice", $page_title);
  8. Core::addBreadcrumb("/account/practice/$id", "Form");
  9. $json = json_decode($template->json);
  10. // Reset
  11. $reset = http::value("reset");
  12. $tests = $_SESSION["practice_tests"] ?? [];
  13. $test = $tests[$id] ?? false;
  14. if(!$test || $reset) {
  15. $test = new PracticeTest;
  16. $test->tester_id = Core::$user->id;
  17. $test->status = Test::STATUS_CONFIRMED;
  18. $tests[$id] = $test;
  19. $_SESSION["practice_tests"] = $tests;
  20. if($reset) {
  21. // Force redirect if resetting
  22. http::redirect("/account/practice/$id");
  23. }
  24. }
  25. $is_confirmed = $test->status == Test::STATUS_CONFIRMED;
  26. $is_started = $test->status == Test::STATUS_STARTED;
  27. $is_unverified = $test->status == Test::STATUS_PENDING_VERIFICATION;
  28. $submit = http::value("submit");
  29. if($submit) {
  30. $previous_timezone = date_default_timezone_get();
  31. date_default_timezone_set("UTC");
  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. if($form_exception->hasErrors()) throw $form_exception;
  92. // Before the Test
  93. if($is_confirmed) {
  94. $test->status = Test::STATUS_STARTED;
  95. $test->dts_start = Util::getDateTime();
  96. $test->dts_updated = Util::getDateTime();
  97. $tests[$id] = $test;
  98. $_SESSION["practice_tests"] = $tests;
  99. HTTP::redirect("/account/practice/$id");
  100. }
  101. // During the test
  102. if($is_started || $is_unverified) {
  103. $test->status = Test::STATUS_PENDING_VERIFICATION;
  104. if($is_started) {
  105. $unix_start = strtotime($test->dts_start);
  106. $unix_end = $unix_start + floor($duration / 1000); // $duration is milliseconds
  107. $test->dts_end = date("Y-m-d H:i:s", $unix_end);
  108. }
  109. $test->pass = ($test->challenge_stage <= 2) ? 1 : 0; // Pass = Challenged during browsing or before
  110. $test->dts_updated = Util::getDateTime();
  111. $tests[$id] = $test;
  112. $_SESSION["practice_tests"] = $tests;
  113. JSON::success();
  114. }
  115. }
  116. catch(FormException $e) {
  117. $e->generateAlerts();
  118. }
  119. catch(Exception $e) {
  120. Core::addAlert("danger", $e->getMessage());
  121. }
  122. date_default_timezone_set($previous_timezone);
  123. }
  124. if($is_unverified) {
  125. $json = json_decode($template->json);
  126. $fields = array_merge($json->beforeTest, $json->duringTest);
  127. }
  128. View::includeOnce("header", compact("page_title"));
  129. View::include("practice_form", compact("test", "template", "fields"));
  130. View::includeOnce("footer");
  131. ?>