xref: /plugin/bez/mdl/ThreadFactory.php (revision 16c7b168a60daa2c9b9ddcfa51e05d123a2a17dc)
1<?php
2
3namespace dokuwiki\plugin\bez\mdl;
4
5//if(!defined('DOKU_INC')) die();
6
7//require_once 'factory.php';
8//require_once 'thread.php';
9
10
11
12use Assetic\Exception\Exception;
13
14class ThreadFactory extends Factory {
15
16//	public function __construct($model) {
17//		parent::__construct($model);
18
19		/* state_string:
20			0 -> opened
21			1 -> closed
22			2 -> rejected
23			if state = 0 and all tasks are done -> done
24		*/
25//		$this->select_query = "SELECT *,
26//					(CASE
27//                        WHEN state = 2
28//							THEN '".$this->model->action->getLang('state_rejected')."'
29//						WHEN coordinator = '-proposal'
30//							THEN '".$this->model->action->getLang('state_proposal')."'
31//						WHEN state = 0 	AND assigned_tasks_count > 0
32//										AND opened_tasks_count = 0
33//							THEN '".$this->model->action->getLang('state_done')."'
34//						WHEN state = 0
35//							THEN '".$this->model->action->getLang('state_opened')."'
36//						WHEN state = 1
37//							THEN '".$this->model->action->getLang('state_closed')."'
38//					END) AS state_string,
39//
40//					(CASE
41//                        WHEN state = 2
42//							THEN '2'
43//						WHEN coordinator = '-proposal'
44//							THEN '-proposal'
45//						WHEN state = 0 	AND assigned_tasks_count > 0
46//										AND opened_tasks_count = 0
47//							THEN '-done'
48//						WHEN state = 0
49//							THEN '0'
50//						WHEN state = 1
51//							THEN '1'
52//					END) AS full_state,
53//
54//					(CASE 	WHEN state = 2 then '3'
55//                            WHEN task_priority IS NULL THEN 'None'
56//							ELSE task_priority
57//					END) AS priority
58//
59//					FROM (SELECT issues.*,
60//							(SELECT COUNT(*) FROM tasks
61//								WHERE tasks.issue = issues.id)
62//							AS assigned_tasks_count,
63//							(SELECT COUNT(*) FROM tasks
64//								WHERE tasks.issue = issues.id AND tasks.state = 0)
65//							AS opened_tasks_count,
66//							(SELECT MIN((CASE	WHEN tasks.state > 0 THEN '3'
67//								WHEN tasks.plan_date >= date('now', '+1 month') THEN '2'
68//								WHEN tasks.plan_date >= date('now') THEN '1'
69//								ELSE '0' END)) FROM tasks WHERE tasks.issue = issues.id)
70//							AS task_priority,
71//                            (SELECT SUM(tasks.cost) FROM tasks
72//								WHERE tasks.issue = issues.id)
73//                            AS cost,
74//					issuetypes.".$this->model->conf['lang']." AS type_string
75//					FROM issues
76//						LEFT JOIN issuetypes ON issues.type = issuetypes.id)";
77//	}
78
79    protected function select_query() {
80        return "SELECT thread.*, label.id AS label_id, label.name AS label_name FROM thread
81                        LEFT JOIN thread_label ON thread.id = thread_label.thread_id
82                        LEFT JOIN label ON label.id = thread_label.label_id";
83    }
84
85    public function get_years_scope() {
86        $r = $this->model->sqlite->query('SELECT create_date FROM thread ORDER BY id LIMIT 1');
87        $date = $this->model->sqlite->res2single($r);
88
89        //get only year
90		$first =  (int) substr($date, 0, strpos($date, '-'));
91        $last = (int) date('Y');
92
93		$years = array();
94		for ($year = $first; $year <= $last; $year++) {
95			$years[] = (string) $year;
96        }
97		return $years;
98    }
99
100    public function users_involvement() {
101        $sql = 'SELECT user_id,
102                       SUM(original_poster),
103                       SUM(coordinator),
104                       SUM(commentator),
105                       SUM(task_assignee),
106                       COUNT(*)
107                       FROM thread_participant
108                       GROUP BY user_id
109                       ORDER BY user_id';
110
111        $r = $this->model->sqlite->query($sql);
112        return $r;
113    }
114
115    public function initial_save(Entity $thread, $data) {
116        parent::initial_save($thread, $data);
117
118        $thread->set_data($data);
119        $label_ids = array();
120        if (isset($data['label_id']) && $data['label_id'] != '') {
121            $label_ids[] = $data['label_id'];
122        }
123        try {
124            $this->beginTransaction();
125            $this->save($thread);
126
127            foreach($label_ids as $label_id) {
128                $thread->add_label($label_id);
129            }
130
131            $thread->set_participant_flags($thread->original_poster, array('original_poster', 'subscribent'));
132            if($thread->coordinator != null) {
133                $thread->set_participant_flags($thread->coordinator, array('coordinator', 'subscribent'));
134            }
135
136            $this->commitTransaction();
137        } catch(Exception $exception) {
138            $this->rollbackTransaction();
139        }
140
141        if ($thread->state != 'proposal' && $this->model->user_nick != $thread->coordinator) {
142            $thread->mail_inform_coordinator();
143        }
144    }
145
146    public function update_save(Entity $thread, $data) {
147        parent::update_save($thread, $data);
148
149        $prev_coordinator = $thread->coordinator;
150        $thread->set_data($data);
151        $label_ids = array();
152        if (isset($data['label_id']) && $data['label_id'] != '') {
153            $label_ids[] = $data['label_id'];
154        }
155        try {
156            $this->beginTransaction();
157            $this->save($thread);
158
159            $cur_label_ids = array_keys($thread->get_labels());
160            $labels_to_add = array_diff($label_ids, $cur_label_ids);
161            $labels_to_rem = array_diff($cur_label_ids, $label_ids);
162
163            foreach($labels_to_add as $label_id) {
164                $thread->add_label($label_id);
165            }
166
167            foreach($labels_to_rem as $label_id) {
168                $thread->remove_label($label_id);
169            }
170
171            if($thread->coordinator != null && $thread->coordinator != $prev_coordinator) {
172                $thread->remove_participant_flags($prev_coordinator, array('coordinator'));
173                $thread->set_participant_flags($thread->coordinator, array('subscribent', 'coordinator'));
174            }
175
176            $this->commitTransaction();
177        } catch(Exception $exception) {
178            $this->rollbackTransaction();
179        }
180
181        if ($thread->state != 'proposal' && $this->model->user_nick != $thread->coordinator) {
182            $thread->mail_inform_coordinator();
183        }
184    }
185}
186