summaryrefslogtreecommitdiff
path: root/app/Analytic/EstimatedTimeComparisonAnalytic.php
blob: 490bcd50ef5bef2d1f34257215cdda43eeef238b (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
<?php

namespace Kanboard\Analytic;

use Kanboard\Core\Base;
use Kanboard\Model\Task;

/**
 * Estimated/Spent Time Comparison
 *
 * @package  analytic
 * @author   Frederic Guillot
 */
class EstimatedTimeComparisonAnalytic extends Base
{
    /**
     * Build report
     *
     * @access public
     * @param  integer   $project_id    Project id
     * @return array
     */
    public function build($project_id)
    {
        $rows = $this->db->table(Task::TABLE)
            ->columns('SUM(time_estimated) AS time_estimated', 'SUM(time_spent) AS time_spent', 'is_active')
            ->eq('project_id', $project_id)
            ->groupBy('is_active')
            ->findAll();

        $metrics = array(
            'open' => array(
                'time_spent' => 0,
                'time_estimated' => 0,
            ),
            'closed' => array(
                'time_spent' => 0,
                'time_estimated' => 0,
            ),
        );

        foreach ($rows as $row) {
            $key = $row['is_active'] == Task::STATUS_OPEN ? 'open' : 'closed';
            $metrics[$key]['time_spent'] = $row['time_spent'];
            $metrics[$key]['time_estimated'] = $row['time_estimated'];
        }

        return $metrics;
    }
}