test.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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", "retest", "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. $project = $this->getProject();
  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($project->isNighttime()) {
  53. $builder->addParagraph("<b><font color=\"red\">Evening Test to be done between 7PM and midnight.</font></b>");
  54. }
  55. if($message) {
  56. $builder->addParagraph($message);
  57. }
  58. $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.");
  59. $builder->addAcceptDeclineButtons($base_url);
  60. $builder->addParagraph("The site address is: <b>$address</b>");
  61. $builder->addParagraph("The date that need to complete and submit the results by is: <font color=\"red\"><b>$date_due</b></font>");
  62. $builder->addParagraph("For methodology and further notes please check the <b>Useful Documents</b> section on the <b>Account</b> page.");
  63. $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.");
  64. $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>.");
  65. $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.");
  66. $content = $builder->build();
  67. // Send
  68. $mail = new Email;
  69. $mail->setHtmlContent($content);
  70. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  71. $mail->addAddress($tester->email, $tester->getDisplayName());
  72. $mail->setSubject("You have been allocated a test");
  73. $mail->send();
  74. }
  75. public function sendClaimedEmail() {
  76. $tester = $this->getTester();
  77. $location = $this->getLocation();
  78. $project = $this->getProject();
  79. // Content
  80. $name = htmlspecialchars($tester->first_name);
  81. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  82. $date_due = date("j M Y", strtotime($this->dts_due));
  83. // Build
  84. $builder = new EmailBuilder;
  85. $builder->addParagraph("<b>Hi $name,</b>");
  86. $builder->addLeadParagraph("Congrats, you have claimed a test!");
  87. if($project->isNighttime()) {
  88. $builder->addParagraph("<b><font color=\"red\">Evening Test to be done between 7PM and midnight.</font></b>");
  89. }
  90. $builder->addParagraph("The site address is: <b>$address</b>");
  91. $builder->addParagraph("The date that need to complete and submit the results by is: <font color=\"red\"><b>$date_due</b></font>");
  92. $builder->addParagraph("For methodology and further notes please check the <b>Useful Documents</b> section on the <b>Account</b> page.");
  93. $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.");
  94. $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.");
  95. $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.");
  96. $content = $builder->build();
  97. // Send
  98. $mail = new Email;
  99. $mail->setHtmlContent($content);
  100. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  101. $mail->addAddress($tester->email, $tester->getDisplayName());
  102. $mail->setSubject("You have claimed a test");
  103. $mail->send();
  104. }
  105. public function sendDeclineEmail() {
  106. $tester = $this->getTester();
  107. $location = $this->getLocation();
  108. $project = $this->getProject();
  109. // Content
  110. $name = htmlspecialchars($tester->getDisplayName());
  111. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  112. $project_name = htmlspecialchars($project->name);
  113. $project_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/projects/" . $project->id);
  114. $internal_id = "N/A";
  115. if($location->internal_id) $internal_id = htmlspecialchars($location->internal_id);
  116. // Build
  117. $builder = new EmailBuilder;
  118. $builder->addParagraph("<b>Hi,</b>");
  119. $builder->addLeadParagraph("A tester has declined a test.");
  120. $builder->addParagraph("<b>Project:</b><br />$project_name");
  121. $builder->addParagraph("<b>Site:</b><br />$address<br />ID: $location->id | Internal ID: $internal_id");
  122. $builder->addParagraph("<b>Who:</b><br />$name<br />ID: $tester->id");
  123. $builder->addParagraph("<a href=\"$project_url\">Click here</a> to manage the project and reassign the test.");
  124. $content = $builder->build();
  125. // Send
  126. $mail = new Email;
  127. $mail->setHtmlContent($content);
  128. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  129. $mail->addAddress(EMAIL_CONTACT, "Check Policy Contact");
  130. $mail->setSubject("Test declined: $project->name / $location->name");
  131. $mail->send();
  132. }
  133. public function sendResultEmail($to_address) {
  134. $location = $this->getLocation();
  135. $project = $this->getProject();
  136. $template = $project->getTemplate();
  137. $subject_format_file_path = $template->getProcessingFilePath("result_email_subject_format.txt");
  138. $subject_format = file_get_contents($subject_format_file_path);
  139. // Content
  140. $pass_text = strtoupper($this->getPassText());
  141. $stage = $this->challenge_stage;
  142. $location_parts = [$location->internal_id, $location->name];
  143. $location_string = implode(" : ", array_filter($location_parts));
  144. $unix_start = strtotime($this->dts_start . " UTC");
  145. $date_time = date("d/m/Y H:i", $unix_start);
  146. // Subject
  147. $search_replace = [
  148. "{pass}" => $pass_text,
  149. "{stage}" => $stage,
  150. "{location}" => $location_string,
  151. "{date-time}" => $date_time,
  152. ];
  153. $subject = str_replace(
  154. array_keys($search_replace),
  155. array_values($search_replace),
  156. $subject_format
  157. );
  158. // Attachments
  159. $pdf_path = $this->getDirectory() . "/pdf.pdf";
  160. $pdf_name = implode(" - ", array_filter([$location->internal_id, date("jS M Y")])) . ".pdf";
  161. // Build
  162. $builder = new EmailBuilder;
  163. $builder->addLeadParagraph("Congratulations!<br />A test has been completed at your location.");
  164. $builder->addParagraph("Please see attached PDF and do not hesitate to contact us if you have any questions.");
  165. $content = $builder->build();
  166. // Send
  167. $mail = new Email;
  168. $mail->setHtmlContent($content);
  169. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  170. $mail->addAddress($to_address);
  171. $mail->addAttachment($pdf_path, $pdf_name);
  172. $mail->setSubject($subject);
  173. $mail->send();
  174. }
  175. public function decline() {
  176. $this->sendDeclineEmail();
  177. $this->status = Test::STATUS_UNASSIGNED;
  178. $this->tester_id = null;
  179. $this->dts_due = null;
  180. $this->dts_assigned = null;
  181. $this->dts_updated = Util::getDateTime();
  182. return $this->commit();
  183. }
  184. public function getChallengeStageText() {
  185. if(is_null($this->challenge_stage)) return "";
  186. return Test::STAGES[$this->challenge_stage];
  187. }
  188. public function getStatusText() {
  189. return Test::STATUSES[$this->status];
  190. }
  191. public function getStatusClass() {
  192. switch($this->status) {
  193. case Test::STATUS_CANCELLED:
  194. return "secondary";
  195. case Test::STATUS_UNASSIGNED:
  196. return "danger";
  197. case Test::STATUS_ASSIGNED:
  198. case Test::STATUS_CONFIRMED:
  199. case Test::STATUS_STARTED:
  200. return "warning";
  201. case Test::STATUS_PENDING_VERIFICATION:
  202. return "primary";
  203. case Test::STATUS_COMPLETED:
  204. return "success";
  205. }
  206. }
  207. public function getProject() {
  208. return Project::get($this->project_id);
  209. }
  210. public function getLocation() {
  211. return Location::get($this->location_id);
  212. }
  213. public function getTester() {
  214. return User::get($this->tester_id);
  215. }
  216. public function getVerifier() {
  217. return User::get($this->verifier_id);
  218. }
  219. public function getValues($as_assoc_array = false) {
  220. $rows = TestValue::query(["test_id" => $this->id]);
  221. if($as_assoc_array) {
  222. $k = array_column($rows, "name");
  223. $v = array_column($rows, "value");
  224. $values = array_combine($k, $v);
  225. }
  226. else {
  227. $values = $rows;
  228. }
  229. return $values;
  230. }
  231. public function getDirectory() {
  232. $directory = ROOT_DIR . "/files/tests/" . $this->id;
  233. if($this->project_id == "PRACTICE") {
  234. $directory = ROOT_DIR . "/files/users/$this->tester_id/practice_tests/$this->template_id";
  235. }
  236. if(!file_exists($directory)) mkdir($directory, 0777, true);
  237. return $directory;
  238. }
  239. public function getDuration() {
  240. if(is_null($this->dts_end)) return false;
  241. $seconds = Util::getDurationFromDTS($this->dts_start, $this->dts_end);
  242. return $seconds;
  243. }
  244. public function getDisplayDuration() {
  245. $secs = $this->getDuration();
  246. $mins = floor($secs / 60);
  247. $secs = $secs % 60;
  248. return "$mins min. $secs sec.";
  249. }
  250. public function getPassClass() {
  251. if(is_null($this->pass)) return "secondary";
  252. return $this->pass ? "success" : "danger";
  253. }
  254. public function getPassIcon() {
  255. if(is_null($this->pass)) return "clock";
  256. return $this->pass ? "check-circle" : "times-circle";
  257. }
  258. public function getPassText() {
  259. if(is_null($this->pass)) return "Pending";
  260. return $this->pass ? "Pass" : "Fail";
  261. }
  262. public function getPDF() {
  263. $id = $this->id;
  264. $output = false;
  265. require ROOT_DIR . "/controllers/test_pdf.php";
  266. return $pdf;
  267. }
  268. public function assignTester($tester) {
  269. $this->tester_id = $tester->id;
  270. $this->dts_assigned = Util::getDateTime();
  271. $success = $this->commit();
  272. // TODO: Notify tester
  273. return $success;
  274. }
  275. public function getPreviousTest() {
  276. $nvp = [
  277. "dts_start" => (new Constraint("LESS_THAN"))->setValues($this->dts_start),
  278. "location_id" => $this->location_id,
  279. "status" => Test::STATUS_COMPLETED,
  280. ];
  281. $tests = Test::query($nvp, [
  282. "sort" => "dts_start",
  283. "order" => "desc",
  284. ]);
  285. return $tests ? $tests[0] : false;
  286. }
  287. public function unassignTester() {
  288. $this->status = Test::STATUS_UNASSIGNED;
  289. $this->tester_id = null;
  290. $this->dts_due = null;
  291. $this->dts_assigned = null;
  292. $success = $this->commit();
  293. return $success;
  294. }
  295. public function isPendingVerification() {
  296. return $this->status == Test::STATUS_PENDING_VERIFICATION;
  297. }
  298. }
  299. ?>