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_with_closing_comment($thread) { 53 $sql = "SELECT task.id, task.type, task.content_html, task.state, task.cost, task.plan_date, task.close_date, 54 task_comment.content_html AS task_comment_content_html, task.assignee 55 FROM task LEFT JOIN task_comment ON task.id = task_comment.task_id 56 WHERE task.thread_id = ? 57 GROUP BY task.id 58 ORDER BY task_comment.id, task.plan_date"; 59 $stmt = $this->model->sqlite->query($sql, $thread->id); 60 $stmt->setFetchMode(\PDO::FETCH_OBJ); 61 62 return $stmt; 63 } 64 65 public function get_by_type($thread) { 66 $stmt = $this->get_with_closing_comment($thread); 67 68 $by_type = array('correction' => array(), 'corrective' => array(), 'preventive' => array()); 69 foreach ($stmt as $task) { 70 $by_type[$task->type][$task->id] = $task; 71 } 72 73 return $by_type; 74 } 75 76 public function users_involvement(\DatePeriod $period=NULL) { 77 if ($period) { 78 $from = $period->getStartDate()->format(\DateTime::ISO8601); 79 $to = $period->getEndDate()->format(\DateTime::ISO8601); 80 81 $sql = "SELECT task_participant.user_id, 82 SUM(task_participant.original_poster) AS original_poster_sum, 83 SUM(task_participant.assignee) AS assignee_sum, 84 SUM(task_participant.commentator) AS commentator_sum 85 FROM task_participant JOIN task ON task_participant.task_id = task.id 86 WHERE task.create_date BETWEEN ? AND ? 87 GROUP BY user_id 88 ORDER BY user_id"; 89 $r = $this->model->sqlite->query($sql, $from, $to); 90 } else { 91 $sql = "SELECT user_id, 92 SUM(original_poster) AS original_poster_sum, 93 SUM(assignee) AS assignee_sum, 94 SUM(commentator) AS commentator_sum 95 FROM task_participant 96 GROUP BY user_id 97 ORDER BY user_id"; 98 $r = $this->model->sqlite->query($sql); 99 } 100 101 return $r; 102 } 103 104 public function report(\DatePeriod $period=NULL) { 105 if ($period) { 106 $from = $period->getStartDate()->format(\DateTime::ISO8601); 107 $to = $period->getEndDate()->format(\DateTime::ISO8601); 108 109 $sql = "SELECT task_program_name, 110 COUNT(CASE WHEN state = 'opened' THEN 1 END) AS opened, 111 COUNT(CASE WHEN state = 'done' AND close_date <= plan_date THEN 1 END) 112 AS closed_on_time, 113 COUNT(CASE WHEN state = 'done' AND close_date > plan_date THEN 1 END) 114 AS closed_after_the_dedline, 115 SUM(cost) AS total_cost, 116 (CASE WHEN state = 'done' THEN 117 SUM(cost) 118 END) AS cost_of_closed, 119 COUNT(*) AS 'count_all' 120 FROM task_view 121 WHERE create_date BETWEEN ? AND ? 122 GROUP BY task_program_name 123 ORDER BY task_program_name"; 124 $r = $this->model->sqlite->query($sql, $from, $to); 125 } else { 126 $sql = "SELECT task_program_name, 127 COUNT(CASE WHEN state = 'opened' THEN 1 END) AS opened, 128 COUNT(CASE WHEN state = 'done' AND close_date <= plan_date THEN 1 END) 129 AS closed_on_time, 130 COUNT(CASE WHEN state = 'done' AND close_date > plan_date THEN 1 END) 131 AS closed_after_the_dedline, 132 SUM(cost) AS total_cost, 133 (CASE WHEN state = 'done' THEN 134 SUM(cost) 135 END) AS cost_of_closed, 136 COUNT(*) AS 'count_all' 137 FROM task_view 138 GROUP BY task_program_name 139 ORDER BY task_program_name"; 140 $r = $this->model->sqlite->query($sql); 141 } 142 143 return $r; 144 } 145 146 public function initial_save(Entity $task, $data) { 147 try { 148 $this->beginTransaction(); 149 parent::initial_save($task, $data); 150 151 $task->set_participant_flags($task->original_poster, array('subscribent', 'original_poster')); 152 $task->set_participant_flags($task->assignee, array('subscribent', 'assignee')); 153 154 if ($task->thread_id != '') { 155 $task->thread->set_participant_flags($task->assignee, array('subscribent', 'task_assignee')); 156 $task->thread->update_last_activity(); 157 } 158 159 $this->commitTransaction(); 160 161 //notifications 162 if ($this->model->user_nick != $task->assignee) { 163 $task->mail_notify_assignee(); 164 } 165 if ($task->thread_id != '') { 166 $task->thread->mail_notify_task_added($task); 167 } 168 } catch(Exception $exception) { 169 $this->rollbackTransaction(); 170 } 171 } 172 173 public function update_save(Entity $task, $data) { 174 try { 175 $this->beginTransaction(); 176 $prev_assignee = $task->assignee; 177 parent::update_save($task, $data); 178 179 if($task->assignee != $prev_assignee) { 180 $task->remove_participant_flags($prev_assignee, array('assignee')); 181 $task->set_participant_flags($task->assignee, array('subscribent', 'assignee')); 182 } 183 184 if ($task->thread_id != '' && $task->assignee != $prev_assignee) { 185 if ($this->model->taskFactory->count(array( 186 'thread_id' => $task->thread_id, 187 'assignee' => $prev_assignee)) == 0) { 188 $task->thread->remove_participant_flags($prev_assignee, array('task_assignee')); 189 } 190 $task->thread->set_participant_flags($task->assignee, array('subscribent', 'task_assignee')); 191 } 192 193 $this->commitTransaction(); 194 //notifications 195 if ($prev_assignee != $task->assignee && $this->model->user_nick != $task->assignee) { 196 $task->mail_notify_assignee(); 197 } 198 } catch(Exception $exception) { 199 $this->rollbackTransaction(); 200 } 201 } 202}