xref: /plugin/bez/mdl/ThreadFactory.php (revision eb2e6be9c74b2df263bd0a7ed247ee70c3d7cbd1)
1<?php
2
3namespace dokuwiki\plugin\bez\mdl;
4
5use Assetic\Exception\Exception;
6
7class ThreadFactory extends Factory {
8
9    public function get_table_view() {
10        return 'thread_view';
11    }
12
13    public function get_years_scope() {
14        $r = $this->model->sqlite->query('SELECT create_date FROM thread ORDER BY id LIMIT 1');
15        $date = $this->model->sqlite->res2single($r);
16
17        //get only year
18		$first =  (int) substr($date, 0, strpos($date, '-'));
19        $last = (int) date('Y');
20
21		$years = array();
22		for ($year = $first; $year <= $last; $year++) {
23			$years[] = (string) $year;
24        }
25		return $years;
26    }
27
28    public function users_involvement($range=array()) {
29        if (count($range) > 0) {
30            $from = date('c	', strtotime($range[0]));
31            if (count($range) == 1) {
32                $to = date('c');
33            } else {
34                $to = date('c', strtotime($range[1]));
35            }
36            $sql = "SELECT thread_participant.user_id,
37                       SUM(thread_participant.original_poster) AS original_poster_sum,
38                       SUM(thread_participant.coordinator) AS coordinator_sum,
39                       SUM(thread_participant.commentator) AS commentator_sum,
40                       SUM(thread_participant.task_assignee) AS task_assignee_sum
41                       FROM thread_participant JOIN thread ON thread_participant.thread_id = thread.id
42                       WHERE thread.create_date BETWEEN ? AND ?
43                       GROUP BY user_id
44                       ORDER BY user_id";
45            $r = $this->model->sqlite->query($sql, $from, $to);
46        } else {
47            $sql = "SELECT user_id,
48                           SUM(original_poster) AS original_poster_sum,
49                           SUM(coordinator) AS coordinator_sum,
50                           SUM(commentator) AS commentator_sum,
51                           SUM(task_assignee) AS task_assignee_sum
52                           FROM thread_participant
53                           GROUP BY user_id
54                           ORDER BY user_id";
55
56            $r = $this->model->sqlite->query($sql);
57        }
58        return $r;
59    }
60
61    public function initial_save(Entity $thread, $data) {
62        $label_ids = array();
63        if (isset($data['label_id']) && $data['label_id'] != '') {
64            $label_ids[] = $data['label_id'];
65        }
66        try {
67            $this->beginTransaction();
68
69            parent::initial_save($thread, $data);
70
71            foreach($label_ids as $label_id) {
72                $thread->add_label($label_id);
73            }
74
75            $thread->set_participant_flags($thread->original_poster, array('original_poster', 'subscribent'));
76            if($thread->coordinator != null) {
77                $thread->set_participant_flags($thread->coordinator, array('coordinator', 'subscribent'));
78            }
79
80            if ($this->model->get_level() >= BEZ_AUTH_LEADER) {
81                $private = false;
82                if (isset($data['private'])) {
83                    $private = true;
84                }
85                $thread->set_private_flag($private);
86            }
87
88            $this->commitTransaction();
89
90            if ($thread->state != 'proposal' && $this->model->user_nick != $thread->coordinator) {
91                $thread->mail_inform_coordinator();
92            } elseif ($thread->state == 'proposal') {
93                $thread->mail_inform_admins();
94            }
95
96        } catch(Exception $exception) {
97            $this->rollbackTransaction();
98        }
99    }
100
101    public function update_save(Entity $thread, $data) {
102        $prev_coordinator = $thread->coordinator;
103
104        $label_ids = array();
105        if (isset($data['label_id']) && $data['label_id'] != '') {
106            $label_ids[] = $data['label_id'];
107        }
108        try {
109            $this->beginTransaction();
110            parent::update_save($thread, $data);
111
112            $cur_label_ids = array_keys($thread->get_labels());
113            $labels_to_add = array_diff($label_ids, $cur_label_ids);
114            $labels_to_rem = array_diff($cur_label_ids, $label_ids);
115
116            foreach($labels_to_add as $label_id) {
117                $thread->add_label($label_id);
118            }
119
120            foreach($labels_to_rem as $label_id) {
121                $thread->remove_label($label_id);
122            }
123
124            if ($thread->coordinator != null && $thread->coordinator != $prev_coordinator) {
125                if ($prev_coordinator != null) {
126                    $thread->remove_participant_flags($prev_coordinator, array('coordinator'));
127                }
128                $thread->set_participant_flags($thread->coordinator, array('subscribent', 'coordinator'));
129            }
130
131            if ($thread->acl_of('private') >= BEZ_PERMISSION_CHANGE) {
132                $private = false;
133                if (isset($data['private'])) {
134                    $private = true;
135                }
136                $thread->set_private_flag($private);
137            }
138
139            $this->commitTransaction();
140        } catch(Exception $exception) {
141            $this->rollbackTransaction();
142        }
143
144        if ($thread->state != 'proposal' && $this->model->user_nick != $thread->coordinator) {
145            $thread->mail_inform_coordinator();
146        }
147    }
148}
149