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