1
0

practice.php 4.6 KB

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