test.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. <?php
  2. /**
  3. * @property int $id
  4. * @property int $project_id
  5. * @property int $location_id
  6. * @property int $tester_id
  7. * @property int $status
  8. * @property int $status_tester_invoice
  9. * @property float $customer_fee
  10. * @property float $test_fee
  11. * @property string $test_time
  12. * @property float $test_travel_cost
  13. * @property string $shop_left
  14. * @property string $shop_right
  15. * @property string $attire
  16. * @property int $challenge_stage
  17. * @property string $challenge_description
  18. * @property string $challenger_name
  19. * @property string $challenger_description
  20. * @property string $challenger_gender
  21. * @property string $challenger_eye_contact
  22. * @property string $additional_notes
  23. * @property string $dts_due
  24. * @property string $dts_start
  25. * @property string $dts_end
  26. * @property string $dts_tester_invoice_generated
  27. * @property string $dts_tester_invoice_paid
  28. * @property int $verifier_id
  29. * @property string $verifier_notes
  30. * @property bool $pass
  31. * @property bool $retest
  32. * @property string $dts_created
  33. * @property string $dts_updated
  34. * @property string $dts_assigned
  35. * @property string $dts_verified
  36. *
  37. * @method Test get(int $id)
  38. */
  39. class Test extends Model {
  40. protected static $table = "test";
  41. protected static $id_field = "id";
  42. 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"];
  43. const TEST_FIELDS = ["shop_left", "shop_right", "attire", "challenge_stage", "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact", "additional_notes"];
  44. // Constants
  45. const STATUS_CANCELLED = 0;
  46. const STATUS_UNASSIGNED = 1; // Location needs a tester
  47. const STATUS_ASSIGNED = 2; // Tester assigned
  48. const STATUS_CONFIRMED = 3; // Tester has said they can do it
  49. const STATUS_STARTED = 4;
  50. const STATUS_PENDING_VERIFICATION = 8;
  51. const STATUS_COMPLETED = 16;
  52. const STATUS_TESTER_INVOICE_NONE = 0;
  53. const STATUS_TESTER_INVOICE_PENDING = 100;
  54. const STATUS_TESTER_INVOICE_PAID = 200;
  55. const STATUSES = [
  56. 0 => "Cancelled",
  57. 1 => "Unassigned",
  58. 2 => "Assigned",
  59. 3 => "Confirmed",
  60. 4 => "Started",
  61. 8 => "Pending Verification",
  62. 16 => "Completed",
  63. ];
  64. const STATUSES_TESTER_INVOICE = [
  65. Test::STATUS_TESTER_INVOICE_NONE,
  66. Test::STATUS_TESTER_INVOICE_PENDING,
  67. Test::STATUS_TESTER_INVOICE_PAID
  68. ];
  69. const STAGES = [
  70. 0 => "",
  71. 1 => "Challenge on entry",
  72. 2 => "Challenge while browsing",
  73. 3 => "Challenge during play",
  74. 4 => "Challenge on exit",
  75. 5 => "No challenge",
  76. ];
  77. const TEST_TIME_ANYTIME = 1;
  78. const TEST_TIME_DAY = 2;
  79. const TEST_TIME_NIGHT = 3;
  80. // Static methods
  81. public static function getStatusTesterInvoiceText($status) {
  82. switch($status) {
  83. case Test::STATUS_TESTER_INVOICE_NONE:
  84. return __("No Invoice Generated");
  85. case Test::STATUS_TESTER_INVOICE_PENDING:
  86. return __("Pending Payment");
  87. case Test::STATUS_TESTER_INVOICE_PAID:
  88. return __("Paid");
  89. }
  90. return __("Unknown");
  91. }
  92. public static function getStatusTesterInvoiceClass($status) {
  93. switch($status) {
  94. case Test::STATUS_TESTER_INVOICE_NONE:
  95. return "secondary";
  96. case Test::STATUS_TESTER_INVOICE_PENDING:
  97. return "warning";
  98. case Test::STATUS_TESTER_INVOICE_PAID:
  99. return "success";
  100. }
  101. return "";
  102. }
  103. public static function exportCSV($processing_file, $tests = []) {
  104. // Preload
  105. Project::load(array_column($tests, "project_id"));
  106. Location::load(array_column($tests, "location_id"));
  107. User::load(array_column($tests, "tester_id"));
  108. // Get processing file
  109. $processing_file_path = ROOT_DIR . "/export_processing/{$processing_file}";
  110. if(!file_exists($processing_file_path)) throw new Exception("Processing file not found.");
  111. // Output
  112. HTTP::setDownloadHeaders("export.csv");
  113. $handle = fopen("php://output", "w");
  114. require $processing_file_path;
  115. fclose($handle);
  116. }
  117. // Object methods
  118. public function sendAssignEmail($message) {
  119. $tester = $this->getTester();
  120. $location = $this->getLocation();
  121. $project = $this->getProject();
  122. // Content
  123. $name = htmlspecialchars($tester->first_name);
  124. $message = htmlspecialchars($message);
  125. $base_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/tests/" . $this->id);
  126. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  127. $date_due = date("j M Y", strtotime($this->dts_due));
  128. $admin_first_name = htmlspecialchars(Core::$user->first_name);
  129. $admin_phone = htmlspecialchars(Core::$user->phone);
  130. // Build
  131. $builder = new EmailBuilder;
  132. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  133. $builder->addLeadParagraph(__("Congrats, you have been allocated 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. if($message) {
  138. $builder->addParagraph($message);
  139. }
  140. $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."),
  141. '<b>', '</b>',
  142. '<b>', '</b>',
  143. '<b>', '</b>'
  144. ));
  145. $builder->addAcceptDeclineButtons($base_url);
  146. $builder->addParagraph(sprintf(__("The site address is: %s"), "<b>{$address}</b>"));
  147. $builder->addParagraph(sprintf(__("The date that need to complete and submit the results by is: %s"), "<font color=\"red\"><b>$date_due</b></font>"));
  148. $builder->addParagraph(sprintf(__("For methodology and further notes please check the %sUseful Documents%s section on the %sAccount%s page."),
  149. '<b>', '</b>',
  150. '<b>', '</b>'
  151. ));
  152. $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."),
  153. '<b>', '</b>',
  154. '<b>', '</b>'
  155. ));
  156. $builder->addParagraph(sprintf(__("If you have any questions please send them to %s or call %s on %s."),
  157. "<a href=\"mailto:tests@check-policy.com\">tests@check-policy.com</a>",
  158. "<b>$admin_first_name</b>",
  159. "<b>$admin_phone</b>"
  160. ));
  161. $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."),
  162. '<b>', '</b>'
  163. ));
  164. $content = $builder->build();
  165. // Send
  166. $mail = new Email;
  167. $mail->setHtmlContent($content);
  168. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  169. $mail->addAddress($tester->email, $tester->getDisplayName());
  170. $mail->setSubject(__("You have been allocated a test"));
  171. $mail->send();
  172. }
  173. public function sendClaimedEmail() {
  174. $tester = $this->getTester();
  175. $location = $this->getLocation();
  176. $project = $this->getProject();
  177. // Content
  178. $name = htmlspecialchars($tester->first_name);
  179. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  180. $date_due = date("j M Y", strtotime($this->dts_due));
  181. // Build
  182. $builder = new EmailBuilder;
  183. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  184. $builder->addLeadParagraph(__("Congrats, you have claimed a test!"));
  185. if($project->isNighttime()) {
  186. $builder->addParagraph("<b><font color=\"red\">" . __("Evening Test to be done between 7PM and midnight.") . "</font></b>");
  187. }
  188. $builder->addParagraph(sprintf(__("The site address is: %s"), "<b>$address</b>"));
  189. $builder->addParagraph(sprintf(__("The date that need to complete and submit the results by is: %s"), "<font color=\"red\"><b>$date_due</b></font>"));
  190. $builder->addParagraph(sprintf(__("For methodology and further notes please check the %sUseful Documents%s section on the %sAccount%s page."),
  191. '<b>', '</b>',
  192. '<b>', '</b>'
  193. ));
  194. $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."),
  195. '<b>', '</b>',
  196. '<b>', '</b>'
  197. ));
  198. $builder->addParagraph(sprintf(__("If you have any questions please send them to %s."),
  199. "<a href=\"mailto:tests@check-policy.com\">tests@check-policy.com</a>"
  200. ));
  201. $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."));
  202. $content = $builder->build();
  203. // Send
  204. $mail = new Email;
  205. $mail->setHtmlContent($content);
  206. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  207. $mail->addAddress($tester->email, $tester->getDisplayName());
  208. $mail->setSubject(__("You have claimed a test"));
  209. $mail->send();
  210. }
  211. public function sendDeclineEmail() {
  212. $tester = $this->getTester();
  213. $location = $this->getLocation();
  214. $project = $this->getProject();
  215. // Content
  216. $name = htmlspecialchars($tester->getDisplayName());
  217. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  218. $project_name = htmlspecialchars($project->name);
  219. $project_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/projects/" . $project->id);
  220. $internal_id = "N/A";
  221. if($location->internal_id) $internal_id = htmlspecialchars($location->internal_id);
  222. // Build
  223. $builder = new EmailBuilder;
  224. $builder->addParagraph("<b>Hi,</b>");
  225. $builder->addLeadParagraph("A tester has declined a test.");
  226. $builder->addParagraph("<b>Project:</b><br />$project_name");
  227. $builder->addParagraph("<b>Site:</b><br />$address<br />ID: $location->id | Internal ID: $internal_id");
  228. $builder->addParagraph("<b>Who:</b><br />$name<br />ID: $tester->id");
  229. $builder->addParagraph("<a href=\"$project_url\">Click here</a> to manage the project and reassign the test.");
  230. $content = $builder->build();
  231. // Send
  232. $mail = new Email;
  233. $mail->setHtmlContent($content);
  234. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  235. $mail->addAddress(EMAIL_CONTACT, APP_NAME . " Contact");
  236. $mail->setSubject("Test declined: $project->name / $location->name");
  237. $mail->send();
  238. }
  239. public function sendResultEmail($to_address) {
  240. $location = $this->getLocation();
  241. $project = $this->getProject();
  242. $template = $project->getTemplate();
  243. $subject_format_file_path = $template->getProcessingFilePath("result_email_subject_format.txt");
  244. $subject_format = file_get_contents($subject_format_file_path);
  245. // Content
  246. $pass_text = strtoupper($this->getPassText());
  247. $stage = $this->challenge_stage;
  248. $location_parts = [$location->internal_id, $location->name];
  249. $location_string = implode(" : ", array_filter($location_parts));
  250. $unix_start = strtotime($this->dts_start . " UTC");
  251. $date_time = date("d/m/Y H:i", $unix_start);
  252. // Subject
  253. $search_replace = [
  254. "{pass}" => $pass_text,
  255. "{stage}" => $stage,
  256. "{location}" => $location_string,
  257. "{date-time}" => $date_time,
  258. ];
  259. $subject = str_replace(
  260. array_keys($search_replace),
  261. array_values($search_replace),
  262. $subject_format
  263. );
  264. // Attachments
  265. $pdf_path = $this->getDirectory() . "/pdf.pdf";
  266. $pdf_name = implode(" - ", array_filter([$location->internal_id, date("jS M Y")])) . ".pdf";
  267. // Lead paragraph
  268. $lead_paragraph = "";
  269. if($this->pass) {
  270. $lead_paragraph .= "Congratulations!<br />";
  271. }
  272. $lead_paragraph .= "A test has been completed at your location.";
  273. // Build
  274. $builder = new EmailBuilder;
  275. $builder->addLeadParagraph($lead_paragraph);
  276. $builder->addParagraph("Please see attached PDF and do not hesitate to contact us if you have any questions.");
  277. $content = $builder->build();
  278. // Send
  279. $mail = new Email;
  280. $mail->setHtmlContent($content);
  281. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  282. $mail->addAddress($to_address);
  283. $mail->addAttachment($pdf_path, $pdf_name);
  284. $mail->setSubject($subject);
  285. $mail->send();
  286. }
  287. public function decline() {
  288. $this->sendDeclineEmail();
  289. $this->status = Test::STATUS_UNASSIGNED;
  290. $this->tester_id = null;
  291. $this->dts_due = null;
  292. $this->dts_assigned = null;
  293. $this->dts_updated = Util::getDateTime();
  294. return $this->commit();
  295. }
  296. public function getChallengeStageText() {
  297. if(is_null($this->challenge_stage)) return "";
  298. return Test::STAGES[$this->challenge_stage];
  299. }
  300. public function getStatusText() {
  301. return Test::STATUSES[$this->status];
  302. }
  303. public function getStatusClass() {
  304. switch($this->status) {
  305. case Test::STATUS_CANCELLED:
  306. return "secondary";
  307. case Test::STATUS_UNASSIGNED:
  308. return "danger";
  309. case Test::STATUS_ASSIGNED:
  310. case Test::STATUS_CONFIRMED:
  311. case Test::STATUS_STARTED:
  312. return "warning";
  313. case Test::STATUS_PENDING_VERIFICATION:
  314. return "primary";
  315. case Test::STATUS_COMPLETED:
  316. return "success";
  317. }
  318. }
  319. public function getProject() {
  320. return Project::get($this->project_id);
  321. }
  322. /**
  323. * @return Location
  324. */
  325. public function getLocation() {
  326. return Location::get($this->location_id);
  327. }
  328. /**
  329. * @return User
  330. */
  331. public function getTester() {
  332. return User::get($this->tester_id);
  333. }
  334. /**
  335. * @return User
  336. */
  337. public function getVerifier() {
  338. return User::get($this->verifier_id);
  339. }
  340. public function getValues($as_assoc_array = false) {
  341. $rows = TestValue::query(["test_id" => $this->id]);
  342. if($as_assoc_array) {
  343. $k = array_column($rows, "name");
  344. $v = array_column($rows, "value");
  345. $values = array_combine($k, $v);
  346. }
  347. else {
  348. $values = $rows;
  349. }
  350. return $values;
  351. }
  352. public function getDirectory() {
  353. $directory = ROOT_DIR . "/files/tests/" . $this->id;
  354. if($this->project_id == "PRACTICE") {
  355. $directory = ROOT_DIR . "/files/users/$this->tester_id/practice_tests/$this->template_id";
  356. }
  357. if(!file_exists($directory)) mkdir($directory, 0777, true);
  358. return $directory;
  359. }
  360. public function getDuration() {
  361. if(is_null($this->dts_end)) return false;
  362. $seconds = Util::getDurationFromDTS($this->dts_start, $this->dts_end);
  363. return $seconds;
  364. }
  365. public function getDisplayDuration() {
  366. $secs = $this->getDuration();
  367. $mins = floor($secs / 60);
  368. $secs = $secs % 60;
  369. return "$mins min. $secs sec.";
  370. }
  371. /**
  372. * @return string Bootstrap class name
  373. */
  374. public function getPassClass() {
  375. if(is_null($this->pass)) return "secondary";
  376. return $this->pass ? "success" : "danger";
  377. }
  378. /**
  379. * @return string FA icon name without prefix
  380. */
  381. public function getPassIcon() {
  382. if(is_null($this->pass)) return "clock";
  383. return $this->pass ? "check-circle" : "times-circle";
  384. }
  385. /**
  386. * @return string Pending, Pass or Fail
  387. */
  388. public function getPassText() {
  389. if(is_null($this->pass)) return __("Pending");
  390. return $this->pass ? __("Pass") : __("Fail");
  391. }
  392. public function getMPDF() {
  393. $id = $this->id;
  394. $output = false;
  395. require ROOT_DIR . "/controllers/test_pdf.php";
  396. return $mpdf;
  397. }
  398. public function assignTester($tester) {
  399. $this->tester_id = $tester->id;
  400. $this->dts_assigned = Util::getDateTime();
  401. $success = $this->commit();
  402. // TODO: Notify tester
  403. return $success;
  404. }
  405. public function getPreviousTest() {
  406. $nvp = [
  407. "dts_start" => (new Constraint("LESS_THAN"))->setValues($this->dts_start),
  408. "location_id" => $this->location_id,
  409. "status" => Test::STATUS_COMPLETED,
  410. ];
  411. $tests = Test::query($nvp, [
  412. "sort" => "dts_start",
  413. "order" => "desc",
  414. ]);
  415. return $tests ? $tests[0] : false;
  416. }
  417. public function unassignTester() {
  418. $this->status = Test::STATUS_UNASSIGNED;
  419. $this->tester_id = null;
  420. $this->dts_due = null;
  421. $this->dts_assigned = null;
  422. $success = $this->commit();
  423. return $success;
  424. }
  425. public function isPass(): bool {
  426. if(is_null($this->pass)) return false;
  427. return !empty($this->pass);
  428. }
  429. public function isPending(): bool {
  430. return is_null($this->pass);
  431. }
  432. public function isFail(): bool {
  433. if(is_null($this->pass)) return false;
  434. return empty($this->pass);
  435. }
  436. public function isCompleted() {
  437. return $this->status == Test::STATUS_COMPLETED;
  438. }
  439. public function isPendingVerification() {
  440. return $this->status == Test::STATUS_PENDING_VERIFICATION;
  441. }
  442. public function hasPDF() {
  443. $directory = $this->getDirectory();
  444. return file_exists($directory . "/pdf.pdf");
  445. }
  446. public function getPDFUrl() {
  447. return "/account/tests/{$this->id}/files/pdf";
  448. }
  449. public function hasTesterInvoicePDF() {
  450. $directory = $this->getDirectory();
  451. return file_exists($directory . "/tester_invoice.pdf");
  452. }
  453. public function markTesterInvoicePaid() {
  454. if($this->status_tester_invoice == Test::STATUS_TESTER_INVOICE_PENDING) {
  455. $this->status_tester_invoice = Test::STATUS_TESTER_INVOICE_PAID;
  456. $this->dts_tester_invoice_paid = Util::getDateTime();
  457. return $this->commit();
  458. }
  459. return false;
  460. }
  461. public function rejectTesterInvoice() {
  462. if($this->status_tester_invoice == Test::STATUS_TESTER_INVOICE_PENDING) {
  463. $this->status_tester_invoice = Test::STATUS_TESTER_INVOICE_NONE;
  464. $this->dts_tester_invoice_generated = null;
  465. $pdf_filepath = $this->getDirectory() . "/tester_invoice.pdf";
  466. unlink($pdf_filepath);
  467. return $this->commit();
  468. }
  469. return false;
  470. }
  471. public function getTesterInvoiceReference(): string {
  472. $location = $this->getLocation();
  473. $tester = $this->getTester();
  474. return
  475. "T" . str_pad($this->id, 3, "0", STR_PAD_LEFT) .
  476. "/L" . str_pad($location->id, 3, "0", STR_PAD_LEFT) .
  477. "/U" . str_pad($tester->id, 3, "0", STR_PAD_LEFT);
  478. }
  479. }
  480. ?>