|
|
@@ -1,7 +1,7 @@
|
|
|
<?php /** @noinspection PhpUnhandledExceptionInspection */
|
|
|
|
|
|
/**
|
|
|
- * @var Test $test
|
|
|
+ * @var int $id
|
|
|
*/
|
|
|
|
|
|
Core::addScript("public/js/app-test.js");
|
|
|
@@ -9,14 +9,8 @@ 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();
|
|
|
@@ -27,453 +21,19 @@ $template->setKeys($template_keys);
|
|
|
|
|
|
$page_title = $location->name;
|
|
|
|
|
|
+$user = Core::$user;
|
|
|
+
|
|
|
Core::addBreadcrumb("/account", "Account");
|
|
|
Core::addBreadcrumb("/account/tests", "Tests");
|
|
|
Core::addBreadcrumb("/account/tests/$id", $page_title);
|
|
|
|
|
|
-// Do test
|
|
|
-if(Core::$user->isTester()) {
|
|
|
-
|
|
|
- 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
|
|
|
-
|
|
|
- $required_key = $field->requiredKey ?? null;
|
|
|
- if($required_key && !in_array($required_key, $template_keys)) 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));
|
|
|
- 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 && $value === null) $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 = 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");
|
|
|
-
|
|
|
+if($user->isTester()) {
|
|
|
+ require ROOT_DIR . "/controllers/test_tester_form.php";
|
|
|
}
|
|
|
-
|
|
|
-// Verify test
|
|
|
-else if(Core::$user->canVerifyTests()) {
|
|
|
-
|
|
|
- $json = json_decode($template->getJSON());
|
|
|
- $fields = array_merge($json->beforeTest, [(object) ["type" => "hr"]], $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();
|
|
|
-
|
|
|
- $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
|
|
|
- $new_test_project = false;
|
|
|
- $new_test_project_id = http::value("new_test_project_id", "int");
|
|
|
- $new_test_is_retest = http::value("new_test_is_retest", "int");
|
|
|
- if($new_test_project_id) {
|
|
|
- $new_test_project = Project::get($new_test_project_id);
|
|
|
- if(!$new_test_project || $new_test_project->customer_id != $customer->id) throw new Exception("The new test 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 !== null && $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 !== null && $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;
|
|
|
-
|
|
|
- $required_key = $field->requiredKey ?? null;
|
|
|
- if($required_key && !in_array($required_key, $template_keys)) 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($new_test_project) {
|
|
|
- $new_test = new Test;
|
|
|
- $new_test->project_id = $new_test_project->id;
|
|
|
- $new_test->location_id = $location->id;
|
|
|
- $new_test->status = Test::STATUS_UNASSIGNED;
|
|
|
- $new_test->dts_created = Util::getDateTime();
|
|
|
- if($new_test_is_retest) {
|
|
|
- $new_test->retest = 1;
|
|
|
- }
|
|
|
- $success = $new_test->commit();
|
|
|
- }
|
|
|
-
|
|
|
- // 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
|
|
|
- $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();
|
|
|
-
|
|
|
- /** @var Project[] $future_projects */
|
|
|
- $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", "tester", "location", "project", "future_projects", "customer", "template", "fields", "values", "tester_message"));
|
|
|
- View::includeOnce("footer");
|
|
|
- return;
|
|
|
+else if($user->canVerifyTests()) {
|
|
|
+ require ROOT_DIR . "/controllers/test_verify_form.php";
|
|
|
}
|
|
|
-
|
|
|
-// View test results
|
|
|
-else if(Core::$user->canAccessTestResult()) {
|
|
|
+else if($user->canAccessTestResult()) {
|
|
|
HTTP::redirect("/account/tests/$id/result");
|
|
|
}
|
|
|
else {
|