xref: /plugin/bez/mdl/TaskFactory.php (revision 522c019c0bd8bcd6e522fddaa2cf94cce8e0780d)
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($range=array()) {
65        if (count($range) > 0) {
66            $from = date('Y-m-d', strtotime($range[0]));
67            if (count($range) == 1) {
68                $to = date('Y-m-d');
69            } else {
70                $to = date('Y-m-d', strtotime($range[1]));
71            }
72            $sql = "SELECT task_participant.user_id,
73                       SUM(task_participant.original_poster) AS original_poster_sum,
74                       SUM(task_participant.assignee) AS assignee_sum,
75                       SUM(task_participant.commentator) AS commentator_sum
76                       FROM task_participant JOIN task ON task_participant.task_id = task.id
77                       WHERE task.create_date BETWEEN ? AND ?
78                       GROUP BY user_id
79                       ORDER BY user_id";
80            $r = $this->model->sqlite->query($sql, $from, $to);
81        } else {
82            $sql = "SELECT user_id,
83                       SUM(original_poster) AS original_poster_sum,
84                       SUM(assignee) AS assignee_sum,
85                       SUM(commentator) AS commentator_sum
86                       FROM task_participant
87                       GROUP BY user_id
88                       ORDER BY user_id";
89            $r = $this->model->sqlite->query($sql);
90        }
91
92        return $r;
93    }
94
95    public function initial_save(Entity $task, $data) {
96        try {
97            $this->beginTransaction();
98            parent::initial_save($task, $data);
99
100            $task->set_participant_flags($task->original_poster, array('subscribent', 'original_poster'));
101            $task->set_participant_flags($task->assignee, array('subscribent', 'assignee'));
102
103            if ($task->thread_id != '') {
104                $task->thread->set_participant_flags($task->assignee, array('subscribent', 'task_assignee'));
105                $task->thread->update_last_activity();
106            }
107
108            $this->commitTransaction();
109
110            //notifications
111            if ($this->model->user_nick != $task->assignee) {
112                $task->mail_notify_assignee();
113            }
114            if ($task->thread_id != '') {
115                $task->thread->mail_notify_task_added($task);
116            }
117        } catch(Exception $exception) {
118            $this->rollbackTransaction();
119        }
120    }
121
122    public function update_save(Entity $task, $data) {
123        try {
124            $this->beginTransaction();
125            $prev_assignee = $task->assignee;
126            parent::update_save($task, $data);
127
128            if($task->assignee != $prev_assignee) {
129                $task->remove_participant_flags($prev_assignee, array('assignee'));
130                $task->set_participant_flags($task->assignee, array('subscribent', 'assignee'));
131            }
132
133            if ($task->thread_id != '' && $task->assignee != $prev_assignee) {
134                if ($this->model->taskFactory->count(array(
135                    'thread_id' => $task->thread_id,
136                    'assignee'  => $prev_assignee)) == 0) {
137                    $task->thread->remove_participant_flags($prev_assignee, array('task_assignee'));
138                }
139                $task->thread->set_participant_flags($task->assignee, array('subscribent', 'task_assignee'));
140            }
141
142            $this->commitTransaction();
143            //notifications
144            if ($prev_assignee != $task->assignee && $this->model->user_nick != $task->assignee) {
145                $task->mail_notify_assignee();
146            }
147        } catch(Exception $exception) {
148            $this->rollbackTransaction();
149        }
150    }
151}