test_tester_form.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. $void = HTTP::value("void", "int");
  31. // Parse before the test form
  32. if($test->isConfirmed()) {
  33. $fields = $json->beforeTest;
  34. }
  35. // Parse after the test form
  36. $has_started = $test->hasStarted();
  37. if($has_started || $test->isPendingVerification()) {
  38. $fields = $json->duringTest;
  39. // Start/End times
  40. if($void) {
  41. $unix_start = time();
  42. $unix_end = time();
  43. }
  44. else if($has_started) {
  45. $start_override = HTTP::value("start_override");
  46. $end_override = HTTP::value("end_override");
  47. if($start_override && $end_override) {
  48. $dt_start = new DateTime($start_override, new DateTimeZone(TIMEZONE));
  49. $dt_end = new DateTime($end_override, new DateTimeZone(TIMEZONE));
  50. $unix_start = $dt_start->format("U");
  51. $unix_end = $dt_end->format("U");
  52. if($unix_start > $unix_end) $form_exception->addError(__("Entry Time must not be after Exit Time."));
  53. if($unix_start === $unix_end) $form_exception->addError(__("Entry Time must not equal the Exit Time."));
  54. if(!$unix_start && $unix_end && $test->dts_start) {
  55. $recorded_start_unix = Util::getUnixTimeFromUTC($test->dts_start);
  56. if($unix_end < $recorded_start_unix) $form_exception->addError(__("Exit Time cannot be later than the recorded Entry Time."));
  57. }
  58. }
  59. else {
  60. $form_exception->addError(__("Entry Time and Exit Time must be filled in."));
  61. }
  62. }
  63. }
  64. if(empty($fields)) throw new LogicException(__("No fields can be shown."));
  65. if(!$void) {
  66. foreach($fields as $field) {
  67. $name = $field->name ?? false;
  68. if(empty($name)) continue; // Not a field
  69. $required_key = $field->requiredKey ?? null;
  70. if($required_key && !in_array($required_key, $template_keys, true)) continue;
  71. $label = $field->label ?? "";
  72. $type = $field->type;
  73. $required = $field->required ?? false;
  74. $values = $field->values ?? [];
  75. if($type === "file") {
  76. $file = (object) $_FILES[$name];
  77. try {
  78. if(!$required && $file->error === UPLOAD_ERR_NO_FILE) continue;
  79. if($required && $file->error === UPLOAD_ERR_NO_FILE) {
  80. throw new Exception("$label is required.");
  81. }
  82. // Detect upload error
  83. if($file->error !== UPLOAD_ERR_OK) throw new Exception(HTTP::getUploadErrorString($file->error));
  84. // Get image details
  85. $size = @getimagesize($file->tmp_name);
  86. if(!$size) throw new Exception(sprintf(__("The uploaded file for %s is not an image."), $label));
  87. [$width, $height, $type] = $size;
  88. if($type !== IMAGETYPE_JPEG) throw new Exception(sprintf(__("The uploaded file for %s is not a JPEG."), $label));
  89. // Resize image
  90. $output_image = Image::normalise($file->tmp_name);
  91. // Write image to disk
  92. $output_path = $test->getDirectory() . "/" . $name . ".jpg";
  93. $success = imagejpeg($output_image, $output_path, 60);
  94. imagedestroy($output_image);
  95. if(!$success) throw new Exception(__("Failed to save %s.", $label));
  96. }
  97. catch(Exception $e) {
  98. $form_exception->addError($e->getMessage());
  99. }
  100. }
  101. else {
  102. $value = http::value($name, "trim");
  103. if($value === "") $value = null;
  104. if($required && $value === null) $form_exception->addError(sprintf(__("%s is required."), $label));
  105. if($values && !in_array($value, $values, false)) $form_exception->addError(sprintf(__("Value for %s is invalid."), $label));
  106. if(in_array($name, Test::TEST_FIELDS, true)) {
  107. $test->$name = $value;
  108. }
  109. else {
  110. $test_value = TestValue::get($test->id, $name);
  111. if(!$test_value) {
  112. $test_value = new TestValue;
  113. $test_value->test_id = $test->id;
  114. $test_value->name = $name;
  115. }
  116. $test_value->value = $value;
  117. $success = $test_value->commit();
  118. if(!$success) $form_exception->addError(sprintf(__("Failed to save value for %s."), $label));
  119. }
  120. }
  121. }
  122. }
  123. if($form_exception->hasErrors()) throw $form_exception;
  124. date_default_timezone_set("UTC");
  125. // Before the Test
  126. if($test->isConfirmed()) {
  127. $test->status = Test::STATUS_STARTED;
  128. $test->dts_start = Util::getDateTime();
  129. $test->dts_updated = Util::getDateTime();
  130. $success = $test->commit();
  131. if(!$success) throw new Exception(__("Failed to save test. Please try again later."));
  132. Core::addAlert("success", __("Photos uploaded successfully."));
  133. HTTP::redirect("/account/tests/$id");
  134. }
  135. // During the test
  136. if($has_started || $test->isPendingVerification()) {
  137. $test->status = Test::STATUS_PENDING_VERIFICATION;
  138. if($has_started) {
  139. /**
  140. * @var int $unix_start
  141. * @var int $unix_end
  142. */
  143. if($unix_start && $unix_end) {
  144. $test->dts_start = date("Y-m-d H:i:s", $unix_start);
  145. $test->dts_end = date("Y-m-d H:i:s", $unix_end);
  146. }
  147. else if($unix_end) {
  148. $test->dts_end = date("Y-m-d H:i:s", $unix_end);
  149. }
  150. }
  151. if($void) {
  152. $test->void = 1;
  153. $test->void_notes = HTTP::value("void_notes", "trim");
  154. if(empty($test->void_notes)) throw new Exception(__("Venue Closed Notes is required."));
  155. }
  156. else {
  157. $test->pass = 1;
  158. if($template->hasPassFail()) {
  159. $test->pass = ($test->challenge_stage <= $template->getPassStage()) ? 1 : 0; // Pass = Challenged during browsing or before
  160. }
  161. }
  162. $test->dts_updated = Util::getDateTime();
  163. $success = $test->commit();
  164. if(!$success) throw new Exception(__("Failed to save test. Please try again later."));
  165. JSON::success();
  166. }
  167. }
  168. catch(FormException $e) {
  169. if(HTTP::isAJAX()) {
  170. $e->generateJSONError();
  171. }
  172. else {
  173. $e->generateAlerts();
  174. }
  175. }
  176. catch(Exception $e) {
  177. if(HTTP::isAJAX()) {
  178. JSON::fail($e->getMessage());
  179. }
  180. else {
  181. Core::addAlert("danger", $e->getMessage());
  182. }
  183. }
  184. date_default_timezone_set($previous_timezone);
  185. }
  186. View::includeOnce("header", compact("page_title"));
  187. View::include("test_form", compact("test", "location", "project", "template"));
  188. View::includeOnce("footer");