1
0

test.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 or call %s on %s."),
  148. "<a href=\"mailto:tests@check-policy.com\">tests@check-policy.com</a>",
  149. "<b>$admin_first_name</b>",
  150. "<b>$admin_phone</b>"
  151. ));
  152. $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."));
  153. $content = $builder->build();
  154. // Send
  155. $mail = new Email;
  156. $mail->setHtmlContent($content);
  157. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  158. $mail->addAddress($tester->email, $tester->getDisplayName());
  159. $mail->setSubject(__("You have claimed a test"));
  160. $mail->send();
  161. }
  162. public function sendDeclineEmail() {
  163. $tester = $this->getTester();
  164. $location = $this->getLocation();
  165. $project = $this->getProject();
  166. // Content
  167. $name = htmlspecialchars($tester->getDisplayName());
  168. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  169. $project_name = htmlspecialchars($project->name);
  170. $project_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/projects/" . $project->id);
  171. $internal_id = "N/A";
  172. if($location->internal_id) $internal_id = htmlspecialchars($location->internal_id);
  173. // Build
  174. $builder = new EmailBuilder;
  175. $builder->addParagraph("<b>Hi,</b>");
  176. $builder->addLeadParagraph("A tester has declined a test.");
  177. $builder->addParagraph("<b>Project:</b><br />$project_name");
  178. $builder->addParagraph("<b>Site:</b><br />$address<br />ID: $location->id | Internal ID: $internal_id");
  179. $builder->addParagraph("<b>Who:</b><br />$name<br />ID: $tester->id");
  180. $builder->addParagraph("<a href=\"$project_url\">Click here</a> to manage the project and reassign the test.");
  181. $content = $builder->build();
  182. // Send
  183. $mail = new Email;
  184. $mail->setHtmlContent($content);
  185. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  186. $mail->addAddress(EMAIL_CONTACT, APP_NAME . " Contact");
  187. $mail->setSubject("Test declined: $project->name / $location->name");
  188. $mail->send();
  189. }
  190. public function sendResultEmail($to_address) {
  191. $location = $this->getLocation();
  192. $project = $this->getProject();
  193. $template = $project->getTemplate();
  194. $subject_format_file_path = $template->getProcessingFilePath("result_email_subject_format.txt");
  195. $subject_format = file_get_contents($subject_format_file_path);
  196. // Content
  197. $pass_text = strtoupper($this->getPassText());
  198. $stage = $this->challenge_stage;
  199. $location_parts = [$location->internal_id, $location->name];
  200. $location_string = implode(" : ", array_filter($location_parts));
  201. $unix_start = strtotime($this->dts_start . " UTC");
  202. $date_time = date("d/m/Y H:i", $unix_start);
  203. // Subject
  204. $search_replace = [
  205. "{pass}" => $pass_text,
  206. "{stage}" => $stage,
  207. "{location}" => $location_string,
  208. "{date-time}" => $date_time,
  209. ];
  210. $subject = str_replace(
  211. array_keys($search_replace),
  212. array_values($search_replace),
  213. $subject_format
  214. );
  215. // Attachments
  216. $pdf_path = $this->getDirectory() . "/pdf.pdf";
  217. $pdf_name = implode(" - ", array_filter([$location->internal_id, date("jS M Y")])) . ".pdf";
  218. // Lead paragraph
  219. $lead_paragraph = "";
  220. if($this->pass) {
  221. $lead_paragraph .= "Congratulations!<br />";
  222. }
  223. $lead_paragraph .= "A test has been completed at your location.";
  224. // Build
  225. $builder = new EmailBuilder;
  226. $builder->addLeadParagraph($lead_paragraph);
  227. $builder->addParagraph("Please see attached PDF and do not hesitate to contact us if you have any questions.");
  228. $content = $builder->build();
  229. // Send
  230. $mail = new Email;
  231. $mail->setHtmlContent($content);
  232. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  233. $mail->addAddress($to_address);
  234. $mail->addAttachment($pdf_path, $pdf_name);
  235. $mail->setSubject($subject);
  236. $mail->send();
  237. }
  238. public function decline() {
  239. $this->sendDeclineEmail();
  240. $this->status = Test::STATUS_UNASSIGNED;
  241. $this->tester_id = null;
  242. $this->dts_due = null;
  243. $this->dts_assigned = null;
  244. $this->dts_updated = Util::getDateTime();
  245. return $this->commit();
  246. }
  247. public function getChallengeStageText() {
  248. if(is_null($this->challenge_stage)) return "";
  249. return Test::STAGES[$this->challenge_stage];
  250. }
  251. public function getStatusText() {
  252. return Test::STATUSES[$this->status];
  253. }
  254. public function getStatusClass() {
  255. switch($this->status) {
  256. case Test::STATUS_CANCELLED:
  257. return "secondary";
  258. case Test::STATUS_UNASSIGNED:
  259. return "danger";
  260. case Test::STATUS_ASSIGNED:
  261. case Test::STATUS_CONFIRMED:
  262. case Test::STATUS_STARTED:
  263. return "warning";
  264. case Test::STATUS_PENDING_VERIFICATION:
  265. return "primary";
  266. case Test::STATUS_COMPLETED:
  267. return "success";
  268. }
  269. }
  270. public function getProject() {
  271. return Project::get($this->project_id);
  272. }
  273. public function getLocation() {
  274. return Location::get($this->location_id);
  275. }
  276. public function getTester() {
  277. return User::get($this->tester_id);
  278. }
  279. public function getVerifier() {
  280. return User::get($this->verifier_id);
  281. }
  282. public function getValues($as_assoc_array = false) {
  283. $rows = TestValue::query(["test_id" => $this->id]);
  284. if($as_assoc_array) {
  285. $k = array_column($rows, "name");
  286. $v = array_column($rows, "value");
  287. $values = array_combine($k, $v);
  288. }
  289. else {
  290. $values = $rows;
  291. }
  292. return $values;
  293. }
  294. public function getDirectory() {
  295. $directory = ROOT_DIR . "/files/tests/" . $this->id;
  296. if($this->project_id == "PRACTICE") {
  297. $directory = ROOT_DIR . "/files/users/$this->tester_id/practice_tests/$this->template_id";
  298. }
  299. if(!file_exists($directory)) mkdir($directory, 0777, true);
  300. return $directory;
  301. }
  302. public function getDuration() {
  303. if(is_null($this->dts_end)) return false;
  304. $seconds = Util::getDurationFromDTS($this->dts_start, $this->dts_end);
  305. return $seconds;
  306. }
  307. public function getDisplayDuration() {
  308. $secs = $this->getDuration();
  309. $mins = floor($secs / 60);
  310. $secs = $secs % 60;
  311. return "$mins min. $secs sec.";
  312. }
  313. public function getPassClass() {
  314. if(is_null($this->pass)) return "secondary";
  315. return $this->pass ? "success" : "danger";
  316. }
  317. public function getPassIcon() {
  318. if(is_null($this->pass)) return "clock";
  319. return $this->pass ? "check-circle" : "times-circle";
  320. }
  321. public function getPassText() {
  322. if(is_null($this->pass)) return "Pending";
  323. return $this->pass ? __("Pass") : __("Fail");
  324. }
  325. public function getMPDF() {
  326. $id = $this->id;
  327. $output = false;
  328. require ROOT_DIR . "/controllers/test_pdf.php";
  329. return $mpdf;
  330. }
  331. public function assignTester($tester) {
  332. $this->tester_id = $tester->id;
  333. $this->dts_assigned = Util::getDateTime();
  334. $success = $this->commit();
  335. // TODO: Notify tester
  336. return $success;
  337. }
  338. public function getPreviousTest() {
  339. $nvp = [
  340. "dts_start" => (new Constraint("LESS_THAN"))->setValues($this->dts_start),
  341. "location_id" => $this->location_id,
  342. "status" => Test::STATUS_COMPLETED,
  343. ];
  344. $tests = Test::query($nvp, [
  345. "sort" => "dts_start",
  346. "order" => "desc",
  347. ]);
  348. return $tests ? $tests[0] : false;
  349. }
  350. public function unassignTester() {
  351. $this->status = Test::STATUS_UNASSIGNED;
  352. $this->tester_id = null;
  353. $this->dts_due = null;
  354. $this->dts_assigned = null;
  355. $success = $this->commit();
  356. return $success;
  357. }
  358. public function isPendingVerification() {
  359. return $this->status == Test::STATUS_PENDING_VERIFICATION;
  360. }
  361. public function hasTesterInvoicePDF() {
  362. $directory = $this->getDirectory();
  363. return file_exists($directory . "/tester_invoice.pdf");
  364. }
  365. public function markTesterInvoicePaid() {
  366. if($this->status_tester_invoice == Test::STATUS_TESTER_INVOICE_PENDING) {
  367. $this->status_tester_invoice = Test::STATUS_TESTER_INVOICE_PAID;
  368. $this->dts_tester_invoice_paid = Util::getDateTime();
  369. return $this->commit();
  370. }
  371. return false;
  372. }
  373. public function rejectTesterInvoice() {
  374. if($this->status_tester_invoice == Test::STATUS_TESTER_INVOICE_PENDING) {
  375. $this->status_tester_invoice = Test::STATUS_TESTER_INVOICE_NONE;
  376. $this->dts_tester_invoice_generated = null;
  377. $pdf_filepath = $this->getDirectory() . "/tester_invoice.pdf";
  378. unlink($pdf_filepath);
  379. return $this->commit();
  380. }
  381. return false;
  382. }
  383. }
  384. ?>