| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704 |
- <?php /** @noinspection HtmlDeprecatedTag */
- /** @noinspection HtmlDeprecatedAttribute */
- use Mpdf\Mpdf;
- /**
- * @property int $id
- * @property int $project_id
- * @property int $location_id
- * @property int $tester_id
- * @property int $status
- * @property int $status_tester_invoice
- * @property float $customer_fee
- * @property float $test_fee
- * @property string $test_time
- * @property float $test_travel_cost
- * @property string $shop_left
- * @property string $shop_right
- * @property string $attire
- * @property int $challenge_stage
- * @property string $challenge_description
- * @property string $challenger_name
- * @property string $challenger_description
- * @property string $challenger_gender
- * @property string $challenger_eye_contact
- * @property string $additional_notes
- * @property string $dts_due
- * @property string $dts_start
- * @property string $dts_end
- * @property string $dts_tester_invoice_generated
- * @property string $dts_tester_invoice_paid
- * @property int $verifier_id
- * @property string $verifier_notes
- * @property bool $pass
- * @property bool $retest
- * @property string $dts_created
- * @property string $dts_updated
- * @property string $dts_assigned
- * @property string $dts_verified
- */
- class Test extends Model {
- protected static $table = "test";
- protected static $id_field = "id";
- protected static $fillable_fields = ["project_id", "location_id", "tester_id", "status", "status_tester_invoice", "customer_fee", "test_fee", "test_time", "test_travel_cost", "shop_left", "shop_right", "attire", "challenge_stage", "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact", "additional_notes", "dts_due", "dts_start", "dts_end", "dts_tester_invoice_generated", "dts_tester_invoice_paid", "verifier_id", "verifier_notes", "pass", "retest", "dts_created", "dts_updated", "dts_assigned", "dts_verified"];
- const TEST_FIELDS = ["shop_left", "shop_right", "attire", "challenge_stage", "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact", "additional_notes"];
- // Constants
- const STATUS_CANCELLED = 0;
- const STATUS_UNASSIGNED = 1; // Location needs a tester
- const STATUS_ASSIGNED = 2; // Tester assigned
- const STATUS_CONFIRMED = 3; // Tester has said they can do it
- const STATUS_STARTED = 4;
- const STATUS_PENDING_VERIFICATION = 8;
- const STATUS_COMPLETED = 16;
- const STATUS_TESTER_INVOICE_NONE = 0;
- const STATUS_TESTER_INVOICE_PENDING = 100;
- const STATUS_TESTER_INVOICE_PAID = 200;
- const STATUSES = [
- 0 => "Cancelled",
- 1 => "Unassigned",
- 2 => "Assigned",
- 3 => "Confirmed",
- 4 => "Started",
- 8 => "Pending Verification",
- 16 => "Completed",
- ];
- const STATUSES_TESTER_INVOICE = [
- Test::STATUS_TESTER_INVOICE_NONE,
- Test::STATUS_TESTER_INVOICE_PENDING,
- Test::STATUS_TESTER_INVOICE_PAID
- ];
- const STAGES = [
- 0 => "",
- 1 => "Challenge on entry",
- 2 => "Challenge while browsing",
- 3 => "Challenge during play",
- 4 => "Challenge on exit",
- 5 => "No challenge",
- ];
- const TEST_TIME_ANYTIME = 1;
- const TEST_TIME_DAY = 2;
- const TEST_TIME_NIGHT = 3;
- // Static methods
- /**
- * @param int $status
- * @return string
- */
- public static function getStatusTesterInvoiceText(int $status): string {
- switch($status) {
- case Test::STATUS_TESTER_INVOICE_NONE:
- return __("No Invoice Generated");
- case Test::STATUS_TESTER_INVOICE_PENDING:
- return __("Pending Payment");
- case Test::STATUS_TESTER_INVOICE_PAID:
- return __("Paid");
- }
- return __("Unknown");
- }
- /**
- * @param int $status
- * @return string
- */
- public static function getStatusTesterInvoiceClass(int $status): string {
- switch($status) {
- case Test::STATUS_TESTER_INVOICE_NONE:
- return "secondary";
- case Test::STATUS_TESTER_INVOICE_PENDING:
- return "warning";
- case Test::STATUS_TESTER_INVOICE_PAID:
- return "success";
- }
- return "";
- }
- /**
- * @param string $processing_file
- * @param array $tests
- * @throws Exception
- */
- public static function exportCSV(string $processing_file, array $tests = []): void {
- // Preload
- Project::load(array_column($tests, "project_id"));
- Location::load(array_column($tests, "location_id"));
- User::load(array_column($tests, "tester_id"));
- // Get processing file
- $processing_file_path = ROOT_DIR . "/export_processing/{$processing_file}";
- if(!file_exists($processing_file_path)) throw new Exception("Processing file not found.");
- // Output
- HTTP::setDownloadHeaders("export.csv");
- $handle = fopen("php://output", "w");
- require $processing_file_path;
- fclose($handle);
- }
- // Object methods
- /**
- * @param string $message
- * @return bool
- * @throws Exception
- */
- public function sendAssignEmail(string $message): bool {
- $tester = $this->getTester();
- $location = $this->getLocation();
- $project = $this->getProject();
- // Content
- $name = htmlspecialchars($tester->first_name);
- $message = htmlspecialchars($message);
- $base_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/tests/" . $this->id);
- $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
- $date_due = date("j M Y", strtotime($this->dts_due));
- $admin_first_name = htmlspecialchars(Core::$user->first_name);
- $admin_phone = htmlspecialchars(Core::$user->phone);
- // Build
- $builder = new EmailBuilder;
- $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
- $builder->addLeadParagraph(__("Congrats, you have been allocated a test!"));
- if($project->isNighttime()) {
- $builder->addParagraph("<b><font color=\"red\">" . __("Evening Test to be done between 7PM and midnight.") . "</font></b>");
- }
- if($message) {
- $builder->addParagraph($message);
- }
- $builder->addParagraph(sprintf(__("We need you to confirm if you are able to do the test. Please check the details below and then confirm by pressing the %sAccept%s or %sDecline%s buttons and once you tap %sAccept%s, the test will be allocated to you."),
- '<b>', '</b>',
- '<b>', '</b>',
- '<b>', '</b>'
- ));
- $builder->addAcceptDeclineButtons($base_url);
- $builder->addParagraph(sprintf(__("The site address is: %s"), "<b>{$address}</b>"));
- $builder->addParagraph(sprintf(__("The date that need to complete and submit the results by is: %s"), "<font color=\"red\"><b>$date_due</b></font>"));
- $builder->addParagraph(sprintf(__("For methodology and further notes please check the %sUseful Documents%s section on the %sAccount%s page."),
- '<b>', '</b>',
- '<b>', '</b>'
- ));
- $builder->addParagraph(sprintf(__("If you agree to conduct the test, log in and then go to %sTests%s where you will be able to see the tests available to you. Please tap the %sBegin%s button and follow the instructions that is shown."),
- '<b>', '</b>',
- '<b>', '</b>'
- ));
- $builder->addParagraph(sprintf(__("If you have any questions please send them to %s or call %s on %s."),
- "<a href=\"mailto:tests@check-policy.com\">tests@check-policy.com</a>",
- "<b>$admin_first_name</b>",
- "<b>$admin_phone</b>"
- ));
- $builder->addParagraph(sprintf(__("If you are unable to submit the results via the web-based app please contact us and we can send you an offline form to complete. Please accept the test by tapping the %sAccept%s button and email us back saying you would like an offline form."),
- '<b>', '</b>'
- ));
- $content = $builder->build();
- // Send
- $mail = new Email;
- $mail->setHtmlContent($content);
- $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
- $mail->addAddress($tester->email, $tester->getDisplayName());
- $mail->setSubject(__("You have been allocated a test"));
- $mail->send();
- return true;
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function sendClaimedEmail(): bool {
- $tester = $this->getTester();
- $location = $this->getLocation();
- $project = $this->getProject();
- // Content
- $name = htmlspecialchars($tester->first_name);
- $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
- $date_due = date("j M Y", strtotime($this->dts_due));
- // Build
- $builder = new EmailBuilder;
- $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
- $builder->addLeadParagraph(__("Congrats, you have claimed a test!"));
- if($project->isNighttime()) {
- $builder->addParagraph("<b><font color=\"red\">" . __("Evening Test to be done between 7PM and midnight.") . "</font></b>");
- }
- $builder->addParagraph(sprintf(__("The site address is: %s"), "<b>$address</b>"));
- $builder->addParagraph(sprintf(__("The date that need to complete and submit the results by is: %s"), "<font color=\"red\"><b>$date_due</b></font>"));
- $builder->addParagraph(sprintf(__("For methodology and further notes please check the %sUseful Documents%s section on the %sAccount%s page."),
- '<b>', '</b>',
- '<b>', '</b>'
- ));
- $builder->addParagraph(sprintf(__("Log in and then go to %sTests%s where you will be able to see the tests available to you. Please tap the %sBegin%s button and follow the instructions that is shown."),
- '<b>', '</b>',
- '<b>', '</b>'
- ));
- $builder->addParagraph(sprintf(__("If you have any questions please send them to %s."),
- "<a href=\"mailto:tests@check-policy.com\">tests@check-policy.com</a>"
- ));
- $builder->addParagraph(__("If you are unable to submit the results via the web-based app please contact us and we can send you an offline form to complete."));
- $content = $builder->build();
- // Send
- $mail = new Email;
- $mail->setHtmlContent($content);
- $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
- $mail->addAddress($tester->email, $tester->getDisplayName());
- $mail->setSubject(__("You have claimed a test"));
- $mail->send();
- return true;
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function sendDeclineEmail(): bool {
- $tester = $this->getTester();
- $location = $this->getLocation();
- $project = $this->getProject();
- // Content
- $name = htmlspecialchars($tester->getDisplayName());
- $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
- $project_name = htmlspecialchars($project->name);
- $project_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/projects/" . $project->id);
- $internal_id = "N/A";
- if($location->internal_id) $internal_id = htmlspecialchars($location->internal_id);
- // Build
- $builder = new EmailBuilder;
- $builder->addParagraph("<b>Hi,</b>");
- $builder->addLeadParagraph("A tester has declined a test.");
- $builder->addParagraph("<b>Project:</b><br />$project_name");
- $builder->addParagraph("<b>Site:</b><br />$address<br />ID: $location->id | Internal ID: $internal_id");
- $builder->addParagraph("<b>Who:</b><br />$name<br />ID: $tester->id");
- $builder->addParagraph("<a href=\"$project_url\">Click here</a> to manage the project and reassign the test.");
- $content = $builder->build();
- // Send
- $mail = new Email;
- $mail->setHtmlContent($content);
- $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
- $mail->addAddress(EMAIL_CONTACT, APP_NAME . " Contact");
- $mail->setSubject("Test declined: $project->name / $location->name");
- $mail->send();
- return true;
- }
- /**
- * @param string $to_address
- * @return bool
- * @throws Exception
- */
- public function sendResultEmail(string $to_address): bool {
- $location = $this->getLocation();
- $project = $this->getProject();
- $template = $project->getTemplate();
- $subject_format_file_path = $template->getProcessingFilePath("result_email_subject_format.txt");
- $subject_format = file_get_contents($subject_format_file_path);
- // Content
- $pass_text = strtoupper($this->getPassText());
- $stage = $this->challenge_stage;
- $location_parts = [$location->internal_id, $location->name];
- $location_string = implode(" : ", array_filter($location_parts));
- $unix_start = strtotime($this->dts_start . " UTC");
- $date_time = date("d/m/Y H:i", $unix_start);
- // Subject
- $search_replace = [
- "{pass}" => $pass_text,
- "{stage}" => $stage,
- "{location}" => $location_string,
- "{date-time}" => $date_time,
- ];
- $subject = str_replace(
- array_keys($search_replace),
- array_values($search_replace),
- $subject_format
- );
- // Attachments
- $pdf_path = $this->getDirectory() . "/pdf.pdf";
- $pdf_name = implode(" - ", array_filter([$location->internal_id, date("jS M Y")])) . ".pdf";
- // Lead paragraph
- $lead_paragraph = "";
- if($this->pass) {
- $lead_paragraph .= "Congratulations!<br />";
- }
- $lead_paragraph .= "A test has been completed at your location.";
- // Build
- $builder = new EmailBuilder;
- $builder->addLeadParagraph($lead_paragraph);
- $builder->addParagraph("Please see attached PDF and do not hesitate to contact us if you have any questions.");
- $content = $builder->build();
- // Send
- $mail = new Email;
- $mail->setHtmlContent($content);
- $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
- $mail->addAddress($to_address);
- $mail->addAttachment($pdf_path, $pdf_name);
- $mail->setSubject($subject);
- $mail->send();
- return true;
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function decline(): bool {
- $this->sendDeclineEmail();
- $this->status = Test::STATUS_UNASSIGNED;
- $this->tester_id = null;
- $this->dts_due = null;
- $this->dts_assigned = null;
- $this->dts_updated = Util::getDateTime();
- return $this->commit();
- }
- /**
- * @return string
- */
- public function getChallengeStageText(): string {
- if(is_null($this->challenge_stage)) return "";
- return Test::STAGES[$this->challenge_stage];
- }
- /**
- * @return string
- */
- public function getStatusText(): string {
- return Test::STATUSES[$this->status];
- }
- /**
- * @return string
- */
- public function getStatusClass(): string {
- switch($this->status) {
- case Test::STATUS_CANCELLED:
- return "secondary";
- case Test::STATUS_UNASSIGNED:
- return "danger";
- case Test::STATUS_ASSIGNED:
- case Test::STATUS_CONFIRMED:
- case Test::STATUS_STARTED:
- return "warning";
- case Test::STATUS_PENDING_VERIFICATION:
- return "primary";
- case Test::STATUS_COMPLETED:
- return "success";
- }
- return "";
- }
- /**
- * @return Project|null
- * @throws Exception
- */
- public function getProject(): ?Project {
- return Project::get($this->project_id);
- }
- /**
- * @return Location
- * @throws Exception
- */
- public function getLocation(): ?Location {
- return Location::get($this->location_id);
- }
- /**
- * @return User
- * @throws Exception
- */
- public function getTester(): ?User {
- return User::get($this->tester_id);
- }
- /**
- * @return User
- * @throws Exception
- */
- public function getVerifier(): ?User {
- return User::get($this->verifier_id);
- }
- /**
- * @param bool $as_assoc_array
- * @return array
- * @throws Exception
- */
- public function getValues(bool $as_assoc_array = false): array {
- $rows = TestValue::query(["test_id" => $this->id]);
- if($as_assoc_array) {
- $k = array_column($rows, "name");
- $v = array_column($rows, "value");
- $values = array_combine($k, $v);
- }
- else {
- $values = $rows;
- }
- return $values;
- }
- /**
- * @return string
- */
- public function getDirectory(): string {
- $directory = ROOT_DIR . "/files/tests/" . $this->id;
- if(!file_exists($directory)) mkdir($directory, 0777, true);
- return $directory;
- }
- /**
- * @return bool|float|int
- */
- public function getDuration() {
- if(is_null($this->dts_end)) return false;
- $seconds = Util::getDurationFromDTS($this->dts_start, $this->dts_end);
- return $seconds;
- }
- /**
- * @return string
- */
- public function getDisplayDuration(): string {
- $secs = $this->getDuration();
- $mins = floor($secs / 60);
- $secs = $secs % 60;
- return "$mins min. $secs sec.";
- }
- /**
- * @return string Bootstrap class name
- */
- public function getPassClass(): string {
- if(is_null($this->pass)) return "secondary";
- return $this->pass ? "success" : "danger";
- }
- /**
- * @return string FA icon name without prefix
- */
- public function getPassIcon(): string {
- if(is_null($this->pass)) return "clock";
- return $this->pass ? "check-circle" : "times-circle";
- }
- /**
- * @return string Pending, Pass or Fail
- */
- public function getPassText(): string {
- if(is_null($this->pass)) return __("Pending");
- return $this->pass ? __("Pass") : __("Fail");
- }
- public function getMPDF(): Mpdf {
- /** @var Mpdf $mpdf */
- $id = $this->id;
- $output = false;
- require ROOT_DIR . "/controllers/test_pdf.php";
- return $mpdf;
- }
- /**
- * @param User $tester
- * @return bool
- * @throws Exception
- */
- public function assignTester(User $tester): bool {
- $this->tester_id = $tester->id;
- $this->dts_assigned = Util::getDateTime();
- $success = $this->commit();
- // TODO: Notify tester
- return $success;
- }
- /**
- * @return Test|null
- * @throws Exception
- */
- public function getPreviousTest(): ?Test {
- $nvp = [
- "dts_start" => (new Constraint("LESS_THAN"))->setValues($this->dts_start),
- "location_id" => $this->location_id,
- "status" => Test::STATUS_COMPLETED,
- ];
- $tests = Test::query($nvp, [
- "sort" => "dts_start",
- "order" => "desc",
- ]);
- return $tests ? $tests[0] : null;
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function unassignTester(): bool {
- $this->status = Test::STATUS_UNASSIGNED;
- $this->tester_id = null;
- $this->dts_due = null;
- $this->dts_assigned = null;
- return $this->commit();
- }
- /**
- * @return bool
- */
- public function isPass(): bool {
- if(is_null($this->pass)) return false;
- return !empty($this->pass);
- }
- /**
- * @return bool
- */
- public function isPending(): bool {
- return is_null($this->pass);
- }
- /**
- * @return bool
- */
- public function isFail(): bool {
- if(is_null($this->pass)) return false;
- return empty($this->pass);
- }
- /**
- * @return bool
- */
- public function isCompleted() {
- return $this->status == Test::STATUS_COMPLETED;
- }
- /**
- * @return bool
- */
- public function isPendingVerification() {
- return $this->status == Test::STATUS_PENDING_VERIFICATION;
- }
- /**
- * @return bool
- */
- public function hasPDF(): bool {
- $directory = $this->getDirectory();
- return file_exists($directory . "/pdf.pdf");
- }
- /**
- * @return string
- */
- public function getPDFUrl(): string {
- return "/account/tests/{$this->id}/files/pdf";
- }
- /**
- * @return bool
- */
- public function hasTesterInvoicePDF(): bool {
- $directory = $this->getDirectory();
- return file_exists($directory . "/tester_invoice.pdf");
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function markTesterInvoicePaid(): bool {
- if($this->status_tester_invoice == Test::STATUS_TESTER_INVOICE_PENDING) {
- $this->status_tester_invoice = Test::STATUS_TESTER_INVOICE_PAID;
- $this->dts_tester_invoice_paid = Util::getDateTime();
- return $this->commit();
- }
- return false;
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function rejectTesterInvoice(): bool {
- if($this->status_tester_invoice == Test::STATUS_TESTER_INVOICE_PENDING) {
- $this->status_tester_invoice = Test::STATUS_TESTER_INVOICE_NONE;
- $this->dts_tester_invoice_generated = null;
- $pdf_filepath = $this->getDirectory() . "/tester_invoice.pdf";
- unlink($pdf_filepath);
- return $this->commit();
- }
- return false;
- }
- /**
- * @return string
- * @throws Exception
- */
- public function getTesterInvoiceReference(): string {
- $location = $this->getLocation();
- $tester = $this->getTester();
- return
- "T" . str_pad($this->id, 3, "0", STR_PAD_LEFT) .
- "/L" . str_pad($location->id, 3, "0", STR_PAD_LEFT) .
- "/U" . str_pad($tester->id, 3, "0", STR_PAD_LEFT);
- }
- }
|