| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- <?php
- /**
- * @var Test $test
- */
- Core::addScript("public/js/app-test.js");
- $test = Test::get($id);
- if(!$test) throw new Exception(__("Test cannot be found."));
- $is_confirmed = $test->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->getJSON());
- $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(sprintf(__("The uploaded file for %s is not an image."), $label));
- list($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";
- 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(sprintf(__("%s is required."), $label));
- if($values && !in_array($value, $values)) $form_exception->addError(sprintf(__("Value for %s is invalid."), $label));
- 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(sprintf(__("Failed to save value for %s."), $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->getJSON());
- $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");
- $accept_tester_invoice = http::value("accept_tester_invoice");
- $reject_tester_invoice = http::value("reject_tester_invoice");
- 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->customer_fee = null;
- $customer_fee = http::value("customer_fee", "float");
- if($customer_fee !== false && $customer_fee != $project->default_customer_fee) {
- $test->customer_fee = $customer_fee;
- }
- $test->test_fee = null;
- $tester_fee = http::value("tester_fee", "float");
- if($tester_fee !== false && $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);
- }
- }
- if($test->status_tester_invoice == Test::STATUS_TESTER_INVOICE_PENDING) {
- if($accept_tester_invoice) {
- $success = $test->markTesterInvoicePaid();
- if(!$success) throw new Exception("Failed to mark tester invoice as paid.");
- }
- if($reject_tester_invoice) {
- $success = $test->rejectTesterInvoice();
- if(!$success) throw new Exception("Failed to reject tester invoice.");
- }
- }
- Core::addAlert("success", "Test updated 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."));
- }
- ?>
|