1<?php 2 3namespace dokuwiki\plugin\bez\mdl; 4 5class TaskFactory extends Factory { 6 7 public function get_table_view() { 8 return 'task_view'; 9 } 10 11 public function get_years_scope() { 12 $r = $this->model->sqlite->query('SELECT 13 MIN(create_date), 14 MIN(plan_date), 15 DATE(), 16 MAX(close_date), 17 MAX(plan_date) 18 FROM task'); 19 $data = $this->model->sqlite->res_fetch_array($r); 20 21 $min_date = min($data[0], $data[1], $data[2]); 22 $max_date = max($data[2], $data[3], $data[4]); 23 24 //get only year 25 $first = (int) substr($min_date, 0, strpos($min_date, '-')); 26 $last = (int) substr($max_date, 0, strpos($max_date, '-')); 27 28 $years = array(); 29 for ($year = $first; $year <= $last; $year++) { 30 $years[] = (string) $year; 31 } 32 return $years; 33 } 34 35 public function get_from_thread(Thread $thread) { 36 $tasks = $this->model->taskFactory->get_all(array('thread_id' => $thread->id), 37 'thread_comment_id', false, array('thread' => $thread)); 38 $by_thread_comment = array('corrections' => array()); 39 foreach ($tasks as $task) { 40 if ($task->thread_comment_id == null) { 41 $by_thread_comment['corrections'][$task->id] = $task; 42 continue; 43 } 44 if (!isset($by_thread_comment[$task->thread_comment_id])) { 45 $by_thread_comment[$task->thread_comment_id] = array(); 46 } 47 $by_thread_comment[$task->thread_comment_id][$task->id] = $task; 48 } 49 return $by_thread_comment; 50 } 51 52 public function get_by_type($thread) { 53 $tasks = $this->model->taskFactory->get_all(array('thread_id' => $thread->id), 54 'thread_comment_id', false, array('thread' => $thread)); 55 56 $by_type = array('correction' => array(), 'corrective' => array(), 'preventive' => array()); 57 foreach ($tasks as $task) { 58 $by_type[$task->type][$task->id] = $task; 59 } 60 61 return $by_type; 62 } 63 64 public function users_involvement(\DatePeriod $period=NULL) { 65 if ($period) { 66 $from = $period->getStartDate()->format(\DateTime::ISO8601); 67 $to = $period->getEndDate()->format(\DateTime::ISO8601); 68 69 $sql = "SELECT task_participant.user_id, 70 SUM(task_participant.original_poster) AS original_poster_sum, 71 SUM(task_participant.assignee) AS assignee_sum, 72 SUM(task_participant.commentator) AS commentator_sum 73 FROM task_participant JOIN task ON task_participant.task_id = task.id 74 WHERE task.create_date BETWEEN ? AND ? 75 GROUP BY user_id 76 ORDER BY user_id"; 77 $r = $this->model->sqlite->query($sql, $from, $to); 78 } else { 79 $sql = "SELECT user_id, 80 SUM(original_poster) AS original_poster_sum, 81 SUM(assignee) AS assignee_sum, 82 SUM(commentator) AS commentator_sum 83 FROM task_participant 84 GROUP BY user_id 85 ORDER BY user_id"; 86 $r = $this->model->sqlite->query($sql); 87 } 88 89 return $r; 90 } 91 92 public function report(\DatePeriod $period=NULL) { 93 if ($period) { 94 $from = $period->getStartDate()->format(\DateTime::ISO8601); 95 $to = $period->getEndDate()->format(\DateTime::ISO8601); 96 97 $sql = "SELECT task_program_name, 98 COUNT(CASE WHEN state = 'opened' THEN 1 END) AS opened, 99 COUNT(CASE WHEN state = 'done' AND close_date <= plan_date THEN 1 END) 100 AS closed_on_time, 101 COUNT(CASE WHEN state = 'done' AND close_date > plan_date THEN 1 END) 102 AS closed_after_the_dedline, 103 SUM(cost) AS total_cost, 104 (CASE WHEN state = 'done' THEN 105 SUM(cost) 106 END) AS cost_of_closed, 107 COUNT(*) AS 'count_all' 108 FROM task_view 109 WHERE create_date BETWEEN ? AND ? 110 GROUP BY task_program_name 111 ORDER BY task_program_name"; 112 $r = $this->model->sqlite->query($sql, $from, $to); 113 } else { 114 $sql = "SELECT task_program_name, 115 COUNT(CASE WHEN state = 'opened' THEN 1 END) AS opened, 116 COUNT(CASE WHEN state = 'done' AND close_date <= plan_date THEN 1 END) 117 AS closed_on_time, 118 COUNT(CASE WHEN state = 'done' AND close_date > plan_date THEN 1 END) 119 AS closed_after_the_dedline, 120 SUM(cost) AS total_cost, 121 (CASE WHEN state = 'done' THEN 122 SUM(cost) 123 END) AS cost_of_closed, 124 COUNT(*) AS 'count_all' 125 FROM task_view 126 GROUP BY task_program_name 127 ORDER BY task_program_name"; 128 $r = $this->model->sqlite->query($sql); 129 } 130 131 return $r; 132 } 133 134 public function initial_save(Entity $task, $data) { 135 try { 136 $this->beginTransaction(); 137 parent::initial_save($task, $data); 138 139 $task->set_participant_flags($task->original_poster, array('subscribent', 'original_poster')); 140 $task->set_participant_flags($task->assignee, array('subscribent', 'assignee')); 141 142 if ($task->thread_id != '') { 143 $task->thread->set_participant_flags($task->assignee, array('subscribent', 'task_assignee')); 144 $task->thread->update_last_activity(); 145 } 146 147 $this->commitTransaction(); 148 149 //notifications 150 if ($this->model->user_nick != $task->assignee) { 151 $task->mail_notify_assignee(); 152 } 153 if ($task->thread_id != '') { 154 $task->thread->mail_notify_task_added($task); 155 } 156 } catch(Exception $exception) { 157 $this->rollbackTransaction(); 158 } 159 } 160 161 public function update_save(Entity $task, $data) { 162 try { 163 $this->beginTransaction(); 164 $prev_assignee = $task->assignee; 165 parent::update_save($task, $data); 166 167 if($task->assignee != $prev_assignee) { 168 $task->remove_participant_flags($prev_assignee, array('assignee')); 169 $task->set_participant_flags($task->assignee, array('subscribent', 'assignee')); 170 } 171 172 if ($task->thread_id != '' && $task->assignee != $prev_assignee) { 173 if ($this->model->taskFactory->count(array( 174 'thread_id' => $task->thread_id, 175 'assignee' => $prev_assignee)) == 0) { 176 $task->thread->remove_participant_flags($prev_assignee, array('task_assignee')); 177 } 178 $task->thread->set_participant_flags($task->assignee, array('subscribent', 'task_assignee')); 179 } 180 181 $this->commitTransaction(); 182 //notifications 183 if ($prev_assignee != $task->assignee && $this->model->user_nick != $task->assignee) { 184 $task->mail_notify_assignee(); 185 } 186 } catch(Exception $exception) { 187 $this->rollbackTransaction(); 188 } 189 } 190}