project.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. /**
  30. * @var Test[] $tests
  31. */
  32. protected $tests = null;
  33. // Object methods
  34. /**
  35. * @return Customer
  36. */
  37. public function getCustomer() {
  38. return Customer::get($this->customer_id);
  39. }
  40. /**
  41. * @return Test[]
  42. */
  43. public function getTests() {
  44. if(!is_null($this->tests)) return $this->tests;
  45. $nvp = [];
  46. $nvp["project_id"] = $this->id;
  47. $nvp["status"] = (new Constraint("MORE_THAN"))->setValues(Test::STATUS_CANCELLED);
  48. $this->tests = Test::query($nvp);
  49. return $this->tests;
  50. }
  51. public function getTestCount() {
  52. if(is_null($this->tests)) $this->getTests();
  53. return count($this->tests);
  54. }
  55. public function getPassCount() {
  56. $count = 0;
  57. $tests = $this->getTests();
  58. foreach($tests as $test) {
  59. if(!$test->isPass()) continue;
  60. ++$count;
  61. }
  62. return $count;
  63. }
  64. public function getFailCount() {
  65. $count = 0;
  66. $tests = $this->getTests();
  67. foreach($tests as $test) {
  68. if(!$test->isFail()) continue;
  69. ++$count;
  70. }
  71. return $count;
  72. }
  73. public function getPendingCount() {
  74. $count = 0;
  75. $tests = $this->getTests();
  76. foreach($tests as $test) {
  77. if(!$test->isPending()) continue;
  78. ++$count;
  79. }
  80. return $count;
  81. }
  82. public function getLocations() {
  83. $tests = $this->getTests();
  84. return Location::query(["id" => array_column($tests, "location_id")]);
  85. }
  86. public function getDisplayAgeRange() {
  87. if($this->min_age && $this->max_age) {
  88. return $this->min_age . " to " . $this->max_age;
  89. }
  90. else if($this->min_age) {
  91. return "At least " . $this->min_age;
  92. }
  93. else if($this->max_age) {
  94. return "Up to " . $this->max_age;
  95. }
  96. return "No age restriction";
  97. }
  98. public function getCompletedPercentage() {
  99. $test_count = $this->test_count;
  100. $tests = $this->getTests();
  101. $completed_tests = array_filter($tests, function($test) {
  102. return $test->status == Test::STATUS_COMPLETED;
  103. });
  104. $completed_test_count = count($completed_tests);
  105. return floor(($completed_test_count / $test_count) * 100);
  106. }
  107. /**
  108. * @return Template
  109. */
  110. public function getTemplate() {
  111. return Template::get($this->template_id);
  112. }
  113. public function getTemplateOverrides() {
  114. $overrides = json_decode($this->template_overrides_json) ?: [];
  115. return $overrides;
  116. }
  117. public function getTimeOfDayText() {
  118. switch($this->time_of_day) {
  119. case "A": return "Any";
  120. case "D": return "Day";
  121. case "N": return "Night";
  122. }
  123. }
  124. public function getTimeOfDayIcon() {
  125. switch($this->time_of_day) {
  126. case "A": return "adjust";
  127. case "D": return "sun";
  128. case "N": return "moon";
  129. }
  130. }
  131. public function getURL() {
  132. return "/account/projects/{$this->id}";
  133. }
  134. public function getDirectory() {
  135. $directory = ROOT_DIR . "/files/projects/" . $this->id;
  136. if(!file_exists($directory)) mkdir($directory, 0777, true);
  137. return $directory;
  138. }
  139. public function hasInvoicePDF() {
  140. $directory = $this->getDirectory();
  141. return file_exists($directory . "/invoice.pdf");
  142. }
  143. public function isAnytime() {
  144. return $this->time_of_day == "A";
  145. }
  146. public function isDaytime() {
  147. return $this->time_of_day == "D";
  148. }
  149. public function isNighttime() {
  150. return $this->time_of_day == "N";
  151. }
  152. public function isPastDeadline() {
  153. $deadline_unix = Util::getUnixTimeFromUTC($this->dts_deadline);
  154. return time() > $deadline_unix;
  155. }
  156. public function isFinished() {
  157. return $this->dts_finished === null;
  158. }
  159. public function markFinished() {
  160. if(!Core::$user) throw new Exception("Trying to mark project as finished while not logged in.");
  161. if(!Core::$user->isAdmin()) throw new Exception("Trying to mark project as finished with non-admin account.");
  162. $this->dts_finished = Util::getUTCDateTime();
  163. $this->user_id_finished = Core::$user->id;
  164. return $this->commit();
  165. }
  166. public function markUnfinished() {
  167. $this->dts_finished = null;
  168. $this->user_id_finished = null;
  169. return $this->commit();
  170. }
  171. public function getDisplayReportMonth() {
  172. $report_month = "-";
  173. if($this->dts_report) {
  174. $dt_report = new DateTime($this->dts_report . " UTC");
  175. $report_month = $dt_report->format("M Y");
  176. }
  177. return $report_month;
  178. }
  179. public function getDataTableRow() {
  180. $tests = $this->getTests();
  181. return [
  182. "id" => (int) $this->id,
  183. "rowId" => "project-row-{$this->id}",
  184. "name" => $this->name,
  185. "testCount" => count($tests),
  186. "timeOfDay" => $this->getTimeOfDayText(),
  187. "unixStart" => Util::getUnixTimeFromUTC($this->dts_start),
  188. "unixEnd" => Util::getUnixTimeFromUTC($this->dts_end),
  189. "reportMonth" => $this->getDisplayReportMonth(),
  190. "unixReport" => $this->dts_report ? Util::getUnixTimeFromUTC($this->dts_report) : 0,
  191. ];
  192. }
  193. public function getTestResultsUrl(): string {
  194. $customer = $this->getCustomer();
  195. $customer_slug = Util::slugWithId($customer->name, $customer->id);
  196. $project_slug = Util::slugWithId($this->name, $this->id);
  197. return "/account/test-results/{$customer_slug}/{$project_slug}";
  198. }
  199. }