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

namespace Kanboard\Model;

use Kanboard\Core\Base;

/**
 * Board model
 *
 * @package  Kanboard\Model
 * @author   Frederic Guillot
 */
class BoardModel extends Base
{
    /**
     * Get Kanboard default columns
     *
     * @access public
     * @return string[]
     */
    public function getDefaultColumns()
    {
        return array(t('Backlog'), t('Ready'), t('Work in progress'), t('Done'));
    }

    /**
     * Get user default columns
     *
     * @access public
     * @return array
     */
    public function getUserColumns()
    {
        $column_names = explode(',', $this->configModel->get('board_columns', implode(',', $this->getDefaultColumns())));
        $columns = array();

        foreach ($column_names as $column_name) {
            $column_name = trim($column_name);

            if (! empty($column_name)) {
                $columns[] = array('title' => $column_name, 'task_limit' => 0, 'description' => '');
            }
        }

        return $columns;
    }

    /**
     * Create a board with default columns, must be executed inside a transaction
     *
     * @access public
     * @param  integer  $project_id   Project id
     * @param  array    $columns      Column parameters [ 'title' => 'boo', 'task_limit' => 2 ... ]
     * @return boolean
     */
    public function create($project_id, array $columns)
    {
        $position = 0;

        foreach ($columns as $column) {
            $values = array(
                'title' => $column['title'],
                'position' => ++$position,
                'project_id' => $project_id,
                'task_limit' => $column['task_limit'],
                'description' => $column['description'],
            );

            if (! $this->db->table(ColumnModel::TABLE)->save($values)) {
                return false;
            }
        }

        return true;
    }

    /**
     * Copy board columns from a project to another one
     *
     * @author Antonio Rabelo
     * @param  integer    $project_from      Project Template
     * @param  integer    $project_to        Project that receives the copy
     * @return boolean
     */
    public function duplicate($project_from, $project_to)
    {
        $columns = $this->db->table(ColumnModel::TABLE)
                            ->columns('title', 'task_limit', 'description')
                            ->eq('project_id', $project_from)
                            ->asc('position')
                            ->findAll();

        return $this->boardModel->create($project_to, $columns);
    }

    /**
     * Get all tasks sorted by columns and swimlanes
     *
     * @access public
     * @param  integer  $project_id
     * @param  callable $callback
     * @return array
     */
    public function getBoard($project_id, $callback = null)
    {
        $swimlanes = $this->swimlaneModel->getSwimlanes($project_id);
        $columns = $this->columnModel->getAll($project_id);
        $nb_columns = count($columns);

        for ($i = 0, $ilen = count($swimlanes); $i < $ilen; $i++) {
            $swimlanes[$i]['columns'] = $columns;
            $swimlanes[$i]['nb_columns'] = $nb_columns;
            $swimlanes[$i]['nb_tasks'] = 0;
            $swimlanes[$i]['nb_swimlanes'] = $ilen;

            for ($j = 0; $j < $nb_columns; $j++) {
                $column_id = $columns[$j]['id'];
                $swimlane_id = $swimlanes[$i]['id'];

                if (! isset($swimlanes[0]['columns'][$j]['nb_column_tasks'])) {
                    $swimlanes[0]['columns'][$j]['nb_column_tasks'] = 0;
                    $swimlanes[0]['columns'][$j]['total_score'] = 0;
                }

                $swimlanes[$i]['columns'][$j]['tasks'] = $callback === null ? $this->taskFinderModel->getTasksByColumnAndSwimlane($project_id, $column_id, $swimlane_id) : $callback($project_id, $column_id, $swimlane_id);
                $swimlanes[$i]['columns'][$j]['nb_tasks'] = count($swimlanes[$i]['columns'][$j]['tasks']);
                $swimlanes[$i]['columns'][$j]['score'] = $this->getColumnSum($swimlanes[$i]['columns'][$j]['tasks'], 'score');
                $swimlanes[$i]['nb_tasks'] += $swimlanes[$i]['columns'][$j]['nb_tasks'];
                $swimlanes[0]['columns'][$j]['nb_column_tasks'] += $swimlanes[$i]['columns'][$j]['nb_tasks'];
                $swimlanes[0]['columns'][$j]['total_score'] += $swimlanes[$i]['columns'][$j]['score'];
            }
        }

        return $swimlanes;
    }

    /**
     * Calculate the sum of the defined field for a list of tasks
     *
     * @access public
     * @param  array   $tasks
     * @param  string  $field
     * @return integer
     */
    public function getColumnSum(array &$tasks, $field)
    {
        $sum = 0;

        foreach ($tasks as $task) {
            $sum += $task[$field];
        }

        return $sum;
    }

    /**
     * Get the total of tasks per column
     *
     * @access public
     * @param  integer   $project_id
     * @param  boolean   $prepend       Prepend default value
     * @return array
     */
    public function getColumnStats($project_id, $prepend = false)
    {
        $listing = $this->db
                        ->hashtable(TaskModel::TABLE)
                        ->eq('project_id', $project_id)
                        ->eq('is_active', 1)
                        ->groupBy('column_id')
                        ->getAll('column_id', 'COUNT(*) AS total');

        return $prepend ? array(-1 => t('All columns')) + $listing : $listing;
    }
}