test_tester_form.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var int $id
  4. * @var Test $test
  5. * @var string $page_title
  6. * @var Location $location
  7. * @var Project $project
  8. * @var Template $template
  9. * @var string[] $template_keys
  10. */
  11. $is_confirmed = $test->status == Test::STATUS_CONFIRMED;
  12. $is_started = $test->status == Test::STATUS_STARTED;
  13. $is_unverified = $test->status == Test::STATUS_PENDING_VERIFICATION;
  14. Core::$show_footer_content = false;
  15. if(Core::$user->id != $test->tester_id) throw new Exception(__("You do not have permission to view this page."));
  16. if($test->status == Test::STATUS_ASSIGNED) throw new Exception(__("You do not have permission to view this page as you have not accepted the test yet."));
  17. // Show completed message
  18. if($test->status > Test::STATUS_PENDING_VERIFICATION) {
  19. View::includeOnce("header", compact("page_title"));
  20. View::include("test_form", compact("test", "location", "project", "template"));
  21. View::includeOnce("footer");
  22. return;
  23. }
  24. $json = json_decode($template->getJSON());
  25. $submit = http::value("submit");
  26. if($submit) {
  27. $previous_timezone = date_default_timezone_get();
  28. try {
  29. Core::validateCSRFToken($submit);
  30. $form_exception = new FormException;
  31. if($is_confirmed) {
  32. // Parse before the test form
  33. $fields = $json->beforeTest;
  34. }
  35. if($is_started || $is_unverified) {
  36. // Parse after the test form
  37. $fields = $json->duringTest;
  38. // Start/End times
  39. if($is_started) {
  40. $start_override = http::getDateTimeFromFields("start_override");
  41. $end_override = http::getDateTimeFromFields("end_override");
  42. $start_unix = null;
  43. $end_unix = null;
  44. if($start_override && $end_override) {
  45. $start_unix = strtotime($start_override);
  46. $end_unix = strtotime($end_override);
  47. if($start_unix > $end_unix) $form_exception->addError(__("Entry Time must not be after Exit Time."));
  48. if(!$start_unix && $end_unix && $test->dts_start) {
  49. $recorded_start_unix = Util::getUnixTimeFromUTC($test->dts_start);
  50. if($end_unix < $recorded_start_unix) $form_exception->addError(__("Exit Time cannot be later than the recorded Entry Time."));
  51. }
  52. }
  53. else {
  54. $form_exception->addError(__("Entry Time and Exit Time must be filled in."));
  55. }
  56. }
  57. }
  58. if(empty($fields)) throw new LogicException(__("No fields can be shown."));
  59. foreach($fields as $field) {
  60. $name = $field->name ?? false;
  61. if(empty($name)) continue; // Not a field
  62. $required_key = $field->requiredKey ?? null;
  63. if($required_key && !in_array($required_key, $template_keys)) continue;
  64. $label = $field->label ?? "";
  65. $type = $field->type;
  66. $required = $field->required ?? false;
  67. $values = $field->values ?? [];
  68. if($type == "file") {
  69. $file = (object) $_FILES[$name];
  70. try {
  71. if(!$required && $file->error == UPLOAD_ERR_NO_FILE) continue;
  72. if($required && $file->error == UPLOAD_ERR_NO_FILE) {
  73. throw new Exception("$label is required.");
  74. }
  75. // Detect upload error
  76. if($file->error != UPLOAD_ERR_OK) throw new Exception(HTTP::getUploadErrorString($file->error));
  77. // Get image details
  78. $size = @getimagesize($file->tmp_name);
  79. if(!$size) throw new Exception(sprintf(__("The uploaded file for %s is not an image."), $label));
  80. [$width, $height, $type] = $size;
  81. if($type != IMAGETYPE_JPEG) throw new Exception(sprintf(__("The uploaded file for %s is not a JPEG."), $label));
  82. // Resize image
  83. $output_image = Image::normalise($file->tmp_name);
  84. // Write image to disk
  85. $output_path = $test->getDirectory() . "/" . $name . ".jpg";
  86. imagejpeg($output_image, $output_path, 60);
  87. imagedestroy($output_image);
  88. }
  89. catch(Exception $e) {
  90. $form_exception->addError($e->getMessage());
  91. }
  92. }
  93. else {
  94. $value = http::value($name, "trim");
  95. if($required && $value === null) $form_exception->addError(sprintf(__("%s is required."), $label));
  96. if($values && !in_array($value, $values)) $form_exception->addError(sprintf(__("Value for %s is invalid."), $label));
  97. if(in_array($name, Test::TEST_FIELDS)) {
  98. $test->$name = $value;
  99. }
  100. else {
  101. $test_value = TestValue::get($test->id, $name);
  102. if(!$test_value) {
  103. $test_value = new TestValue;
  104. $test_value->test_id = $test->id;
  105. $test_value->name = $name;
  106. }
  107. $test_value->value = $value;
  108. $success = $test_value->commit();
  109. if(!$success) $form_exception->addError(sprintf(__("Failed to save value for %s."), $label));
  110. }
  111. }
  112. }
  113. if($form_exception->hasErrors()) throw $form_exception;
  114. date_default_timezone_set("UTC");
  115. // Before the Test
  116. if($is_confirmed) {
  117. $test->status = Test::STATUS_STARTED;
  118. $test->dts_start = Util::getDateTime();
  119. $test->dts_updated = Util::getDateTime();
  120. $success = $test->commit();
  121. if(!$success) throw new Exception(__("Failed to save test. Please try again later."));
  122. Core::addAlert("success", __("Photos uploaded successfully."));
  123. HTTP::redirect("/account/tests/$id");
  124. }
  125. // During the test
  126. if($is_started || $is_unverified) {
  127. $test->status = Test::STATUS_PENDING_VERIFICATION;
  128. if($is_started) {
  129. /**
  130. * @var int $start_unix
  131. * @var int $end_unix
  132. */
  133. if($start_unix && $end_unix) {
  134. $test->dts_start = date("Y-m-d H:i:s", $start_unix);
  135. $test->dts_end = date("Y-m-d H:i:s", $end_unix);
  136. }
  137. else if($end_unix) {
  138. $test->dts_end = date("Y-m-d H:i:s", $end_unix);
  139. }
  140. }
  141. $test->pass = 1;
  142. if($template->hasPassFail()) {
  143. $test->pass = ($test->challenge_stage <= $template->getPassStage()) ? 1 : 0; // Pass = Challenged during browsing or before
  144. }
  145. $test->dts_updated = Util::getDateTime();
  146. $success = $test->commit();
  147. if(!$success) throw new Exception(__("Failed to save test. Please try again later."));
  148. JSON::success();
  149. }
  150. }
  151. catch(FormException $e) {
  152. if(HTTP::isAJAX()) {
  153. $e->generateJSONError();
  154. }
  155. else {
  156. $e->generateAlerts();
  157. }
  158. }
  159. catch(Exception $e) {
  160. if(HTTP::isAJAX()) {
  161. JSON::fail($e->getMessage());
  162. }
  163. else {
  164. Core::addAlert("danger", $e->getMessage());
  165. }
  166. }
  167. date_default_timezone_set($previous_timezone);
  168. }
  169. View::includeOnce("header", compact("page_title"));
  170. View::include("test_form", compact("test", "location", "project", "template"));
  171. View::includeOnce("footer");