1
0

test_tester_form.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. Core::$show_footer_content = false;
  12. if((int) Core::$user->getId() !== $test->getTesterId()) throw new Exception(__("You do not have permission to view this page."));
  13. if($test->getStatus() === Test::STATUS_ASSIGNED) throw new Exception(__("You do not have permission to view this page as you have not accepted the test yet."));
  14. // Show completed message
  15. if($test->getStatus() > Test::STATUS_PENDING_VERIFICATION) {
  16. View::includeOnce("header", compact("page_title"));
  17. View::include("test_form", compact("test", "location", "project", "template"));
  18. View::includeOnce("footer");
  19. return;
  20. }
  21. $json = json_decode($template->getJSON());
  22. $submit = http::value("submit");
  23. if($submit) {
  24. $previous_timezone = date_default_timezone_get();
  25. $unix_start = null;
  26. $unix_end = null;
  27. try {
  28. Core::validateCSRFToken($submit);
  29. $form_exception = new FormException;
  30. // Parse before the test form
  31. if($test->isConfirmed()) {
  32. $fields = $json->beforeTest;
  33. }
  34. // Parse after the test form
  35. $has_started = $test->hasStarted();
  36. if($has_started || $test->isPendingVerification()) {
  37. $fields = $json->duringTest;
  38. // Start/End times
  39. if($has_started) {
  40. $start_override = HTTP::value("start_override");
  41. $end_override = HTTP::value("end_override");
  42. if($start_override && $end_override) {
  43. $dt_start = new DateTime($start_override, new DateTimeZone(TIMEZONE));
  44. $dt_end = new DateTime($end_override, new DateTimeZone(TIMEZONE));
  45. $unix_start = $dt_start->format("U");
  46. $unix_end = $dt_end->format("U");
  47. if($unix_start > $unix_end) $form_exception->addError(__("Entry Time must not be after Exit Time."));
  48. if($unix_start === $unix_end) $form_exception->addError(__("Entry Time must not equal the Exit Time."));
  49. if(!$unix_start && $unix_end && $test->dts_start) {
  50. $recorded_start_unix = Util::getUnixTimeFromUTC($test->dts_start);
  51. if($unix_end < $recorded_start_unix) $form_exception->addError(__("Exit Time cannot be later than the recorded Entry Time."));
  52. }
  53. }
  54. else {
  55. $form_exception->addError(__("Entry Time and Exit Time must be filled in."));
  56. }
  57. }
  58. }
  59. if(empty($fields)) throw new LogicException(__("No fields can be shown."));
  60. foreach($fields as $field) {
  61. $name = $field->name ?? false;
  62. if(empty($name)) continue; // Not a field
  63. $required_key = $field->requiredKey ?? null;
  64. if($required_key && !in_array($required_key, $template_keys, true)) continue;
  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("$label is required.");
  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. [$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. $success = imagejpeg($output_image, $output_path, 60);
  88. imagedestroy($output_image);
  89. if(!$success) throw new Exception(__("Failed to save %s.", $label));
  90. if(!file_exists($output_path)) throw new Exception(__("Failed to save %s."));
  91. }
  92. catch(Exception $e) {
  93. $form_exception->addError($e->getMessage());
  94. }
  95. }
  96. else {
  97. $value = http::value($name, "trim");
  98. if($value === "") $value = null;
  99. if($required && $value === null) $form_exception->addError(sprintf(__("%s is required."), $label));
  100. if($values && !in_array($value, $values, false)) $form_exception->addError(sprintf(__("Value for %s is invalid."), $label));
  101. if(in_array($name, Test::TEST_FIELDS, true)) {
  102. $test->$name = $value;
  103. }
  104. else {
  105. $test_value = TestValue::get($test->id, $name);
  106. if(!$test_value) {
  107. $test_value = new TestValue;
  108. $test_value->test_id = $test->id;
  109. $test_value->name = $name;
  110. }
  111. $test_value->value = $value;
  112. $success = $test_value->commit();
  113. if(!$success) $form_exception->addError(sprintf(__("Failed to save value for %s."), $label));
  114. }
  115. }
  116. }
  117. if($form_exception->hasErrors()) throw $form_exception;
  118. date_default_timezone_set("UTC");
  119. // Before the Test
  120. if($test->isConfirmed()) {
  121. $test->status = Test::STATUS_STARTED;
  122. $test->dts_start = Util::getDateTime();
  123. $test->dts_updated = Util::getDateTime();
  124. $success = $test->commit();
  125. if(!$success) throw new Exception(__("Failed to save test. Please try again later."));
  126. Core::addAlert("success", __("Photos uploaded successfully."));
  127. HTTP::redirect("/account/tests/$id");
  128. }
  129. // During the test
  130. if($has_started || $test->isPendingVerification()) {
  131. $test->status = Test::STATUS_PENDING_VERIFICATION;
  132. if($has_started) {
  133. /**
  134. * @var int $unix_start
  135. * @var int $unix_end
  136. */
  137. if($unix_start && $unix_end) {
  138. $test->dts_start = date("Y-m-d H:i:s", $unix_start);
  139. $test->dts_end = date("Y-m-d H:i:s", $unix_end);
  140. }
  141. else if($unix_end) {
  142. $test->dts_end = date("Y-m-d H:i:s", $unix_end);
  143. }
  144. }
  145. $test->pass = 1;
  146. if($template->hasPassFail()) {
  147. $test->pass = ($test->challenge_stage <= $template->getPassStage()) ? 1 : 0; // Pass = Challenged during browsing or before
  148. }
  149. $test->dts_updated = Util::getDateTime();
  150. $success = $test->commit();
  151. if(!$success) throw new Exception(__("Failed to save test. Please try again later."));
  152. JSON::success();
  153. }
  154. }
  155. catch(FormException $e) {
  156. if(HTTP::isAJAX()) {
  157. $e->generateJSONError();
  158. }
  159. else {
  160. $e->generateAlerts();
  161. }
  162. }
  163. catch(Exception $e) {
  164. if(HTTP::isAJAX()) {
  165. JSON::fail($e->getMessage());
  166. }
  167. else {
  168. Core::addAlert("danger", $e->getMessage());
  169. }
  170. }
  171. date_default_timezone_set($previous_timezone);
  172. }
  173. View::includeOnce("header", compact("page_title"));
  174. View::include("test_form", compact("test", "location", "project", "template"));
  175. View::includeOnce("footer");