summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/Model/SubtaskModel.php15
-rw-r--r--app/Model/SubtaskStatusModel.php3
-rw-r--r--app/Model/SubtaskTimeTrackingModel.php23
-rw-r--r--app/ServiceProvider/EventDispatcherProvider.php2
-rw-r--r--app/Subscriber/SubtaskTimeTrackingSubscriber.php48
-rw-r--r--tests/units/Job/SubtaskEventJobTest.php2
-rw-r--r--tests/units/Model/SubtaskModelTest.php42
-rw-r--r--tests/units/Model/SubtaskTimeTrackingModelTest.php37
8 files changed, 114 insertions, 58 deletions
diff --git a/app/Model/SubtaskModel.php b/app/Model/SubtaskModel.php
index 568e27a4..608ffce7 100644
--- a/app/Model/SubtaskModel.php
+++ b/app/Model/SubtaskModel.php
@@ -173,6 +173,7 @@ class SubtaskModel extends Base
$subtask_id = $this->db->table(self::TABLE)->persist($values);
if ($subtask_id !== false) {
+ $this->subtaskTimeTrackingModel->updateTaskTimeTracking($values['task_id']);
$this->queueManager->push($this->subtaskEventJob->withParams($subtask_id, self::EVENT_CREATE));
}
@@ -183,17 +184,21 @@ class SubtaskModel extends Base
* Update
*
* @access public
- * @param array $values Form values
- * @param bool $fire_events If true, will be called an event
+ * @param array $values
+ * @param bool $fire_event
* @return bool
*/
- public function update(array $values, $fire_events = true)
+ public function update(array $values, $fire_event = true)
{
$this->prepare($values);
$result = $this->db->table(self::TABLE)->eq('id', $values['id'])->save($values);
- if ($result && $fire_events) {
- $this->queueManager->push($this->subtaskEventJob->withParams($values['id'], self::EVENT_UPDATE, $values));
+ if ($result) {
+ $this->subtaskTimeTrackingModel->updateTaskTimeTracking($values['task_id']);
+
+ if ($fire_event) {
+ $this->queueManager->push($this->subtaskEventJob->withParams($values['id'], self::EVENT_UPDATE, $values));
+ }
}
return $result;
diff --git a/app/Model/SubtaskStatusModel.php b/app/Model/SubtaskStatusModel.php
index 26cbb67d..c99d6055 100644
--- a/app/Model/SubtaskStatusModel.php
+++ b/app/Model/SubtaskStatusModel.php
@@ -63,8 +63,11 @@ class SubtaskStatusModel extends Base
if (empty($subtask['user_id']) && $this->userSession->isLogged()) {
$values['user_id'] = $this->userSession->getId();
+ $subtask['user_id'] = $values['user_id'];
}
+ $this->subtaskTimeTrackingModel->toggleTimer($subtask_id, $subtask['user_id'], $status);
+
return $this->subtaskModel->update($values) ? $status : false;
}
diff --git a/app/Model/SubtaskTimeTrackingModel.php b/app/Model/SubtaskTimeTrackingModel.php
index 062e594a..3b1b97e4 100644
--- a/app/Model/SubtaskTimeTrackingModel.php
+++ b/app/Model/SubtaskTimeTrackingModel.php
@@ -160,6 +160,28 @@ class SubtaskTimeTrackingModel extends Base
}
/**
+ * Start or stop timer according to subtask status
+ *
+ * @access public
+ * @param integer $subtask_id
+ * @param integer $user_id
+ * @param integer $status
+ * @return boolean
+ */
+ public function toggleTimer($subtask_id, $user_id, $status)
+ {
+ if ($this->configModel->get('subtask_time_tracking') == 1) {
+ if ($status == SubtaskModel::STATUS_INPROGRESS) {
+ return $this->subtaskTimeTrackingModel->logStartTime($subtask_id, $user_id);
+ } elseif ($status == SubtaskModel::STATUS_DONE) {
+ return $this->subtaskTimeTrackingModel->logEndTime($subtask_id, $user_id);
+ }
+ }
+
+ return false;
+ }
+
+ /**
* Log start time
*
* @access public
@@ -252,7 +274,6 @@ class SubtaskTimeTrackingModel extends Base
{
$subtask = $this->subtaskModel->getById($subtask_id);
- // Fire the event subtask.update
return $this->subtaskModel->update(array(
'id' => $subtask['id'],
'time_spent' => $subtask['time_spent'] + $time_spent,
diff --git a/app/ServiceProvider/EventDispatcherProvider.php b/app/ServiceProvider/EventDispatcherProvider.php
index 57543fe4..ebf42cbf 100644
--- a/app/ServiceProvider/EventDispatcherProvider.php
+++ b/app/ServiceProvider/EventDispatcherProvider.php
@@ -11,7 +11,6 @@ use Kanboard\Subscriber\BootstrapSubscriber;
use Kanboard\Subscriber\NotificationSubscriber;
use Kanboard\Subscriber\ProjectDailySummarySubscriber;
use Kanboard\Subscriber\ProjectModificationDateSubscriber;
-use Kanboard\Subscriber\SubtaskTimeTrackingSubscriber;
use Kanboard\Subscriber\TransitionSubscriber;
use Kanboard\Subscriber\RecurringTaskSubscriber;
@@ -31,7 +30,6 @@ class EventDispatcherProvider implements ServiceProviderInterface
$container['dispatcher']->addSubscriber(new ProjectDailySummarySubscriber($container));
$container['dispatcher']->addSubscriber(new ProjectModificationDateSubscriber($container));
$container['dispatcher']->addSubscriber(new NotificationSubscriber($container));
- $container['dispatcher']->addSubscriber(new SubtaskTimeTrackingSubscriber($container));
$container['dispatcher']->addSubscriber(new TransitionSubscriber($container));
$container['dispatcher']->addSubscriber(new RecurringTaskSubscriber($container));
diff --git a/app/Subscriber/SubtaskTimeTrackingSubscriber.php b/app/Subscriber/SubtaskTimeTrackingSubscriber.php
deleted file mode 100644
index 7e39c126..00000000
--- a/app/Subscriber/SubtaskTimeTrackingSubscriber.php
+++ /dev/null
@@ -1,48 +0,0 @@
-<?php
-
-namespace Kanboard\Subscriber;
-
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
-use Kanboard\Model\SubtaskModel;
-use Kanboard\Event\SubtaskEvent;
-
-class SubtaskTimeTrackingSubscriber extends BaseSubscriber implements EventSubscriberInterface
-{
- public static function getSubscribedEvents()
- {
- return array(
- SubtaskModel::EVENT_CREATE => 'updateTaskTime',
- SubtaskModel::EVENT_DELETE => 'updateTaskTime',
- SubtaskModel::EVENT_UPDATE => array(
- array('logStartEnd', 10),
- array('updateTaskTime', 0),
- )
- );
- }
-
- public function updateTaskTime(SubtaskEvent $event)
- {
- if (isset($event['task_id'])) {
- $this->logger->debug('Subscriber executed: '.__METHOD__);
- $this->subtaskTimeTrackingModel->updateTaskTimeTracking($event['task_id']);
- }
- }
-
- public function logStartEnd(SubtaskEvent $event)
- {
- if (isset($event['status']) && $this->configModel->get('subtask_time_tracking') == 1) {
- $this->logger->debug('Subscriber executed: '.__METHOD__);
- $subtask = $this->subtaskModel->getById($event['id']);
-
- if (empty($subtask['user_id'])) {
- return false;
- }
-
- if ($subtask['status'] == SubtaskModel::STATUS_INPROGRESS) {
- return $this->subtaskTimeTrackingModel->logStartTime($subtask['id'], $subtask['user_id']);
- } else {
- return $this->subtaskTimeTrackingModel->logEndTime($subtask['id'], $subtask['user_id']);
- }
- }
- }
-}
diff --git a/tests/units/Job/SubtaskEventJobTest.php b/tests/units/Job/SubtaskEventJobTest.php
index 66c3db05..bdc30b51 100644
--- a/tests/units/Job/SubtaskEventJobTest.php
+++ b/tests/units/Job/SubtaskEventJobTest.php
@@ -41,7 +41,7 @@ class SubtaskEventJobTest extends Base
$this->assertEquals(1, $projectModel->create(array('name' => 'test1')));
$this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1)));
$this->assertEquals(1, $subtaskModel->create(array('task_id' => 1, 'title' => 'before')));
- $this->assertTrue($subtaskModel->update(array('id' => 1, 'title' => 'after')));
+ $this->assertTrue($subtaskModel->update(array('id' => 1, 'task_id' => 1, 'title' => 'after')));
$this->assertTrue($subtaskModel->remove(1));
$called = $this->container['dispatcher']->getCalledListeners();
diff --git a/tests/units/Model/SubtaskModelTest.php b/tests/units/Model/SubtaskModelTest.php
index 23183d22..eed37cf3 100644
--- a/tests/units/Model/SubtaskModelTest.php
+++ b/tests/units/Model/SubtaskModelTest.php
@@ -5,6 +5,7 @@ require_once __DIR__.'/../Base.php';
use Kanboard\Model\TaskCreationModel;
use Kanboard\Model\SubtaskModel;
use Kanboard\Model\ProjectModel;
+use Kanboard\Model\TaskFinderModel;
class SubtaskModelTest extends Base
{
@@ -30,6 +31,24 @@ class SubtaskModelTest extends Base
$this->assertEquals(1, $subtask['position']);
}
+ public function testCreationUpdateTaskTimeTracking()
+ {
+ $taskCreationModel = new TaskCreationModel($this->container);
+ $subtaskModel = new SubtaskModel($this->container);
+ $projectModel = new ProjectModel($this->container);
+ $taskFinderModel = new TaskFinderModel($this->container);
+
+ $this->assertEquals(1, $projectModel->create(array('name' => 'test')));
+ $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1)));
+
+ $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #1', 'task_id' => 1, 'time_estimated' => 2, 'time_spent' => 1)));
+ $this->assertEquals(2, $subtaskModel->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_estimated' => 5, 'time_spent' => 5)));
+
+ $task = $taskFinderModel->getById(1);
+ $this->assertEquals(7, $task['time_estimated']);
+ $this->assertEquals(6, $task['time_spent']);
+ }
+
public function testModification()
{
$taskCreationModel = new TaskCreationModel($this->container);
@@ -40,7 +59,7 @@ class SubtaskModelTest extends Base
$this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1)));
$this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #1', 'task_id' => 1)));
- $this->assertTrue($subtaskModel->update(array('id' => 1, 'user_id' => 1, 'status' => SubtaskModel::STATUS_INPROGRESS)));
+ $this->assertTrue($subtaskModel->update(array('id' => 1, 'task_id' => 1, 'user_id' => 1, 'status' => SubtaskModel::STATUS_INPROGRESS)));
$subtask = $subtaskModel->getById(1);
$this->assertNotEmpty($subtask);
@@ -54,6 +73,27 @@ class SubtaskModelTest extends Base
$this->assertEquals(1, $subtask['position']);
}
+ public function testModificationUpdateTaskTimeTracking()
+ {
+ $taskCreationModel = new TaskCreationModel($this->container);
+ $subtaskModel = new SubtaskModel($this->container);
+ $projectModel = new ProjectModel($this->container);
+ $taskFinderModel = new TaskFinderModel($this->container);
+
+ $this->assertEquals(1, $projectModel->create(array('name' => 'test')));
+ $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1)));
+
+ $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #1', 'task_id' => 1)));
+ $this->assertEquals(2, $subtaskModel->create(array('title' => 'subtask #2', 'task_id' => 1)));
+ $this->assertTrue($subtaskModel->update(array('id' => 1, 'task_id' => 1, 'time_estimated' => 2, 'time_spent' => 1)));
+ $this->assertTrue($subtaskModel->update(array('id' => 2, 'task_id' => 1, 'time_estimated' => 2, 'time_spent' => 1)));
+ $this->assertTrue($subtaskModel->update(array('id' => 1, 'task_id' => 1, 'time_estimated' => 5, 'time_spent' => 5)));
+
+ $task = $taskFinderModel->getById(1);
+ $this->assertEquals(7, $task['time_estimated']);
+ $this->assertEquals(6, $task['time_spent']);
+ }
+
public function testRemove()
{
$taskCreationModel = new TaskCreationModel($this->container);
diff --git a/tests/units/Model/SubtaskTimeTrackingModelTest.php b/tests/units/Model/SubtaskTimeTrackingModelTest.php
index cfee5b14..8b0fe698 100644
--- a/tests/units/Model/SubtaskTimeTrackingModelTest.php
+++ b/tests/units/Model/SubtaskTimeTrackingModelTest.php
@@ -2,6 +2,7 @@
require_once __DIR__.'/../Base.php';
+use Kanboard\Model\ConfigModel;
use Kanboard\Model\TaskFinderModel;
use Kanboard\Model\TaskCreationModel;
use Kanboard\Model\SubtaskModel;
@@ -10,6 +11,42 @@ use Kanboard\Model\ProjectModel;
class SubtaskTimeTrackingModelTest extends Base
{
+ public function testToggleTimer()
+ {
+ $taskCreationModel = new TaskCreationModel($this->container);
+ $subtaskModel = new SubtaskModel($this->container);
+ $subtaskTimeTrackingModel = new SubtaskTimeTrackingModel($this->container);
+ $projectModel = new ProjectModel($this->container);
+
+ $this->assertEquals(1, $projectModel->create(array('name' => 'test1')));
+ $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1)));
+ $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #2', 'task_id' => 1, 'user_id' => 1)));
+
+ $this->assertFalse($subtaskTimeTrackingModel->toggleTimer(1, 1, SubtaskModel::STATUS_TODO));
+ $this->assertTrue($subtaskTimeTrackingModel->toggleTimer(1, 1, SubtaskModel::STATUS_INPROGRESS));
+ $this->assertTrue($subtaskTimeTrackingModel->toggleTimer(1, 1, SubtaskModel::STATUS_DONE));
+ }
+
+ public function testToggleTimerWhenFeatureDisabled()
+ {
+ $configModel = new ConfigModel($this->container);
+ $configModel->save(array('subtask_time_tracking' => '0'));
+ $this->container['memoryCache']->flush();
+
+ $taskCreationModel = new TaskCreationModel($this->container);
+ $subtaskModel = new SubtaskModel($this->container);
+ $subtaskTimeTrackingModel = new SubtaskTimeTrackingModel($this->container);
+ $projectModel = new ProjectModel($this->container);
+
+ $this->assertEquals(1, $projectModel->create(array('name' => 'test1')));
+ $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1)));
+ $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #2', 'task_id' => 1, 'user_id' => 1)));
+
+ $this->assertFalse($subtaskTimeTrackingModel->toggleTimer(1, 1, SubtaskModel::STATUS_TODO));
+ $this->assertFalse($subtaskTimeTrackingModel->toggleTimer(1, 1, SubtaskModel::STATUS_INPROGRESS));
+ $this->assertFalse($subtaskTimeTrackingModel->toggleTimer(1, 1, SubtaskModel::STATUS_DONE));
+ }
+
public function testHasTimer()
{
$taskCreationModel = new TaskCreationModel($this->container);