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

namespace Kanboard\Model;

use Kanboard\Core\Base;

/**
 * Task Analytic
 *
 * @package  Kanboard\Model
 * @author   Frederic Guillot
 */
class TaskAnalyticModel extends Base
{
    /**
     * Get the time between date_creation and date_completed or now if empty
     *
     * @access public
     * @param  array   $task
     * @return integer
     */
    public function getLeadTime(array $task)
    {
        return ($task['date_completed'] ?: time()) - $task['date_creation'];
    }

    /**
     * Get the time between date_started and date_completed or now if empty
     *
     * @access public
     * @param  array   $task
     * @return integer
     */
    public function getCycleTime(array $task)
    {
        if (empty($task['date_started'])) {
            return 0;
        }

        return ($task['date_completed'] ?: time()) - $task['date_started'];
    }

    /**
     * Get the average time spent in each column
     *
     * @access public
     * @param  array   $task
     * @return array
     */
    public function getTimeSpentByColumn(array $task)
    {
        $result = array();
        $columns = $this->columnModel->getList($task['project_id']);
        $sums = $this->transitionModel->getTimeSpentByTask($task['id']);

        foreach ($columns as $column_id => $column_title) {
            $time_spent = isset($sums[$column_id]) ? $sums[$column_id] : 0;

            if ($task['column_id'] == $column_id) {
                $time_spent += ($task['date_completed'] ?: time()) - $task['date_moved'];
            }

            $result[] = array(
                'id' => $column_id,
                'title' => $column_title,
                'time_spent' => $time_spent,
            );
        }

        return $result;
    }
}