setTable('goals'); } /** * Add a goal * @param users_o $created_by The user id who created the goal * @param departments_o[] $departments The departments for the goal * @param goals_criteria $criteria The criteria object * @return department_goals_o * @throws Exception If the object was not created successfully */ public function add(users_o $created_by, array $departments, goals_criteria $criteria): department_goals_o { // Sanitize the input $created_by->requireSelected(); foreach ($departments as $dept) { $dept->requireSelected(); } // Set the departments on the criteria object so validation and output inclusion works correctly $criteria->departments->set($departments); // Validate and sanitize criteria object $criteria->validateAndSanitize(); // Add the object $tmp_id = self::add_object([ 'created_by' => (int)$created_by->id, 'departments' => array_map(fn($dept) => (int)$dept->id, $departments), 'criteria' => (array)$criteria->toArray(), ]); $this->id = $tmp_id; self::getObjectProperties(); self::objectChanged(); return $this; } public function getObjectProperties(): void { $this->created_by = new object_property($this->table, $this->id, 'created_by', 'int', false); $this->departments = new object_property($this->table, $this->id, 'departments', 'json', false); $this->criteria = new object_property($this->table, $this->id, 'criteria', 'json', false); $this->created_at = new object_property($this->table, $this->id, 'created_at', 'timestamp', false); $this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'timestamp', false); $this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'timestamp', false); } public function objectChanged(): void { //TODO: Add cache invalidation } /** * @throws Exception */ public function asArray($include_progress = true): array { return [ 'id' => (int)$this->id, 'created_by' => (int)$this->created_by->value(), 'departments' => (array)$this->departments->value(), 'criteria' => (array)$this->criteria->value(), 'progress' => [ 'all' => $include_progress ? goals_criteria::calculateProgressDetailsFromArray((array)$this->criteria->value()) : null, 'departmental_distribution' => $include_progress ? goals_criteria::calculateDepartmentalProgressFromArray((array)$this->criteria->value()) : null, // array of department_id => [ 'all' => [ 'count' => int, 'target' => int ], 'today' => [ 'count' => int, 'target' => int ], 'week' => [ 'count' => int, 'target' => int ], 'month' => [ 'count' => int, 'target' => int ], 'year' => [ 'count' => int, 'target' => int ], 'to_date' => [ 'count' => int, 'target' => int ] ] // Showing progress for different time periods (Never starting before the goal start date, and never ending after the goal end date): 'today' => $include_progress ? goals_criteria::calculateProgressDetailsFromArray((array)$this->criteria->value(), 'today') : null, 'week' => $include_progress ? goals_criteria::calculateProgressDetailsFromArray((array)$this->criteria->value(), 'week') : null, 'month' => $include_progress ? goals_criteria::calculateProgressDetailsFromArray((array)$this->criteria->value(), 'month') : null, 'year' => $include_progress ? goals_criteria::calculateProgressDetailsFromArray((array)$this->criteria->value(), 'year') : null, 'to_date' => $include_progress ? goals_criteria::calculateProgressDetailsFromArray((array)$this->criteria->value(), 'to_date') : null, ], 'created_at' => (string)$this->created_at->value(), 'updated_at' => (string)$this->updated_at->value(), ]; } }