| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- <?php
- class Test extends Model {
- protected static $table = "test";
- protected static $id_field = "id";
- protected static $fillable_fields = ["project_id", "location_id", "tester_id", "status", "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", "verifier_id", "verifier_notes", "pass", "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 STATUSES = [
- 0 => "Cancelled",
- 1 => "Unassigned",
- 2 => "Assigned",
- 3 => "Confirmed",
- 4 => "Started",
- 8 => "Pending Verification",
- 16 => "Completed",
- ];
- 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;
- // Object methods
- public function sendAssignEmail($message) {
- $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>Hi $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("We need you to confirm if you are able to do the test. Please check the details below and then confirm by pressing the <b>Accept</b> or <b>Decline</b> buttons and once you tap <b>Accept</b>, the test will be allocated to you.");
- $builder->addAcceptDeclineButtons($base_url);
- $builder->addParagraph("The site address is: <b>$address</b>");
- $builder->addParagraph("The date that need to complete and submit the results by is: <font color=\"red\"><b>$date_due</b></font>");
- $builder->addParagraph("For methodology and further notes please check the <b>Useful Documents</b> section on the <b>Account</b> page.");
- $builder->addParagraph("If you agree to conduct the test, log in and then go to <b>Tests</b> where you will be able to see the tests available to you. Please tap the <b>Begin</b> button and follow the instructions that is shown.");
- $builder->addParagraph("If you have any questions please send them to <a href=\"mailto:tests@check-policy.com\">tests@check-policy.com</a> or call <b>$admin_first_name</b> on <b>$admin_phone</b>.");
- $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. Please accept the test by tapping the <b>Accept</b> button and email us back saying you would like an offline form.");
- $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();
- }
- public function sendClaimedEmail() {
- $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>Hi $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("The site address is: <b>$address</b>");
- $builder->addParagraph("The date that need to complete and submit the results by is: <font color=\"red\"><b>$date_due</b></font>");
- $builder->addParagraph("For methodology and further notes please check the <b>Useful Documents</b> section on the <b>Account</b> page.");
- $builder->addParagraph("Log in and then go to <b>Tests</b> where you will be able to see the tests available to you. Please tap the <b>Begin</b> button and follow the instructions that is shown.");
- $builder->addParagraph("If you have any questions please send them to <a href=\"mailto:tests@check-policy.com\">tests@check-policy.com</a> or call <b>Check Policy</b> on 07841 353775.");
- $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();
- }
- public function sendDeclineEmail() {
- $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, "Check Policy Contact");
- $mail->setSubject("Test declined: $project->name / $location->name");
- $mail->send();
- }
- public function sendResultEmail($to_address) {
- $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";
- // Build
- $builder = new EmailBuilder;
- $builder->addLeadParagraph("Congratulations!<br />A test has been completed at your location.");
- $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();
- }
- public function decline() {
- $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();
- }
- public function getChallengeStageText() {
- if(is_null($this->challenge_stage)) return "";
- return Test::STAGES[$this->challenge_stage];
- }
- public function getStatusText() {
- return Test::STATUSES[$this->status];
- }
- public function getStatusClass() {
- 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";
- }
- }
- public function getProject() {
- return Project::get($this->project_id);
- }
- public function getLocation() {
- return Location::get($this->location_id);
- }
- public function getTester() {
- return User::get($this->tester_id);
- }
- public function getVerifier() {
- return User::get($this->verifier_id);
- }
- public function getValues($as_assoc_array = false) {
- $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;
- }
- public function getDirectory() {
- $directory = ROOT_DIR . "/files/tests/" . $this->id;
- if($this->project_id == "PRACTICE") {
- $directory = ROOT_DIR . "/files/users/$this->tester_id/practice_tests/$this->template_id";
- }
- if(!file_exists($directory)) mkdir($directory, 0777, true);
- return $directory;
- }
- public function getDuration() {
- if(is_null($this->dts_end)) return false;
- $seconds = Util::getDurationFromDTS($this->dts_start, $this->dts_end);
- return $seconds;
- }
- public function getDisplayDuration() {
- $secs = $this->getDuration();
- $mins = floor($secs / 60);
- $secs = $secs % 60;
- return "$mins min. $secs sec.";
- }
- public function getPassClass() {
- if(is_null($this->pass)) return "secondary";
- return $this->pass ? "success" : "danger";
- }
- public function getPassIcon() {
- if(is_null($this->pass)) return "clock";
- return $this->pass ? "check-circle" : "times-circle";
- }
- public function getPassText() {
- if(is_null($this->pass)) return "Pending";
- return $this->pass ? "Pass" : "Fail";
- }
- public function getPDF() {
- $id = $this->id;
- $output = false;
- require ROOT_DIR . "/controllers/test_pdf.php";
- return $pdf;
- }
- public function assignTester($tester) {
- $this->tester_id = $tester->id;
- $this->dts_assigned = Util::getDateTime();
- $success = $this->commit();
- // TODO: Notify tester
- return $success;
- }
- public function unassignTester() {
- $this->tester_id = null;
- $this->dts_due = null;
- $this->dts_assigned = null;
- $success = $this->commit();
- return $success;
- }
- }
- ?>
|