test_tester_form.php 6.4 KB

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