| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- <?php
- 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();
- $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();
- date_default_timezone_set("UTC");
- 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;
- if($is_started) {
- $duration = http::value("duration", "int");
- if(!$duration) $form_exception->addError("Duration was not submitted.");
- }
- }
- 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;
- // 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.");
- HTTP::redirect("/account/tests/$id");
- }
- // During the test
- if($is_started || $is_unverified) {
- $test->status = Test::STATUS_PENDING_VERIFICATION;
- if($is_started) {
- $unix_start = strtotime($test->dts_start);
- $unix_end = $unix_start + floor($duration / 1000); // $duration is milliseconds
- $test->dts_end = date("Y-m-d H:i:s", $unix_end);
- }
- $test->pass = ($test->challenge_stage <= 2) ? 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) {
- $e->generateAlerts();
- }
- catch(Exception $e) {
- 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(empty($test->verifier_id)) {
- $test->verifier_id = Core::$user->id;
- $test->dts_verified = Util::getDateTime();
- }
- $test->verifier_notes = http::value("verifier_notes", "trim");
- // 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->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();
- // Save PDF
- $pdf = $test->getPDF();
- $pdf_string = $pdf->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");
- 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);
- }
- }
- Core::addAlert("success", "Test verified successfully.");
- HTTP::redirect("/account/tests/$id");
- }
- catch(FormException $e) {
- $e->generateAlerts();
- }
- catch(Exception $e) {
- Core::addAlert("danger", $e->getMessage());
- }
- }
- View::includeOnce("header", compact("page_title"));
- View::include("test_verify_form", compact("test", "has_pdf", "tester", "location", "project", "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.");
- }
- ?>
|