project.php 4.0 KB

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