Create TaskModification model

This commit is contained in:
Frédéric Guillot 2014-11-23 15:55:59 -05:00
parent 35e4c1daaa
commit f684602ebe
18 changed files with 326 additions and 123 deletions

View File

@ -16,6 +16,7 @@ use Core\Tool;
* @property \Model\Comment $comment
* @property \Model\Task $task
* @property \Model\TaskCreation $taskCreation
* @property \Model\TaskModification $taskModification
* @property \Model\TaskFinder $taskFinder
* @property \Model\TaskStatus $taskStatus
*/

View File

@ -67,7 +67,7 @@ class TaskAssignCategoryColor extends Base
'category_id' => $this->getParam('category_id'),
);
return $this->task->update($values, false);
return $this->taskModification->update($values, false);
}
/**

View File

@ -67,7 +67,7 @@ class TaskAssignCategoryLabel extends Base
'category_id' => isset($data['category_id']) ? $data['category_id'] : $this->getParam('category_id'),
);
return $this->task->update($values, false);
return $this->taskModification->update($values, false);
}
/**

View File

@ -67,7 +67,7 @@ class TaskAssignColorCategory extends Base
'color_id' => $this->getParam('color_id'),
);
return $this->task->update($values, false);
return $this->taskModification->update($values, false);
}
/**

View File

@ -68,7 +68,7 @@ class TaskAssignColorUser extends Base
'color_id' => $this->getParam('color_id'),
);
return $this->task->update($values, false);
return $this->taskModification->update($values, false);
}
/**

View File

@ -67,7 +67,7 @@ class TaskAssignCurrentUser extends Base
'owner_id' => $this->acl->getUserId(),
);
return $this->task->update($values, false);
return $this->taskModification->update($values, false);
}
/**

View File

@ -68,7 +68,7 @@ class TaskAssignSpecificUser extends Base
'owner_id' => $this->getParam('user_id'),
);
return $this->task->update($values, false);
return $this->taskModification->update($values, false);
}
/**

View File

@ -64,7 +64,7 @@ class TaskAssignUser extends Base
'owner_id' => $data['owner_id'],
);
return $this->task->update($values, false);
return $this->taskModification->update($values, false);
}
/**

View File

@ -34,6 +34,7 @@ use Model\LastLogin;
* @property \Model\SubTask $subTask
* @property \Model\Task $task
* @property \Model\TaskCreation $taskCreation
* @property \Model\TaskModification $taskModification
* @property \Model\TaskHistory $taskHistory
* @property \Model\TaskExport $taskExport
* @property \Model\TaskFinder $taskFinder

View File

@ -62,7 +62,7 @@ class Board extends Base
list($valid,) = $this->taskValidator->validateAssigneeModification($values);
if ($valid && $this->task->update($values)) {
if ($valid && $this->taskModification->update($values)) {
$this->session->flash(t('Task updated successfully.'));
}
else {
@ -101,7 +101,7 @@ class Board extends Base
list($valid,) = $this->taskValidator->validateCategoryModification($values);
if ($valid && $this->task->update($values)) {
if ($valid && $this->taskModification->update($values)) {
$this->session->flash(t('Task updated successfully.'));
}
else {

View File

@ -204,7 +204,7 @@ class Task extends Base
if ($valid) {
if ($this->task->update($values)) {
if ($this->taskModification->update($values)) {
$this->session->flash(t('Task updated successfully.'));
if ($this->request->getIntegerParam('ajax')) {
@ -245,7 +245,7 @@ class Task extends Base
list($valid, $errors) = $this->taskValidator->validateTimeModification($values);
if ($valid && $this->task->update($values)) {
if ($valid && $this->taskModification->update($values)) {
$this->session->flash(t('Task updated successfully.'));
}
else {
@ -386,7 +386,7 @@ class Task extends Base
if ($valid) {
if ($this->task->update($values)) {
if ($this->taskModification->update($values)) {
$this->session->flash(t('Task updated successfully.'));
}
else {

View File

@ -39,89 +39,6 @@ class Task extends Base
const EVENT_CREATE_UPDATE = 'task.create_update';
const EVENT_ASSIGNEE_CHANGE = 'task.assignee_change';
/**
* Prepare data before task creation or modification
*
* @access public
* @param array $values Form values
*/
public function prepare(array &$values)
{
$this->dateParser->convert($values, array('date_due', 'date_started'));
$this->removeFields($values, array('another_task', 'id'));
$this->resetFields($values, array('date_due', 'date_started', 'score', 'category_id', 'time_estimated', 'time_spent'));
$this->convertIntegerFields($values, array('is_active'));
}
/**
* Prepare data before task modification
*
* @access public
* @param array $values Form values
*/
public function prepareModification(array &$values)
{
$this->prepare($values);
$values['date_modification'] = time();
}
/**
* Update a task
*
* @access public
* @param array $values Form values
* @param boolean $trigger_Events Trigger events
* @return boolean
*/
public function update(array $values, $trigger_events = true)
{
// Fetch original task
$original_task = $this->taskFinder->getById($values['id']);
if (! $original_task) {
return false;
}
// Prepare data
$updated_task = $values;
$this->prepareModification($updated_task);
$result = $this->db->table(self::TABLE)->eq('id', $values['id'])->update($updated_task);
if ($result && $trigger_events) {
$this->triggerUpdateEvents($original_task, $updated_task);
}
return true;
}
/**
* Trigger events for task modification
*
* @access public
* @param array $original_task Original task data
* @param array $updated_task Updated task data
*/
public function triggerUpdateEvents(array $original_task, array $updated_task)
{
$events = array();
if (isset($updated_task['owner_id']) && $original_task['owner_id'] != $updated_task['owner_id']) {
$events[] = self::EVENT_ASSIGNEE_CHANGE;
}
else {
$events[] = self::EVENT_CREATE_UPDATE;
$events[] = self::EVENT_UPDATE;
}
$event_data = array_merge($original_task, $updated_task);
$event_data['task_id'] = $original_task['id'];
foreach ($events as $event) {
$this->event->trigger($event, $event_data);
}
}
/**
* Remove a task
*

View File

@ -0,0 +1,73 @@
<?php
namespace Model;
/**
* Task Modification
*
* @package model
* @author Frederic Guillot
*/
class TaskModification extends Base
{
/**
* Update a task
*
* @access public
* @param array $values
* @param boolean $fire_events
* @return boolean
*/
public function update(array $values, $fire_events = true)
{
$original_task = $this->taskFinder->getById($values['id']);
$this->prepare($values);
$result = $this->db->table(Task::TABLE)->eq('id', $original_task['id'])->update($values);
if ($result && $fire_events) {
$this->fireEvents($original_task, $values);
}
return $result;
}
/**
* Fire events
*
* @access public
* @param array $task
* @param array $new_values
*/
public function fireEvents(array $task, array $new_values)
{
$event_data = array_merge($task, $new_values, array('task_id' => $task['id']));
if (isset($new_values['owner_id']) && $task['owner_id'] != $new_values['owner_id']) {
$events = array(Task::EVENT_ASSIGNEE_CHANGE);
}
else {
$events = array(Task::EVENT_CREATE_UPDATE, Task::EVENT_UPDATE);
}
foreach ($events as $event) {
$this->event->trigger($event, $event_data);
}
}
/**
* Prepare data before task modification
*
* @access public
* @param array $values Form values
*/
public function prepare(array &$values)
{
$this->dateParser->convert($values, array('date_due', 'date_started'));
$this->removeFields($values, array('another_task', 'id'));
$this->resetFields($values, array('date_due', 'date_started', 'score', 'category_id', 'time_estimated', 'time_spent'));
$this->convertIntegerFields($values, array('is_active'));
$values['date_modification'] = time();
}
}

View File

@ -109,6 +109,14 @@ class TaskPosition extends Base
});
}
/**
* Fire events
*
* @access public
* @param array $task
* @param integer $new_column_id
* @param integer $new_position
*/
public function fireEvents(array $task, $new_column_id, $new_position)
{
$event_data = array(

View File

@ -8,6 +8,7 @@ $models = array(
'ProjectPermission',
'Task',
'TaskCreation',
'TaskModification',
'TaskFinder',
'TaskPosition',
'TaskStatus',
@ -136,7 +137,7 @@ $server->register('createTask', function($title, $project_id, $color_id = '', $c
return $taskCreationModel->create($values);
});
$server->register('updateTask', function($id, $title = null, $project_id = null, $color_id = null, $column_id = null, $owner_id = null, $creator_id = null, $date_due = null, $description = null, $category_id = null, $score = null) use ($taskModel, $taskValidatorModel) {
$server->register('updateTask', function($id, $title = null, $project_id = null, $color_id = null, $column_id = null, $owner_id = null, $creator_id = null, $date_due = null, $description = null, $category_id = null, $score = null) use ($taskModificationModel, $taskValidatorModel) {
$values = array(
'id' => $id,
@ -159,7 +160,7 @@ $server->register('updateTask', function($id, $title = null, $project_id = null,
}
list($valid) = $taskValidatorModel->validateApiModification($values);
return $valid && $taskModel->update($values);
return $valid && $taskModificationModel->update($values);
});

View File

@ -0,0 +1,225 @@
<?php
require_once __DIR__.'/Base.php';
use Model\Task;
use Model\TaskCreation;
use Model\TaskModification;
use Model\TaskFinder;
use Model\TaskStatus;
use Model\Project;
use Model\ProjectPermission;
class TaskModificationTest extends Base
{
public function testChangeTitle()
{
$p = new Project($this->container);
$tc = new TaskCreation($this->container);
$tm = new TaskModification($this->container);
$tf = new TaskFinder($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
$this->assertTrue($tm->update(array('id' => 1, 'title' => 'Task #1')));
$this->assertTrue($this->container['event']->isEventTriggered(Task::EVENT_CREATE_UPDATE));
$this->assertTrue($this->container['event']->isEventTriggered(Task::EVENT_UPDATE));
$event_data = $this->container['event']->getEventData(Task::EVENT_UPDATE);
$this->assertNotEmpty($event_data);
$this->assertEquals(1, $event_data['task_id']);
$this->assertEquals('Task #1', $event_data['title']);
$task = $tf->getById(1);
$this->assertEquals('Task #1', $task['title']);
}
public function testChangeAssignee()
{
$p = new Project($this->container);
$tc = new TaskCreation($this->container);
$tm = new TaskModification($this->container);
$tf = new TaskFinder($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
$task = $tf->getById(1);
$this->assertEquals(0, $task['owner_id']);
$this->assertTrue($tm->update(array('id' => 1, 'owner_id' => 1)));
$this->assertTrue($this->container['event']->isEventTriggered(Task::EVENT_ASSIGNEE_CHANGE));
$event_data = $this->container['event']->getEventData(Task::EVENT_ASSIGNEE_CHANGE);
$this->assertNotEmpty($event_data);
$this->assertEquals(1, $event_data['task_id']);
$this->assertEquals(1, $event_data['owner_id']);
$task = $tf->getById(1);
$this->assertEquals(1, $task['owner_id']);
}
public function testChangeDescription()
{
$p = new Project($this->container);
$tc = new TaskCreation($this->container);
$tm = new TaskModification($this->container);
$tf = new TaskFinder($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
$task = $tf->getById(1);
$this->assertEquals('', $task['description']);
$this->assertTrue($tm->update(array('id' => 1, 'description' => 'test')));
$task = $tf->getById(1);
$this->assertEquals('test', $task['description']);
}
public function testChangeCategory()
{
$p = new Project($this->container);
$tc = new TaskCreation($this->container);
$tm = new TaskModification($this->container);
$tf = new TaskFinder($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
$task = $tf->getById(1);
$this->assertEquals(0, $task['category_id']);
$this->assertTrue($tm->update(array('id' => 1, 'category_id' => 1)));
$task = $tf->getById(1);
$this->assertEquals(1, $task['category_id']);
}
public function testChangeColor()
{
$p = new Project($this->container);
$tc = new TaskCreation($this->container);
$tm = new TaskModification($this->container);
$tf = new TaskFinder($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
$task = $tf->getById(1);
$this->assertEquals('yellow', $task['color_id']);
$this->assertTrue($tm->update(array('id' => 1, 'color_id' => 'blue')));
$task = $tf->getById(1);
$this->assertEquals('blue', $task['color_id']);
}
public function testChangeScore()
{
$p = new Project($this->container);
$tc = new TaskCreation($this->container);
$tm = new TaskModification($this->container);
$tf = new TaskFinder($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
$task = $tf->getById(1);
$this->assertEquals(0, $task['score']);
$this->assertTrue($tm->update(array('id' => 1, 'score' => 13)));
$task = $tf->getById(1);
$this->assertEquals(13, $task['score']);
}
public function testChangeDueDate()
{
$p = new Project($this->container);
$tc = new TaskCreation($this->container);
$tm = new TaskModification($this->container);
$tf = new TaskFinder($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
$task = $tf->getById(1);
$this->assertEquals(0, $task['date_due']);
$this->assertTrue($tm->update(array('id' => 1, 'date_due' => '2014-11-24')));
$task = $tf->getById(1);
$this->assertEquals('2014-11-24', date('Y-m-d', $task['date_due']));
$this->assertTrue($tm->update(array('id' => 1, 'date_due' => time())));
$task = $tf->getById(1);
$this->assertEquals(date('Y-m-d'), date('Y-m-d', $task['date_due']));
}
public function testChangeStartedDate()
{
$p = new Project($this->container);
$tc = new TaskCreation($this->container);
$tm = new TaskModification($this->container);
$tf = new TaskFinder($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
$task = $tf->getById(1);
$this->assertEquals(0, $task['date_started']);
$this->assertTrue($tm->update(array('id' => 1, 'date_started' => '2014-11-24')));
$task = $tf->getById(1);
$this->assertEquals('2014-11-24', date('Y-m-d', $task['date_started']));
$this->assertTrue($tm->update(array('id' => 1, 'date_started' => time())));
$task = $tf->getById(1);
$this->assertEquals(date('Y-m-d'), date('Y-m-d', $task['date_started']));
}
public function testChangeTimeEstimated()
{
$p = new Project($this->container);
$tc = new TaskCreation($this->container);
$tm = new TaskModification($this->container);
$tf = new TaskFinder($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
$task = $tf->getById(1);
$this->assertEquals(0, $task['time_estimated']);
$this->assertTrue($tm->update(array('id' => 1, 'time_estimated' => 13.3)));
$task = $tf->getById(1);
$this->assertEquals(13.3, $task['time_estimated']);
}
public function testChangeTimeSpent()
{
$p = new Project($this->container);
$tc = new TaskCreation($this->container);
$tm = new TaskModification($this->container);
$tf = new TaskFinder($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
$task = $tf->getById(1);
$this->assertEquals(0, $task['time_spent']);
$this->assertTrue($tm->update(array('id' => 1, 'time_spent' => 13.3)));
$task = $tf->getById(1);
$this->assertEquals(13.3, $task['time_spent']);
}
}

View File

@ -146,27 +146,4 @@ class TaskTest extends Base
$this->assertNotEmpty($task);
$this->assertEquals(0, $task['owner_id']);
}
public function testEvents()
{
$t = new Task($this->container);
$tc = new TaskCreation($this->container);
$ts = new TaskStatus($this->container);
$p = new Project($this->container);
// We create a project
$this->assertEquals(1, $p->create(array('name' => 'test')));
// We create task
$this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1)));
// We update a task
$this->assertTrue($t->update(array('title' => 'test2', 'id' => 1)));
$this->assertTrue($this->container['event']->isEventTriggered(Task::EVENT_UPDATE));
$this->assertTrue($this->container['event']->isEventTriggered(Task::EVENT_CREATE_UPDATE));
// We change the assignee
$this->assertTrue($t->update(array('owner_id' => 1, 'id' => 1)));
$this->assertTrue($this->container['event']->isEventTriggered(Task::EVENT_ASSIGNEE_CHANGE));
}
}

View File

@ -3,7 +3,7 @@
require_once __DIR__.'/Base.php';
use Model\SubTask;
use Model\Task;
use Model\TaskModification;
use Model\TaskCreation;
use Model\TaskFinder;
use Model\Project;
@ -13,7 +13,7 @@ class TimeTrackingTest extends Base
{
public function testCalculateTime()
{
$t = new Task($this->container);
$tm = new TaskModification($this->container);
$tc = new TaskCreation($this->container);
$tf = new TaskFinder($this->container);
$p = new Project($this->container);
@ -22,7 +22,7 @@ class TimeTrackingTest extends Base
$this->assertEquals(1, $p->create(array('name' => 'Project #1')));
$this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1, 'time_estimated' => 4.5)));
$this->assertTrue($t->update(array('id' => 1, 'time_spent' => 3.5)));
$this->assertTrue($tm->update(array('id' => 1, 'time_spent' => 3.5)));
$task = $tf->getById(1);
$this->assertNotEmpty($task);