test.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 TEST_TIME_ANYTIME = 1;
  26. const TEST_TIME_DAY = 2;
  27. const TEST_TIME_NIGHT = 3;
  28. // Object methods
  29. public function sendAssignEmail($message) {
  30. $tester = $this->getTester();
  31. $location = $this->getLocation();
  32. $tester_message = "";
  33. if($message) {
  34. $tester_message = Email::getTextCellHtml($message);
  35. }
  36. $date_due = date("j M Y", strtotime($this->dts_due));
  37. $mail = new Email();
  38. $mail->loadHtmlTemplate("test_assign");
  39. $mail->setValues([
  40. "name" => $tester->first_name,
  41. "tester-message" => $tester_message,
  42. "test-url" => PROTOCOL . "://" . DOMAIN . "/account/tests/" . $this->id,
  43. "address" => $location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]),
  44. "date-due" => $date_due,
  45. "admin-first-name" => Core::$user->first_name,
  46. "admin-phone" => Core::$user->phone
  47. ]);
  48. $mail->setFrom(EMAIL_FROM, EMAIL_FROM_NAME);
  49. $mail->addAddress($tester->email, $tester->getDisplayName());
  50. $mail->setSubject("You have been allocated a test");
  51. $mail->send();
  52. }
  53. public function getStatusText() {
  54. return Test::STATUSES[$this->status];
  55. }
  56. public function getStatusClass() {
  57. switch($this->status) {
  58. case Test::STATUS_CANCELLED:
  59. return "secondary";
  60. case Test::STATUS_UNASSIGNED:
  61. return "danger";
  62. case Test::STATUS_ASSIGNED:
  63. case Test::STATUS_CONFIRMED:
  64. case Test::STATUS_STARTED:
  65. return "warning";
  66. case Test::STATUS_PENDING_VERIFICATION:
  67. return "primary";
  68. case Test::STATUS_COMPLETED:
  69. return "success";
  70. }
  71. }
  72. public function getProject() {
  73. return Project::get($this->project_id);
  74. }
  75. public function getLocation() {
  76. return Location::get($this->location_id);
  77. }
  78. public function getTester() {
  79. return User::get($this->tester_id);
  80. }
  81. public function getVerifier() {
  82. return User::get($this->verifier_id);
  83. }
  84. public function getValues() {
  85. return TestValue::query(["test_id" => $this->id]);
  86. }
  87. public function getDirectory() {
  88. $directory = ROOT_DIR . "/files/tests/" . $this->id;
  89. if(!file_exists($directory)) mkdir($directory, 0777, true);
  90. return $directory;
  91. }
  92. public function getDuration() {
  93. if(is_null($this->dts_end)) return false;
  94. $seconds = Util::getDurationFromDTS($this->dts_start, $this->dts_end);
  95. return $seconds;
  96. }
  97. public function getDisplayDuration() {
  98. $secs = $this->getDuration();
  99. $mins = floor($secs / 60);
  100. $secs = $secs % 60;
  101. return "$mins min. $secs sec.";
  102. }
  103. public function getPassClass() {
  104. if(is_null($this->pass)) return "secondary";
  105. return $this->pass ? "success" : "danger";
  106. }
  107. public function getPassIcon() {
  108. if(is_null($this->pass)) return "clock";
  109. return $this->pass ? "check-circle" : "times-circle";
  110. }
  111. public function getPassText() {
  112. if(is_null($this->pass)) return "Pending";
  113. return $this->pass ? "Pass" : "Fail";
  114. }
  115. public function assignTester($tester) {
  116. $this->tester_id = $tester->id;
  117. $this->dts_assigned = Util::getDateTime();
  118. $success = $this->commit();
  119. // TODO: Notify tester
  120. return $success;
  121. }
  122. }
  123. ?>