test_tester_form.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. /** @var array $file */
  71. $file = $_FILES[$name] ?? null;
  72. if($file) {
  73. $file_error = $file["error"];
  74. }
  75. else {
  76. $file_error = UPLOAD_ERR_NO_FILE;
  77. }
  78. try {
  79. if(!$required && $file_error === UPLOAD_ERR_NO_FILE) {
  80. continue;
  81. }
  82. if($required && $file_error === UPLOAD_ERR_NO_FILE) {
  83. throw new Exception("$label is required.");
  84. }
  85. // Detect upload error
  86. if($file_error !== UPLOAD_ERR_OK) {
  87. throw new Exception(HTTP::getUploadErrorString($file_error));
  88. }
  89. // Get image details
  90. $size = @getimagesize($file["tmp_name"]);
  91. if(!$size) {
  92. throw new Exception(sprintf(__("The uploaded file for %s is not an image."), $label));
  93. }
  94. [$width, $height, $type] = $size;
  95. if(!in_array($type, [IMAGETYPE_JPEG, IMAGETYPE_PNG])) {
  96. throw new Exception(sprintf(__("The uploaded file for %s is not a JPEG or PNG."), $label));
  97. }
  98. // Resize image
  99. $output_image = Image::normalise($file["tmp_name"]);
  100. // Write image to disk
  101. $output_path = $test->getDirectory() . "/" . $name . ".jpg";
  102. $success = imagejpeg($output_image, $output_path, 60);
  103. imagedestroy($output_image);
  104. if(!$success) throw new Exception(__("Failed to save %s.", $label));
  105. if(!file_exists($output_path)) throw new Exception(__("Failed to save %s."));
  106. }
  107. catch(Exception $e) {
  108. $form_exception->addError($e->getMessage());
  109. }
  110. }
  111. else {
  112. $value = http::value($name, "trim");
  113. if($value === "") $value = null;
  114. if($required && $value === null) $form_exception->addError(sprintf(__("%s is required."), $label));
  115. if($values && !in_array($value, $values, false)) $form_exception->addError(sprintf(__("Value for %s is invalid."), $label));
  116. if(in_array($name, Test::TEST_FIELDS, true)) {
  117. $test->$name = $value;
  118. }
  119. else {
  120. $test_value = TestValue::get($test->id, $name);
  121. if(!$test_value) {
  122. $test_value = new TestValue;
  123. $test_value->test_id = $test->id;
  124. $test_value->name = $name;
  125. }
  126. $test_value->value = $value;
  127. $success = $test_value->commit();
  128. if(!$success) $form_exception->addError(sprintf(__("Failed to save value for %s."), $label));
  129. }
  130. }
  131. }
  132. if($form_exception->hasErrors()) throw $form_exception;
  133. date_default_timezone_set("UTC");
  134. // Before the Test
  135. if($test->isConfirmed()) {
  136. $test->status = Test::STATUS_STARTED;
  137. $test->dts_start = Util::getDateTime();
  138. $test->dts_updated = Util::getDateTime();
  139. $success = $test->commit();
  140. if(!$success) throw new Exception(__("Failed to save test. Please try again later."));
  141. Core::addAlert("success", __("Photos uploaded successfully."));
  142. HTTP::redirect("/account/tests/$id");
  143. }
  144. // During the test
  145. if($has_started || $test->isPendingVerification()) {
  146. $test->status = Test::STATUS_PENDING_VERIFICATION;
  147. if($has_started) {
  148. /**
  149. * @var int $unix_start
  150. * @var int $unix_end
  151. */
  152. if($unix_start && $unix_end) {
  153. $test->dts_start = date("Y-m-d H:i:s", $unix_start);
  154. $test->dts_end = date("Y-m-d H:i:s", $unix_end);
  155. }
  156. else if($unix_end) {
  157. $test->dts_end = date("Y-m-d H:i:s", $unix_end);
  158. }
  159. }
  160. $test->pass = 1;
  161. if($template->hasPassFail()) {
  162. $test->pass = ($test->challenge_stage <= $template->getPassStage()) ? 1 : 0; // Pass = Challenged during browsing or before
  163. }
  164. $test->dts_updated = Util::getDateTime();
  165. $success = $test->commit();
  166. if(!$success) throw new Exception(__("Failed to save test. Please try again later."));
  167. JSON::success();
  168. }
  169. }
  170. catch(FormException $e) {
  171. if(HTTP::isAJAX()) {
  172. $e->generateJSONError();
  173. }
  174. else {
  175. $e->generateAlerts();
  176. }
  177. }
  178. catch(Exception $e) {
  179. if(HTTP::isAJAX()) {
  180. JSON::fail($e->getMessage());
  181. }
  182. else {
  183. Core::addAlert("danger", $e->getMessage());
  184. }
  185. }
  186. date_default_timezone_set($previous_timezone);
  187. }
  188. View::includeOnce("header", compact("page_title"));
  189. View::include("test_form", compact("test", "location", "project", "template"));
  190. View::includeOnce("footer");