8fe81c50 |
1 | <?php |
2 | /*************************************************************************** |
9f5bd98e |
3 | * Copyright (C) 2003-2010 Polytechnique.org * |
8fe81c50 |
4 | * http://opensource.polytechnique.org/ * |
5 | * * |
6 | * This program is free software; you can redistribute it and/or modify * |
7 | * it under the terms of the GNU General Public License as published by * |
8 | * the Free Software Foundation; either version 2 of the License, or * |
9 | * (at your option) any later version. * |
10 | * * |
11 | * This program is distributed in the hope that it will be useful, * |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
14 | * GNU General Public License for more details. * |
15 | * * |
16 | * You should have received a copy of the GNU General Public License * |
17 | * along with this program; if not, write to the Free Software * |
18 | * Foundation, Inc., * |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
20 | ***************************************************************************/ |
21 | |
5c6e38d7 |
22 | // {{{ class Survey : root of any survey, contains all questions |
23 | class Survey |
8fe81c50 |
24 | { |
5c6e38d7 |
25 | // {{{ static properties and functions, regarding survey modes and question types |
26 | const MODE_ALL = 0; |
27 | const MODE_XANON = 1; |
28 | const MODE_XIDENT = 2; |
cd723c19 |
29 | private static $longModes = array(self::MODE_ALL => "sondage ouvert à tout le monde, anonyme", |
5c6e38d7 |
30 | self::MODE_XANON => "sondage restreint aux polytechniciens, anonyme", |
31 | self::MODE_XIDENT => "sondage restreint aux polytechniciens, non anonyme"); |
32 | private static $shortModes = array(self::MODE_ALL => "tout le monde, anonyme", |
33 | self::MODE_XANON => "polytechniciens, anonyme", |
34 | self::MODE_XIDENT => "polytechniciens, non anonyme"); |
8fe81c50 |
35 | |
5c6e38d7 |
36 | public static function getModes($long = true) { |
37 | return ($long)? self::$longModes : self::$shortModes; |
8fe81c50 |
38 | } |
8fe81c50 |
39 | |
02d94468 |
40 | private static $types = array('text' => 'Texte court', |
41 | 'textarea' => 'Texte long', |
cd723c19 |
42 | 'num' => 'Numérique', |
43 | 'radio' => 'Choix multiples (une réponse)', |
44 | 'checkbox' => 'Choix multiples (plusieurs réponses)', |
45 | 'radiotable' => 'Questions multiples à choix multiples (une réponse)', |
46 | 'checkboxtable' => 'Questions multiples à choix mutliples (plusieurs réponses)'); |
8fe81c50 |
47 | |
48 | public static function getTypes() |
49 | { |
50 | return self::$types; |
51 | } |
52 | |
53 | public static function isType($t) |
54 | { |
55 | return array_key_exists($t, self::$types); |
56 | } |
57 | // }}} |
58 | |
5c6e38d7 |
59 | // {{{ properties, constructor and basic methods |
8fe81c50 |
60 | private $id; |
5c6e38d7 |
61 | private $title; |
62 | private $description; |
63 | private $end; |
64 | private $mode; |
65 | private $promos; |
66 | private $valid; |
67 | private $questions; |
8fe81c50 |
68 | |
5c6e38d7 |
69 | public function __construct($args, $id = -1, $valid = false, $questions = null) |
8fe81c50 |
70 | { |
8fe81c50 |
71 | $this->update($args); |
5c6e38d7 |
72 | $this->id = $id; |
73 | $this->valid = $valid; |
74 | $this->questions = ($questions == null)? array() : $questions; |
8fe81c50 |
75 | } |
76 | |
5c6e38d7 |
77 | public function update($args) |
8fe81c50 |
78 | { |
5c6e38d7 |
79 | $this->title = $args['title']; |
80 | $this->description = $args['description']; |
0f396347 |
81 | $this->end = $args['end']; |
0b78af47 |
82 | $this->mode = (isset($args['mode']))? $args['mode'] : self::MODE_ALL; |
83 | if ($this->mode == self::MODE_ALL) { |
5c6e38d7 |
84 | $args['promos'] = ''; |
8fe81c50 |
85 | } |
0dc8c5a4 |
86 | $args['promos'] = str_replace(' ', '', $args['promos']); |
5c6e38d7 |
87 | $this->promos = ($args['promos'] == '' || preg_match('#^(\d{4}-?|(\d{4})?-\d{4})(,(\d{4}-?|(\d{4})?-\d{4}))*$#', $args['promos']))? $args['promos'] : '#'; |
8fe81c50 |
88 | } |
89 | // }}} |
90 | |
5c6e38d7 |
91 | // {{{ functions to access general information |
92 | public function isMode($mode) |
8fe81c50 |
93 | { |
5c6e38d7 |
94 | return ($this->mode == $mode); |
8fe81c50 |
95 | } |
96 | |
5c6e38d7 |
97 | public function checkPromo($promo) |
8fe81c50 |
98 | { |
4b8c8634 |
99 | if ($this->promos == '') { |
100 | return true; |
101 | } |
fd0084aa |
102 | $promos = explode(',', $this->promos); |
5c6e38d7 |
103 | foreach ($promos as $p) { |
104 | if ((preg_match('#^\d{4}$#', $p) && $p == $promo) || |
105 | (preg_match('#^\d{4}-$#', $p) && intval(substr($p, 0, 4)) <= $promo) || |
106 | (preg_match('#^-\d{4}$#', $p) && intval(substr($p, 1)) >= $promo) || |
107 | (preg_match('#^\d{4}-\d{4}$#', $p) && intval(substr($p, 0, 4)) <= $promo && intval(substr($p, 5)) >= $promo)) { |
108 | return true; |
109 | } |
8fe81c50 |
110 | } |
5c6e38d7 |
111 | return false; |
8fe81c50 |
112 | } |
113 | |
5c6e38d7 |
114 | public function isValid() |
8fe81c50 |
115 | { |
5c6e38d7 |
116 | return $this->valid; |
8fe81c50 |
117 | } |
118 | |
5c6e38d7 |
119 | public function isEnded() |
8fe81c50 |
120 | { |
5c6e38d7 |
121 | return (strtotime($this->end) - time() <= 0); |
8fe81c50 |
122 | } |
8fe81c50 |
123 | |
5c6e38d7 |
124 | public function getTitle() |
8fe81c50 |
125 | { |
5c6e38d7 |
126 | return $this->title; |
8fe81c50 |
127 | } |
128 | // }}} |
129 | |
9f01f40b |
130 | // {{{ function toArray() : converts a question (or the whole survey) to array, with results if the survey is ended |
5c6e38d7 |
131 | public function toArray($i = 'all') |
8fe81c50 |
132 | { |
9f01f40b |
133 | if ($i != 'all' && $i != 'root') { // if a specific question is requested, then just returns this question converted to array |
5c6e38d7 |
134 | $i = intval($i); |
135 | if (array_key_exists($i, $this->questions)) { |
136 | return $this->questions[$i]->toArray(); |
8fe81c50 |
137 | } else { |
5c6e38d7 |
138 | return null; |
8fe81c50 |
139 | } |
9f01f40b |
140 | } else { // else returns the root converted to array in any case |
5c6e38d7 |
141 | $a = array('title' => $this->title, |
142 | 'description' => $this->description, |
143 | 'end' => $this->end, |
144 | 'mode' => $this->mode, |
145 | 'promos' => $this->promos, |
146 | 'valid' => $this->valid, |
147 | 'type' => 'root'); |
148 | if ($this->id != -1) { |
149 | $a['id'] = $this->id; |
150 | } |
9f01f40b |
151 | if ($this->isEnded()) { // if the survey is ended, then adds here the number of votes |
152 | $sql = 'SELECT COUNT(id) |
153 | FROM survey_votes |
154 | WHERE survey_id={?};'; |
155 | $tot = XDB::query($sql, $this->id); |
156 | $a['votes'] = $tot->fetchOneCell(); |
157 | } |
158 | if ($i == 'all' && count($this->questions) > 0) { // if the whole survey is requested, then returns all the questions converted to array |
5c6e38d7 |
159 | $qArr = array(); |
160 | for ($k = 0; $k < count($this->questions); $k++) { |
161 | $q = $this->questions[$k]->toArray(); |
162 | $q['id'] = $k; |
9f01f40b |
163 | if ($this->isEnded()) { // if the survey is ended, then adds here the results of this question |
02d94468 |
164 | $q['result'] = $this->questions[$k]->getResultArray($this->id, $k); |
9f01f40b |
165 | } |
5c6e38d7 |
166 | $qArr[$k] = $q; |
8fe81c50 |
167 | } |
5c6e38d7 |
168 | $a['questions'] = $qArr; |
8fe81c50 |
169 | } |
5c6e38d7 |
170 | return $a; |
8fe81c50 |
171 | } |
172 | } |
5c6e38d7 |
173 | // }}} |
8fe81c50 |
174 | |
4b8c8634 |
175 | // {{{ function toCSV() : builds a CSV file containing all the results of the survey |
176 | public function toCSV($sep = ',', $enc = '"', $asep='|') |
177 | { |
178 | $nbq = count($this->questions); |
179 | //require_once dirname(__FILE__) . '/../../classes/varstream.php'; |
180 | VarStream::init(); |
181 | global $csv_output; |
182 | $csv_output = ''; |
183 | $csv = fopen('var://csv_output', 'w'); |
184 | $line = ($this->isMode(self::MODE_XIDENT))? array('id', 'Nom', 'Prenom', 'Promo') : array('id'); |
8e9f416e |
185 | $qids = array(); |
4b8c8634 |
186 | for ($qid = 0; $qid < $nbq; $qid++) { |
8e9f416e |
187 | $qids[$qid] = count($line); // stores the first id of a question (in case of questions with subquestions) |
188 | array_splice($line, count($line), 0, $this->questions[$qid]->getCSVColumns()); // the first line contains the questions |
4b8c8634 |
189 | } |
8e9f416e |
190 | $nbf = count($line); |
4b8c8634 |
191 | $users = array(); |
192 | if ($this->isMode(self::MODE_XIDENT)) { // if the mode is non anonymous |
193 | $sql = 'SELECT v.id AS vid, a.nom, a.prenom, a.promo |
194 | FROM survey_votes AS v |
195 | INNER JOIN auth_user_md5 AS a |
196 | ON a.user_id=v.user_id |
197 | WHERE v.survey_id={?} |
198 | ORDER BY vid ASC;'; |
199 | $res = XDB::iterator($sql, $this->id); // retrieves all users data |
200 | for ($u = $res->next(); $u != null; $u = $res->next()) { |
201 | $users[$u['vid']] = array('nom' => $u['nom'], 'prenom' => $u['prenom'], 'promo' => $u['promo']); |
202 | } |
203 | } |
8e9f416e |
204 | $sql = 'SELECT v.id AS vid, a.question_id AS qid, a.answer AS answer |
4b8c8634 |
205 | FROM survey_votes AS v |
206 | LEFT JOIN survey_answers AS a |
207 | ON a.vote_id=v.id |
208 | WHERE v.survey_id={?} |
8e9f416e |
209 | ORDER BY vid ASC, qid ASC, answer ASC;'; |
4b8c8634 |
210 | $res = XDB::iterator($sql, $this->id); // retrieves all answers from database |
211 | $cur = $res->next(); |
212 | $vid = -1; |
213 | $vid_ = 0; |
4b8c8634 |
214 | while ($cur != null) { |
215 | if ($vid != $cur['vid']) { // if the vote id changes, then starts a new line |
216 | fputcsv($csv, $line, $sep, $enc); // stores the former line into $csv_output |
217 | $vid = $cur['vid']; |
8e9f416e |
218 | $line = array_fill(0, $nbf, ''); // creates an array full of empty string |
4b8c8634 |
219 | $line[0] = $vid_; // the first field is a 'clean' vote id (not the one stored in database) |
220 | if ($this->isMode(self::MODE_XIDENT)) { // if the mode is non anonymous |
221 | if (array_key_exists($vid, $users) && is_array($users[$vid])) { // and if the user data can be found |
222 | $line[1] = $users[$vid]['nom']; // adds the user data (in the first fields of the line) |
223 | $line[2] = $users[$vid]['prenom']; |
224 | $line[3] = $users[$vid]['promo']; |
225 | } |
226 | } |
227 | $vid_++; |
228 | } |
8e9f416e |
229 | $ans = $this->questions[$cur['qid']]->formatAnswer($cur['answer']); // formats the current answer |
230 | if (!is_null($ans)) { |
231 | if (is_array($ans)) { |
232 | $fid = $qids[$cur['qid']] + $ans['id']; // computes the field id |
233 | $a = $ans['answer']; |
234 | } else { |
235 | $fid = $qids[$cur['qid']]; |
236 | $a = $ans; |
237 | } |
238 | if ($line[$fid] != '') { // if this field already contains something |
239 | $line[$fid] .= $asep; // then adds a separator before adding the new answer |
240 | } |
241 | $line[$fid] .= $a; // adds the current answer to the correct field |
4b8c8634 |
242 | } |
4b8c8634 |
243 | $cur = $res->next(); // gets next answer |
244 | } |
245 | fputcsv($csv, $line, $sep, $enc); // stores the last line into $csv_output |
246 | return $csv_output; |
247 | } |
248 | // }}} |
249 | |
5c6e38d7 |
250 | // {{{ function factory($type, $args) : builds a question according to the given type |
251 | public function factory($t, $args) |
8fe81c50 |
252 | { |
5c6e38d7 |
253 | switch ($t) { |
254 | case 'text': |
255 | return new SurveyText($args); |
256 | case 'textarea': |
257 | return new SurveyTextarea($args); |
258 | case 'num': |
259 | return new SurveyNum($args); |
260 | case 'radio': |
261 | return new SurveyRadio($args); |
262 | case 'checkbox': |
263 | return new SurveyCheckbox($args); |
02d94468 |
264 | case 'radiotable': |
265 | return new SurveyRadioTable($args); |
266 | case 'checkboxtable': |
267 | return new SurveyCheckboxTable($args); |
5c6e38d7 |
268 | default: |
269 | return null; |
8fe81c50 |
270 | } |
5c6e38d7 |
271 | } |
272 | // }}} |
273 | |
274 | // {{{ questions manipulation functions |
275 | public function addQuestion($i, $c) |
276 | { |
cd129064 |
277 | $i = intval($i); |
5c6e38d7 |
278 | if ($this->valid || $i > count($this->questions)) { |
279 | return false; |
280 | } else { |
281 | array_splice($this->questions, $i, 0, array($c)); |
8fe81c50 |
282 | return true; |
283 | } |
8fe81c50 |
284 | } |
285 | |
5c6e38d7 |
286 | public function delQuestion($i) |
8fe81c50 |
287 | { |
cd129064 |
288 | $i = intval($i); |
5c6e38d7 |
289 | if ($this->valid || !array_key_exists($i, $this->questions)) { |
290 | return false; |
291 | } else { |
292 | array_splice($this->questions, $i, 1); |
8fe81c50 |
293 | return true; |
294 | } |
8fe81c50 |
295 | } |
8fe81c50 |
296 | |
5c6e38d7 |
297 | public function editQuestion($i, $a) |
8fe81c50 |
298 | { |
5c6e38d7 |
299 | if ($i == 'root') { |
8fe81c50 |
300 | $this->update($a); |
8fe81c50 |
301 | } else { |
5c6e38d7 |
302 | $i = intval($i); |
303 | if ($this->valid ||!array_key_exists($i, $this->questions)) { |
304 | return false; |
305 | } else { |
306 | $this->questions[$i]->update($a); |
8fe81c50 |
307 | } |
8fe81c50 |
308 | } |
5c6e38d7 |
309 | return true; |
8fe81c50 |
310 | } |
311 | // }}} |
312 | |
5c6e38d7 |
313 | // {{{ function checkSyntax() : checks syntax of the questions (currently the root only) before storing the survey in database |
314 | private static $errorMessages = array( |
cd723c19 |
315 | "datepassed" => "la date de fin de sondage est déjà dépassée : vous devez préciser une date future", |
316 | "promoformat" => "les restrictions à certaines promotions sont mal formattées" |
5c6e38d7 |
317 | ); |
318 | |
319 | public function checkSyntax() |
8fe81c50 |
320 | { |
5c6e38d7 |
321 | $rArr = array(); |
0f396347 |
322 | // checks that the end date given is not already passed |
323 | // (unless the survey has already been validated : an admin can have a validated survey expired) |
324 | if (!$this->valid && $this->isEnded()) { |
325 | $rArr[] = array('question' => 'root', 'error' => self::$errorMessages["datepassed"]); |
5c6e38d7 |
326 | } |
327 | if ($this->promos != '' && !preg_match('#^(\d{4}-?|(\d{4})?-\d{4})(,(\d{4}-?|(\d{4})?-\d{4}))*$#', $this->promos)) { |
328 | $rArr[] = array('question' => 'root', 'error' => self::$errorMessages["promoformat"]); |
8fe81c50 |
329 | } |
5c6e38d7 |
330 | return (empty($rArr))? null : $rArr; |
8fe81c50 |
331 | } |
5c6e38d7 |
332 | // }}} |
8fe81c50 |
333 | |
9f01f40b |
334 | // {{{ functions that manipulate surveys in database |
5c6e38d7 |
335 | // {{{ static function retrieveList() : gets the list of available survey (current, old and not validated surveys) |
336 | public static function retrieveList($type, $tpl = true) |
8fe81c50 |
337 | { |
5c6e38d7 |
338 | switch ($type) { |
5c6e38d7 |
339 | case 'c': |
340 | case 'current': |
797d27db |
341 | $where = 'end > NOW()'; |
5c6e38d7 |
342 | break; |
343 | case 'o': |
344 | case 'old': |
797d27db |
345 | $where = 'end <= NOW()'; |
5c6e38d7 |
346 | break; |
347 | default: |
8fe81c50 |
348 | return null; |
349 | } |
5c6e38d7 |
350 | $sql = 'SELECT id, title, end, mode |
351 | FROM survey_surveys |
8e9f416e |
352 | WHERE '.$where.' |
353 | ORDER BY end DESC;'; |
5c6e38d7 |
354 | if ($tpl) { |
355 | return XDB::iterator($sql); |
356 | } else { |
357 | return XDB::iterRow($sql); |
358 | } |
8fe81c50 |
359 | } |
360 | // }}} |
361 | |
5c6e38d7 |
362 | // {{{ static function retrieveSurvey() : gets a survey in database (and unserialize the survey object structure) |
363 | public static function retrieveSurvey($sid) |
8fe81c50 |
364 | { |
797d27db |
365 | $sql = 'SELECT questions, title, description, end, mode, promos |
5c6e38d7 |
366 | FROM survey_surveys |
367 | WHERE id={?}'; |
368 | $res = XDB::query($sql, $sid); |
369 | $data = $res->fetchOneAssoc(); |
370 | if (is_null($data) || !is_array($data)) { |
371 | return null; |
8fe81c50 |
372 | } |
797d27db |
373 | $survey = new Survey($data, $sid, true, unserialize($data['questions'])); |
5c6e38d7 |
374 | return $survey; |
8fe81c50 |
375 | } |
376 | // }}} |
377 | |
5c6e38d7 |
378 | // {{{ static function retrieveSurveyInfo() : gets information about a survey (title, description, end date, restrictions) but does not unserialize the survey object structure |
379 | public static function retrieveSurveyInfo($sid) |
8fe81c50 |
380 | { |
797d27db |
381 | $sql = 'SELECT title, description, end, mode, promos |
5c6e38d7 |
382 | FROM survey_surveys |
383 | WHERE id={?}'; |
384 | $res = XDB::query($sql, $sid); |
385 | return $res->fetchOneAssoc(); |
8fe81c50 |
386 | } |
387 | // }}} |
8fe81c50 |
388 | |
797d27db |
389 | // {{{ static function retrieveSurveyReq() : gets a survey request to validate |
390 | public static function retrieveSurveyReq($id) |
391 | { |
392 | require_once 'validations.inc.php'; |
393 | $surveyreq = Validate::get_request_by_id($id); |
394 | if ($surveyreq == null) { |
395 | return null; |
396 | } |
397 | $data = array('title' => $surveyreq->title, |
398 | 'description' => $surveyreq->description, |
399 | 'end' => $surveyreq->end, |
400 | 'mode' => $surveyreq->mode, |
401 | 'promos' => $surveyreq->promos); |
402 | $survey = new Survey($data, $id, false, $surveyreq->questions); |
403 | return $survey; |
404 | } |
405 | // }}} |
406 | |
5c6e38d7 |
407 | // {{{ function proposeSurvey() : stores a proposition of survey in database (before validation) |
408 | public function proposeSurvey() |
8fe81c50 |
409 | { |
797d27db |
410 | require_once 'validations.inc.php'; |
5daf68f6 |
411 | $surveyreq = new SurveyReq($this->title, $this->description, $this->end, $this->mode, $this->promos, $this->questions, S::user()); |
797d27db |
412 | return $surveyreq->submit(); |
8fe81c50 |
413 | } |
5c6e38d7 |
414 | // }}} |
8fe81c50 |
415 | |
5c6e38d7 |
416 | // {{{ function updateSurvey() : updates a survey in database (before validation) |
417 | public function updateSurvey() |
8fe81c50 |
418 | { |
797d27db |
419 | if ($this->valid) { |
420 | $sql = 'UPDATE survey_surveys |
421 | SET questions={?}, |
422 | title={?}, |
423 | description={?}, |
424 | end={?}, |
425 | mode={?}, |
426 | promos={?} |
427 | WHERE id={?};'; |
428 | return XDB::execute($sql, serialize($this->questions), $this->title, $this->description, $this->end, $this->mode, $this->promos, $this->id); |
429 | } else { |
430 | require_once 'validations.inc.php'; |
431 | $surveyreq = Validate::get_request_by_id($this->id); |
432 | if ($surveyreq == null) { |
433 | return false; |
434 | } |
435 | return $surveyreq->updateReq($this->title, $this->description, $this->end, $this->mode, $this->promos, $this->questions); |
8fe81c50 |
436 | } |
8fe81c50 |
437 | } |
5c6e38d7 |
438 | // }}} |
8fe81c50 |
439 | |
5c6e38d7 |
440 | // {{{ functions vote() and hasVoted() : handles vote to a survey |
441 | public function vote($uid, $args) |
442 | { |
443 | XDB::execute('INSERT INTO survey_votes |
444 | SET survey_id={?}, user_id={?};', $this->id, $uid); // notes the user as having voted |
445 | $vid = XDB::insertId(); |
446 | for ($i = 0; $i < count($this->questions); $i++) { |
447 | $ans = $this->questions[$i]->checkAnswer($args[$i]); |
9f01f40b |
448 | if (!is_null($ans) && is_array($ans)) { |
449 | foreach ($ans as $a) { |
450 | XDB::execute('INSERT INTO survey_answers |
451 | SET vote_id = {?}, |
452 | question_id = {?}, |
453 | answer = {?}', $vid, $i, $a); |
454 | } |
5c6e38d7 |
455 | } |
456 | } |
8fe81c50 |
457 | } |
458 | |
5c6e38d7 |
459 | public function hasVoted($uid) |
8fe81c50 |
460 | { |
5c6e38d7 |
461 | $res = XDB::query('SELECT id |
462 | FROM survey_votes |
463 | WHERE survey_id={?} AND user_id={?};', $this->id, $uid); // checks whether the user has already voted |
464 | return ($res->numRows() != 0); |
8fe81c50 |
465 | } |
5c6e38d7 |
466 | // }}} |
8fe81c50 |
467 | |
5c6e38d7 |
468 | // {{{ static function deleteSurvey() : deletes a survey (and all its votes) |
469 | public static function deleteSurvey($sid) |
8fe81c50 |
470 | { |
5c6e38d7 |
471 | $sql = 'DELETE s.*, v.*, a.* |
472 | FROM survey_surveys AS s |
473 | LEFT JOIN survey_votes AS v |
474 | ON v.survey_id=s.id |
475 | LEFT JOIN survey_answers AS a |
476 | ON a.vote_id=v.id |
477 | WHERE s.id={?};'; |
478 | return XDB::execute($sql, $sid); |
8fe81c50 |
479 | } |
480 | // }}} |
481 | |
5c6e38d7 |
482 | // {{{ static function purgeVotes() : clears all votes concerning a survey (I'm not sure whether it's really useful) |
483 | public static function purgeVotes($sid) |
8fe81c50 |
484 | { |
5c6e38d7 |
485 | $sql = 'DELETE v.*, a.* |
486 | FROM survey_votes AS v |
487 | LEFT JOIN survey_answers AS a |
488 | ON a.vote_id=v.id |
489 | WHERE v.survey_id={?};'; |
490 | return XDB::execute($sql, $sid); |
8fe81c50 |
491 | } |
492 | // }}} |
493 | |
5c6e38d7 |
494 | // }}} |
495 | } |
496 | // }}} |
8fe81c50 |
497 | |
5c6e38d7 |
498 | // {{{ abstract class SurveyQuestion |
499 | abstract class SurveyQuestion |
500 | { |
501 | // {{{ common properties, constructor, and basic methods |
502 | private $question; |
503 | private $comment; |
8fe81c50 |
504 | |
5c6e38d7 |
505 | public function __construct($args) |
8fe81c50 |
506 | { |
5c6e38d7 |
507 | $this->update($args); |
8fe81c50 |
508 | } |
509 | |
5c6e38d7 |
510 | public function update($a) |
8fe81c50 |
511 | { |
5c6e38d7 |
512 | $this->question = $a['question']; |
513 | $this->comment = $a['comment']; |
8fe81c50 |
514 | } |
515 | |
5c6e38d7 |
516 | abstract protected function getQuestionType(); |
517 | // }}} |
518 | |
519 | // {{{ function toArray() : converts to array |
8fe81c50 |
520 | public function toArray() |
521 | { |
5c6e38d7 |
522 | return array('type' => $this->getQuestionType(), 'question' => $this->question, 'comment' => $this->comment); |
8fe81c50 |
523 | } |
5c6e38d7 |
524 | // }}} |
8fe81c50 |
525 | |
5c6e38d7 |
526 | // {{{ function checkSyntax() : checks question elements (before storing into database), not currently needed (with new structure) |
527 | protected function checkSyntax() |
8fe81c50 |
528 | { |
5c6e38d7 |
529 | return null; |
8fe81c50 |
530 | } |
531 | // }}} |
532 | |
02d94468 |
533 | // {{{ function checkAnswer : returns a correct answer (or a null value if error) |
5c6e38d7 |
534 | public function checkAnswer($ans) |
8fe81c50 |
535 | { |
9f01f40b |
536 | return null; |
8fe81c50 |
537 | } |
538 | // }}} |
539 | |
4b8c8634 |
540 | // {{{ functions regarding the results of a survey |
02d94468 |
541 | abstract public function getResultArray($sid, $qid); |
4b8c8634 |
542 | |
8e9f416e |
543 | public function formatAnswer($ans) |
544 | { |
545 | return $ans; |
546 | } |
547 | |
548 | public function getCSVColumns() |
4b8c8634 |
549 | { |
550 | return $this->question; |
551 | } |
8fe81c50 |
552 | // }}} |
553 | } |
554 | // }}} |
555 | |
fd0084aa |
556 | // {{{ abstract class SurveySimple and its derived classes : "open" questions |
9f01f40b |
557 | // {{{ abstract class SurveySimple extends SurveyQuestion |
8fe81c50 |
558 | abstract class SurveySimple extends SurveyQuestion |
559 | { |
5c6e38d7 |
560 | public function checkAnswer($ans) |
8fe81c50 |
561 | { |
9f01f40b |
562 | return array($ans); |
563 | } |
564 | |
02d94468 |
565 | public function getResultArray($sid, $qid) |
9f01f40b |
566 | { |
567 | $sql = 'SELECT answer |
568 | FROM survey_answers |
569 | WHERE vote_id IN (SELECT id FROM survey_votes WHERE survey_id={?}) |
570 | AND question_id={?} |
571 | ORDER BY RAND() |
572 | LIMIT 5;'; |
02d94468 |
573 | $res = XDB::query($sql, $sid, $qid); |
574 | return $res->fetchAllAssoc(); |
8fe81c50 |
575 | } |
576 | } |
9f01f40b |
577 | // }}} |
8fe81c50 |
578 | |
579 | // {{{ class SurveyText extends SurveySimple : simple text field, allowing a few words |
580 | class SurveyText extends SurveySimple |
581 | { |
5c6e38d7 |
582 | public function getQuestionType() |
8fe81c50 |
583 | { |
584 | return "text"; |
585 | } |
586 | } |
587 | // }}} |
588 | |
589 | // {{{ class SurveyTextarea extends SurveySimple : textarea field, allowing longer comments |
590 | class SurveyTextarea extends SurveySimple |
591 | { |
5c6e38d7 |
592 | public function getQuestionType() |
8fe81c50 |
593 | { |
594 | return "textarea"; |
595 | } |
596 | } |
597 | // }}} |
598 | |
599 | // {{{ class SurveyNum extends SurveySimple : allows numerical answers |
600 | class SurveyNum extends SurveySimple |
601 | { |
5c6e38d7 |
602 | public function checkAnswer($ans) |
8fe81c50 |
603 | { |
9f01f40b |
604 | return array(intval($ans)); |
8fe81c50 |
605 | } |
606 | |
607 | protected function getQuestionType() |
608 | { |
609 | return "num"; |
610 | } |
611 | } |
612 | // }}} |
613 | // }}} |
614 | |
9f01f40b |
615 | // {{{ abstract class SurveyList and its derived classes : restricted questions that allows only a list of possible answers |
616 | // {{{ abstract class SurveyList extends SurveyQuestion |
5c6e38d7 |
617 | abstract class SurveyList extends SurveyQuestion |
8fe81c50 |
618 | { |
5c6e38d7 |
619 | protected $choices; |
8fe81c50 |
620 | |
5c6e38d7 |
621 | public function update($args) |
8fe81c50 |
622 | { |
623 | parent::update($args); |
0f396347 |
624 | $this->choices = array(); |
02d94468 |
625 | foreach ($args['choices'] as $val) { |
eadfa7ba |
626 | if (trim($val) || trim($val) == '0') { |
0f396347 |
627 | $this->choices[] = $val; |
628 | } |
629 | } |
8fe81c50 |
630 | } |
631 | |
5c6e38d7 |
632 | public function toArray() |
8fe81c50 |
633 | { |
5c6e38d7 |
634 | $rArr = parent::toArray(); |
8fe81c50 |
635 | $rArr['choices'] = $this->choices; |
8fe81c50 |
636 | return $rArr; |
637 | } |
638 | |
02d94468 |
639 | public function getResultArray($sid, $qid) |
9f01f40b |
640 | { |
641 | $sql = 'SELECT answer, COUNT(id) AS count |
642 | FROM survey_answers |
643 | WHERE vote_id IN (SELECT id FROM survey_votes WHERE survey_id={?}) |
644 | AND question_id={?} |
645 | GROUP BY answer ASC'; |
02d94468 |
646 | $res = XDB::query($sql, $sid, $qid); |
647 | return $res->fetchAllAssoc(); |
9f01f40b |
648 | } |
4b8c8634 |
649 | |
8e9f416e |
650 | public function formatAnswer($ans) |
4b8c8634 |
651 | { |
8e9f416e |
652 | if (array_key_exists($ans, $this->choices)) { |
653 | return $this->choices[$ans]; |
654 | } else { |
655 | return null; |
4b8c8634 |
656 | } |
4b8c8634 |
657 | } |
8fe81c50 |
658 | } |
9f01f40b |
659 | // }}} |
8fe81c50 |
660 | |
661 | // {{{ class SurveyRadio extends SurveyList : radio question, allows one answer among the list offered |
662 | class SurveyRadio extends SurveyList |
663 | { |
5c6e38d7 |
664 | public function checkAnswer($ans) |
8fe81c50 |
665 | { |
02d94468 |
666 | $a = intval($ans); |
667 | return (array_key_exists($a, $this->choices))? array($a) : null; |
8fe81c50 |
668 | } |
669 | |
670 | protected function getQuestionType() |
671 | { |
672 | return "radio"; |
673 | } |
674 | } |
675 | // }}} |
676 | |
677 | // {{{ class SurveyCheckbox extends SurveyList : checkbox question, allows any number of answers among the list offered |
678 | class SurveyCheckbox extends SurveyList |
679 | { |
5c6e38d7 |
680 | public function checkAnswer($ans) |
8fe81c50 |
681 | { |
9f01f40b |
682 | $rep = array(); |
5c6e38d7 |
683 | foreach ($ans as $a) { |
9f01f40b |
684 | $a = intval($a); |
685 | if (array_key_exists($a, $this->choices)) { |
686 | $rep[] = $a; |
8fe81c50 |
687 | } |
688 | } |
9f01f40b |
689 | return (count($rep) == 0)? null : $rep; |
8fe81c50 |
690 | } |
691 | |
692 | protected function getQuestionType() |
693 | { |
694 | return "checkbox"; |
695 | } |
696 | } |
697 | // }}} |
698 | // }}} |
699 | |
02d94468 |
700 | // {{{ abstract class SurveyTable and its derived classes : table question, each column represents a choice, each line represents a question |
701 | // {{{ abstract class SurveyTable extends SurveyList |
702 | abstract class SurveyTable extends SurveyList |
703 | { |
704 | protected $subquestions; |
705 | |
706 | public function update($args) |
707 | { |
708 | parent::update($args); |
709 | $this->subquestions = array(); |
710 | foreach ($args['subquestions'] as $val) { |
711 | if (trim($val) || trim($val) == '0') { |
712 | $this->subquestions[] = $val; |
713 | } |
714 | } |
715 | } |
716 | |
717 | public function toArray() |
718 | { |
719 | $rArr = parent::toArray(); |
720 | $rArr['subquestions'] = $this->subquestions; |
721 | return $rArr; |
722 | } |
723 | |
724 | public function getResultArray($sid, $qid) |
725 | { |
726 | $sql = 'SELECT answer, COUNT(id) AS count |
727 | FROM survey_answers |
728 | WHERE vote_id IN (SELECT id FROM survey_votes WHERE survey_id={?}) |
729 | AND question_id={?} |
730 | GROUP BY answer ASC'; |
731 | $res = XDB::iterator($sql, $sid, $qid); |
732 | $result = array(); |
733 | for ($i = 0; $i < count($this->subquestions); $i++) { |
734 | $result[$i] = array_fill(0, count($this->choices), 0); |
735 | } |
736 | while ($r = $res->next()) { |
737 | list($i, $j) = explode(':', $r['answer']); |
738 | $result[$i][$j] = $r['count']; |
739 | } |
740 | return $result; |
741 | } |
742 | |
8e9f416e |
743 | public function formatAnswer($ans) |
02d94468 |
744 | { |
8e9f416e |
745 | list($q, $c) = explode(':', $ans); |
746 | if (array_key_exists($q, $this->subquestions) && array_key_exists($c, $this->choices)) { |
747 | return array('id' => $q, 'answer' => $this->choices[$c]); |
748 | } else { |
749 | return null; |
02d94468 |
750 | } |
8e9f416e |
751 | } |
752 | |
753 | public function getCSVColumns() |
754 | { |
755 | $q = parent::getCSVColumns(); |
756 | if (empty($this->subquestions)) { |
757 | return $q; |
758 | } |
759 | $a = array(); |
760 | for ($k = 0; $k < count($this->subquestions); $k++) { |
761 | $a[$k] = $q.' : '.$this->subquestions[$k]; |
02d94468 |
762 | } |
8e9f416e |
763 | return $a; |
02d94468 |
764 | } |
765 | } |
766 | // }}} |
767 | |
768 | // {{{ class SurveyRadioTable extends SurveyTable : SurveyTable with radio type choices |
769 | class SurveyRadioTable extends SurveyTable |
770 | { |
771 | public function checkAnswer($ans) |
772 | { |
773 | $rep = array(); |
774 | foreach ($ans as $k => $a) { |
775 | if (!array_key_exists($k, $this->subquestions)) { |
776 | continue; |
777 | } |
778 | $a = intval($a); |
779 | if (array_key_exists($a, $this->choices)) { |
780 | $rep[] = $k . ':' . $a; |
781 | } |
782 | } |
783 | return (count($rep) == 0)? null : $rep; |
784 | } |
785 | |
786 | protected function getQuestionType() |
787 | { |
788 | return "radiotable"; |
789 | } |
790 | |
791 | } |
792 | // }}} |
793 | |
794 | // {{{ class SurveyCheckboxTable extends SurveyTable : SurveyTable with checkbox type choices |
795 | class SurveyCheckboxTable extends SurveyTable |
796 | { |
797 | public function checkAnswer($ans) |
798 | { |
799 | $rep = array(); |
800 | foreach ($ans as $k => $aa) { |
801 | if (!array_key_exists($k, $this->subquestions)) { |
802 | continue; |
803 | } |
804 | foreach ($aa as $a) { |
805 | $a = intval($a); |
806 | if (array_key_exists($a, $this->choices)) { |
807 | $rep[] = $k . ':' . $a; |
808 | } |
809 | } |
810 | } |
811 | return (count($rep) == 0)? null : $rep; |
812 | } |
813 | |
814 | protected function getQuestionType() |
815 | { |
816 | return "checkboxtable"; |
817 | } |
818 | |
819 | } |
820 | // }}} |
821 | // }}} |
822 | |
8602c852 |
823 | // vim:set et sw=4 sts=4 ts=4 foldmethod=marker enc=utf-8: |
8fe81c50 |
824 | ?> |