| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- class Project extends Model {
- protected static $table = "project";
- protected static $id_field = "id";
- 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"];
- protected static $field_map = [
- ];
- // Object methods
- public function getCustomer() {
- return Customer::get($this->customer_id);
- }
- public function getTests() {
- $nvp = [];
- $nvp["project_id"] = $this->id;
- $nvp["status"] = (new Constraint("MORE_THAN"))->setValues(Test::STATUS_CANCELLED);
- return Test::query($nvp);
- }
- public function getLocations() {
- $tests = $this->getTests();
- return Location::query(["id" => array_column($tests, "location_id")]);
- }
- public function getDisplayAgeRange() {
- if($this->min_age && $this->max_age) {
- return $this->min_age . " to " . $this->max_age;
- }
- else if($this->min_age) {
- return "At least " . $this->min_age;
- }
- else if($this->max_age) {
- return "Up to " . $this->max_age;
- }
- return "No age restriction";
- }
- public function getCompletedPercentage() {
- $test_count = $this->test_count;
- $tests = $this->getTests();
- $completed_tests = array_filter($tests, function($test) {
- return $test->status == Test::STATUS_COMPLETED;
- });
- $completed_test_count = count($completed_tests);
- return floor(($completed_test_count / $test_count) * 100);
- }
- public function getTemplate() {
- return Template::get($this->template_id);
- }
- public function getTemplateOverrides() {
- $overrides = json_decode($this->template_overrides_json) ?: [];
- return $overrides;
- }
- public function getTimeOfDayText() {
- switch($this->time_of_day) {
- case "A": return "Any";
- case "D": return "Day";
- case "N": return "Night";
- }
- }
- public function getTimeOfDayIcon() {
- switch($this->time_of_day) {
- case "A": return "adjust";
- case "D": return "sun";
- case "N": return "moon";
- }
- }
- public function getURL() {
- return "/account/projects/{$this->id}";
- }
- public function getDirectory() {
- $directory = ROOT_DIR . "/files/projects/" . $this->id;
- if(!file_exists($directory)) mkdir($directory, 0777, true);
- return $directory;
- }
- public function hasInvoicePDF() {
- $directory = $this->getDirectory();
- return file_exists($directory . "/invoice.pdf");
- }
- public function isAnytime() {
- return $this->time_of_day == "A";
- }
- public function isDaytime() {
- return $this->time_of_day == "D";
- }
- public function isNighttime() {
- return $this->time_of_day == "N";
- }
- public function isPastDeadline() {
- $deadline_unix = Util::getUnixTimeFromUTC($this->dts_deadline);
- return time() > $deadline_unix;
- }
- public function markFinished() {
- if(!Core::$user) throw new Exception("Trying to mark project as finished while not logged in.");
- if(!Core::$user->isAdmin()) throw new Exception("Trying to mark project as finished with non-admin account.");
- $this->dts_finished = Util::getUTCDateTime();
- $this->user_id_finished = Core::$user->id;
- return $this->commit();
- }
- public function markUnfinished() {
- $this->dts_finished = null;
- $this->user_id_finished = null;
- return $this->commit();
- }
- public function getDisplayReportMonth() {
- $report_month = "-";
- if($this->dts_report) {
- $dt_report = new DateTime($this->dts_report . " UTC");
- $report_month = $dt_report->format("M Y");
- }
- return $report_month;
- }
- public function getDataTableRow() {
- $tests = $this->getTests();
- return [
- "id" => (int) $this->id,
- "rowId" => "project-row-{$this->id}",
- "name" => $this->name,
- "testCount" => count($tests),
- "timeOfDay" => $this->getTimeOfDayText(),
- "unixStart" => Util::getUnixTimeFromUTC($this->dts_start),
- "unixEnd" => Util::getUnixTimeFromUTC($this->dts_end),
- "reportMonth" => $this->getDisplayReportMonth(),
- "unixReport" => $this->dts_report ? Util::getUnixTimeFromUTC($this->dts_report) : 0,
- ];
- }
- }
|