test.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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", "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"];
  6. const TEST_FIELDS = ["shop_left", "shop_right", "attire", "challenge_stage", "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact", "additional_notes"];
  7. // Constants
  8. const STATUS_CANCELLED = 0;
  9. const STATUS_UNASSIGNED = 1; // Location needs a tester
  10. const STATUS_ASSIGNED = 2; // Tester assigned
  11. const STATUS_CONFIRMED = 3; // Tester has said they can do it
  12. const STATUS_STARTED = 4;
  13. const STATUS_PENDING_VERIFICATION = 8;
  14. const STATUS_COMPLETED = 16;
  15. const STATUSES = [
  16. 0 => "Cancelled",
  17. 1 => "Unassigned",
  18. 2 => "Assigned",
  19. 3 => "Confirmed",
  20. 4 => "Started",
  21. 8 => "Pending Verification",
  22. 16 => "Completed",
  23. ];
  24. const STAGES = [
  25. 0 => "",
  26. 1 => "Challenge on entry",
  27. 2 => "Challenge while browsing",
  28. 3 => "Challenge during play",
  29. 4 => "Challenge on exit",
  30. 5 => "No challenge",
  31. ];
  32. const TEST_TIME_ANYTIME = 1;
  33. const TEST_TIME_DAY = 2;
  34. const TEST_TIME_NIGHT = 3;
  35. // Object methods
  36. public function sendAssignEmail($message) {
  37. $tester = $this->getTester();
  38. $location = $this->getLocation();
  39. // Content
  40. $name = htmlspecialchars($tester->first_name);
  41. $message = htmlspecialchars($message);
  42. $base_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/tests/" . $this->id);
  43. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  44. $date_due = date("j M Y", strtotime($this->dts_due));
  45. $admin_first_name = htmlspecialchars(Core::$user->first_name);
  46. $admin_phone = htmlspecialchars(Core::$user->phone);
  47. // Build
  48. $builder = new EmailBuilder;
  49. $builder->addParagraph("<b>Hi $name,</b>");
  50. $builder->addLeadParagraph("Congrats, you have been allocated a test!");
  51. if($message) {
  52. $builder->addParagraph($message);
  53. }
  54. $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.");
  55. $builder->addAcceptDeclineButtons($base_url);
  56. $builder->addParagraph("The site address is: <b>$address</b>");
  57. $builder->addParagraph("The date that need to complete and submit the results by is: <font color=\"red\"><b>$date_due</b></font>");
  58. $builder->addParagraph("For methodology and further notes please check the <b>Useful Documents</b> section on the <b>Account</b> page.");
  59. $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.");
  60. $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>.");
  61. $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.");
  62. $content = $builder->build();
  63. // Send
  64. $mail = new Email;
  65. $mail->setHtmlContent($content);
  66. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  67. $mail->addAddress($tester->email, $tester->getDisplayName());
  68. $mail->setSubject("You have been allocated a test");
  69. $mail->send();
  70. }
  71. public function sendClaimedEmail() {
  72. $tester = $this->getTester();
  73. $location = $this->getLocation();
  74. // Content
  75. $name = htmlspecialchars($tester->first_name);
  76. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  77. $date_due = date("j M Y", strtotime($this->dts_due));
  78. // Build
  79. $builder = new EmailBuilder;
  80. $builder->addParagraph("<b>Hi $name,</b>");
  81. $builder->addLeadParagraph("Congrats, you have claimed a test!");
  82. $builder->addParagraph("The site address is: <b>$address</b>");
  83. $builder->addParagraph("The date that need to complete and submit the results by is: <font color=\"red\"><b>$date_due</b></font>");
  84. $builder->addParagraph("For methodology and further notes please check the <b>Useful Documents</b> section on the <b>Account</b> page.");
  85. $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.");
  86. $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.");
  87. $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.");
  88. $content = $builder->build();
  89. // Send
  90. $mail = new Email;
  91. $mail->setHtmlContent($content);
  92. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  93. $mail->addAddress($tester->email, $tester->getDisplayName());
  94. $mail->setSubject("You have claimed a test");
  95. $mail->send();
  96. }
  97. public function sendDeclineEmail() {
  98. $tester = $this->getTester();
  99. $location = $this->getLocation();
  100. $project = $this->getProject();
  101. // Content
  102. $name = htmlspecialchars($tester->getDisplayName());
  103. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  104. $project_name = htmlspecialchars($project->name);
  105. $project_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/projects/" . $project->id);
  106. $internal_id = "N/A";
  107. if($location->internal_id) $internal_id = htmlspecialchars($location->internal_id);
  108. // Build
  109. $builder = new EmailBuilder;
  110. $builder->addParagraph("<b>Hi,</b>");
  111. $builder->addLeadParagraph("A tester has declined a test.");
  112. $builder->addParagraph("<b>Project:</b><br />$project_name");
  113. $builder->addParagraph("<b>Site:</b><br />$address<br />ID: $location->id | Internal ID: $internal_id");
  114. $builder->addParagraph("<b>Who:</b><br />$name<br />ID: $tester->id");
  115. $builder->addParagraph("<a href=\"$project_url\">Click here</a> to manage the project and reassign the test.");
  116. $content = $builder->build();
  117. // Send
  118. $mail = new Email;
  119. $mail->setHtmlContent($content);
  120. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  121. $mail->addAddress(EMAIL_CONTACT, "Check Policy Contact");
  122. $mail->setSubject("Test declined: $project->name / $location->name");
  123. $mail->send();
  124. }
  125. public function sendResultEmail($to_address) {
  126. $location = $this->getLocation();
  127. $project = $this->getProject();
  128. $template = $project->getTemplate();
  129. $subject_format_file_path = $template->getProcessingFilePath("result_email_subject_format.txt");
  130. $subject_format = file_get_contents($subject_format_file_path);
  131. // Content
  132. $pass_text = strtoupper($this->getPassText());
  133. $stage = $this->challenge_stage;
  134. $location_parts = [$location->internal_id, $location->name];
  135. $location_string = implode(" : ", array_filter($location_parts));
  136. $unix_start = strtotime($this->dts_start . " UTC");
  137. $date_time = date("d/m/Y H:i", $unix_start);
  138. // Subject
  139. $search_replace = [
  140. "{pass}" => $pass_text,
  141. "{stage}" => $stage,
  142. "{location}" => $location_string,
  143. "{date-time}" => $date_time,
  144. ];
  145. $subject = str_replace(
  146. array_keys($search_replace),
  147. array_values($search_replace),
  148. $subject_format
  149. );
  150. // Attachments
  151. $pdf_path = $this->getDirectory() . "/pdf.pdf";
  152. $pdf_name = implode(" - ", array_filter([$location->internal_id, date("jS M Y")])) . ".pdf";
  153. // Build
  154. $builder = new EmailBuilder;
  155. $builder->addLeadParagraph("Congratulations!<br />A test has been completed at your location.");
  156. $builder->addParagraph("Please see attached PDF and do not hesitate to contact us if you have any questions.");
  157. $content = $builder->build();
  158. // Send
  159. $mail = new Email;
  160. $mail->setHtmlContent($content);
  161. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  162. $mail->addAddress($to_address);
  163. $mail->addAttachment($pdf_path, $pdf_name);
  164. $mail->setSubject($subject);
  165. $mail->send();
  166. }
  167. public function decline() {
  168. $this->sendDeclineEmail();
  169. $this->status = Test::STATUS_UNASSIGNED;
  170. $this->tester_id = null;
  171. $this->dts_due = null;
  172. $this->dts_assigned = null;
  173. $this->dts_updated = Util::getDateTime();
  174. return $this->commit();
  175. }
  176. public function getChallengeStageText() {
  177. if(is_null($this->challenge_stage)) return "";
  178. return Test::STAGES[$this->challenge_stage];
  179. }
  180. public function getStatusText() {
  181. return Test::STATUSES[$this->status];
  182. }
  183. public function getStatusClass() {
  184. switch($this->status) {
  185. case Test::STATUS_CANCELLED:
  186. return "secondary";
  187. case Test::STATUS_UNASSIGNED:
  188. return "danger";
  189. case Test::STATUS_ASSIGNED:
  190. case Test::STATUS_CONFIRMED:
  191. case Test::STATUS_STARTED:
  192. return "warning";
  193. case Test::STATUS_PENDING_VERIFICATION:
  194. return "primary";
  195. case Test::STATUS_COMPLETED:
  196. return "success";
  197. }
  198. }
  199. public function getProject() {
  200. return Project::get($this->project_id);
  201. }
  202. public function getLocation() {
  203. return Location::get($this->location_id);
  204. }
  205. public function getTester() {
  206. return User::get($this->tester_id);
  207. }
  208. public function getVerifier() {
  209. return User::get($this->verifier_id);
  210. }
  211. public function getValues($as_assoc_array = false) {
  212. $rows = TestValue::query(["test_id" => $this->id]);
  213. if($as_assoc_array) {
  214. $k = array_column($rows, "name");
  215. $v = array_column($rows, "value");
  216. $values = array_combine($k, $v);
  217. }
  218. else {
  219. $values = $rows;
  220. }
  221. return $values;
  222. }
  223. public function getDirectory() {
  224. $directory = ROOT_DIR . "/files/tests/" . $this->id;
  225. if($this->project_id == "PRACTICE") {
  226. $directory = ROOT_DIR . "/files/users/$this->tester_id/practice_tests/$this->template_id";
  227. }
  228. if(!file_exists($directory)) mkdir($directory, 0777, true);
  229. return $directory;
  230. }
  231. public function getDuration() {
  232. if(is_null($this->dts_end)) return false;
  233. $seconds = Util::getDurationFromDTS($this->dts_start, $this->dts_end);
  234. return $seconds;
  235. }
  236. public function getDisplayDuration() {
  237. $secs = $this->getDuration();
  238. $mins = floor($secs / 60);
  239. $secs = $secs % 60;
  240. return "$mins min. $secs sec.";
  241. }
  242. public function getPassClass() {
  243. if(is_null($this->pass)) return "secondary";
  244. return $this->pass ? "success" : "danger";
  245. }
  246. public function getPassIcon() {
  247. if(is_null($this->pass)) return "clock";
  248. return $this->pass ? "check-circle" : "times-circle";
  249. }
  250. public function getPassText() {
  251. if(is_null($this->pass)) return "Pending";
  252. return $this->pass ? "Pass" : "Fail";
  253. }
  254. public function getPDF() {
  255. $id = $this->id;
  256. $output = false;
  257. require ROOT_DIR . "/controllers/test_pdf.php";
  258. return $pdf;
  259. }
  260. public function assignTester($tester) {
  261. $this->tester_id = $tester->id;
  262. $this->dts_assigned = Util::getDateTime();
  263. $success = $this->commit();
  264. // TODO: Notify tester
  265. return $success;
  266. }
  267. }
  268. ?>