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