| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?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
- */
- $customer = $project->getCustomer();
- $tester = $test->getTester();
- $fields = $test->getFieldsForVerifyForm();
- $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->getStatus() !== 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::value("start_override");
- if($start_override) {
- $dt_start = new DateTime($start_override, new DateTimeZone(TIMEZONE));
- $unix_start = $dt_start->format("U");
- }
- $end_override = HTTP::value("end_override");
- if($end_override) {
- $dt_end = new DateTime($end_override, new DateTimeZone(TIMEZONE));
- $unix_end = $dt_end->format("U");
- }
- 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->test_travel_cost = HTTP::value("travel_cost", "float") ?? 0;
- if($test->test_travel_cost < 0 || $test->test_travel_cost > 999.99) $form_exception->addError("Travel costs must be between 0 and 999.99.");
- // Test forms
- if($test->isVoid() === false) {
- 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, true)) {
- $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();
- }
- }
- }
- else {
- $test->void = (int) HTTP::value("void", "int");
- $test->void_notes = HTTP::value("void_notes", "trim");
- }
- // 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 = (int) 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();
- if($test->isVoid() === false) {
- $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");
- $include_previous_test = (bool) http::value("include_previous_test");
- $email_addresses = [];
- if($email_customer && $customer->email_after_test && $customer->email) {
- $email_addresses[] = $customer->email;
- }
- if($email_location && $customer->email_locations_after_test && $location->email) {
- $email_addresses[] = $location->email;
- }
- if($email_location_area_manager && $customer->email_locations_after_test && $location->area_manager_email) {
- $email_addresses[] = $location->area_manager_email;
- }
- if(EMAIL_TEST_RESULT) {
- $email_addresses[] = EMAIL_TEST_RESULT;
- }
- if(EMAIL_TEST_RESULT_COPY) {
- $email_addresses[] = EMAIL_TEST_RESULT_COPY;
- }
- foreach($email_addresses as $email_address) {
- $test->sendResultEmail($email_address, $include_previous_test);
- }
- }
- }
- if($test->getTesterInvoiceStatus() === 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, static function(Project $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", "tester_message"));
- View::includeOnce("footer");
|