xref: /plugin/bez/mdl/TaskFactory.php (revision 16c7b168a60daa2c9b9ddcfa51e05d123a2a17dc)
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_from_thread(Thread $thread) {
12        $tasks = $this->model->taskFactory->get_all(array('thread_id' => $thread->id),
13                                            'thread_comment_id', false, array('thread' => $thread));
14        $by_thread_comment = array('corrections' => array());
15        foreach ($tasks as $task) {
16            if ($task->thread_comment_id == null) {
17                $by_thread_comment['corrections'][$task->id] = $task;
18                continue;
19            }
20            if (!isset($by_thread_comment[$task->thread_comment_id])) {
21                $by_thread_comment[$task->thread_comment_id] = array();
22            }
23            $by_thread_comment[$task->thread_comment_id][$task->id] = $task;
24        }
25        return $by_thread_comment;
26    }
27
28    public function get_by_type($thread) {
29        $tasks = $this->model->taskFactory->get_all(array('thread_id' => $thread->id),
30                                            'thread_comment_id', false, array('thread' => $thread));
31
32        $by_type = array('correction' => array(), 'corrective' => array(), 'preventive' => array());
33        foreach ($tasks as $task) {
34            $by_type[$task->type][$task->id] = $task;
35        }
36
37        return $by_type;
38    }
39
40    public function users_involvement() {
41        $sql = 'SELECT user_id,
42                       SUM(original_poster),
43                       SUM(assignee),
44                       SUM(commentator),
45                       COUNT(*)
46                       FROM task_participant
47                       GROUP BY user_id
48                       ORDER BY user_id';
49
50        $r = $this->model->sqlite->query($sql);
51        return $r;
52    }
53
54    public function initial_save(Entity $task, $data) {
55        parent::initial_save($task, $data);
56
57        $task->set_data($data);
58        try {
59            $this->beginTransaction();
60            $this->save($task);
61
62            if ($task->thread) {
63                $task->thread->set_participant_flags($task->assignee, array('subscribent', 'task_assignee'));
64                $task->thread->update_last_activity();
65            }
66
67            $this->commitTransaction();
68        } catch(Exception $exception) {
69            $this->rollbackTransaction();
70        }
71
72        //$task->mail_notify_add();
73    }
74
75    public function update_save(Entity $task, $data) {
76        parent::update_save($task, $data);
77
78        $task->set_data($data);
79        try {
80            $this->beginTransaction();
81            $this->save($task);
82
83            if ($task->thread) {
84            }
85
86            $this->commitTransaction();
87        } catch(Exception $exception) {
88            $this->rollbackTransaction();
89        }
90    }
91}