1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Model; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Database\Model; |
15
|
|
|
use PicoDb\Table; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Project activity model. |
19
|
|
|
*/ |
20
|
|
|
class ProjectActivityModel extends Model |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* SQL table name. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
const TABLE = 'project_activities'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Maximum number of events. |
31
|
|
|
* |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
const MAX_EVENTS = 1000; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Add a new event for the project. |
38
|
|
|
* |
39
|
|
|
* @param int $project_id Project id |
40
|
|
|
* @param int $task_id Task id |
41
|
|
|
* @param int $creator_id User id |
42
|
|
|
* @param string $event_name Event name |
43
|
|
|
* @param array $data Event data (will be serialized) |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function createEvent($project_id, $task_id, $creator_id, $event_name, array $data) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$values = [ |
50
|
|
|
'project_id' => $project_id, |
51
|
|
|
'task_id' => $task_id, |
52
|
|
|
'creator_id' => $creator_id, |
53
|
|
|
'event_name' => $event_name, |
54
|
|
|
'date_creation' => time(), |
55
|
|
|
'data' => json_encode($data), |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
$this->cleanup(self::MAX_EVENTS - 1); |
59
|
|
|
|
60
|
|
|
return $this->db->table(self::TABLE)->insert($values); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get query. |
65
|
|
|
* |
66
|
|
|
* @return Table |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function getQuery() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
return $this |
|
|
|
|
71
|
|
|
->db |
72
|
|
|
->table(self::TABLE) |
73
|
|
|
->columns( |
74
|
|
|
self::TABLE.'.*', |
75
|
|
|
'uc.username AS author_username', |
76
|
|
|
'uc.name AS author_name', |
77
|
|
|
'uc.email', |
78
|
|
|
'uc.avatar_path' |
79
|
|
|
) |
80
|
|
|
->join(TaskModel::TABLE, 'id', 'task_id') |
81
|
|
|
->join(ProjectModel::TABLE, 'id', 'project_id') |
82
|
|
|
->left(UserModel::TABLE, 'uc', 'id', self::TABLE, 'creator_id'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Remove old event entries to avoid large table. |
87
|
|
|
* |
88
|
|
|
* @param int $max Maximum number of items to keep in the table |
89
|
|
|
*/ |
90
|
|
|
public function cleanup($max) |
91
|
|
|
{ |
92
|
|
|
$total = $this->db->table(self::TABLE)->count(); |
|
|
|
|
93
|
|
|
|
94
|
|
|
if ($total > $max) { |
95
|
|
|
$ids = $this->db->table(self::TABLE)->asc('id')->limit($total - $max)->findAllByColumn('id'); |
|
|
|
|
96
|
|
|
$this->db->table(self::TABLE)->in('id', $ids)->remove(); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.