xref: /plugin/bez/mdl/ThreadFactory.php (revision 6fa92f543f15922cfc8ce756fa03e751cd3ae28f)
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 kpi($range=array()) {
62        if (count($range) > 0) {
63            $from = date('c	', strtotime($range[0]));
64            if (count($range) == 1) {
65                $to = date('c');
66            } else {
67                $to = date('c', strtotime($range[1]));
68            }
69            $sql = "SELECT COUNT(*)*1.0/COUNT(DISTINCT thread_id) AS kpi
70                       FROM thread_participant JOIN thread ON thread_participant.thread_id = thread.id
71                       WHERE thread.create_date BETWEEN ? AND ?
72                       GROUP BY thread_id";
73            $r = $this->model->sqlite->query($sql, $from, $to);
74        } else {
75            $sql = "SELECT COUNT(*)*1.0/COUNT(DISTINCT thread_id) AS kpi
76                      FROM thread_participant";
77
78            $r = $this->model->sqlite->query($sql);
79        }
80
81        return $r->fetchColumn();
82    }
83
84    public function initial_save(Entity $thread, $data) {
85        $label_ids = array();
86        if (isset($data['label_id']) && $data['label_id'] != '') {
87            $label_ids[] = $data['label_id'];
88        }
89        try {
90            $this->beginTransaction();
91
92            parent::initial_save($thread, $data);
93
94            foreach($label_ids as $label_id) {
95                $thread->add_label($label_id);
96            }
97
98            $thread->set_participant_flags($thread->original_poster, array('original_poster', 'subscribent'));
99            if($thread->coordinator != null) {
100                $thread->set_participant_flags($thread->coordinator, array('coordinator', 'subscribent'));
101            }
102
103            if ($this->model->get_level() >= BEZ_AUTH_LEADER) {
104                $private = false;
105                if (isset($data['private'])) {
106                    $private = true;
107                }
108                $thread->set_private_flag($private);
109            }
110
111            $this->commitTransaction();
112
113            if ($thread->state != 'proposal' && $this->model->user_nick != $thread->coordinator) {
114                $thread->mail_inform_coordinator();
115            } elseif ($thread->state == 'proposal') {
116                $thread->mail_inform_admins();
117            }
118
119        } catch(Exception $exception) {
120            $this->rollbackTransaction();
121        }
122    }
123
124    public function update_save(Entity $thread, $data) {
125        $prev_coordinator = $thread->coordinator;
126
127        $label_ids = array();
128        if (isset($data['label_id']) && $data['label_id'] != '') {
129            $label_ids[] = $data['label_id'];
130        }
131        try {
132            $this->beginTransaction();
133            parent::update_save($thread, $data);
134
135            $cur_label_ids = array_keys($thread->get_labels());
136            $labels_to_add = array_diff($label_ids, $cur_label_ids);
137            $labels_to_rem = array_diff($cur_label_ids, $label_ids);
138
139            foreach($labels_to_add as $label_id) {
140                $thread->add_label($label_id);
141            }
142
143            foreach($labels_to_rem as $label_id) {
144                $thread->remove_label($label_id);
145            }
146
147            if ($thread->coordinator != null && $thread->coordinator != $prev_coordinator) {
148                if ($prev_coordinator != null) {
149                    $thread->remove_participant_flags($prev_coordinator, array('coordinator'));
150                }
151                $thread->set_participant_flags($thread->coordinator, array('subscribent', 'coordinator'));
152            }
153
154            if ($thread->acl_of('private') >= BEZ_PERMISSION_CHANGE) {
155                $private = false;
156                if (isset($data['private'])) {
157                    $private = true;
158                }
159                $thread->set_private_flag($private);
160            }
161
162            $this->commitTransaction();
163        } catch(Exception $exception) {
164            $this->rollbackTransaction();
165        }
166
167        if ($thread->state != 'proposal' && $this->model->user_nick != $thread->coordinator) {
168            $thread->mail_inform_coordinator();
169        }
170    }
171}
172