1
0

test.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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", "test_fee", "test_time", "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", "verifier_id", "verifier_notes", "pass", "dts_created", "dts_updated", "dts_assigned", "dts_verified"];
  6. protected static $field_map = [
  7. ];
  8. // Constants
  9. const STATUS_CANCELLED = 0;
  10. const STATUS_UNASSIGNED = 1; // Location needs a tester
  11. const STATUS_ASSIGNED = 2; // Tester assigned
  12. const STATUS_CONFIRMED = 3; // Tester has said they can do it
  13. const STATUS_STARTED = 4;
  14. const STATUS_PENDING_VERIFICATION = 8;
  15. const STATUS_COMPLETED = 16;
  16. const STATUSES = [
  17. 0 => "Cancelled",
  18. 1 => "Unassigned",
  19. 2 => "Assigned",
  20. 3 => "Confirmed",
  21. 4 => "Started",
  22. 8 => "Pending Verification",
  23. 16 => "Completed",
  24. ];
  25. const STAGES = [
  26. 0 => "",
  27. 1 => "Challenge on entry",
  28. 2 => "Challenge while browsing",
  29. 3 => "Challenge during play",
  30. 4 => "Challenge on exit",
  31. 5 => "No challenge",
  32. ];
  33. const TEST_TIME_ANYTIME = 1;
  34. const TEST_TIME_DAY = 2;
  35. const TEST_TIME_NIGHT = 3;
  36. // Object methods
  37. public function sendAssignEmail($message) {
  38. $tester = $this->getTester();
  39. $location = $this->getLocation();
  40. $tester_message = "";
  41. if($message) {
  42. $tester_message = Email::getTextCellHtml($message);
  43. }
  44. $date_due = date("j M Y", strtotime($this->dts_due));
  45. $mail = new Email();
  46. $mail->loadHtmlTemplate("test_assign");
  47. $mail->setValues([
  48. "name" => $tester->first_name,
  49. "tester-message" => $tester_message,
  50. "test-url" => PROTOCOL . "://" . DOMAIN . "/account/tests/" . $this->id,
  51. "address" => $location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]),
  52. "date-due" => $date_due,
  53. "admin-first-name" => Core::$user->first_name,
  54. "admin-phone" => Core::$user->phone
  55. ]);
  56. $mail->setFrom(EMAIL_FROM, EMAIL_FROM_NAME);
  57. $mail->addAddress($tester->email, $tester->getDisplayName());
  58. $mail->setSubject("You have been allocated a test");
  59. $mail->send();
  60. }
  61. public function getChallengeStageText() {
  62. return Test::STAGES[$this->challenge_stage];
  63. }
  64. public function getStatusText() {
  65. return Test::STATUSES[$this->status];
  66. }
  67. public function getStatusClass() {
  68. switch($this->status) {
  69. case Test::STATUS_CANCELLED:
  70. return "secondary";
  71. case Test::STATUS_UNASSIGNED:
  72. return "danger";
  73. case Test::STATUS_ASSIGNED:
  74. case Test::STATUS_CONFIRMED:
  75. case Test::STATUS_STARTED:
  76. return "warning";
  77. case Test::STATUS_PENDING_VERIFICATION:
  78. return "primary";
  79. case Test::STATUS_COMPLETED:
  80. return "success";
  81. }
  82. }
  83. public function getProject() {
  84. return Project::get($this->project_id);
  85. }
  86. public function getLocation() {
  87. return Location::get($this->location_id);
  88. }
  89. public function getTester() {
  90. return User::get($this->tester_id);
  91. }
  92. public function getVerifier() {
  93. return User::get($this->verifier_id);
  94. }
  95. public function getValues($as_assoc_array = false) {
  96. $rows = TestValue::query(["test_id" => $this->id]);
  97. if($as_assoc_array) {
  98. $k = array_column($rows, "name");
  99. $v = array_column($rows, "value");
  100. $values = array_combine($k, $v);
  101. }
  102. else {
  103. $values = $rows;
  104. }
  105. return $values;
  106. }
  107. public function getDirectory() {
  108. $directory = ROOT_DIR . "/files/tests/" . $this->id;
  109. if(!file_exists($directory)) mkdir($directory, 0777, true);
  110. return $directory;
  111. }
  112. public function getDuration() {
  113. if(is_null($this->dts_end)) return false;
  114. $seconds = Util::getDurationFromDTS($this->dts_start, $this->dts_end);
  115. return $seconds;
  116. }
  117. public function getDisplayDuration() {
  118. $secs = $this->getDuration();
  119. $mins = floor($secs / 60);
  120. $secs = $secs % 60;
  121. return "$mins min. $secs sec.";
  122. }
  123. public function getPassClass() {
  124. if(is_null($this->pass)) return "secondary";
  125. return $this->pass ? "success" : "danger";
  126. }
  127. public function getPassIcon() {
  128. if(is_null($this->pass)) return "clock";
  129. return $this->pass ? "check-circle" : "times-circle";
  130. }
  131. public function getPassText() {
  132. if(is_null($this->pass)) return "Pending";
  133. return $this->pass ? "Pass" : "Fail";
  134. }
  135. public function assignTester($tester) {
  136. $this->tester_id = $tester->id;
  137. $this->dts_assigned = Util::getDateTime();
  138. $success = $this->commit();
  139. // TODO: Notify tester
  140. return $success;
  141. }
  142. }
  143. ?>