test.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. <?php /** @noinspection HtmlDeprecatedTag */
  2. /** @noinspection HtmlDeprecatedAttribute */
  3. use Carbon\CarbonImmutable;
  4. use Mpdf\Mpdf;
  5. /**
  6. * @property int $id
  7. * @property int $project_id
  8. * @property int $location_id
  9. * @property int $tester_id
  10. * @property int $stripe_invoice_id
  11. * @property int $status
  12. * @property int $status_tester_invoice
  13. * @property int $payment_required
  14. * @property float $customer_fee
  15. * @property float $test_fee
  16. * @property string $test_time
  17. * @property float $test_travel_cost
  18. * @property string $test_travel_cost_description
  19. * @property string $shop_left
  20. * @property string $shop_right
  21. * @property string $attire
  22. * @property int $challenge_stage
  23. * @property string $challenge_description
  24. * @property string $challenger_name
  25. * @property string $challenger_description
  26. * @property string $challenger_gender
  27. * @property string $challenger_eye_contact
  28. * @property string $additional_notes
  29. * @property string $dts_due
  30. * @property string $dts_start
  31. * @property string $dts_end
  32. * @property string $dts_tester_invoice_generated
  33. * @property string $dts_tester_invoice_paid
  34. * @property int $verifier_id
  35. * @property string $verifier_notes
  36. * @property int $pass
  37. * @property int $retest
  38. * @property int $void
  39. * @property int $void_primary
  40. * @property int $void_secondary
  41. * @property string $void_notes
  42. * @property string $dts_created
  43. * @property string $dts_updated
  44. * @property string $dts_assigned
  45. * @property string $dts_rejected
  46. * @property string $dts_verified
  47. * @property string $dts_paid
  48. */
  49. class Test extends Model {
  50. protected static $table = "test";
  51. protected static $id_field = "id";
  52. protected static $fillable_fields = [
  53. "project_id", "location_id", "tester_id", "stripe_invoice_id",
  54. "status", "status_tester_invoice",
  55. "payment_required",
  56. "customer_fee", "test_fee", "test_time", "test_travel_cost", "test_travel_cost_description",
  57. "shop_left", "shop_right", "attire",
  58. "challenge_stage",
  59. "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact",
  60. "additional_notes",
  61. "dts_due", "dts_start", "dts_end", "dts_tester_invoice_generated", "dts_tester_invoice_paid",
  62. "verifier_id", "verifier_notes",
  63. "pass", "retest",
  64. "void", "void_notes",
  65. "void_primary",
  66. "void_secondary",
  67. "dts_created", "dts_updated",
  68. "dts_assigned", "dts_rejected", "dts_verified", "dts_paid",
  69. ];
  70. public const TEST_FIELDS = ["shop_left", "shop_right", "attire", "challenge_stage", "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact", "additional_notes"];
  71. // Constants
  72. public const STATUS_CANCELLED = 0;
  73. public const STATUS_UNASSIGNED = 1; // Location needs a tester
  74. public const STATUS_ASSIGNED = 2; // Tester assigned
  75. public const STATUS_CONFIRMED = 3; // Tester has said they can do it
  76. public const STATUS_STARTED = 4;
  77. public const STATUS_PENDING_VERIFICATION = 8;
  78. public const STATUS_COMPLETED = 16;
  79. public const STATUS_TESTER_INVOICE_NONE = 0;
  80. public const STATUS_TESTER_INVOICE_PENDING = 100;
  81. public const STATUS_TESTER_INVOICE_PAID = 200;
  82. public const STATUSES = [
  83. 0 => "Cancelled",
  84. 1 => "Unassigned",
  85. 2 => "Assigned",
  86. 3 => "Confirmed",
  87. 4 => "Started",
  88. 8 => "Pending Verification",
  89. 16 => "Completed",
  90. ];
  91. public const STATUSES_TESTER_INVOICE = [
  92. Test::STATUS_TESTER_INVOICE_NONE,
  93. Test::STATUS_TESTER_INVOICE_PENDING,
  94. Test::STATUS_TESTER_INVOICE_PAID
  95. ];
  96. public const STAGES = [
  97. 0 => "",
  98. 1 => "Challenge on entry",
  99. 2 => "Challenge while browsing",
  100. 3 => "Challenge during play",
  101. 4 => "Challenge on exit",
  102. 5 => "No challenge",
  103. ];
  104. // Static methods
  105. /**
  106. * @param int $status
  107. * @return string
  108. */
  109. public static function getStatusTesterInvoiceText(int $status): string {
  110. switch($status) {
  111. case self::STATUS_TESTER_INVOICE_NONE:
  112. return __("No Invoice Generated");
  113. case self::STATUS_TESTER_INVOICE_PENDING:
  114. return __("Pending Payment");
  115. case self::STATUS_TESTER_INVOICE_PAID:
  116. return __("Paid");
  117. }
  118. return __("Unknown");
  119. }
  120. /**
  121. * @param int $status
  122. * @return string
  123. */
  124. public static function getStatusTesterInvoiceClass(int $status): string {
  125. switch($status) {
  126. case self::STATUS_TESTER_INVOICE_NONE:
  127. return "secondary";
  128. case self::STATUS_TESTER_INVOICE_PENDING:
  129. return "warning";
  130. case self::STATUS_TESTER_INVOICE_PAID:
  131. return "success";
  132. }
  133. return "";
  134. }
  135. /**
  136. * @param string $processing_file
  137. * @param array $tests
  138. * @throws Exception
  139. */
  140. public static function exportCSV(string $processing_file, array $tests = []): void {
  141. // Preload
  142. Project::load(array_column($tests, "project_id"));
  143. Location::load(array_column($tests, "location_id"));
  144. User::load(array_column($tests, "tester_id"));
  145. // Get processing file
  146. $processing_file_path = ROOT_DIR . "/export_processing/{$processing_file}";
  147. if(!file_exists($processing_file_path)) throw new Exception("Processing file not found.");
  148. // Output
  149. HTTP::setDownloadHeaders("export.csv");
  150. $handle = fopen("php://output", "wb");
  151. /** @noinspection PhpIncludeInspection */
  152. require $processing_file_path;
  153. fclose($handle);
  154. }
  155. // Object methods
  156. /**
  157. * @param string $message
  158. * @return bool
  159. * @throws Exception
  160. */
  161. public function sendAssignEmail(string $message): bool {
  162. $tester = $this->getTester();
  163. $location = $this->getLocation();
  164. $project = $this->getProject();
  165. // Content
  166. $name = htmlspecialchars($tester->first_name);
  167. $message = htmlspecialchars($message);
  168. $base_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/tests/" . $this->id);
  169. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  170. $date_due = date("j M Y", strtotime($this->dts_due));
  171. $admin_first_name = htmlspecialchars(Core::$user->first_name);
  172. $admin_phone = htmlspecialchars(Core::$user->phone);
  173. // Build
  174. $builder = new EmailBuilder;
  175. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  176. $builder->addLeadParagraph(__("Congrats, you have been allocated a test!"));
  177. if($project->isNighttime()) {
  178. $builder->addParagraph("<b><font color=\"red\">" . __("Evening Test to be done between 7PM and midnight.") . "</font></b>");
  179. }
  180. if($message) {
  181. $builder->addParagraph($message);
  182. }
  183. $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."),
  184. '<b>', '</b>',
  185. '<b>', '</b>',
  186. '<b>', '</b>'
  187. ));
  188. $builder->addAcceptDeclineButtons($base_url);
  189. $builder->addParagraph(sprintf(__("The site address is: %s"), "<b>{$address}</b>"));
  190. $builder->addParagraph(sprintf(__("The date that need to complete and submit the results by is: %s"), "<font color=\"red\"><b>$date_due</b></font>"));
  191. $builder->addParagraph(sprintf(__("For methodology and further notes please check the %sUseful Documents%s section on the %sAccount%s page."),
  192. '<b>', '</b>',
  193. '<b>', '</b>'
  194. ));
  195. $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."),
  196. '<b>', '</b>',
  197. '<b>', '</b>'
  198. ));
  199. $builder->addParagraph(sprintf(__("If you have any questions please send them to %s or call %s on %s."),
  200. "<a href=\"mailto:tests@check-policy.com\">tests@check-policy.com</a>",
  201. "<b>$admin_first_name</b>",
  202. "<b>$admin_phone</b>"
  203. ));
  204. $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."),
  205. '<b>', '</b>'
  206. ));
  207. $content = $builder->build();
  208. // Send
  209. $mail = new Email;
  210. $mail->setHtmlContent($content);
  211. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  212. $mail->addAddress($tester->email, $tester->getDisplayName());
  213. $mail->setSubject(__("You have been allocated a test"));
  214. $mail->send();
  215. return true;
  216. }
  217. /**
  218. * @return bool
  219. * @throws Exception
  220. */
  221. public function sendClaimedEmail(): bool {
  222. $tester = $this->getTester();
  223. $location = $this->getLocation();
  224. $project = $this->getProject();
  225. // Content
  226. $name = htmlspecialchars($tester->first_name);
  227. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  228. $date_due = date("j M Y", strtotime($this->dts_due));
  229. // Build
  230. $builder = new EmailBuilder;
  231. $builder->addParagraph("<b>" . sprintf(__("Hi %s"), $name) . "</b>");
  232. $builder->addLeadParagraph(__("Congrats, you have claimed a test!"));
  233. if($project->isNighttime()) {
  234. $builder->addParagraph("<b><font color=\"red\">" . __("Evening Test to be done between 7PM and midnight.") . "</font></b>");
  235. }
  236. $builder->addParagraph(sprintf(__("The site address is: %s"), "<b>$address</b>"));
  237. $builder->addParagraph(sprintf(__("The date that need to complete and submit the results by is: %s"), "<font color=\"red\"><b>$date_due</b></font>"));
  238. $builder->addParagraph(sprintf(__("For methodology and further notes please check the %sUseful Documents%s section on the %sAccount%s page."),
  239. '<b>', '</b>',
  240. '<b>', '</b>'
  241. ));
  242. $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."),
  243. '<b>', '</b>',
  244. '<b>', '</b>'
  245. ));
  246. $builder->addParagraph(sprintf(__("If you have any questions please send them to %s."),
  247. "<a href=\"mailto:tests@check-policy.com\">tests@check-policy.com</a>"
  248. ));
  249. $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."));
  250. $content = $builder->build();
  251. // Send
  252. $mail = new Email;
  253. $mail->setHtmlContent($content);
  254. $mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
  255. $mail->addAddress($tester->email, $tester->getDisplayName());
  256. $mail->setSubject(__("You have claimed a test"));
  257. $mail->send();
  258. return true;
  259. }
  260. /**
  261. * @return bool
  262. * @throws Exception
  263. */
  264. public function sendDeclineEmail(): bool {
  265. $tester = $this->getTester();
  266. $location = $this->getLocation();
  267. $project = $this->getProject();
  268. // Content
  269. $name = htmlspecialchars($tester->getDisplayName());
  270. $address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
  271. $project_name = htmlspecialchars($project->name);
  272. $project_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/projects/" . $project->id);
  273. $internal_id = "N/A";
  274. if($location->internal_id) $internal_id = htmlspecialchars($location->internal_id);
  275. // Build
  276. $builder = new EmailBuilder;
  277. $builder->addParagraph("<b>Hi,</b>");
  278. $builder->addLeadParagraph("A tester has declined a test.");
  279. $builder->addParagraph("<b>Project:</b><br />$project_name");
  280. $builder->addParagraph("<b>Site:</b><br />$address<br />ID: $location->id | Internal ID: $internal_id");
  281. $builder->addParagraph("<b>Who:</b><br />$name<br />ID: $tester->id");
  282. $builder->addParagraph("<a href=\"$project_url\">Click here</a> to manage the project and reassign the test.");
  283. $content = $builder->build();
  284. // Send
  285. $mail = new Email;
  286. $mail->setHtmlContent($content);
  287. $mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
  288. $mail->addAddress(EMAIL_CONTACT, APP_NAME . " Contact");
  289. $mail->setSubject("Test declined: $project->name / $location->name");
  290. $mail->send();
  291. return true;
  292. }
  293. /**
  294. * @param string $to_address
  295. * @param bool $include_previous_test
  296. * @return bool
  297. * @throws Exception
  298. */
  299. public function sendResultEmail(string $to_address, bool $include_previous_test): bool {
  300. $location = $this->getLocation();
  301. $project = $this->getProject();
  302. $template = $project->getTemplate();
  303. $subject_format_file_path = $template->getProcessingFilePath("result_email_subject_format.txt");
  304. $subject_format = file_get_contents($subject_format_file_path);
  305. // Content
  306. $pass_text = strtoupper($this->getPassText());
  307. $stage = $this->challenge_stage;
  308. $location_parts = [$location->internal_id, $location->name];
  309. $location_string = implode(" : ", array_filter($location_parts));
  310. $unix_start = strtotime($this->dts_start . " UTC");
  311. $date_time = date("d/m/Y H:i", $unix_start);
  312. // Subject
  313. $search_replace = [
  314. "{pass}" => $pass_text,
  315. "{stage}" => $stage,
  316. "{location}" => $location_string,
  317. "{date-time}" => $date_time,
  318. ];
  319. $subject = str_replace(
  320. array_keys($search_replace),
  321. array_values($search_replace),
  322. $subject_format
  323. );
  324. // Attachments
  325. $pdf_path = $this->getDirectory() . "/pdf.pdf";
  326. $pdf_name = implode(" - ", array_filter([$location->internal_id, date("jS M Y")])) . ".pdf";
  327. // Lead paragraph
  328. $lead_paragraph = "";
  329. if($this->pass) {
  330. $lead_paragraph .= "Congratulations!<br />";
  331. }
  332. $lead_paragraph .= "A test has been completed at your location.";
  333. // Build
  334. $builder = new EmailBuilder;
  335. $builder->addLeadParagraph($lead_paragraph);
  336. $builder->addParagraph("Please see attached PDF and do not hesitate to contact us if you have any questions.");
  337. // Previous test
  338. $previous_pdf_path = $previous_pdf_name = null;
  339. if($include_previous_test) {
  340. $previous_test = $this->getPreviousTest();
  341. if($previous_test && $previous_test->hasPDF()) {
  342. $builder->addParagraph("The previous test result has also been attached for your reference.");
  343. $previous_dt = new DateTime($previous_test->dts_start, new DateTimeZone("UTC"));
  344. $previous_pdf_path = $previous_test->getPDFPath();
  345. $previous_pdf_name = implode(" - ", array_filter([$location->internal_id, $previous_dt->format("jS M Y")])) . ".pdf";
  346. }
  347. }
  348. $content = $builder->build();
  349. // Send
  350. $mail = new Email();
  351. $mail->setHtmlContent($content);
  352. $mail->setFrom(EMAIL_PDF_FROM_ADDRESS, EMAIL_FROM_NAME);
  353. $mail->addAddress($to_address);
  354. $mail->addAttachment($pdf_path, $pdf_name);
  355. if($previous_pdf_name) {
  356. $mail->addAttachment($previous_pdf_path, $previous_pdf_name);
  357. }
  358. $mail->setSubject($subject);
  359. $mail->send();
  360. return true;
  361. }
  362. /**
  363. * @return bool
  364. * @throws Exception
  365. */
  366. public function decline(): bool {
  367. $this->sendDeclineEmail();
  368. $user_id = $this->tester_id;
  369. $location_id = $this->location_id;
  370. $this->status = self::STATUS_UNASSIGNED;
  371. $this->tester_id = null;
  372. $this->dts_due = null;
  373. $this->dts_assigned = null;
  374. $this->dts_rejected = Util::getDateTime();
  375. $this->dts_updated = Util::getDateTime();
  376. $this->commit();
  377. $reject = UserLocationReject::get($user_id, $location_id);
  378. if(!$reject) {
  379. $reject = new UserLocationReject();
  380. $reject->user_id = $user_id;
  381. $reject->location_id = $location_id;
  382. $reject->reject_count = 0;
  383. }
  384. $reject->reject_count++;
  385. $reject->dts_last_rejected = DB::dts();
  386. $reject->commit();
  387. return true;
  388. }
  389. /**
  390. * @return string
  391. */
  392. public function getChallengeStageText(): string {
  393. if(is_null($this->challenge_stage)) return "";
  394. return self::STAGES[$this->challenge_stage];
  395. }
  396. /**
  397. * @return string
  398. */
  399. public function getStatusText(): string {
  400. return self::STATUSES[$this->status];
  401. }
  402. /**
  403. * @return string
  404. */
  405. public function getStatusClass(): string {
  406. switch($this->status) {
  407. case self::STATUS_CANCELLED:
  408. return "secondary";
  409. case self::STATUS_UNASSIGNED:
  410. return "danger";
  411. case self::STATUS_ASSIGNED:
  412. case self::STATUS_CONFIRMED:
  413. case self::STATUS_STARTED:
  414. return "warning";
  415. case self::STATUS_PENDING_VERIFICATION:
  416. return "primary";
  417. case self::STATUS_COMPLETED:
  418. return "success";
  419. }
  420. return "";
  421. }
  422. /**
  423. * @return Project
  424. * @throws Exception
  425. */
  426. public function getProject(): Project {
  427. return Project::get($this->project_id);
  428. }
  429. /**
  430. * @return Location
  431. * @throws Exception
  432. */
  433. public function getLocation(): Location {
  434. return Location::get($this->location_id);
  435. }
  436. /**
  437. * @return User
  438. * @throws Exception
  439. */
  440. public function getTester(): ?User {
  441. return User::get($this->tester_id);
  442. }
  443. /**
  444. * @return User
  445. * @throws Exception
  446. */
  447. public function getVerifier(): ?User {
  448. return User::get($this->verifier_id);
  449. }
  450. /**
  451. * @param bool $as_assoc_array
  452. * @return TestValue[]
  453. * @throws Exception
  454. */
  455. public function getValues(bool $as_assoc_array = false): array {
  456. $rows = TestValue::query(["test_id" => $this->id]);
  457. if($as_assoc_array) {
  458. $values = [];
  459. foreach($rows as $row) {
  460. $values[$row->name] = $row->value;
  461. }
  462. }
  463. else {
  464. $values = $rows;
  465. }
  466. return $values;
  467. }
  468. /**
  469. * @return string
  470. */
  471. public function getDirectory(): string {
  472. $directory = ROOT_DIR . "/files/tests/" . $this->id;
  473. if(!file_exists($directory)) mkdir($directory, 0777, true);
  474. return $directory;
  475. }
  476. /**
  477. * @return bool|float|int
  478. */
  479. public function getDuration() {
  480. if($this->dts_end === null) return false;
  481. return Util::getDurationFromDTS($this->dts_start, $this->dts_end);
  482. }
  483. /**
  484. * @return string
  485. */
  486. public function getDisplayDuration(): string {
  487. $secs = $this->getDuration();
  488. $mins = floor($secs / 60);
  489. $secs %= 60;
  490. return "$mins min. $secs sec.";
  491. }
  492. /**
  493. * @return string Bootstrap class name
  494. */
  495. public function getPassClass(): string {
  496. if($this->isVoid()) return "muted";
  497. if($this->pass === null) return "secondary";
  498. return $this->pass ? "success" : "danger";
  499. }
  500. /**
  501. * @return string FA icon name without prefix
  502. */
  503. public function getPassIcon(): string {
  504. if($this->isVoid()) return "minus-circle";
  505. if($this->pass === null) return "clock";
  506. return $this->pass ? "check-circle" : "times-circle";
  507. }
  508. /**
  509. * @return string Pending, Pass or Fail
  510. */
  511. public function getPassText(): string {
  512. if($this->isVoid() || $this->isPrimaryVoid()) return __("Void");
  513. if($this->pass === null) return __("Pending");
  514. return $this->pass ? __("Pass") : __("Fail");
  515. }
  516. /** @noinspection PhpUnusedLocalVariableInspection */
  517. public function getMPDF(): Mpdf {
  518. $id = $this->id;
  519. $output = false;
  520. require ROOT_DIR . "/controllers/test_pdf.php";
  521. /** @var Mpdf $mpdf */
  522. return $mpdf;
  523. }
  524. /**
  525. * @param User $tester
  526. * @return bool
  527. * @throws Exception
  528. */
  529. public function assignTester(User $tester): bool {
  530. $this->tester_id = $tester->id;
  531. $this->dts_assigned = Util::getDateTime();
  532. $this->dts_rejected = null;
  533. // TODO: Notify tester
  534. return $this->commit();
  535. }
  536. /**
  537. * @return Test|null
  538. * @throws Exception
  539. */
  540. public function getPreviousTest(): ?Test {
  541. $nvp = [
  542. "dts_start" => (new Constraint("LESS_THAN"))->setValues($this->dts_start),
  543. "location_id" => $this->location_id,
  544. "status" => self::STATUS_COMPLETED,
  545. ];
  546. $tests = self::query($nvp, [
  547. "sort" => "dts_start",
  548. "order" => "desc",
  549. ]);
  550. return $tests ? $tests[0] : null;
  551. }
  552. /**
  553. * @return bool
  554. * @throws Exception
  555. */
  556. public function unassignTester(): bool {
  557. $this->status = self::STATUS_UNASSIGNED;
  558. $this->tester_id = null;
  559. $this->dts_due = null;
  560. $this->dts_assigned = null;
  561. $this->dts_rejected = null;
  562. return $this->commit();
  563. }
  564. /**
  565. * @return bool
  566. */
  567. public function isPass(): bool {
  568. return (int) $this->pass === 1;
  569. }
  570. /**
  571. * @return bool
  572. */
  573. public function isPending(): bool {
  574. return $this->pass === null;
  575. }
  576. /**
  577. * @return bool
  578. */
  579. public function isFail(): bool {
  580. return (int) $this->pass === 0;
  581. }
  582. /**
  583. * @return bool
  584. */
  585. public function isCompleted(): bool {
  586. return $this->getStatus() === self::STATUS_COMPLETED;
  587. }
  588. public function isDone(): bool {
  589. return in_array($this->getStatus(), [self::STATUS_CANCELLED, self::STATUS_COMPLETED]);
  590. }
  591. /**
  592. * @return bool
  593. */
  594. public function isPendingVerification(): bool {
  595. return $this->getStatus() === self::STATUS_PENDING_VERIFICATION;
  596. }
  597. public function getPDFPath(): string {
  598. $directory = $this->getDirectory();
  599. return sprintf("%s/pdf.pdf", $directory);
  600. }
  601. /**
  602. * @return bool
  603. */
  604. public function hasPDF(): bool {
  605. return file_exists($this->getPDFPath());
  606. }
  607. /**
  608. * @return string
  609. */
  610. public function getPDFUrl(): string {
  611. return "/account/tests/{$this->id}/files/pdf";
  612. }
  613. public function getTesterInvoicePDFPath(): string {
  614. return sprintf("%s/tester_invoice.pdf", $this->getDirectory());
  615. }
  616. /**
  617. * @return bool
  618. */
  619. public function hasTesterInvoicePDF(): bool {
  620. return file_exists($this->getTesterInvoicePDFPath());
  621. }
  622. public function getTravelCostsPhotoPath(): string {
  623. return sprintf("%s/travel_costs.jpg", $this->getDirectory());
  624. }
  625. public function hasTravelCostsPhoto(): bool {
  626. return file_exists($this->getTravelCostsPhotoPath());
  627. }
  628. /**
  629. * @return bool
  630. * @throws Exception
  631. */
  632. public function markTesterInvoicePaid(): bool {
  633. if($this->getTesterInvoiceStatus() === self::STATUS_TESTER_INVOICE_PENDING) {
  634. $this->status_tester_invoice = self::STATUS_TESTER_INVOICE_PAID;
  635. $this->dts_tester_invoice_paid = Util::getDateTime();
  636. return $this->commit();
  637. }
  638. return false;
  639. }
  640. /**
  641. * @return bool
  642. * @throws Exception
  643. */
  644. public function rejectTesterInvoice(): bool {
  645. if($this->getTesterInvoiceStatus() === self::STATUS_TESTER_INVOICE_PENDING) {
  646. $this->status_tester_invoice = self::STATUS_TESTER_INVOICE_NONE;
  647. $this->dts_tester_invoice_generated = null;
  648. if($this->hasTesterInvoicePDF()) {
  649. unlink($this->getTesterInvoicePDFPath());
  650. }
  651. if($this->hasTravelCostsPhoto()) {
  652. unlink($this->getTravelCostsPhotoPath());
  653. }
  654. return $this->commit();
  655. }
  656. return false;
  657. }
  658. /**
  659. * @return string
  660. * @throws Exception
  661. */
  662. public function getTesterInvoiceReference(): string {
  663. $location = $this->getLocation();
  664. $tester = $this->getTester();
  665. return
  666. "T" . str_pad($this->id, 3, "0", STR_PAD_LEFT) .
  667. "/L" . str_pad($location->id, 3, "0", STR_PAD_LEFT) .
  668. "/U" . str_pad($tester->id, 3, "0", STR_PAD_LEFT);
  669. }
  670. /**
  671. * @return float
  672. * @throws Exception
  673. */
  674. public function getTestFee(): float {
  675. return $this->test_fee !== null ? (float) $this->test_fee : (float) $this->getProject()->default_tester_fee;
  676. }
  677. public function getTravelCosts(): float {
  678. return (float) $this->test_travel_cost;
  679. }
  680. /**
  681. * @return float
  682. * @throws Exception
  683. */
  684. public function getInvoiceTotal(): float {
  685. $template = $this->getProject()->getTemplate();
  686. $values = $this->getValues(true);
  687. // Base cost
  688. $fee = $this->getTestFee();
  689. $travel_cost = $this->getTravelCosts();
  690. $invoice_total = $fee + $travel_cost;
  691. // Additional tester invoice fields
  692. $tester_invoice_fields = $template->getTesterInvoiceFields();
  693. foreach($tester_invoice_fields as $field) {
  694. $invoice_total += (float) ($values[$field->name] ?? 0);
  695. }
  696. return $invoice_total;
  697. }
  698. public function isVoid(): bool {
  699. return (bool) $this->void;
  700. }
  701. public function isPrimaryVoid(): bool {
  702. return (bool) $this->void_primary;
  703. }
  704. public function getStatus(): int {
  705. return $this->status;
  706. }
  707. public function getTesterId(): int {
  708. return $this->tester_id;
  709. }
  710. /**
  711. * @return bool Whether the tester has accepted the test
  712. */
  713. public function isConfirmed(): bool {
  714. return $this->getStatus() === self::STATUS_CONFIRMED;
  715. }
  716. public function hasStarted(): bool {
  717. return $this->getStatus() === self::STATUS_STARTED;
  718. }
  719. public function isAssigned(): bool {
  720. return $this->getStatus() === self::STATUS_ASSIGNED;
  721. }
  722. /**
  723. * @return array
  724. * @throws Exception
  725. */
  726. public function getFieldsForVerifyForm(): array {
  727. $json = json_decode($this->getProject()->getTemplate()->getJSON());
  728. return array_merge($json->beforeTest, [(object) ["type" => "hr"]], $json->duringTest);
  729. }
  730. public function getTesterInvoiceStatus(): int {
  731. return $this->status_tester_invoice;
  732. }
  733. public function isCancelled(): bool {
  734. return $this->getStatus() === self::STATUS_CANCELLED;
  735. }
  736. public function hasDueDateTime(): bool {
  737. return $this->dts_due !== null;
  738. }
  739. public function getTimeDue(): int {
  740. if(!$this->dts_due) {
  741. return 0;
  742. }
  743. return $this->getDateTimeDue()->getTimestamp();
  744. }
  745. public function hasNotStarted(): bool {
  746. return $this->status < self::STATUS_STARTED;
  747. }
  748. public function isOverdue(): bool {
  749. return time() > $this->getTimeDue();
  750. }
  751. public function getDateTimeDue(): ?CarbonImmutable {
  752. return Util::getDateTimeFromUtcString($this->dts_due);
  753. }
  754. public function getDateTimeStart(): ?CarbonImmutable {
  755. return Util::getDateTimeFromUtcString($this->dts_start);
  756. }
  757. public function getDateTimeEnd(): ?CarbonImmutable {
  758. return Util::getDateTimeFromUtcString($this->dts_end);
  759. }
  760. public function getVerifierNotesHtml(): string {
  761. return nl2br(htmlspecialchars($this->verifier_notes));
  762. }
  763. /**
  764. * @return float
  765. * @throws Exception
  766. */
  767. public function getTesterInvoiceCosts(): float {
  768. return $this->getProject()->getTemplate()->getTesterInvoiceCosts($this->getValues(true));
  769. }
  770. public function getDateTimeRejected(): ?CarbonImmutable {
  771. if(!$this->dts_rejected) {
  772. return null;
  773. }
  774. return CarbonImmutable::parse($this->dts_rejected, "UTC");
  775. }
  776. public function renderTestStatus(User $user): void {
  777. ?>
  778. <a href="/account/tests/<?=$this->id?>">
  779. <?php
  780. if($this->isPass()) {
  781. ?>
  782. <i class="fa fa-fw fa-check-circle text-success"></i> Passed on <?=$user->formatDate($this->getDateTimeStart())?>
  783. <?php
  784. }
  785. elseif($this->isVoid()) {
  786. ?>
  787. <i class="fa fa-fw fa-minus-circle text-secondary"></i> Void on <?=$user->formatDate($this->getDateTimeStart())?>
  788. <?php
  789. }
  790. elseif($this->isFail()) {
  791. ?>
  792. <i class="fa fa-fw fa-times-circle text-danger"></i> Failed on <?=$user->formatDate($this->getDateTimeStart())?>
  793. <?php
  794. }
  795. else {
  796. ?>
  797. <i class="fa fa-fw fa-question-circle text-warning"></i> Result Pending (Test <?=htmlspecialchars($this->getStatusText())?>)
  798. <?php
  799. }
  800. ?>
  801. </a>
  802. <?php
  803. }
  804. }