summaryrefslogtreecommitdiff
path: root/app/Model/ProjectActivityModel.php
blob: 739fd7873bb12d39005755a7222285584be2835b (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
<?php

namespace Kanboard\Model;

use Kanboard\Core\Base;
use PicoDb\Table;

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

    /**
     * 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)
    {
        return $this->db->table(self::TABLE)->insert(array(
            'project_id' => $project_id,
            'task_id' => $task_id,
            'creator_id' => $creator_id,
            'event_name' => $event_name,
            'date_creation' => time(),
            'data' => json_encode($data),
        ));
    }

    /**
     * Get query
     *
     * @access public
     * @return Table
     */
    public function getQuery()
    {
        return $this
            ->db
            ->table(ProjectActivityModel::TABLE)
            ->columns(
                ProjectActivityModel::TABLE.'.*',
                'uc.username AS author_username',
                'uc.name AS author_name',
                'uc.email',
                'uc.avatar_path'
            )
            ->join(TaskModel::TABLE, 'id', 'task_id')
            ->join(ProjectModel::TABLE, 'id', 'project_id')
            ->left(UserModel::TABLE, 'uc', 'id', ProjectActivityModel::TABLE, 'creator_id');
    }

    /**
     * Remove old event entries to avoid large table
     *
     * @access public
     * @param  integer $ts Timestamp
     */
    public function cleanup($ts)
    {
        $this->db->table(self::TABLE)->lt('date_creation', $ts)->remove();
    }
}