diff options
Diffstat (limited to 'app/Model/ProjectActivity.php')
-rw-r--r-- | app/Model/ProjectActivity.php | 162 |
1 files changed, 107 insertions, 55 deletions
diff --git a/app/Model/ProjectActivity.php b/app/Model/ProjectActivity.php index 6d6ef454..27f1cfcd 100644 --- a/app/Model/ProjectActivity.php +++ b/app/Model/ProjectActivity.php @@ -2,9 +2,6 @@ namespace Model; -use Core\Template; -use Event\ProjectActivityListener; - /** * Project activity model * @@ -25,7 +22,7 @@ class ProjectActivity extends Base * * @var integer */ - const MAX_EVENTS = 5000; + const MAX_EVENTS = 1000; /** * Add a new event for the project @@ -46,7 +43,7 @@ class ProjectActivity extends Base 'creator_id' => $creator_id, 'event_name' => $event_name, 'date_creation' => time(), - 'data' => serialize($data), + 'data' => json_encode($data), ); $this->cleanup(self::MAX_EVENTS - 1); @@ -59,42 +56,101 @@ class ProjectActivity extends Base * @access public * @param integer $project_id Project id * @param integer $limit Maximum events number + * @param integer $start Timestamp of earliest activity + * @param integer $end Timestamp of latest activity * @return array */ - public function getProject($project_id, $limit = 50) + public function getProject($project_id, $limit = 50, $start = null, $end = null) { - return $this->getProjects(array($project_id), $limit); + return $this->getProjects(array($project_id), $limit, $start, $end); } /** * Get all events for the given projects list * * @access public - * @param integer $project_id Project id + * @param integer[] $project_ids Projects id * @param integer $limit Maximum events number + * @param integer $start Timestamp of earliest activity + * @param integer $end Timestamp of latest activity * @return array */ - public function getProjects(array $projects, $limit = 50) + public function getProjects(array $project_ids, $limit = 50, $start = null, $end = null) { - if (empty($projects)) { + if (empty($project_ids)) { return array(); } - $events = $this->db->table(self::TABLE) - ->columns( - self::TABLE.'.*', - User::TABLE.'.username AS author_username', - User::TABLE.'.name AS author_name' - ) - ->in('project_id', $projects) - ->join(User::TABLE, 'id', 'creator_id') - ->desc('id') - ->limit($limit) - ->findAll(); + $query = $this + ->db + ->table(self::TABLE) + ->columns( + self::TABLE.'.*', + User::TABLE.'.username AS author_username', + User::TABLE.'.name AS author_name', + User::TABLE.'.email' + ) + ->in('project_id', $project_ids) + ->join(User::TABLE, 'id', 'creator_id') + ->desc(self::TABLE.'.id') + ->limit($limit); + + return $this->getEvents($query, $start, $end); + } + + /** + * Get all events for the given task + * + * @access public + * @param integer $task_id Task id + * @param integer $limit Maximum events number + * @param integer $start Timestamp of earliest activity + * @param integer $end Timestamp of latest activity + * @return array + */ + public function getTask($task_id, $limit = 50, $start = null, $end = null) + { + $query = $this + ->db + ->table(self::TABLE) + ->columns( + self::TABLE.'.*', + User::TABLE.'.username AS author_username', + User::TABLE.'.name AS author_name', + User::TABLE.'.email' + ) + ->eq('task_id', $task_id) + ->join(User::TABLE, 'id', 'creator_id') + ->desc(self::TABLE.'.id') + ->limit($limit); + + return $this->getEvents($query, $start, $end); + } + + /** + * Common function to return events + * + * @access public + * @param \PicoDb\Table $query PicoDb Query + * @param integer $start Timestamp of earliest activity + * @param integer $end Timestamp of latest activity + * @return array + */ + private function getEvents(\PicoDb\Table $query, $start, $end) + { + if (! is_null($start)){ + $query->gte('date_creation', $start); + } + + if (! is_null($end)){ + $query->lte('date_creation', $end); + } + + $events = $query->findAll(); foreach ($events as &$event) { - $event += unserialize($event['data']); + $event += $this->decode($event['data']); unset($event['data']); $event['author'] = $event['author_name'] ?: $event['author_username']; @@ -127,34 +183,6 @@ class ProjectActivity extends Base } /** - * Attach events to be able to record the history - * - * @access public - */ - public function attachEvents() - { - $events = array( - Task::EVENT_ASSIGNEE_CHANGE, - Task::EVENT_UPDATE, - Task::EVENT_CREATE, - Task::EVENT_CLOSE, - Task::EVENT_OPEN, - Task::EVENT_MOVE_COLUMN, - Task::EVENT_MOVE_POSITION, - Comment::EVENT_UPDATE, - Comment::EVENT_CREATE, - SubTask::EVENT_UPDATE, - SubTask::EVENT_CREATE, - ); - - $listener = new ProjectActivityListener($this->registry); - - foreach ($events as $event_name) { - $this->event->attach($event_name, $listener); - } - } - - /** * Get the event html content * * @access public @@ -163,8 +191,10 @@ class ProjectActivity extends Base */ public function getContent(array $params) { - $tpl = new Template; - return $tpl->load('event_'.str_replace('.', '_', $params['event_name']), $params); + return $this->template->render( + 'event/'.str_replace('.', '_', $params['event_name']), + $params + ); } /** @@ -178,7 +208,13 @@ class ProjectActivity extends Base { switch ($event['event_name']) { case Task::EVENT_ASSIGNEE_CHANGE: - return t('%s change the assignee of the task #%d to %s', $event['author'], $event['task']['id'], $event['task']['assignee_name'] ?: $event['task']['assignee_username']); + $assignee = $event['task']['assignee_name'] ?: $event['task']['assignee_username']; + + if (! empty($assignee)) { + return t('%s change the assignee of the task #%d to %s', $event['author'], $event['task']['id'], $assignee); + } + + return t('%s remove the assignee of the task %s', $event['author'], e('#%d', $event['task']['id'])); case Task::EVENT_UPDATE: return t('%s updated the task #%d', $event['author'], $event['task']['id']); case Task::EVENT_CREATE: @@ -191,9 +227,9 @@ class ProjectActivity extends Base return t('%s moved the task #%d to the column "%s"', $event['author'], $event['task']['id'], $event['task']['column_title']); case Task::EVENT_MOVE_POSITION: return t('%s moved the task #%d to the position %d in the column "%s"', $event['author'], $event['task']['id'], $event['task']['position'], $event['task']['column_title']); - case SubTask::EVENT_UPDATE: + case Subtask::EVENT_UPDATE: return t('%s updated a subtask for the task #%d', $event['author'], $event['task']['id']); - case SubTask::EVENT_CREATE: + case Subtask::EVENT_CREATE: return t('%s created a subtask for the task #%d', $event['author'], $event['task']['id']); case Comment::EVENT_UPDATE: return t('%s updated a comment on the task #%d', $event['author'], $event['task']['id']); @@ -203,4 +239,20 @@ class ProjectActivity extends Base return ''; } } + + /** + * Decode event data, supports unserialize() and json_decode() + * + * @access public + * @param string $data Serialized data + * @return array + */ + public function decode($data) + { + if ($data{0} === 'a') { + return unserialize($data); + } + + return json_decode($data, true) ?: array(); + } } |