test.php 13 KB

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