test.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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", "status_tester_invoice", "customer_fee", "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", "dts_tester_invoice_generated", "dts_tester_invoice_paid", "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 STATUS_TESTER_INVOICE_NONE = 0;
  16. const STATUS_TESTER_INVOICE_PENDING = 100;
  17. const STATUS_TESTER_INVOICE_PAID = 200;
  18. const STATUSES = [
  19. 0 => "Cancelled",
  20. 1 => "Unassigned",
  21. 2 => "Assigned",
  22. 3 => "Confirmed",
  23. 4 => "Started",
  24. 8 => "Pending Verification",
  25. 16 => "Completed",
  26. ];
  27. const STATUSES_TESTER_INVOICE = [
  28. Test::STATUS_TESTER_INVOICE_NONE,
  29. Test::STATUS_TESTER_INVOICE_PENDING,
  30. Test::STATUS_TESTER_INVOICE_PAID
  31. ];
  32. const STAGES = [
  33. 0 => "",
  34. 1 => "Challenge on entry",
  35. 2 => "Challenge while browsing",
  36. 3 => "Challenge during play",
  37. 4 => "Challenge on exit",
  38. 5 => "No challenge",
  39. ];
  40. const TEST_TIME_ANYTIME = 1;
  41. const TEST_TIME_DAY = 2;
  42. const TEST_TIME_NIGHT = 3;
  43. // Static methods
  44. public static function getStatusTesterInvoiceText($status) {
  45. switch($status) {
  46. case Test::STATUS_TESTER_INVOICE_NONE:
  47. return __("No Invoice Generated");
  48. case Test::STATUS_TESTER_INVOICE_PENDING:
  49. return __("Pending Payment");
  50. case Test::STATUS_TESTER_INVOICE_PAID:
  51. return __("Paid");
  52. }
  53. return __("Unknown");
  54. }
  55. public static function getStatusTesterInvoiceClass($status) {
  56. switch($status) {
  57. case Test::STATUS_TESTER_INVOICE_NONE:
  58. return "secondary";
  59. case Test::STATUS_TESTER_INVOICE_PENDING:
  60. return "warning";
  61. case Test::STATUS_TESTER_INVOICE_PAID:
  62. return "success";
  63. }
  64. return "";
  65. }
  66. // Object methods
  67. public function sendAssignEmail($message) {
  68. $tester = $this->getTester();
  69. $location = $this->getLocation();
  70. $project = $this->getProject();
  71. // Content
  72. $name = htmlspecialchars($tester->first_name);
  73. $message = htmlspecialchars($message);
  74. $base_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/tests/" . $this->id);
  75. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  76. $date_due = date("j M Y", strtotime($this->dts_due));
  77. $admin_first_name = htmlspecialchars(Core::$user->first_name);
  78. $admin_phone = htmlspecialchars(Core::$user->phone);
  79. // Build
  80. $builder = new EmailBuilder;
  81. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  82. $builder->addLeadParagraph(__("Congrats, you have been allocated a test!"));
  83. if($project->isNighttime()) {
  84. $builder->addParagraph("<b><font color=\"red\">" . __("Evening Test to be done between 7PM and midnight.") . "</font></b>");
  85. }
  86. if($message) {
  87. $builder->addParagraph($message);
  88. }
  89. $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."),
  90. '<b>', '</b>',
  91. '<b>', '</b>',
  92. '<b>', '</b>'
  93. ));
  94. $builder->addAcceptDeclineButtons($base_url);
  95. $builder->addParagraph(sprintf(__("The site address is: %s"), "<b>{$address}</b>"));
  96. $builder->addParagraph(sprintf(__("The date that need to complete and submit the results by is: %s"), "<font color=\"red\"><b>$date_due</b></font>"));
  97. $builder->addParagraph(sprintf(__("For methodology and further notes please check the %sUseful Documents%s section on the %sAccount%s page."),
  98. '<b>', '</b>',
  99. '<b>', '</b>'
  100. ));
  101. $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."),
  102. '<b>', '</b>',
  103. '<b>', '</b>'
  104. ));
  105. $builder->addParagraph(sprintf(__("If you have any questions please send them to %s or call %s on %s."),
  106. "<a href=\"mailto:tests@check-policy.com\">tests@check-policy.com</a>",
  107. "<b>$admin_first_name</b>",
  108. "<b>$admin_phone</b>"
  109. ));
  110. $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."),
  111. '<b>', '</b>'
  112. ));
  113. $content = $builder->build();
  114. // Send
  115. $mail = new Email;
  116. $mail->setHtmlContent($content);
  117. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  118. $mail->addAddress($tester->email, $tester->getDisplayName());
  119. $mail->setSubject(__("You have been allocated a test"));
  120. $mail->send();
  121. }
  122. public function sendClaimedEmail() {
  123. $tester = $this->getTester();
  124. $location = $this->getLocation();
  125. $project = $this->getProject();
  126. // Content
  127. $name = htmlspecialchars($tester->first_name);
  128. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  129. $date_due = date("j M Y", strtotime($this->dts_due));
  130. // Build
  131. $builder = new EmailBuilder;
  132. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  133. $builder->addLeadParagraph(__("Congrats, you have claimed a test!"));
  134. if($project->isNighttime()) {
  135. $builder->addParagraph("<b><font color=\"red\">" . __("Evening Test to be done between 7PM and midnight.") . "</font></b>");
  136. }
  137. $builder->addParagraph(sprintf(__("The site address is: %s"), "<b>$address</b>"));
  138. $builder->addParagraph(sprintf(__("The date that need to complete and submit the results by is: %s"), "<font color=\"red\"><b>$date_due</b></font>"));
  139. $builder->addParagraph(sprintf(__("For methodology and further notes please check the %sUseful Documents%s section on the %sAccount%s page."),
  140. '<b>', '</b>',
  141. '<b>', '</b>'
  142. ));
  143. $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."),
  144. '<b>', '</b>',
  145. '<b>', '</b>'
  146. ));
  147. $builder->addParagraph(sprintf(__("If you have any questions please send them to %s."),
  148. "<a href=\"mailto:tests@check-policy.com\">tests@check-policy.com</a>"
  149. ));
  150. $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."));
  151. $content = $builder->build();
  152. // Send
  153. $mail = new Email;
  154. $mail->setHtmlContent($content);
  155. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  156. $mail->addAddress($tester->email, $tester->getDisplayName());
  157. $mail->setSubject(__("You have claimed a test"));
  158. $mail->send();
  159. }
  160. public function sendDeclineEmail() {
  161. $tester = $this->getTester();
  162. $location = $this->getLocation();
  163. $project = $this->getProject();
  164. // Content
  165. $name = htmlspecialchars($tester->getDisplayName());
  166. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  167. $project_name = htmlspecialchars($project->name);
  168. $project_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/projects/" . $project->id);
  169. $internal_id = "N/A";
  170. if($location->internal_id) $internal_id = htmlspecialchars($location->internal_id);
  171. // Build
  172. $builder = new EmailBuilder;
  173. $builder->addParagraph("<b>Hi,</b>");
  174. $builder->addLeadParagraph("A tester has declined a test.");
  175. $builder->addParagraph("<b>Project:</b><br />$project_name");
  176. $builder->addParagraph("<b>Site:</b><br />$address<br />ID: $location->id | Internal ID: $internal_id");
  177. $builder->addParagraph("<b>Who:</b><br />$name<br />ID: $tester->id");
  178. $builder->addParagraph("<a href=\"$project_url\">Click here</a> to manage the project and reassign the test.");
  179. $content = $builder->build();
  180. // Send
  181. $mail = new Email;
  182. $mail->setHtmlContent($content);
  183. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  184. $mail->addAddress(EMAIL_CONTACT, APP_NAME . " Contact");
  185. $mail->setSubject("Test declined: $project->name / $location->name");
  186. $mail->send();
  187. }
  188. public function sendResultEmail($to_address) {
  189. $location = $this->getLocation();
  190. $project = $this->getProject();
  191. $template = $project->getTemplate();
  192. $subject_format_file_path = $template->getProcessingFilePath("result_email_subject_format.txt");
  193. $subject_format = file_get_contents($subject_format_file_path);
  194. // Content
  195. $pass_text = strtoupper($this->getPassText());
  196. $stage = $this->challenge_stage;
  197. $location_parts = [$location->internal_id, $location->name];
  198. $location_string = implode(" : ", array_filter($location_parts));
  199. $unix_start = strtotime($this->dts_start . " UTC");
  200. $date_time = date("d/m/Y H:i", $unix_start);
  201. // Subject
  202. $search_replace = [
  203. "{pass}" => $pass_text,
  204. "{stage}" => $stage,
  205. "{location}" => $location_string,
  206. "{date-time}" => $date_time,
  207. ];
  208. $subject = str_replace(
  209. array_keys($search_replace),
  210. array_values($search_replace),
  211. $subject_format
  212. );
  213. // Attachments
  214. $pdf_path = $this->getDirectory() . "/pdf.pdf";
  215. $pdf_name = implode(" - ", array_filter([$location->internal_id, date("jS M Y")])) . ".pdf";
  216. // Lead paragraph
  217. $lead_paragraph = "";
  218. if($this->pass) {
  219. $lead_paragraph .= "Congratulations!<br />";
  220. }
  221. $lead_paragraph .= "A test has been completed at your location.";
  222. // Build
  223. $builder = new EmailBuilder;
  224. $builder->addLeadParagraph($lead_paragraph);
  225. $builder->addParagraph("Please see attached PDF and do not hesitate to contact us if you have any questions.");
  226. $content = $builder->build();
  227. // Send
  228. $mail = new Email;
  229. $mail->setHtmlContent($content);
  230. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  231. $mail->addAddress($to_address);
  232. $mail->addAttachment($pdf_path, $pdf_name);
  233. $mail->setSubject($subject);
  234. $mail->send();
  235. }
  236. public function decline() {
  237. $this->sendDeclineEmail();
  238. $this->status = Test::STATUS_UNASSIGNED;
  239. $this->tester_id = null;
  240. $this->dts_due = null;
  241. $this->dts_assigned = null;
  242. $this->dts_updated = Util::getDateTime();
  243. return $this->commit();
  244. }
  245. public function getChallengeStageText() {
  246. if(is_null($this->challenge_stage)) return "";
  247. return Test::STAGES[$this->challenge_stage];
  248. }
  249. public function getStatusText() {
  250. return Test::STATUSES[$this->status];
  251. }
  252. public function getStatusClass() {
  253. switch($this->status) {
  254. case Test::STATUS_CANCELLED:
  255. return "secondary";
  256. case Test::STATUS_UNASSIGNED:
  257. return "danger";
  258. case Test::STATUS_ASSIGNED:
  259. case Test::STATUS_CONFIRMED:
  260. case Test::STATUS_STARTED:
  261. return "warning";
  262. case Test::STATUS_PENDING_VERIFICATION:
  263. return "primary";
  264. case Test::STATUS_COMPLETED:
  265. return "success";
  266. }
  267. }
  268. public function getProject() {
  269. return Project::get($this->project_id);
  270. }
  271. public function getLocation() {
  272. return Location::get($this->location_id);
  273. }
  274. public function getTester() {
  275. return User::get($this->tester_id);
  276. }
  277. public function getVerifier() {
  278. return User::get($this->verifier_id);
  279. }
  280. public function getValues($as_assoc_array = false) {
  281. $rows = TestValue::query(["test_id" => $this->id]);
  282. if($as_assoc_array) {
  283. $k = array_column($rows, "name");
  284. $v = array_column($rows, "value");
  285. $values = array_combine($k, $v);
  286. }
  287. else {
  288. $values = $rows;
  289. }
  290. return $values;
  291. }
  292. public function getDirectory() {
  293. $directory = ROOT_DIR . "/files/tests/" . $this->id;
  294. if($this->project_id == "PRACTICE") {
  295. $directory = ROOT_DIR . "/files/users/$this->tester_id/practice_tests/$this->template_id";
  296. }
  297. if(!file_exists($directory)) mkdir($directory, 0777, true);
  298. return $directory;
  299. }
  300. public function getDuration() {
  301. if(is_null($this->dts_end)) return false;
  302. $seconds = Util::getDurationFromDTS($this->dts_start, $this->dts_end);
  303. return $seconds;
  304. }
  305. public function getDisplayDuration() {
  306. $secs = $this->getDuration();
  307. $mins = floor($secs / 60);
  308. $secs = $secs % 60;
  309. return "$mins min. $secs sec.";
  310. }
  311. public function getPassClass() {
  312. if(is_null($this->pass)) return "secondary";
  313. return $this->pass ? "success" : "danger";
  314. }
  315. public function getPassIcon() {
  316. if(is_null($this->pass)) return "clock";
  317. return $this->pass ? "check-circle" : "times-circle";
  318. }
  319. public function getPassText() {
  320. if(is_null($this->pass)) return "Pending";
  321. return $this->pass ? __("Pass") : __("Fail");
  322. }
  323. public function getMPDF() {
  324. $id = $this->id;
  325. $output = false;
  326. require ROOT_DIR . "/controllers/test_pdf.php";
  327. return $mpdf;
  328. }
  329. public function assignTester($tester) {
  330. $this->tester_id = $tester->id;
  331. $this->dts_assigned = Util::getDateTime();
  332. $success = $this->commit();
  333. // TODO: Notify tester
  334. return $success;
  335. }
  336. public function getPreviousTest() {
  337. $nvp = [
  338. "dts_start" => (new Constraint("LESS_THAN"))->setValues($this->dts_start),
  339. "location_id" => $this->location_id,
  340. "status" => Test::STATUS_COMPLETED,
  341. ];
  342. $tests = Test::query($nvp, [
  343. "sort" => "dts_start",
  344. "order" => "desc",
  345. ]);
  346. return $tests ? $tests[0] : false;
  347. }
  348. public function unassignTester() {
  349. $this->status = Test::STATUS_UNASSIGNED;
  350. $this->tester_id = null;
  351. $this->dts_due = null;
  352. $this->dts_assigned = null;
  353. $success = $this->commit();
  354. return $success;
  355. }
  356. public function isPendingVerification() {
  357. return $this->status == Test::STATUS_PENDING_VERIFICATION;
  358. }
  359. public function hasTesterInvoicePDF() {
  360. $directory = $this->getDirectory();
  361. return file_exists($directory . "/tester_invoice.pdf");
  362. }
  363. public function markTesterInvoicePaid() {
  364. if($this->status_tester_invoice == Test::STATUS_TESTER_INVOICE_PENDING) {
  365. $this->status_tester_invoice = Test::STATUS_TESTER_INVOICE_PAID;
  366. $this->dts_tester_invoice_paid = Util::getDateTime();
  367. return $this->commit();
  368. }
  369. return false;
  370. }
  371. public function rejectTesterInvoice() {
  372. if($this->status_tester_invoice == Test::STATUS_TESTER_INVOICE_PENDING) {
  373. $this->status_tester_invoice = Test::STATUS_TESTER_INVOICE_NONE;
  374. $this->dts_tester_invoice_generated = null;
  375. $pdf_filepath = $this->getDirectory() . "/tester_invoice.pdf";
  376. unlink($pdf_filepath);
  377. return $this->commit();
  378. }
  379. return false;
  380. }
  381. }
  382. ?>