test.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. // Object methods
  104. public function sendAssignEmail($message) {
  105. $tester = $this->getTester();
  106. $location = $this->getLocation();
  107. $project = $this->getProject();
  108. // Content
  109. $name = htmlspecialchars($tester->first_name);
  110. $message = htmlspecialchars($message);
  111. $base_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/tests/" . $this->id);
  112. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  113. $date_due = date("j M Y", strtotime($this->dts_due));
  114. $admin_first_name = htmlspecialchars(Core::$user->first_name);
  115. $admin_phone = htmlspecialchars(Core::$user->phone);
  116. // Build
  117. $builder = new EmailBuilder;
  118. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  119. $builder->addLeadParagraph(__("Congrats, you have been allocated a test!"));
  120. if($project->isNighttime()) {
  121. $builder->addParagraph("<b><font color=\"red\">" . __("Evening Test to be done between 7PM and midnight.") . "</font></b>");
  122. }
  123. if($message) {
  124. $builder->addParagraph($message);
  125. }
  126. $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."),
  127. '<b>', '</b>',
  128. '<b>', '</b>',
  129. '<b>', '</b>'
  130. ));
  131. $builder->addAcceptDeclineButtons($base_url);
  132. $builder->addParagraph(sprintf(__("The site address is: %s"), "<b>{$address}</b>"));
  133. $builder->addParagraph(sprintf(__("The date that need to complete and submit the results by is: %s"), "<font color=\"red\"><b>$date_due</b></font>"));
  134. $builder->addParagraph(sprintf(__("For methodology and further notes please check the %sUseful Documents%s section on the %sAccount%s page."),
  135. '<b>', '</b>',
  136. '<b>', '</b>'
  137. ));
  138. $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."),
  139. '<b>', '</b>',
  140. '<b>', '</b>'
  141. ));
  142. $builder->addParagraph(sprintf(__("If you have any questions please send them to %s or call %s on %s."),
  143. "<a href=\"mailto:tests@check-policy.com\">tests@check-policy.com</a>",
  144. "<b>$admin_first_name</b>",
  145. "<b>$admin_phone</b>"
  146. ));
  147. $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."),
  148. '<b>', '</b>'
  149. ));
  150. $content = $builder->build();
  151. // Send
  152. $mail = new Email;
  153. $mail->setHtmlContent($content);
  154. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  155. $mail->addAddress($tester->email, $tester->getDisplayName());
  156. $mail->setSubject(__("You have been allocated a test"));
  157. $mail->send();
  158. }
  159. public function sendClaimedEmail() {
  160. $tester = $this->getTester();
  161. $location = $this->getLocation();
  162. $project = $this->getProject();
  163. // Content
  164. $name = htmlspecialchars($tester->first_name);
  165. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  166. $date_due = date("j M Y", strtotime($this->dts_due));
  167. // Build
  168. $builder = new EmailBuilder;
  169. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  170. $builder->addLeadParagraph(__("Congrats, you have claimed a test!"));
  171. if($project->isNighttime()) {
  172. $builder->addParagraph("<b><font color=\"red\">" . __("Evening Test to be done between 7PM and midnight.") . "</font></b>");
  173. }
  174. $builder->addParagraph(sprintf(__("The site address is: %s"), "<b>$address</b>"));
  175. $builder->addParagraph(sprintf(__("The date that need to complete and submit the results by is: %s"), "<font color=\"red\"><b>$date_due</b></font>"));
  176. $builder->addParagraph(sprintf(__("For methodology and further notes please check the %sUseful Documents%s section on the %sAccount%s page."),
  177. '<b>', '</b>',
  178. '<b>', '</b>'
  179. ));
  180. $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."),
  181. '<b>', '</b>',
  182. '<b>', '</b>'
  183. ));
  184. $builder->addParagraph(sprintf(__("If you have any questions please send them to %s."),
  185. "<a href=\"mailto:tests@check-policy.com\">tests@check-policy.com</a>"
  186. ));
  187. $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."));
  188. $content = $builder->build();
  189. // Send
  190. $mail = new Email;
  191. $mail->setHtmlContent($content);
  192. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  193. $mail->addAddress($tester->email, $tester->getDisplayName());
  194. $mail->setSubject(__("You have claimed a test"));
  195. $mail->send();
  196. }
  197. public function sendDeclineEmail() {
  198. $tester = $this->getTester();
  199. $location = $this->getLocation();
  200. $project = $this->getProject();
  201. // Content
  202. $name = htmlspecialchars($tester->getDisplayName());
  203. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  204. $project_name = htmlspecialchars($project->name);
  205. $project_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/projects/" . $project->id);
  206. $internal_id = "N/A";
  207. if($location->internal_id) $internal_id = htmlspecialchars($location->internal_id);
  208. // Build
  209. $builder = new EmailBuilder;
  210. $builder->addParagraph("<b>Hi,</b>");
  211. $builder->addLeadParagraph("A tester has declined a test.");
  212. $builder->addParagraph("<b>Project:</b><br />$project_name");
  213. $builder->addParagraph("<b>Site:</b><br />$address<br />ID: $location->id | Internal ID: $internal_id");
  214. $builder->addParagraph("<b>Who:</b><br />$name<br />ID: $tester->id");
  215. $builder->addParagraph("<a href=\"$project_url\">Click here</a> to manage the project and reassign the test.");
  216. $content = $builder->build();
  217. // Send
  218. $mail = new Email;
  219. $mail->setHtmlContent($content);
  220. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  221. $mail->addAddress(EMAIL_CONTACT, APP_NAME . " Contact");
  222. $mail->setSubject("Test declined: $project->name / $location->name");
  223. $mail->send();
  224. }
  225. public function sendResultEmail($to_address) {
  226. $location = $this->getLocation();
  227. $project = $this->getProject();
  228. $template = $project->getTemplate();
  229. $subject_format_file_path = $template->getProcessingFilePath("result_email_subject_format.txt");
  230. $subject_format = file_get_contents($subject_format_file_path);
  231. // Content
  232. $pass_text = strtoupper($this->getPassText());
  233. $stage = $this->challenge_stage;
  234. $location_parts = [$location->internal_id, $location->name];
  235. $location_string = implode(" : ", array_filter($location_parts));
  236. $unix_start = strtotime($this->dts_start . " UTC");
  237. $date_time = date("d/m/Y H:i", $unix_start);
  238. // Subject
  239. $search_replace = [
  240. "{pass}" => $pass_text,
  241. "{stage}" => $stage,
  242. "{location}" => $location_string,
  243. "{date-time}" => $date_time,
  244. ];
  245. $subject = str_replace(
  246. array_keys($search_replace),
  247. array_values($search_replace),
  248. $subject_format
  249. );
  250. // Attachments
  251. $pdf_path = $this->getDirectory() . "/pdf.pdf";
  252. $pdf_name = implode(" - ", array_filter([$location->internal_id, date("jS M Y")])) . ".pdf";
  253. // Lead paragraph
  254. $lead_paragraph = "";
  255. if($this->pass) {
  256. $lead_paragraph .= "Congratulations!<br />";
  257. }
  258. $lead_paragraph .= "A test has been completed at your location.";
  259. // Build
  260. $builder = new EmailBuilder;
  261. $builder->addLeadParagraph($lead_paragraph);
  262. $builder->addParagraph("Please see attached PDF and do not hesitate to contact us if you have any questions.");
  263. $content = $builder->build();
  264. // Send
  265. $mail = new Email;
  266. $mail->setHtmlContent($content);
  267. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  268. $mail->addAddress($to_address);
  269. $mail->addAttachment($pdf_path, $pdf_name);
  270. $mail->setSubject($subject);
  271. $mail->send();
  272. }
  273. public function decline() {
  274. $this->sendDeclineEmail();
  275. $this->status = Test::STATUS_UNASSIGNED;
  276. $this->tester_id = null;
  277. $this->dts_due = null;
  278. $this->dts_assigned = null;
  279. $this->dts_updated = Util::getDateTime();
  280. return $this->commit();
  281. }
  282. public function getChallengeStageText() {
  283. if(is_null($this->challenge_stage)) return "";
  284. return Test::STAGES[$this->challenge_stage];
  285. }
  286. public function getStatusText() {
  287. return Test::STATUSES[$this->status];
  288. }
  289. public function getStatusClass() {
  290. switch($this->status) {
  291. case Test::STATUS_CANCELLED:
  292. return "secondary";
  293. case Test::STATUS_UNASSIGNED:
  294. return "danger";
  295. case Test::STATUS_ASSIGNED:
  296. case Test::STATUS_CONFIRMED:
  297. case Test::STATUS_STARTED:
  298. return "warning";
  299. case Test::STATUS_PENDING_VERIFICATION:
  300. return "primary";
  301. case Test::STATUS_COMPLETED:
  302. return "success";
  303. }
  304. }
  305. public function getProject() {
  306. return Project::get($this->project_id);
  307. }
  308. /**
  309. * @return Location
  310. */
  311. public function getLocation() {
  312. return Location::get($this->location_id);
  313. }
  314. public function getTester() {
  315. return User::get($this->tester_id);
  316. }
  317. public function getVerifier() {
  318. return User::get($this->verifier_id);
  319. }
  320. public function getValues($as_assoc_array = false) {
  321. $rows = TestValue::query(["test_id" => $this->id]);
  322. if($as_assoc_array) {
  323. $k = array_column($rows, "name");
  324. $v = array_column($rows, "value");
  325. $values = array_combine($k, $v);
  326. }
  327. else {
  328. $values = $rows;
  329. }
  330. return $values;
  331. }
  332. public function getDirectory() {
  333. $directory = ROOT_DIR . "/files/tests/" . $this->id;
  334. if($this->project_id == "PRACTICE") {
  335. $directory = ROOT_DIR . "/files/users/$this->tester_id/practice_tests/$this->template_id";
  336. }
  337. if(!file_exists($directory)) mkdir($directory, 0777, true);
  338. return $directory;
  339. }
  340. public function getDuration() {
  341. if(is_null($this->dts_end)) return false;
  342. $seconds = Util::getDurationFromDTS($this->dts_start, $this->dts_end);
  343. return $seconds;
  344. }
  345. public function getDisplayDuration() {
  346. $secs = $this->getDuration();
  347. $mins = floor($secs / 60);
  348. $secs = $secs % 60;
  349. return "$mins min. $secs sec.";
  350. }
  351. /**
  352. * @return string Bootstrap class name
  353. */
  354. public function getPassClass() {
  355. if(is_null($this->pass)) return "secondary";
  356. return $this->pass ? "success" : "danger";
  357. }
  358. /**
  359. * @return string FA icon name without prefix
  360. */
  361. public function getPassIcon() {
  362. if(is_null($this->pass)) return "clock";
  363. return $this->pass ? "check-circle" : "times-circle";
  364. }
  365. /**
  366. * @return string Pending, Pass or Fail
  367. */
  368. public function getPassText() {
  369. if(is_null($this->pass)) return __("Pending");
  370. return $this->pass ? __("Pass") : __("Fail");
  371. }
  372. public function getMPDF() {
  373. $id = $this->id;
  374. $output = false;
  375. require ROOT_DIR . "/controllers/test_pdf.php";
  376. return $mpdf;
  377. }
  378. public function assignTester($tester) {
  379. $this->tester_id = $tester->id;
  380. $this->dts_assigned = Util::getDateTime();
  381. $success = $this->commit();
  382. // TODO: Notify tester
  383. return $success;
  384. }
  385. public function getPreviousTest() {
  386. $nvp = [
  387. "dts_start" => (new Constraint("LESS_THAN"))->setValues($this->dts_start),
  388. "location_id" => $this->location_id,
  389. "status" => Test::STATUS_COMPLETED,
  390. ];
  391. $tests = Test::query($nvp, [
  392. "sort" => "dts_start",
  393. "order" => "desc",
  394. ]);
  395. return $tests ? $tests[0] : false;
  396. }
  397. public function unassignTester() {
  398. $this->status = Test::STATUS_UNASSIGNED;
  399. $this->tester_id = null;
  400. $this->dts_due = null;
  401. $this->dts_assigned = null;
  402. $success = $this->commit();
  403. return $success;
  404. }
  405. public function isPendingVerification() {
  406. return $this->status == Test::STATUS_PENDING_VERIFICATION;
  407. }
  408. public function hasTesterInvoicePDF() {
  409. $directory = $this->getDirectory();
  410. return file_exists($directory . "/tester_invoice.pdf");
  411. }
  412. public function markTesterInvoicePaid() {
  413. if($this->status_tester_invoice == Test::STATUS_TESTER_INVOICE_PENDING) {
  414. $this->status_tester_invoice = Test::STATUS_TESTER_INVOICE_PAID;
  415. $this->dts_tester_invoice_paid = Util::getDateTime();
  416. return $this->commit();
  417. }
  418. return false;
  419. }
  420. public function rejectTesterInvoice() {
  421. if($this->status_tester_invoice == Test::STATUS_TESTER_INVOICE_PENDING) {
  422. $this->status_tester_invoice = Test::STATUS_TESTER_INVOICE_NONE;
  423. $this->dts_tester_invoice_generated = null;
  424. $pdf_filepath = $this->getDirectory() . "/tester_invoice.pdf";
  425. unlink($pdf_filepath);
  426. return $this->commit();
  427. }
  428. return false;
  429. }
  430. }
  431. ?>