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