xref: /plugin/bez/mdl/TaskFactory.php (revision 6f38077355681f273cc9db672efab6fff436baea)
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() {
65        $sql = 'SELECT user_id,
66                       SUM(original_poster),
67                       SUM(assignee),
68                       SUM(commentator),
69                       COUNT(*)
70                       FROM task_participant
71                       GROUP BY user_id
72                       ORDER BY user_id';
73
74        $r = $this->model->sqlite->query($sql);
75        return $r;
76    }
77
78    public function initial_save(Entity $task, $data) {
79        try {
80            $this->beginTransaction();
81            parent::initial_save($task, $data);
82
83            $task->set_participant_flags($task->original_poster, array('subscribent', 'original_poster'));
84            $task->set_participant_flags($task->assignee, array('subscribent', 'assignee'));
85
86            if ($task->thread_id != '') {
87                $task->thread->set_participant_flags($task->assignee, array('subscribent', 'task_assignee'));
88                $task->thread->update_last_activity();
89            }
90
91            $this->commitTransaction();
92
93            //notifications
94            if ($this->model->user_nick != $task->assignee) {
95                $task->mail_notify_assignee();
96            }
97            if ($task->thread_id != '') {
98                $task->thread->mail_notify_task_added($task);
99            }
100        } catch(Exception $exception) {
101            $this->rollbackTransaction();
102        }
103    }
104
105    public function update_save(Entity $task, $data) {
106        try {
107            $this->beginTransaction();
108            $prev_assignee = $task->assignee;
109            parent::update_save($task, $data);
110
111            if ($task->thread_id != '' && $task->assignee != $prev_assignee) {
112                if ($this->model->taskFactory->count(array(
113                    'thread_id' => $task->thread_id,
114                    'assignee'  => $prev_assignee)) == 0) {
115                    $task->thread->remove_participant_flags($prev_assignee, array('task_assignee'));
116                }
117                $task->thread->set_participant_flags($task->assignee, array('subscribent', 'task_assignee'));
118            }
119
120            $this->commitTransaction();
121            //notifications
122            if ($prev_assignee != $task->assignee && $this->model->user_nick != $task->assignee) {
123                $task->mail_notify_assignee();
124            }
125        } catch(Exception $exception) {
126            $this->rollbackTransaction();
127        }
128    }
129}