test_tester_form.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. use Carbon\CarbonImmutable;
  12. Core::$show_footer_content = false;
  13. if((int) Core::$user->getId() !== $test->getTesterId()) throw new Exception(__("You do not have permission to view this page."));
  14. 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."));
  15. // Show completed message
  16. if($test->getStatus() > Test::STATUS_PENDING_VERIFICATION) {
  17. View::includeOnce("header", compact("page_title"));
  18. View::include("test_form", compact("test", "location", "project", "template"));
  19. View::includeOnce("footer");
  20. return;
  21. }
  22. $json = json_decode($template->getJSON());
  23. $submit = http::value("submit");
  24. if($submit) {
  25. $previous_timezone = date_default_timezone_get();
  26. try {
  27. Core::validateCSRFToken($submit);
  28. $form_exception = new FormException();
  29. // Parse before the test form
  30. if($test->isConfirmed()) {
  31. $fields = $json->beforeTest;
  32. }
  33. // Parse after the test form
  34. $has_started = $test->hasStarted();
  35. if($has_started || $test->isPendingVerification()) {
  36. $fields = $json->duringTest;
  37. // Start/End times
  38. if($has_started) {
  39. $start_override = HTTP::value("start_override");
  40. $end_override = HTTP::value("end_override");
  41. if($start_override && $end_override) {
  42. $dt_start = CarbonImmutable::parse($start_override, TIMEZONE);
  43. $dt_end = CarbonImmutable::parse($end_override, TIMEZONE);
  44. if($dt_start > $dt_end) {
  45. $form_exception->addError(__("Entry Time must not be after Exit Time."));
  46. }
  47. if($dt_start === $dt_end) {
  48. $form_exception->addError(__("Entry Time must not equal the Exit Time."));
  49. }
  50. $test->dts_start = $dt_start->utc()->toDateTimeString();
  51. $test->dts_end = $dt_end->utc()->toDateTimeString();
  52. }
  53. else {
  54. $form_exception->addError(__("Entry Time and Exit Time must be filled in."));
  55. }
  56. }
  57. }
  58. if(empty($fields)) {
  59. throw new LogicException(__("No fields can be shown."));
  60. }
  61. foreach($fields as $field) {
  62. $name = $field->name ?? false;
  63. if(empty($name)) continue; // Not a field
  64. $required_key = $field->requiredKey ?? null;
  65. if($required_key && !in_array($required_key, $template_keys, true)) continue;
  66. $label = $field->label ?? "";
  67. $type = $field->type;
  68. $required = $field->required ?? false;
  69. $values = $field->values ?? [];
  70. if($type === "file") {
  71. /** @var array $file */
  72. $file = $_FILES[$name] ?? null;
  73. if($file) {
  74. $file_error = $file["error"];
  75. }
  76. else {
  77. $file_error = UPLOAD_ERR_NO_FILE;
  78. }
  79. try {
  80. if(!$required && $file_error === UPLOAD_ERR_NO_FILE) {
  81. continue;
  82. }
  83. if($required && $file_error === UPLOAD_ERR_NO_FILE) {
  84. throw new Exception("$label is required.");
  85. }
  86. // Detect upload error
  87. if($file_error !== UPLOAD_ERR_OK) {
  88. throw new Exception(HTTP::getUploadErrorString($file_error));
  89. }
  90. // Get image details
  91. $size = @getimagesize($file["tmp_name"]);
  92. if(!$size) {
  93. throw new Exception(sprintf(__("The uploaded file for %s is not an image."), $label));
  94. }
  95. [$width, $height, $type] = $size;
  96. if(!in_array($type, [IMAGETYPE_JPEG, IMAGETYPE_PNG])) {
  97. throw new Exception(sprintf(__("The uploaded file for %s is not a JPEG or PNG."), $label));
  98. }
  99. // Resize image
  100. $output_image = Image::normalise($file["tmp_name"]);
  101. // Write image to disk
  102. $output_path = $test->getDirectory() . "/" . $name . ".jpg";
  103. $success = imagejpeg($output_image, $output_path, 60);
  104. imagedestroy($output_image);
  105. if(!$success) throw new Exception(__("Failed to save %s.", $label));
  106. if(!file_exists($output_path)) throw new Exception(__("Failed to save %s."));
  107. }
  108. catch(Exception $e) {
  109. $form_exception->addError($e->getMessage());
  110. }
  111. }
  112. else {
  113. $value = http::value($name, "trim");
  114. if($value === "") $value = null;
  115. if($required && $value === null) $form_exception->addError(sprintf(__("%s is required."), $label));
  116. if($values && !in_array($value, $values, false)) $form_exception->addError(sprintf(__("Value for %s is invalid."), $label));
  117. if(in_array($name, Test::TEST_FIELDS, true)) {
  118. $test->$name = $value;
  119. }
  120. else {
  121. $test_value = TestValue::get($test->id, $name);
  122. if(!$test_value) {
  123. $test_value = new TestValue;
  124. $test_value->test_id = $test->id;
  125. $test_value->name = $name;
  126. }
  127. $test_value->value = $value;
  128. $success = $test_value->commit();
  129. if(!$success) $form_exception->addError(sprintf(__("Failed to save value for %s."), $label));
  130. }
  131. }
  132. }
  133. $validate_file = $template->getProcessingDirectoryPath() . "/test_validate.php";
  134. if(file_exists($validate_file)) {
  135. require $validate_file;
  136. }
  137. if($form_exception->hasErrors()) {
  138. throw $form_exception;
  139. }
  140. date_default_timezone_set("UTC");
  141. // Before the Test
  142. if($test->isConfirmed()) {
  143. $test->status = Test::STATUS_STARTED;
  144. $test->dts_start = Util::getDateTime();
  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. Core::addAlert("success", __("Photos uploaded successfully."));
  149. HTTP::redirect("/account/tests/$id");
  150. }
  151. // During the test
  152. if($has_started || $test->isPendingVerification()) {
  153. $test->status = Test::STATUS_PENDING_VERIFICATION;
  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. $test->dts_updated = Util::getDateTime();
  159. $success = $test->commit();
  160. if(!$success) throw new Exception(__("Failed to save test. Please try again later."));
  161. JSON::success();
  162. }
  163. }
  164. catch(FormException $e) {
  165. if(HTTP::isAJAX()) {
  166. $e->generateJSONError();
  167. }
  168. else {
  169. $e->generateAlerts();
  170. }
  171. }
  172. catch(Exception $e) {
  173. if(HTTP::isAJAX()) {
  174. JSON::fail($e->getMessage());
  175. }
  176. else {
  177. Core::addAlert("danger", $e->getMessage());
  178. }
  179. }
  180. date_default_timezone_set($previous_timezone);
  181. }
  182. View::includeOnce("header", compact("page_title"));
  183. View::include("test_form", compact("test", "location", "project", "template"));
  184. View::includeOnce("footer");