project.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * @property int $id
  4. * @property int $customer_id
  5. * @property int $template_id
  6. * @property string $name
  7. * @property int $test_count
  8. * @property int $min_age
  9. * @property int $max_age
  10. * @property float $default_customer_fee
  11. * @property float $default_tester_fee
  12. * @property int $time_of_day
  13. * @property bool $tester_can_claim
  14. * @property string $template_overrides_json
  15. * @property string $dts_start
  16. * @property string $dts_deadline
  17. * @property string $dts_created
  18. * @property string $dts_updated
  19. * @property string $dts_report
  20. * @property string $dts_finished
  21. * @property int $user_id_finished
  22. *
  23. * @method Project get(int $id)
  24. */
  25. class Project extends Model {
  26. protected static $table = "project";
  27. protected static $id_field = "id";
  28. protected static $fillable_fields = ["customer_id", "template_id", "name", "test_count", "min_age", "max_age", "default_customer_fee", "default_tester_fee", "time_of_day", "tester_can_claim", "template_overrides_json", "dts_start", "dts_deadline", "dts_created", "dts_updated", "dts_report", "dts_finished", "user_id_finished"];
  29. protected static $field_map = [
  30. ];
  31. // Object methods
  32. /**
  33. * @return Customer
  34. */
  35. public function getCustomer() {
  36. return Customer::get($this->customer_id);
  37. }
  38. /**
  39. * @return Test[]
  40. */
  41. public function getTests() {
  42. $nvp = [];
  43. $nvp["project_id"] = $this->id;
  44. $nvp["status"] = (new Constraint("MORE_THAN"))->setValues(Test::STATUS_CANCELLED);
  45. return Test::query($nvp);
  46. }
  47. public function getLocations() {
  48. $tests = $this->getTests();
  49. return Location::query(["id" => array_column($tests, "location_id")]);
  50. }
  51. public function getDisplayAgeRange() {
  52. if($this->min_age && $this->max_age) {
  53. return $this->min_age . " to " . $this->max_age;
  54. }
  55. else if($this->min_age) {
  56. return "At least " . $this->min_age;
  57. }
  58. else if($this->max_age) {
  59. return "Up to " . $this->max_age;
  60. }
  61. return "No age restriction";
  62. }
  63. public function getCompletedPercentage() {
  64. $test_count = $this->test_count;
  65. $tests = $this->getTests();
  66. $completed_tests = array_filter($tests, function($test) {
  67. return $test->status == Test::STATUS_COMPLETED;
  68. });
  69. $completed_test_count = count($completed_tests);
  70. return floor(($completed_test_count / $test_count) * 100);
  71. }
  72. /**
  73. * @return Template
  74. */
  75. public function getTemplate() {
  76. return Template::get($this->template_id);
  77. }
  78. public function getTemplateOverrides() {
  79. $overrides = json_decode($this->template_overrides_json) ?: [];
  80. return $overrides;
  81. }
  82. public function getTimeOfDayText() {
  83. switch($this->time_of_day) {
  84. case "A": return "Any";
  85. case "D": return "Day";
  86. case "N": return "Night";
  87. }
  88. }
  89. public function getTimeOfDayIcon() {
  90. switch($this->time_of_day) {
  91. case "A": return "adjust";
  92. case "D": return "sun";
  93. case "N": return "moon";
  94. }
  95. }
  96. public function getURL() {
  97. return "/account/projects/{$this->id}";
  98. }
  99. public function getDirectory() {
  100. $directory = ROOT_DIR . "/files/projects/" . $this->id;
  101. if(!file_exists($directory)) mkdir($directory, 0777, true);
  102. return $directory;
  103. }
  104. public function hasInvoicePDF() {
  105. $directory = $this->getDirectory();
  106. return file_exists($directory . "/invoice.pdf");
  107. }
  108. public function isAnytime() {
  109. return $this->time_of_day == "A";
  110. }
  111. public function isDaytime() {
  112. return $this->time_of_day == "D";
  113. }
  114. public function isNighttime() {
  115. return $this->time_of_day == "N";
  116. }
  117. public function isPastDeadline() {
  118. $deadline_unix = Util::getUnixTimeFromUTC($this->dts_deadline);
  119. return time() > $deadline_unix;
  120. }
  121. public function markFinished() {
  122. if(!Core::$user) throw new Exception("Trying to mark project as finished while not logged in.");
  123. if(!Core::$user->isAdmin()) throw new Exception("Trying to mark project as finished with non-admin account.");
  124. $this->dts_finished = Util::getUTCDateTime();
  125. $this->user_id_finished = Core::$user->id;
  126. return $this->commit();
  127. }
  128. public function markUnfinished() {
  129. $this->dts_finished = null;
  130. $this->user_id_finished = null;
  131. return $this->commit();
  132. }
  133. public function getDisplayReportMonth() {
  134. $report_month = "-";
  135. if($this->dts_report) {
  136. $dt_report = new DateTime($this->dts_report . " UTC");
  137. $report_month = $dt_report->format("M Y");
  138. }
  139. return $report_month;
  140. }
  141. public function getDataTableRow() {
  142. $tests = $this->getTests();
  143. return [
  144. "id" => (int) $this->id,
  145. "rowId" => "project-row-{$this->id}",
  146. "name" => $this->name,
  147. "testCount" => count($tests),
  148. "timeOfDay" => $this->getTimeOfDayText(),
  149. "unixStart" => Util::getUnixTimeFromUTC($this->dts_start),
  150. "unixEnd" => Util::getUnixTimeFromUTC($this->dts_end),
  151. "reportMonth" => $this->getDisplayReportMonth(),
  152. "unixReport" => $this->dts_report ? Util::getUnixTimeFromUTC($this->dts_report) : 0,
  153. ];
  154. }
  155. }