2012-02-05 18:37:25 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class DAO_Tasks
|
|
|
|
extends DAO
|
2016-01-02 11:13:51 +01:00
|
|
|
implements PackageAware
|
2012-02-05 18:37:25 +01:00
|
|
|
{
|
|
|
|
private static $priorities = array(
|
|
|
|
'1' => 'Lowest' ,
|
|
|
|
'2' => 'Low' ,
|
|
|
|
'3' => 'Normal' ,
|
|
|
|
'4' => 'High' ,
|
|
|
|
'5' => 'Very high' ,
|
|
|
|
);
|
|
|
|
|
2016-01-02 11:13:51 +01:00
|
|
|
private $package;
|
|
|
|
|
|
|
|
public function setPackage( Package $package )
|
|
|
|
{
|
|
|
|
if ( $this->package !== null ) {
|
|
|
|
throw new Exception( 'trying to call setPackage() twice' );
|
|
|
|
}
|
|
|
|
$this->package = $package;
|
|
|
|
}
|
|
|
|
|
2012-02-05 18:37:25 +01:00
|
|
|
|
|
|
|
public function translatePriority( $value )
|
|
|
|
{
|
|
|
|
return DAO_Tasks::$priorities[ "$value" ];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getAllTasks( )
|
|
|
|
{
|
|
|
|
return $this->query(
|
2012-02-06 08:55:33 +01:00
|
|
|
'SELECT * FROM tasks_list '
|
2012-02-05 22:22:16 +01:00
|
|
|
. 'ORDER BY ( CASE '
|
2012-02-06 08:55:33 +01:00
|
|
|
. 'WHEN completed_at IS NULL THEN '
|
|
|
|
. 'priority '
|
2012-02-05 22:22:16 +01:00
|
|
|
. 'ELSE '
|
|
|
|
. '-1 '
|
2012-02-15 10:04:11 +01:00
|
|
|
. 'END ) DESC , badness , added_at DESC' )->execute( );
|
2012-02-05 18:37:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllActiveTasks( )
|
|
|
|
{
|
|
|
|
return $this->query(
|
2012-02-06 08:55:33 +01:00
|
|
|
'SELECT * FROM tasks_list '
|
2012-02-15 10:04:11 +01:00
|
|
|
. 'WHERE completed_at IS NULL AND badness = 0 '
|
2012-02-06 08:55:33 +01:00
|
|
|
. 'ORDER BY priority DESC , added_at DESC' )->execute( );
|
2012-02-05 18:37:25 +01:00
|
|
|
}
|
|
|
|
|
2012-02-05 22:22:16 +01:00
|
|
|
public function getAllBlockedTasks( )
|
|
|
|
{
|
|
|
|
return $this->query(
|
2012-02-06 08:55:33 +01:00
|
|
|
'SELECT * FROM tasks_list '
|
2012-02-15 10:04:11 +01:00
|
|
|
. 'WHERE badness <> 0 '
|
|
|
|
. 'ORDER BY priority DESC , badness , added_at DESC' )->execute( );
|
2012-02-05 22:22:16 +01:00
|
|
|
}
|
|
|
|
|
2012-02-05 18:37:25 +01:00
|
|
|
|
|
|
|
public function getTasksAt( Data_Item $item )
|
|
|
|
{
|
|
|
|
return $this->query(
|
2012-02-06 08:55:33 +01:00
|
|
|
'SELECT * FROM tasks_list '
|
2012-02-15 10:04:11 +01:00
|
|
|
. 'WHERE item = $1 AND parent_task IS NULL '
|
2012-02-05 22:22:16 +01:00
|
|
|
. 'ORDER BY ( CASE '
|
2012-02-06 08:55:33 +01:00
|
|
|
. 'WHEN completed_at IS NULL THEN '
|
|
|
|
. 'priority '
|
2012-02-05 22:22:16 +01:00
|
|
|
. 'ELSE '
|
|
|
|
. '-1 '
|
2012-02-15 10:04:11 +01:00
|
|
|
. 'END ) DESC , badness , added_at DESC'
|
2012-02-06 08:55:33 +01:00
|
|
|
)->execute( $item->id );
|
2012-02-05 18:37:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-06 18:12:07 +01:00
|
|
|
public function getUserTasks( $user )
|
|
|
|
{
|
|
|
|
return $this->query(
|
|
|
|
'SELECT * FROM tasks_list '
|
|
|
|
. 'WHERE assigned_to_id = $1 '
|
2012-02-15 10:04:11 +01:00
|
|
|
. 'ORDER BY priority DESC , badness , added_at DESC'
|
2012-02-06 18:12:07 +01:00
|
|
|
)->execute( $user->user_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-05 18:37:25 +01:00
|
|
|
public function addTask( $item , $title , $priority , $description )
|
|
|
|
{
|
|
|
|
$result = $this->query( 'SELECT add_task( $1 , $2 , $3 , $4 , $5 ) AS error' )
|
|
|
|
->execute( $item , $title , $description , $priority , $_SESSION[ 'uid' ] );
|
|
|
|
return $result[0]->error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-09 18:50:54 +01:00
|
|
|
public function addNestedTask( $parent , $title , $priority , $description )
|
|
|
|
{
|
|
|
|
$result = $this->query( 'SELECT tasks_add_nested( $1 , $2 , $3 , $4 , $5 ) AS error' )
|
|
|
|
->execute( $parent , $title , $description , $priority , $_SESSION[ 'uid' ] );
|
|
|
|
return $result[0]->error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-05 18:37:25 +01:00
|
|
|
public function get( $id )
|
|
|
|
{
|
2012-02-09 18:50:54 +01:00
|
|
|
$result = $this->query( 'SELECT * FROM tasks_single_view WHERE id = $1' )->execute( $id );
|
2012-02-05 18:37:25 +01:00
|
|
|
if ( empty( $result ) ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$task = $result[ 0 ];
|
|
|
|
$task->notes = $this->query(
|
2012-02-06 08:55:33 +01:00
|
|
|
'SELECT n.note_id AS id , n.user_id AS uid , u.user_view_name AS author , '
|
2012-02-05 18:37:25 +01:00
|
|
|
. 'n.note_added AS added_at , n.note_text AS "text" '
|
|
|
|
. 'FROM notes n '
|
2012-02-06 08:55:33 +01:00
|
|
|
. 'INNER JOIN users_view u USING (user_id) '
|
2012-02-05 18:37:25 +01:00
|
|
|
. 'WHERE n.task_id = $1 '
|
|
|
|
. 'ORDER BY n.note_added DESC' )->execute( $id );
|
2012-02-09 18:50:54 +01:00
|
|
|
$task->subtasks = $this->query(
|
|
|
|
'SELECT * FROM tasks_list '
|
|
|
|
. 'WHERE parent_task = $1 '
|
|
|
|
. 'ORDER BY ( CASE '
|
|
|
|
. 'WHEN completed_at IS NULL THEN '
|
|
|
|
. 'priority '
|
|
|
|
. 'ELSE '
|
|
|
|
. '-1 '
|
2012-02-15 10:04:11 +01:00
|
|
|
. 'END ) DESC , badness , added_at DESC'
|
2012-02-09 18:50:54 +01:00
|
|
|
)->execute( $id );
|
|
|
|
$task->moveDownTargets = $this->query(
|
|
|
|
'SELECT * FROM tasks_move_down_targets '
|
|
|
|
. 'WHERE task_id = $1 '
|
|
|
|
. 'ORDER BY target_title' )->execute( $id );
|
2012-02-05 19:59:51 +01:00
|
|
|
$task->dependencies = $this->query(
|
2012-02-15 10:04:11 +01:00
|
|
|
'SELECT t.task_id AS id , t.task_title AS title , t.item_id AS item , '
|
2012-02-05 19:59:51 +01:00
|
|
|
. 'i.item_name AS item_name , '
|
2012-02-06 10:45:39 +01:00
|
|
|
. '( ct.completed_task_time IS NOT NULL ) AS completed , '
|
2012-02-15 10:04:11 +01:00
|
|
|
. 'tl.badness AS missing_dependencies '
|
2012-02-05 19:59:51 +01:00
|
|
|
. 'FROM task_dependencies td '
|
|
|
|
. 'INNER JOIN tasks t ON t.task_id = td.task_id_depends '
|
2012-02-06 10:45:39 +01:00
|
|
|
. 'INNER JOIN tasks_list tl ON tl.id = t.task_id '
|
2012-02-09 18:50:54 +01:00
|
|
|
. 'LEFT OUTER JOIN items i USING ( item_id ) '
|
2012-02-05 19:59:51 +01:00
|
|
|
. 'LEFT OUTER JOIN completed_tasks ct ON ct.task_id = t.task_id '
|
|
|
|
. 'WHERE td.task_id = $1 '
|
2012-02-05 20:55:09 +01:00
|
|
|
. 'ORDER BY i.item_name , t.task_priority DESC , t.task_title' )->execute( $id );
|
2012-02-05 19:59:51 +01:00
|
|
|
$task->reverseDependencies = $this->query(
|
2012-02-15 10:04:11 +01:00
|
|
|
'SELECT t.task_id AS id , t.task_title AS title , t.item_id AS item , '
|
2012-02-05 19:59:51 +01:00
|
|
|
. 'i.item_name AS item_name , '
|
|
|
|
. '( ct.completed_task_time IS NOT NULL ) AS completed '
|
|
|
|
. 'FROM task_dependencies td '
|
|
|
|
. 'INNER JOIN tasks t USING( task_id ) '
|
2012-02-09 18:50:54 +01:00
|
|
|
. 'LEFT OUTER JOIN items i USING ( item_id ) '
|
|
|
|
. 'LEFT OUTER JOIN completed_tasks ct ON t.task_id = ct.task_id '
|
2012-02-05 19:59:51 +01:00
|
|
|
. 'WHERE td.task_id_depends = $1 '
|
2012-02-05 20:55:09 +01:00
|
|
|
. 'ORDER BY i.item_name , t.task_priority DESC , t.task_title' )->execute( $id );
|
|
|
|
$task->possibleDependencies = $this->query(
|
2012-02-15 10:04:11 +01:00
|
|
|
'SELECT t.task_id AS id , t.task_title AS title , t.item_id AS item , '
|
2012-02-05 20:55:09 +01:00
|
|
|
. 'i.item_name AS item_name '
|
|
|
|
. 'FROM tasks_possible_dependencies( $1 ) t '
|
2012-02-09 18:50:54 +01:00
|
|
|
. 'LEFT OUTER JOIN items i USING ( item_id ) '
|
2012-02-05 19:59:51 +01:00
|
|
|
. 'ORDER BY i.item_name , t.task_priority , t.task_title' )->execute( $id );
|
2012-02-15 10:04:11 +01:00
|
|
|
$task->lineage = null;
|
2012-02-05 19:59:51 +01:00
|
|
|
|
2012-02-05 18:37:25 +01:00
|
|
|
return $task;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-15 10:04:11 +01:00
|
|
|
public function getLineage( $task )
|
|
|
|
{
|
|
|
|
if ( ! in_array( 'lineage' , get_object_vars( $task ) ) || $task->lineage === null ) {
|
|
|
|
$result = $this->query(
|
|
|
|
'SELECT task_id , task_title '
|
|
|
|
. 'FROM tasks_tree tt '
|
|
|
|
. 'INNER JOIN tasks '
|
|
|
|
. 'ON task_id = tt.task_id_parent '
|
|
|
|
. 'WHERE task_id_child = $1 AND tt_depth > 0 '
|
|
|
|
. 'ORDER BY tt_depth DESC'
|
|
|
|
)->execute( $task->id );
|
|
|
|
|
|
|
|
$task->lineage = array( );
|
|
|
|
foreach ( $result as $row ) {
|
|
|
|
array_push( $task->lineage , array( $row->task_id , $row->task_title ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $task->lineage;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-05 18:37:25 +01:00
|
|
|
public function canDelete( $task )
|
|
|
|
{
|
|
|
|
if ( $task->completed_by !== null ) {
|
2016-01-02 11:13:51 +01:00
|
|
|
$minDeletionTime = $this->package->config(
|
|
|
|
'minDeletionTime' , 7 * 3600 * 24 );
|
2012-02-05 18:37:25 +01:00
|
|
|
$ts = strtotime( $task->completed_at );
|
2016-01-02 11:00:57 +01:00
|
|
|
return empty( $task->reverseDependencies )
|
2016-01-02 11:13:51 +01:00
|
|
|
&& ( time() - $ts > $minDeletionTime );
|
2012-02-05 18:37:25 +01:00
|
|
|
}
|
2016-01-02 11:00:57 +01:00
|
|
|
return empty( $task->subtasks )
|
|
|
|
&& empty( $task->reverseDependencies )
|
|
|
|
&& ( $task->uid == $_SESSION[ 'uid' ] );
|
2012-02-05 18:37:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-05 19:59:51 +01:00
|
|
|
public function canFinish( $task )
|
|
|
|
{
|
|
|
|
assert( $task->completed_at == null );
|
2012-02-15 10:04:11 +01:00
|
|
|
return ( $task->badness == 0 );
|
2012-02-05 19:59:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function canRestart( $task )
|
|
|
|
{
|
|
|
|
assert( $task->completed_at != null );
|
|
|
|
foreach ( $task->reverseDependencies as $dependency ) {
|
|
|
|
if ( $dependency->completed == 't' ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-02-09 18:50:54 +01:00
|
|
|
|
|
|
|
if ( $task->parent_task === null ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$parent = ( $task->parent_task instanceof StdClass ) ? $task->parent_task : $this->get( $task->parent_task );
|
|
|
|
return $parent->completed_by === null;
|
2012-02-05 19:59:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-05 18:37:25 +01:00
|
|
|
public function delete( $task )
|
|
|
|
{
|
|
|
|
$this->query( 'DELETE FROM tasks WHERE task_id = $1' )->execute( $task );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function finish( $task , $noteText )
|
|
|
|
{
|
|
|
|
$this->query( 'SELECT finish_task( $1 , $2 , $3 )' )
|
|
|
|
->execute( $task , $_SESSION[ 'uid' ] , $noteText );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function restart( $task , $noteText )
|
|
|
|
{
|
|
|
|
$this->query( 'SELECT restart_task( $1 , $2 , $3 )' )
|
|
|
|
->execute( $task , $_SESSION[ 'uid' ] , $noteText );
|
|
|
|
}
|
|
|
|
|
2012-02-06 16:38:11 +01:00
|
|
|
public function updateTask( $id , $item , $title , $priority , $description , $assignee )
|
2012-02-05 18:37:25 +01:00
|
|
|
{
|
2012-02-06 16:38:11 +01:00
|
|
|
$result = $this->query( 'SELECT update_task( $1 , $2 , $3 , $4 , $5 , $6 ) AS error' )
|
|
|
|
->execute( $id , $item , $title , $description , $priority , $assignee );
|
2012-02-05 18:37:25 +01:00
|
|
|
return $result[0]->error;
|
|
|
|
}
|
|
|
|
|
2012-02-09 18:50:54 +01:00
|
|
|
public function updateNestedTask( $id , $title , $priority , $description , $assignee )
|
|
|
|
{
|
|
|
|
$result = $this->query( 'SELECT update_task( $1 , $2 , $3 , $4 , $5 ) AS error' )
|
|
|
|
->execute( $id , $title , $description , $priority , $assignee );
|
|
|
|
return $result[0]->error;
|
|
|
|
}
|
|
|
|
|
2012-02-05 18:37:25 +01:00
|
|
|
public function addNote( $task , $note )
|
|
|
|
{
|
|
|
|
$this->query( 'INSERT INTO notes ( task_id , user_id , note_text ) VALUES ( $1 , $2 , $3 )' )
|
|
|
|
->execute( $task , $_SESSION[ 'uid' ] , $note );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNote( $id )
|
|
|
|
{
|
|
|
|
$query = $this->query(
|
|
|
|
'SELECT n.note_id AS id , n.note_text AS text , n.note_added AS added_at , '
|
|
|
|
. 'n.task_id AS task , '
|
|
|
|
. '( n.user_id = $2 AND t.task_id IS NULL ) AS editable '
|
|
|
|
. 'FROM notes n '
|
|
|
|
. 'LEFT OUTER JOIN completed_tasks t USING (task_id) '
|
|
|
|
. 'WHERE n.note_id = $1' );
|
|
|
|
$result = $query->execute( $id , $_SESSION[ 'uid' ] );
|
|
|
|
if ( empty( $result ) ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$result[ 0 ]->editable = ( $result[ 0 ]->editable == 't' );
|
|
|
|
return $result[ 0 ];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteNote( $id )
|
|
|
|
{
|
|
|
|
$this->query( 'DELETE FROM notes WHERE note_id = $1' )->execute( $id );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function updateNote( $id , $text )
|
|
|
|
{
|
|
|
|
$this->query( 'UPDATE notes SET note_text = $2 , note_added = now( ) WHERE note_id = $1' )
|
|
|
|
->execute( $id , $text );
|
|
|
|
}
|
|
|
|
|
2012-02-05 20:55:09 +01:00
|
|
|
public function addDependency( $id , $dependency )
|
|
|
|
{
|
|
|
|
$result = $this->query( 'SELECT tasks_add_dependency( $1 , $2 ) AS error' )
|
|
|
|
->execute( $id , $dependency );
|
|
|
|
return $result[0]->error;
|
|
|
|
}
|
|
|
|
|
2012-02-05 21:22:06 +01:00
|
|
|
public function deleteDependency( $from , $to )
|
|
|
|
{
|
|
|
|
$this->query( 'DELETE FROM task_dependencies WHERE task_id = $1 AND task_id_depends = $2' )
|
|
|
|
->execute( $from , $to );
|
|
|
|
}
|
|
|
|
|
2012-02-06 16:38:11 +01:00
|
|
|
public function assignTaskTo( $task , $user )
|
|
|
|
{
|
|
|
|
$this->query(
|
|
|
|
'UPDATE tasks _task SET user_id_assigned = $2 '
|
|
|
|
. 'FROM tasks _task2 '
|
|
|
|
. 'LEFT OUTER JOIN completed_tasks _completed '
|
|
|
|
. 'USING ( task_id ) '
|
|
|
|
. 'WHERE _task2.task_id = _task.task_id AND _completed.task_id IS NULL AND _task.task_id = $1'
|
|
|
|
)->execute( $task , $user );
|
|
|
|
}
|
2012-02-09 18:50:54 +01:00
|
|
|
|
|
|
|
public function moveUp( $task , $force = false )
|
|
|
|
{
|
|
|
|
$result = $this->query( 'SELECT tasks_move_up( $1 , $2 ) AS success')
|
|
|
|
->execute( $task->id , $force ? 't' : 'f' );
|
|
|
|
return ( $result[0]->success == 't' );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function moveDown( $task , $sibling , $force )
|
|
|
|
{
|
|
|
|
$result = $this->query( 'SELECT tasks_move_down( $1 , $2 , $3 ) AS success')
|
|
|
|
->execute( $task->id , $sibling , $force ? 't' : 'f' );
|
|
|
|
return ( $result[0]->success == 't' );
|
|
|
|
}
|
2012-02-05 18:37:25 +01:00
|
|
|
}
|