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

namespace Kanboard\Model;

/**
 * Task Analytic
 *
 * @package  model
 * @author   Frederic Guillot
 */
class TaskAnalytic 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->board->getColumnsList($task['project_id']);
        $sums = $this->transition->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;
    }
}