Add a method to apply filters for tasks

This commit is contained in:
Frédéric Guillot 2014-04-21 22:44:25 -04:00
parent 9bfd824ab7
commit 919e5d51a4
5 changed files with 76 additions and 58 deletions

View File

@ -42,12 +42,18 @@ class Project extends Base
$this->checkProjectPermissions($project['id']);
$tasks = $this->task->getAllByProjectId($project_id, array(0));
$filters = array(
array('column' => 'project_id', 'operator' => 'eq', 'value' => $project_id),
array('column' => 'is_active', 'operator' => 'eq', 'value' => \Model\Task::STATUS_CLOSED),
);
$tasks = $this->task->find($filters);
$nb_tasks = count($tasks);
$this->response->html($this->template->layout('project_tasks', array(
'menu' => 'projects',
'project' => $project,
'columns' => $this->board->getColumnsList($project_id),
'tasks' => $tasks,
'nb_tasks' => $nb_tasks,
'title' => $project['name'].' ('.$nb_tasks.')'

View File

@ -181,10 +181,24 @@ class Board extends Base
$this->db->startTransaction();
$columns = $this->getColumns($project_id);
$filters = array(
array('column' => 'project_id', 'operator' => 'eq', 'value' => $project_id),
array('column' => 'is_active', 'operator' => 'eq', 'value' => Task::STATUS_OPEN),
);
$taskModel = new Task($this->db, $this->event);
$tasks = $taskModel->find($filters);
foreach ($columns as &$column) {
$column['tasks'] = $taskModel->getAllByColumnId($project_id, $column['id'], array(1));
$column['tasks'] = array();
foreach ($tasks as &$task) {
if ($task['column_id'] == $column['id']) {
$column['tasks'][] = $task;
}
}
}
$this->db->closeTransaction();

View File

@ -3,7 +3,6 @@
namespace Model;
require_once __DIR__.'/base.php';
require_once __DIR__.'/comment.php';
use \SimpleValidator\Validator;
use \SimpleValidator\Validators;
@ -106,42 +105,6 @@ class Task extends Base
}
}
/**
* Get all tasks for a given project
*
* @access public
* @param integer $project_id Project id
* @param array $status List of status id
* @return array
*/
public function getAllByProjectId($project_id, array $status = array(self::STATUS_OPEN, self::STATUS_CLOSED))
{
return $this->db->table(self::TABLE)
->columns(
self::TABLE.'.id',
self::TABLE.'.title',
self::TABLE.'.description',
self::TABLE.'.date_creation',
self::TABLE.'.date_completed',
self::TABLE.'.date_due',
self::TABLE.'.color_id',
self::TABLE.'.project_id',
self::TABLE.'.column_id',
self::TABLE.'.owner_id',
self::TABLE.'.position',
self::TABLE.'.is_active',
self::TABLE.'.score',
Board::TABLE.'.title AS column_title',
User::TABLE.'.username'
)
->join(Board::TABLE, 'id', 'column_id')
->join(User::TABLE, 'id', 'owner_id')
->eq(self::TABLE.'.project_id', $project_id)
->in('is_active', $status)
->desc('date_completed')
->findAll();
}
/**
* Count all tasks for a given project and status
*
@ -160,33 +123,39 @@ class Task extends Base
}
/**
* Get all tasks for a given column
* Get tasks that match defined filters
*
* @access public
* @param integer $project_id Project id
* @param integer $column_id Column id
* @param array $status List of status id
* @param array $filters Filters: [ ['column' => '...', 'operator' => '...', 'value' => '...'], ... ]
* @return array
*/
public function getAllByColumnId($project_id, $column_id, array $status = array(self::STATUS_OPEN))
public function find(array $filters)
{
$tasks = $this->db
$table = $this->db
->table(self::TABLE)
->columns('tasks.id', 'title', 'color_id', 'project_id', 'owner_id', 'column_id', 'position', 'score', 'date_due', 'users.username')
->join('users', 'id', 'owner_id')
->eq('project_id', $project_id)
->eq('column_id', $column_id)
->in('is_active', $status)
->asc('position')
->findAll();
->columns(
'(SELECT count(*) FROM comments WHERE task_id=tasks.id) AS nb_comments',
'tasks.id',
'tasks.title',
'tasks.date_creation',
'tasks.date_completed',
'tasks.date_due',
'tasks.color_id',
'tasks.project_id',
'tasks.column_id',
'tasks.owner_id',
'tasks.position',
'tasks.is_active',
'tasks.score',
'users.username'
)
->join('users', 'id', 'owner_id');
$commentModel = new Comment($this->db, $this->event);
foreach ($tasks as &$task) {
$task['nb_comments'] = $commentModel->count($task['id']);
foreach ($filters as $filter) {
$table->$filter['operator']($filter['column'], $filter['value']);
}
return $tasks;
return $table->findAll();
}
/**

View File

@ -28,7 +28,7 @@
<a href="?controller=task&amp;action=show&amp;task_id=<?= $task['id'] ?>" title="<?= t('View this task') ?>"><?= Helper\escape($task['id']) ?></a>
</td>
<td>
<?= Helper\escape($task['column_title']) ?>
<?= Helper\in_list($task['column_id'], $columns) ?>
</td>
<td>
<a href="?controller=task&amp;action=show&amp;task_id=<?= $task['id'] ?>" title="<?= t('View this task') ?>"><?= Helper\escape($task['title']) ?></a>

View File

@ -7,6 +7,35 @@ use Model\Project;
class TaskTest extends Base
{
public function testFilter()
{
$t = new Task($this->db, $this->event);
$p = new Project($this->db, $this->event);
$this->assertEquals(1, $p->create(array('name' => 'test1')));
$this->assertEquals(1, $t->create(array('title' => 'test a', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1)));
$this->assertEquals(2, $t->create(array('title' => 'test b', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 2)));
$tasks = $t->find(array(array('column' => 'project_id', 'operator' => 'eq', 'value' => '1')));
$this->assertEquals(2, count($tasks));
$this->assertEquals(1, $tasks[0]['id']);
$this->assertEquals(2, $tasks[1]['id']);
$tasks = $t->find(array(
array('column' => 'project_id', 'operator' => 'eq', 'value' => '1'),
array('column' => 'owner_id', 'operator' => 'eq', 'value' => '2'),
));
$this->assertEquals(1, count($tasks));
$this->assertEquals(2, $tasks[0]['id']);
$tasks = $t->find(array(
array('column' => 'project_id', 'operator' => 'eq', 'value' => '1'),
array('column' => 'title', 'operator' => 'like', 'value' => '%b%'),
));
$this->assertEquals(1, count($tasks));
$this->assertEquals(2, $tasks[0]['id']);
}
public function testDateFormat()
{
$t = new Task($this->db, $this->event);