1
0

test.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. class Test extends Model {
  3. protected static $table = "test";
  4. protected static $id_field = "id";
  5. protected static $fillable_fields = ["project_id", "location_id", "tester_id", "status", "test_fee", "test_time", "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"];
  6. protected static $field_map = [
  7. ];
  8. // Constants
  9. const STATUS_CANCELLED = 0;
  10. const STATUS_UNASSIGNED = 1; // Location needs a tester
  11. const STATUS_ASSIGNED = 2; // Tester assigned
  12. const STATUS_CONFIRMED = 3; // Tester has said they can do it
  13. const STATUS_STARTED = 4;
  14. const STATUS_PENDING_VERIFICATION = 8;
  15. const STATUS_COMPLETED = 16;
  16. const STATUSES = [
  17. 0 => "Cancelled",
  18. 1 => "Unassigned",
  19. 2 => "Assigned",
  20. 3 => "Confirmed",
  21. 4 => "Started",
  22. 8 => "Pending Verification",
  23. 16 => "Completed",
  24. ];
  25. const STAGES = [
  26. 0 => "",
  27. 1 => "Challenge on entry",
  28. 2 => "Challenge while browsing",
  29. 3 => "Challenge during play",
  30. 4 => "Challenge on exit",
  31. 5 => "No challenge",
  32. ];
  33. const TEST_TIME_ANYTIME = 1;
  34. const TEST_TIME_DAY = 2;
  35. const TEST_TIME_NIGHT = 3;
  36. // Object methods
  37. public function sendAssignEmail($message) {
  38. $tester = $this->getTester();
  39. $location = $this->getLocation();
  40. // Content
  41. $name = htmlspecialchars($tester->first_name);
  42. $message = htmlspecialchars($message);
  43. $base_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/tests/" . $this->id);
  44. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  45. $date_due = date("j M Y", strtotime($this->dts_due));
  46. $admin_first_name = htmlspecialchars(Core::$user->first_name);
  47. $admin_phone = htmlspecialchars(Core::$user->phone);
  48. // Build
  49. $builder = new EmailBuilder;
  50. $builder->addParagraph("<b>Hi $name,</b>");
  51. $builder->addLeadParagraph("Congrats, you have been allocated a test!");
  52. if($message) {
  53. $builder->addParagraph($message);
  54. }
  55. $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.");
  56. $builder->addAcceptDeclineButtons($base_url);
  57. $builder->addParagraph("The site address is: <b>$address</b>");
  58. $builder->addParagraph("The date that need to complete and submit the results by is: <font color=\"red\"><b>$date_due</b></font>");
  59. $builder->addParagraph("For methodology and further notes please check the <b>Useful Documents</b> section on the <b>Account</b> page.");
  60. $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.");
  61. $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>.");
  62. $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.");
  63. $content = $builder->build();
  64. // Send
  65. $mail = new Email;
  66. $mail->setHtmlContent($content);
  67. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  68. $mail->addAddress($tester->email, $tester->getDisplayName());
  69. $mail->setSubject("You have been allocated a test");
  70. $mail->send();
  71. }
  72. public function getChallengeStageText() {
  73. return Test::STAGES[$this->challenge_stage];
  74. }
  75. public function getStatusText() {
  76. return Test::STATUSES[$this->status];
  77. }
  78. public function getStatusClass() {
  79. switch($this->status) {
  80. case Test::STATUS_CANCELLED:
  81. return "secondary";
  82. case Test::STATUS_UNASSIGNED:
  83. return "danger";
  84. case Test::STATUS_ASSIGNED:
  85. case Test::STATUS_CONFIRMED:
  86. case Test::STATUS_STARTED:
  87. return "warning";
  88. case Test::STATUS_PENDING_VERIFICATION:
  89. return "primary";
  90. case Test::STATUS_COMPLETED:
  91. return "success";
  92. }
  93. }
  94. public function getProject() {
  95. return Project::get($this->project_id);
  96. }
  97. public function getLocation() {
  98. return Location::get($this->location_id);
  99. }
  100. public function getTester() {
  101. return User::get($this->tester_id);
  102. }
  103. public function getVerifier() {
  104. return User::get($this->verifier_id);
  105. }
  106. public function getValues($as_assoc_array = false) {
  107. $rows = TestValue::query(["test_id" => $this->id]);
  108. if($as_assoc_array) {
  109. $k = array_column($rows, "name");
  110. $v = array_column($rows, "value");
  111. $values = array_combine($k, $v);
  112. }
  113. else {
  114. $values = $rows;
  115. }
  116. return $values;
  117. }
  118. public function getDirectory() {
  119. $directory = ROOT_DIR . "/files/tests/" . $this->id;
  120. if(!file_exists($directory)) mkdir($directory, 0777, true);
  121. return $directory;
  122. }
  123. public function getDuration() {
  124. if(is_null($this->dts_end)) return false;
  125. $seconds = Util::getDurationFromDTS($this->dts_start, $this->dts_end);
  126. return $seconds;
  127. }
  128. public function getDisplayDuration() {
  129. $secs = $this->getDuration();
  130. $mins = floor($secs / 60);
  131. $secs = $secs % 60;
  132. return "$mins min. $secs sec.";
  133. }
  134. public function getPassClass() {
  135. if(is_null($this->pass)) return "secondary";
  136. return $this->pass ? "success" : "danger";
  137. }
  138. public function getPassIcon() {
  139. if(is_null($this->pass)) return "clock";
  140. return $this->pass ? "check-circle" : "times-circle";
  141. }
  142. public function getPassText() {
  143. if(is_null($this->pass)) return "Pending";
  144. return $this->pass ? "Pass" : "Fail";
  145. }
  146. public function assignTester($tester) {
  147. $this->tester_id = $tester->id;
  148. $this->dts_assigned = Util::getDateTime();
  149. $success = $this->commit();
  150. // TODO: Notify tester
  151. return $success;
  152. }
  153. }
  154. ?>