status == Test::STATUS_CONFIRMED; $is_started = $test->status == Test::STATUS_STARTED; $is_unverified = $test->status == Test::STATUS_PENDING_VERIFICATION; $location = $test->getLocation(); $project = $test->getProject(); $customer = $project->getCustomer(); $tester = $test->getTester(); $template = $project->getTemplate(); $template_overrides = $project->getTemplateOverrides(); if($template_overrides) { $template->setOverrides($template_overrides); } $page_title = $location->name; Core::addBreadcrumb("/account", "Account"); Core::addBreadcrumb("/account/tests", "Tests"); Core::addBreadcrumb("/account/tests/$id", $page_title); // Do test if(Core::$user->level == User::LEVEL_TESTER) { Core::$show_footer_content = false; if(Core::$user->id != $test->tester_id) throw new Exception("You do not have permission to view this page."); if($test->status == 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->status > 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->json); $submit = http::value("submit"); if($submit) { $previous_timezone = date_default_timezone_get(); try { Core::validateCSRFToken($submit); $form_exception = new FormException; if($is_confirmed) { // Parse before the test form $fields = $json->beforeTest; } if($is_started || $is_unverified) { // Parse after the test form $fields = $json->duringTest; // Start/End times if($is_started) { $start_override = http::getDateTimeFromFields("start_override"); $end_override = http::getDateTimeFromFields("end_override"); $start_unix = null; $end_unix = null; if($start_override && $end_override) { $start_unix = strtotime($start_override); $end_unix = strtotime($end_override); if($start_unix > $end_unix) $form_exception->addError("Entry Time must not be after Exit Time."); if(!$start_unix && $end_unix && $test->dts_start) { $recorded_start_unix = Util::getUnixTimeFromUTC($test->dts_start); if($end_unix < $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."); } } } foreach($fields as $field) { $name = $field->name ?? false; if(empty($name)) continue; // Not a field $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("The uploaded file for $label is not an image."); list($width, $height, $type) = $size; if($type != IMAGETYPE_JPEG) throw new Exception("The uploaded file for $label is not a JPEG."); // Resize image $output_image = Image::normalise($file->tmp_name); // Write image to disk $output_path = $test->getDirectory() . "/" . $name . ".jpg"; imagejpeg($output_image, $output_path, 60); imagedestroy($output_image); } catch(Exception $e) { $form_exception->addError($e->getMessage()); } } else { $value = http::value($name, "trim"); if($required && empty($value)) $form_exception->addError("$label is required."); if($values && !in_array($value, $values)) $form_exception->addError("Value for $label is invalid."); if(in_array($name, Test::TEST_FIELDS)) { $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("Failed to save value for $label."); } } } if($form_exception->hasErrors()) throw $form_exception; date_default_timezone_set("UTC"); // Before the Test if($is_confirmed) { $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($is_started || $is_unverified) { $test->status = Test::STATUS_PENDING_VERIFICATION; if($is_started) { if($start_unix && $end_unix) { $test->dts_start = date("Y-m-d H:i:s", $start_unix); $test->dts_end = date("Y-m-d H:i:s", $end_unix); } else if($end_unix) { $test->dts_end = date("Y-m-d H:i:s", $end_unix); } } $test->pass = ($test->challenge_stage <= $template->pass_stage) ? 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"); } // Verify test else if(Core::$user->level >= User::LEVEL_ADMIN) { $json = json_decode($template->json); $fields = array_merge($json->beforeTest, $json->duringTest); $values = $test->getValues(); $names = array_map(function($value) { return $value->name; }, $values); $values = array_combine( array_map(function($value) { return $value->name; }, $values), array_map(function($value) { return $value->value; }, $values) ); $directory = $test->getDirectory(); $has_pdf = file_exists($directory . "/pdf.pdf"); $submit = http::value("submit"); $tester_message = http::value("tester_message", "trim"); if($submit) { try { Core::validateCSRFToken($submit); $form_exception = new FormException; $accept = http::value("accept"); $reject = http::value("reject"); if(($accept || $reject) && $test->status != Test::STATUS_PENDING_VERIFICATION) { throw new Exception("This test is no longer pending verification."); } if(empty($test->verifier_id)) { $test->verifier_id = Core::$user->id; $test->dts_verified = Util::getDateTime(); } $test->verifier_notes = http::value("verifier_notes", "trim"); // Check selected retest project $retest_project = false; $retest_project_id = http::value("retest_project_id", "int"); if($retest_project_id) { $retest_project = Project::get($retest_project_id); if(!$retest_project || $retest_project->customer_id != $customer->id) throw new Exception("The retest project selected is invalid."); } // Modify test values // Overrides $unix_start = strtotime($test->dts_start . " UTC"); $unix_end = strtotime($test->dts_end . " UTC"); $pass_override = http::value("pass_override"); if(strlen($pass_override) > 0) { $test->pass = $pass_override; } $start_override = http::getDateTimeFromFields("start_override"); if($start_override) { $unix_start = strtotime($start_override); } $end_override = http::getDateTimeFromFields("end_override"); if($end_override) { $unix_end = strtotime($end_override); } if($unix_start > $unix_end) { $form_exception->addError("Start time cannot be after end time."); } else { $test->dts_start = Util::getDateTime($unix_start); $test->dts_end = Util::getDateTime($unix_end); } $test->test_fee = null; $tester_fee = http::value("tester_fee", "float"); if($tester_fee != $project->default_tester_fee) { $test->test_fee = $tester_fee; } // Test forms foreach($fields as $field) { if($field->type == "header") continue; if($field->type == "file") continue; if(empty($field->name)) continue; $value = http::value($field->name, "trim"); if(in_array($field->name, Test::TEST_FIELDS)) $test->{$field->name} = $value; else { $test_value = TestValue::get($id, $field->name); if(!$test_value) { $test_value = new TestValue; $test_value->test_id = $id; $test_value->name = $field->name; } $test_value->value = $value; $test_value->commit(); } } // Accept test as completed if($accept) { $test->status = Test::STATUS_COMPLETED; } // Reject test if($reject) { $test->status = Test::STATUS_CANCELLED; } if($form_exception->hasErrors()) throw $form_exception; $test->retest = http::value("retest", "int"); $test->dts_updated = Util::getDateTime(); $success = $test->commit(); if(!$success) throw new Exception("Failed to update the test. Please try again later."); // If rejected, recreate empty test for the location if($reject) { $new_test = new Test; $new_test->project_id = $test->project_id; $new_test->location_id = $test->location_id; $new_test->status = Test::STATUS_UNASSIGNED; $new_test->dts_created = Util::getDateTime(); $success = $new_test->commit(); if(!$success) throw new Exception("Failed to create a new test. Please try again later."); } if($accept) { $tester->score++; $tester->commit(); if(!$test->pass && $retest_project) { $retest = new Test; $retest->project_id = $retest_project->id; $retest->location_id = $location->id; $retest->status = Test::STATUS_UNASSIGNED; $retest->dts_created = Util::getDateTime(); $retest->retest = 1; $success = $retest->commit(); } // Save PDF $mpdf = $test->getMPDF(); $pdf_string = $mpdf->Output("", "S"); $pdf_path = $directory . "/pdf.pdf"; $success = file_put_contents($pdf_path, $pdf_string); $email_customer = http::value("email_customer"); $email_location = http::value("email_location"); $email_location_area_manager = http::value("email_location_area_manager"); if($email_customer && $customer->email_after_test && $customer->email) { $test->sendResultEmail($customer->email); } if($email_location && $customer->email_locations_after_test && $location->email) { $test->sendResultEmail($location->email); } if($email_location_area_manager && $customer->email_locations_after_test && $location->area_manager_email) { $test->sendResultEmail($location->area_manager_email); } if(EMAIL_TEST_RESULT) { $test->sendResultEmail(EMAIL_TEST_RESULT); } if(EMAIL_TEST_RESULT_COPY) { $test->sendResultEmail(EMAIL_TEST_RESULT_COPY); } } Core::addAlert("success", "Test verified successfully."); HTTP::redirect("/account/tests/$id"); } catch(FormException $e) { $e->generateAlerts(); } catch(Exception $e) { Core::addAlert("danger", $e->getMessage()); } } $projects = $customer->getProjects(); $future_projects = array_filter($projects, function($p) use ($project) { if($p->id == $project->id) return false; if($p->template_id != $project->template_id) return false; $unix_deadline = Util::getUnixTimeFromUTC($p->dts_deadline); return ($unix_deadline > time()); }); View::includeOnce("header", compact("page_title")); View::include("test_verify_form", compact("test", "has_pdf", "tester", "location", "project", "future_projects", "customer", "template", "fields", "values", "tester_message")); View::includeOnce("footer"); return; } // View test results else if(Core::$user->level >= User::LEVEL_VENUE) { HTTP::redirect("/account/tests/$id/result"); } else { throw new Exception("Invalid request."); } ?>