| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php /** @noinspection PhpUnhandledExceptionInspection */
- /**
- * @var int $id
- * @var Test $test
- * @var string $page_title
- * @var Location $location
- * @var Project $project
- * @var Template $template
- * @var string[] $template_keys
- */
- Core::$show_footer_content = false;
- if((int) Core::$user->getId() !== $test->getTesterId()) throw new Exception(__("You do not have permission to view this page."));
- 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."));
- // Show completed message
- if($test->getStatus() > Test::STATUS_PENDING_VERIFICATION) {
- View::includeOnce("header", compact("page_title"));
- View::include("test_form", compact("test", "location", "project", "template"));
- View::includeOnce("footer");
- return;
- }
- $json = json_decode($template->getJSON());
- $submit = http::value("submit");
- if($submit) {
- $previous_timezone = date_default_timezone_get();
- $unix_start = null;
- $unix_end = null;
- try {
- Core::validateCSRFToken($submit);
- $form_exception = new FormException;
- $void = HTTP::value("void", "int");
- // Parse before the test form
- if($test->isConfirmed()) {
- $fields = $json->beforeTest;
- }
- // Parse after the test form
- $has_started = $test->hasStarted();
- if($has_started || $test->isPendingVerification()) {
- $fields = $json->duringTest;
- // Start/End times
- if($void) {
- $unix_start = time();
- $unix_end = time();
- }
- else if($has_started) {
- $start_override = HTTP::value("start_override");
- $end_override = HTTP::value("end_override");
- if($start_override && $end_override) {
- $dt_start = new DateTime($start_override, new DateTimeZone(TIMEZONE));
- $dt_end = new DateTime($end_override, new DateTimeZone(TIMEZONE));
- $unix_start = $dt_start->format("U");
- $unix_end = $dt_end->format("U");
- if($unix_start > $unix_end) $form_exception->addError(__("Entry Time must not be after Exit Time."));
- if($unix_start === $unix_end) $form_exception->addError(__("Entry Time must not equal the Exit Time."));
- if(!$unix_start && $unix_end && $test->dts_start) {
- $recorded_start_unix = Util::getUnixTimeFromUTC($test->dts_start);
- if($unix_end < $recorded_start_unix) $form_exception->addError(__("Exit Time cannot be later than the recorded Entry Time."));
- }
- }
- else {
- $form_exception->addError(__("Entry Time and Exit Time must be filled in."));
- }
- }
- }
- if(empty($fields)) throw new LogicException(__("No fields can be shown."));
- if(!$void) {
- foreach($fields as $field) {
- $name = $field->name ?? false;
- if(empty($name)) continue; // Not a field
- $required_key = $field->requiredKey ?? null;
- if($required_key && !in_array($required_key, $template_keys, true)) continue;
- $label = $field->label ?? "";
- $type = $field->type;
- $required = $field->required ?? false;
- $values = $field->values ?? [];
- if($type === "file") {
- $file = (object) $_FILES[$name];
- try {
- if(!$required && $file->error === UPLOAD_ERR_NO_FILE) continue;
- if($required && $file->error === UPLOAD_ERR_NO_FILE) {
- throw new Exception("$label is required.");
- }
- // Detect upload error
- if($file->error !== UPLOAD_ERR_OK) throw new Exception(HTTP::getUploadErrorString($file->error));
- // Get image details
- $size = @getimagesize($file->tmp_name);
- if(!$size) throw new Exception(sprintf(__("The uploaded file for %s is not an image."), $label));
- [$width, $height, $type] = $size;
- if($type !== IMAGETYPE_JPEG) throw new Exception(sprintf(__("The uploaded file for %s is not a JPEG."), $label));
- // Resize image
- $output_image = Image::normalise($file->tmp_name);
- // Write image to disk
- $output_path = $test->getDirectory() . "/" . $name . ".jpg";
- $success = imagejpeg($output_image, $output_path, 60);
- imagedestroy($output_image);
- if(!$success) throw new Exception(__("Failed to save %s.", $label));
- if(!file_exists($output_path)) throw new Exception(__("Failed to save %s."));
- }
- catch(Exception $e) {
- $form_exception->addError($e->getMessage());
- }
- }
- else {
- $value = http::value($name, "trim");
- if($value === "") $value = null;
- if($required && $value === null) $form_exception->addError(sprintf(__("%s is required."), $label));
- if($values && !in_array($value, $values, false)) $form_exception->addError(sprintf(__("Value for %s is invalid."), $label));
- if(in_array($name, Test::TEST_FIELDS, true)) {
- $test->$name = $value;
- }
- else {
- $test_value = TestValue::get($test->id, $name);
- if(!$test_value) {
- $test_value = new TestValue;
- $test_value->test_id = $test->id;
- $test_value->name = $name;
- }
- $test_value->value = $value;
- $success = $test_value->commit();
- if(!$success) $form_exception->addError(sprintf(__("Failed to save value for %s."), $label));
- }
- }
- }
- }
- if($form_exception->hasErrors()) throw $form_exception;
- date_default_timezone_set("UTC");
- // Before the Test
- if($test->isConfirmed()) {
- $test->status = Test::STATUS_STARTED;
- $test->dts_start = Util::getDateTime();
- $test->dts_updated = Util::getDateTime();
- $success = $test->commit();
- if(!$success) throw new Exception(__("Failed to save test. Please try again later."));
- Core::addAlert("success", __("Photos uploaded successfully."));
- HTTP::redirect("/account/tests/$id");
- }
- // During the test
- if($has_started || $test->isPendingVerification()) {
- $test->status = Test::STATUS_PENDING_VERIFICATION;
- if($has_started) {
- /**
- * @var int $unix_start
- * @var int $unix_end
- */
- if($unix_start && $unix_end) {
- $test->dts_start = date("Y-m-d H:i:s", $unix_start);
- $test->dts_end = date("Y-m-d H:i:s", $unix_end);
- }
- else if($unix_end) {
- $test->dts_end = date("Y-m-d H:i:s", $unix_end);
- }
- }
- if($void) {
- $test->void = 1;
- $test->void_notes = HTTP::value("void_notes", "trim");
- if(empty($test->void_notes)) throw new Exception(__("Venue Closed Notes is required."));
- }
- else {
- $test->pass = 1;
- if($template->hasPassFail()) {
- $test->pass = ($test->challenge_stage <= $template->getPassStage()) ? 1 : 0; // Pass = Challenged during browsing or before
- }
- }
- $test->dts_updated = Util::getDateTime();
- $success = $test->commit();
- if(!$success) throw new Exception(__("Failed to save test. Please try again later."));
- JSON::success();
- }
- }
- catch(FormException $e) {
- if(HTTP::isAJAX()) {
- $e->generateJSONError();
- }
- else {
- $e->generateAlerts();
- }
- }
- catch(Exception $e) {
- if(HTTP::isAJAX()) {
- JSON::fail($e->getMessage());
- }
- else {
- Core::addAlert("danger", $e->getMessage());
- }
- }
- date_default_timezone_set($previous_timezone);
- }
- View::includeOnce("header", compact("page_title"));
- View::include("test_form", compact("test", "location", "project", "template"));
- View::includeOnce("footer");
|