summaryrefslogtreecommitdiff
path: root/app/Model/ProjectActivity.php
blob: 46d71fc717a6d7392c3e7805d9de15dfcc5727cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php

namespace Model;

/**
 * Project activity model
 *
 * @package  model
 * @author   Frederic Guillot
 */
class ProjectActivity extends Base
{
    /**
     * SQL table name
     *
     * @var string
     */
    const TABLE = 'project_activities';

    /**
     * Maximum number of events
     *
     * @var integer
     */
    const MAX_EVENTS = 1000;

    /**
     * Add a new event for the project
     *
     * @access public
     * @param  integer     $project_id      Project id
     * @param  integer     $task_id         Task id
     * @param  integer     $creator_id      User id
     * @param  string      $event_name      Event name
     * @param  array       $data            Event data (will be serialized)
     * @return boolean
     */
    public function createEvent($project_id, $task_id, $creator_id, $event_name, array $data)
    {
        $values = array(
            'project_id' => $project_id,
            'task_id' => $task_id,
            'creator_id' => $creator_id,
            'event_name' => $event_name,
            'date_creation' => time(),
            'data' => json_encode($data),
        );

        $this->cleanup(self::MAX_EVENTS - 1);
        return $this->db->table(self::TABLE)->insert($values);
    }

     /**
     * Get all events for the given task
     *
     * @access public
     * @param  integer[]   $task_ids         Task ids
     * @param  integer     $limit           Maximum events number
     * @param  integer     $start           Timestamp of earliest activity
     * @param  integer     $end             Timestamp of latest activity
     * @return array
     */
    public function getTasks(array $task_ids, $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'
                           )
                           ->in('task_id', $task_ids)
                           ->join(User::TABLE, 'id', 'creator_id')
                           ->desc(self::TABLE.'.id')
                           ->limit($limit);

        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 += $this->decode($event['data']);
            unset($event['data']);

            $event['author'] = $event['author_name'] ?: $event['author_username'];
            $event['event_title'] = $this->getTitle($event);
            $event['event_content'] = $this->getContent($event);
        }

        return $events;
    }
    
    /**
     * Get all events for the given project
     *
     * @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, $start = null, $end = null)
    {
        return $this->getProjects(array($project_id), $limit, $start, $end);
    }

    /**
     * Get all events for the given projects list
     *
     * @access public
     * @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 $project_ids, $limit = 50, $start = null, $end = null)
    {
        if (empty($project_ids)) {
            return array();
        }

        $query = $this->db->table(self::TABLE)
                           ->columns(
                                self::TABLE.'.*',
                                User::TABLE.'.username AS author_username',
                                User::TABLE.'.name AS author_name'
                           )
                           ->in('project_id', $project_ids)
                           ->join(User::TABLE, 'id', 'creator_id')
                           ->desc(self::TABLE.'.id')
                           ->limit($limit);

        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 += $this->decode($event['data']);
            unset($event['data']);

            $event['author'] = $event['author_name'] ?: $event['author_username'];
            $event['event_title'] = $this->getTitle($event);
            $event['event_content'] = $this->getContent($event);
        }

        return $events;
    }

    /**
     * Remove old event entries to avoid large table
     *
     * @access public
     * @param  integer    $max    Maximum number of items to keep in the table
     */
    public function cleanup($max)
    {
        if ($this->db->table(self::TABLE)->count() > $max) {

            $this->db->execute('
                DELETE FROM '.self::TABLE.'
                WHERE id <= (
                    SELECT id FROM (
                        SELECT id FROM '.self::TABLE.' ORDER BY id DESC LIMIT 1 OFFSET '.$max.'
                    ) foo
                )'
            );
        }
    }

    /**
     * Get the event html content
     *
     * @access public
     * @param  array     $params    Event properties
     * @return string
     */
    public function getContent(array $params)
    {
        return $this->template->render(
            'event/'.str_replace('.', '_', $params['event_name']),
            $params
        );
    }

    /**
     * Get the event title (translated)
     *
     * @access public
     * @param  array     $event    Event properties
     * @return string
     */
    public function getTitle(array $event)
    {
        switch ($event['event_name']) {
            case Task::EVENT_ASSIGNEE_CHANGE:
                $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:
                return t('%s created the task #%d', $event['author'], $event['task']['id']);
            case Task::EVENT_CLOSE:
                return t('%s closed the task #%d', $event['author'], $event['task']['id']);
            case Task::EVENT_OPEN:
                return t('%s open the task #%d', $event['author'], $event['task']['id']);
            case Task::EVENT_MOVE_COLUMN:
                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:
                return t('%s updated a subtask for the task #%d', $event['author'], $event['task']['id']);
            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']);
            case Comment::EVENT_CREATE:
                return t('%s commented on the task #%d', $event['author'], $event['task']['id']);
            default:
                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();
    }
}