add urgency sort & fix due date

This commit is contained in:
DavidBadura 2015-02-05 23:34:57 +00:00
parent a25b90caba
commit c8cdba7909
2 changed files with 63 additions and 5 deletions

View File

@ -103,7 +103,9 @@ class Taskwarrior
*/ */
public function filterPending($filter = '') public function filterPending($filter = '')
{ {
return $this->filter($filter . ' status:pending'); $tasks = $this->filter($filter . ' status:pending');
return $this->sort($tasks);
} }
/** /**
@ -235,6 +237,8 @@ class Taskwarrior
if ($task->getDue()) { if ($task->getDue()) {
$options[] = 'due:' . $task->getDue()->format('Ymd\THis\Z'); $options[] = 'due:' . $task->getDue()->format('Ymd\THis\Z');
} else {
$options[] = 'due:';
} }
$options[] = $task->getDescription(); $options[] = $task->getDescription();
@ -254,6 +258,23 @@ class Taskwarrior
$this->setValue($task, 'status', $clean->getStatus()); $this->setValue($task, 'status', $clean->getStatus());
} }
/**
* @param Task[] $tasks
* @return Task[]
*/
private function sort(array $tasks)
{
usort($tasks, function (Task $a, Task $b) {
if(0 != $diff = $b->getUrgency() - $a->getUrgency()) {
return $diff;
}
return $a->getEntry() >= $b->getEntry() ? 1 : -1;
});
return $tasks;
}
/** /**
* @return ProcessBuilder * @return ProcessBuilder
*/ */

View File

@ -217,19 +217,56 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase
$task3 = $this->taskwarrior->find($task1->getUuid()); $task3 = $this->taskwarrior->find($task1->getUuid());
$this->assertEquals($newDate, $task3->getDue()); $this->assertEquals($newDate, $task3->getDue());
$task2->setDue(null);
$this->taskwarrior->save($task2);
$this->taskwarrior->clear();
$task3 = $this->taskwarrior->find($task1->getUuid());
$this->assertNull($task3->getDue());
} }
public function testUrgency() public function testUrgency()
{ {
$date = $this->createDateTime('1989-01-08 11:12:13');
$task1 = new Task(); $task1 = new Task();
$task1->setDescription('foo1'); $task1->setDescription('foo1');
$task1->setDue($date); $task1->setDue($this->createDateTime('1989-01-08 11:12:13'));
$this->taskwarrior->save($task1); $this->taskwarrior->save($task1);
$this->assertEquals(12, $task1->getUrgency()); $this->assertEquals(12, $task1->getUrgency());
$task1->setDue(null);
$this->taskwarrior->save($task1);
$this->assertEquals(0, $task1->getUrgency());
}
public function testUrgencySort()
{
$task1 = new Task();
$task1->setDescription('foo1');
$task2 = new Task();
$task2->setDescription('foo2');
$task2->setDue($this->createDateTime('1989-01-08 11:12:13'));
$task3 = new Task();
$task3->setDescription('foo3');
$this->taskwarrior->save($task1);
$this->taskwarrior->save($task2);
$this->taskwarrior->save($task3);
$this->assertEquals(0, $task1->getUrgency());
$this->assertEquals(12, $task2->getUrgency());
$this->assertEquals(0, $task3->getUrgency());
$tasks = $this->taskwarrior->filterPending();
$this->assertEquals(array($task2, $task1, $task3), $tasks);
} }
/** /**