summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2015-02-07 21:44:51 -0500
committerFrederic Guillot <fred@kanboard.net>2015-02-07 21:44:51 -0500
commitc9cc82eec898654846c3c7e409269166cc5dba7c (patch)
tree4ddc6479ff5447627f07a40f37024420e2bd0a71
parent2d890cbc712371f17ba4bbceb02af3c5ba04e6da (diff)
Update subtask spent based on the time tracking
-rw-r--r--app/Controller/Config.php2
-rw-r--r--app/Model/Subtask.php39
-rw-r--r--app/Model/SubtaskTimeTracking.php68
-rw-r--r--app/Subscriber/SubtaskTimesheetSubscriber.php25
4 files changed, 90 insertions, 44 deletions
diff --git a/app/Controller/Config.php b/app/Controller/Config.php
index 9005c300..01c7ad53 100644
--- a/app/Controller/Config.php
+++ b/app/Controller/Config.php
@@ -38,7 +38,7 @@ class Config extends Base
{
if ($this->request->isPost()) {
- $values = $this->request->getValues();
+ $values = $this->request->getValues() + array('subtask_restriction' => 0, 'subtask_time_tracking' => 0);
if ($this->config->save($values)) {
$this->config->reload();
diff --git a/app/Model/Subtask.php b/app/Model/Subtask.php
index 1d5ed566..048594bd 100644
--- a/app/Model/Subtask.php
+++ b/app/Model/Subtask.php
@@ -176,9 +176,6 @@ class Subtask extends Base
$subtask_id = $this->persist(self::TABLE, $values);
if ($subtask_id) {
-
- $this->updateTaskTimeTracking($values['task_id']);
-
$this->container['dispatcher']->dispatch(
self::EVENT_CREATE,
new SubtaskEvent(array('id' => $subtask_id) + $values)
@@ -202,10 +199,6 @@ class Subtask extends Base
if ($result) {
- if (isset($values['task_id'])) {
- $this->updateTaskTimeTracking($values['task_id']);
- }
-
$this->container['dispatcher']->dispatch(
self::EVENT_UPDATE,
new SubtaskEvent($values)
@@ -231,6 +224,7 @@ class Subtask extends Base
$values = array(
'id' => $subtask['id'],
'status' => ($subtask['status'] + 1) % 3,
+ 'task_id' => $subtask['task_id'],
);
return $this->update($values);
@@ -268,37 +262,6 @@ class Subtask extends Base
}
/**
- * Update task time tracking based on subtasks time tracking
- *
- * @access public
- * @param integer $task_id Task id
- * @return bool
- */
- public function updateTaskTimeTracking($task_id)
- {
- $result = $this->db
- ->table(self::TABLE)
- ->eq('task_id', $task_id)
- ->columns(
- 'SUM(time_spent) AS total_spent',
- 'SUM(time_estimated) AS total_estimated'
- )
- ->findOne();
-
- if (empty($result['total_spent']) && empty($result['total_estimated'])) {
- return true;
- }
-
- return $this->db
- ->table(Task::TABLE)
- ->eq('id', $task_id)
- ->update(array(
- 'time_spent' => $result['total_spent'],
- 'time_estimated' => $result['total_estimated'],
- ));
- }
-
- /**
* Remove
*
* @access public
diff --git a/app/Model/SubtaskTimeTracking.php b/app/Model/SubtaskTimeTracking.php
index cc839ddb..3104dc20 100644
--- a/app/Model/SubtaskTimeTracking.php
+++ b/app/Model/SubtaskTimeTracking.php
@@ -68,6 +68,8 @@ class SubtaskTimeTracking extends Base
*/
public function logEndTime($subtask_id, $user_id)
{
+ $this->updateSubtaskTimeSpent($subtask_id, $user_id);
+
return $this->db
->table(self::TABLE)
->eq('subtask_id', $subtask_id)
@@ -77,4 +79,70 @@ class SubtaskTimeTracking extends Base
'end' => time()
));
}
+
+ /**
+ * Update task time tracking based on subtasks time tracking
+ *
+ * @access public
+ * @param integer $task_id Task id
+ * @return bool
+ */
+ public function updateTaskTimeTracking($task_id)
+ {
+ $result = $this->calculateSubtaskTime($task_id);
+
+ if (empty($result['total_spent']) && empty($result['total_estimated'])) {
+ return true;
+ }
+
+ return $this->db
+ ->table(Task::TABLE)
+ ->eq('id', $task_id)
+ ->update(array(
+ 'time_spent' => $result['total_spent'],
+ 'time_estimated' => $result['total_estimated'],
+ ));
+ }
+
+ /**
+ * Sum time spent and time estimated for all subtasks
+ *
+ * @access public
+ * @param integer $task_id Task id
+ * @return array
+ */
+ public function calculateSubtaskTime($task_id)
+ {
+ return $this->db
+ ->table(Subtask::TABLE)
+ ->eq('task_id', $task_id)
+ ->columns(
+ 'SUM(time_spent) AS total_spent',
+ 'SUM(time_estimated) AS total_estimated'
+ )
+ ->findOne();
+ }
+
+ /**
+ * Update subtask time spent based on the punch clock table
+ *
+ * @access public
+ * @param integer $subtask_id
+ * @param integer $user_id
+ * @return bool
+ */
+ public function updateSubtaskTimeSpent($subtask_id, $user_id)
+ {
+ $start_time = $this->db
+ ->table(self::TABLE)
+ ->eq('subtask_id', $subtask_id)
+ ->eq('user_id', $user_id)
+ ->eq('end', 0)
+ ->findOneColumn('start');
+
+ return $start_time &&
+ $this->db
+ ->getConnection()
+ ->exec('UPDATE '.Subtask::TABLE.' SET time_spent=time_spent+'.round((time() - $start_time) / 3600, 1).' WHERE id='.$subtask_id);
+ }
}
diff --git a/app/Subscriber/SubtaskTimesheetSubscriber.php b/app/Subscriber/SubtaskTimesheetSubscriber.php
index df10fc43..acae9a4b 100644
--- a/app/Subscriber/SubtaskTimesheetSubscriber.php
+++ b/app/Subscriber/SubtaskTimesheetSubscriber.php
@@ -11,21 +11,36 @@ class SubtaskTimesheetSubscriber extends Base implements EventSubscriberInterfac
public static function getSubscribedEvents()
{
return array(
- Subtask::EVENT_UPDATE => array('log', 0),
+ Subtask::EVENT_CREATE => array('updateTaskTime', 0),
+ Subtask::EVENT_UPDATE => array(
+ array('logStartEnd', 10),
+ array('updateTaskTime', 0),
+ )
);
}
- public function log(SubtaskEvent $event)
+ public function updateTaskTime(SubtaskEvent $event)
{
- if (isset($event['status'])) {
+ if (isset($event['task_id'])) {
+ $this->subtaskTimeTracking->updateTaskTimeTracking($event['task_id']);
+ }
+ }
+
+ public function logStartEnd(SubtaskEvent $event)
+ {
+ if ($this->config->get('subtask_time_tracking') == 1 && isset($event['status'])) {
$subtask = $this->subtask->getById($event['id']);
+ if (empty($subtask['user_id'])) {
+ return false;
+ }
+
if ($subtask['status'] == Subtask::STATUS_INPROGRESS) {
- $this->subtaskTimeTracking->logStartTime($subtask['id'], $subtask['user_id']);
+ return $this->subtaskTimeTracking->logStartTime($subtask['id'], $subtask['user_id']);
}
else {
- $this->subtaskTimeTracking->logEndTime($subtask['id'], $subtask['user_id']);
+ return $this->subtaskTimeTracking->logEndTime($subtask['id'], $subtask['user_id']);
}
}
}