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
|
<?php
namespace Kanboard\Plugin\TimeMachine\Model;
use Kanboard\Model\SubtaskModel;
use Kanboard\Model\TaskModel;
use Kanboard\Model\UserModel;
/**
* Class SubtaskTimeTrackingModel
*
* @author yvalentin
* https://yohannvalentin.com
*/
class SubtaskTimeTrackingModel extends \Kanboard\Model\SubtaskTimeTrackingModel
{
/**
*
* @param $subTask_id
*
* @return array
*/
public function getBySubTaskId($subTask_id)
{
$sttts = $this->db
->table(self::TABLE)
->columns(
self::TABLE.'.id',
self::TABLE.'.subtask_id',
self::TABLE.'.end',
self::TABLE.'.start',
self::TABLE.'.time_spent',
self::TABLE.'.user_id',
SubtaskModel::TABLE.'.task_id',
SubtaskModel::TABLE.'.title AS subtask_title',
UserModel::TABLE.'.username',
UserModel::TABLE.'.name AS user_fullname'
)
->join(SubtaskModel::TABLE, 'id', 'subtask_id')
->join(UserModel::TABLE, 'id', 'user_id', self::TABLE)
->eq(SubtaskModel::TABLE.'.id', $subTask_id)
->findAll();
$values = [];
foreach ($sttts as $key => $sttt) {
$values[$sttt['id']] = $sttt;
// This is for form input name
$values[$sttt['id']]['start-'.$sttt['id']] = $sttt['start'];
$values[$sttt['id']]['end-'.$sttt['id']] = $sttt['end'];
}
return $values;
}
/**
* Update a subTask Time Tracking
*
* @param array $values
* @param $subTaskId
* @param $taskId
* @param bool $fireEvent
*
* @return array
* @throws \Exception
*/
public function updates(array $values, $subTaskId, $taskId, $fireEvent = true)
{
$result = [];
foreach ($values as $key => $value) {
$value = $this->dateParser->convert($value, array('start'), true);
$value = $this->dateParser->convert($value, array('end'), true);
if (!empty($value['start']) && !empty($value['end'])) {
$value['time_spent'] = $this->dateParser->getHours(
(new \DateTime())->setTimestamp($value['start']),
(new \DateTime())->setTimestamp($value['end'])
);
} else {
$value['time_spent'] = 0;
$value['start'] = !empty($value['start']) ? $value['start'] : 0;
$value['end'] = !empty($value['end']) ? $value['end'] : 0;
}
$result[] = $this->db->table(self::TABLE)->eq('id', $key)->save($value);
}
if (!in_array(false, $result)) {
// Need to update task time tracking spent time and sub task too
$this->calculateSubtaskTimeSpent($subTaskId, $fireEvent);
}
return $result;
}
/**
* Update subtask time spent By calculation
*
* @access public
* @param integer $subtask_id
* @return bool
*/
public function calculateSubtaskTimeSpent($subtask_id, $fireEvent)
{
$timeSpent = $this->db->table(self::TABLE)
->eq('subtask_id', $subtask_id)
->columns(
'SUM(time_spent) AS time_spent'
)
->findOne();
$subtask = $this->subtaskModel->getById($subtask_id);
return $this->subtaskModel->update(array(
'id' => $subtask['id'],
'time_spent' => $timeSpent['time_spent'],
'task_id' => $subtask['task_id'],
), $fireEvent);
}
/**
* Update task time tracking based on subtasks time tracking
*
* @access public
* @param integer $task_id Task id
* @return bool
*/
public function updateTaskTimeTracking($task_id)
{
$values = $this->calculateSubtaskTime($task_id);
// Do not update time_estimated or spend_time of task if sum of subtask time estimated is equal to 0
if($values['time_estimated'] == 0) {
unset($values['time_estimated']);
}
if($values['time_spent'] == 0) {
unset($values['time_spent']);
}
if (empty($values)) {
return true;
}
return $this->db
->table(TaskModel::TABLE)
->eq('id', $task_id)
->update($values);
}
}
|