Backport
[platal.git] / modules / survey / survey.inc.php
CommitLineData
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
23class 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 &#224; 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
0f396347 40 private static $types = array('text' => 'Texte court',
41 'textarea' => 'Texte long',
42 'num' => 'Num&#233;rique',
43 'radio' => 'Choix multiples (une réponse)',
44 'checkbox' => 'Choix multiples (plusieurs réponses)');
8fe81c50 45
46 public static function getTypes()
47 {
48 return self::$types;
49 }
50
51 public static function isType($t)
52 {
53 return array_key_exists($t, self::$types);
54 }
55 // }}}
56
5c6e38d7 57 // {{{ properties, constructor and basic methods
8fe81c50 58 private $id;
5c6e38d7 59 private $title;
60 private $description;
61 private $end;
62 private $mode;
63 private $promos;
64 private $valid;
65 private $questions;
8fe81c50 66
5c6e38d7 67 public function __construct($args, $id = -1, $valid = false, $questions = null)
8fe81c50 68 {
8fe81c50 69 $this->update($args);
5c6e38d7 70 $this->id = $id;
71 $this->valid = $valid;
72 $this->questions = ($questions == null)? array() : $questions;
8fe81c50 73 }
74
5c6e38d7 75 public function update($args)
8fe81c50 76 {
5c6e38d7 77 $this->title = $args['title'];
78 $this->description = $args['description'];
0f396347 79 $this->end = $args['end'];
0b78af47 80 $this->mode = (isset($args['mode']))? $args['mode'] : self::MODE_ALL;
81 if ($this->mode == self::MODE_ALL) {
5c6e38d7 82 $args['promos'] = '';
8fe81c50 83 }
5c6e38d7 84 $this->promos = ($args['promos'] == '' || preg_match('#^(\d{4}-?|(\d{4})?-\d{4})(,(\d{4}-?|(\d{4})?-\d{4}))*$#', $args['promos']))? $args['promos'] : '#';
8fe81c50 85 }
86 // }}}
87
5c6e38d7 88 // {{{ functions to access general information
89 public function isMode($mode)
8fe81c50 90 {
5c6e38d7 91 return ($this->mode == $mode);
8fe81c50 92 }
93
5c6e38d7 94 public function checkPromo($promo)
8fe81c50 95 {
4b8c8634 96 if ($this->promos == '') {
97 return true;
98 }
fd0084aa 99 $promos = explode(',', $this->promos);
5c6e38d7 100 foreach ($promos as $p) {
101 if ((preg_match('#^\d{4}$#', $p) && $p == $promo) ||
102 (preg_match('#^\d{4}-$#', $p) && intval(substr($p, 0, 4)) <= $promo) ||
103 (preg_match('#^-\d{4}$#', $p) && intval(substr($p, 1)) >= $promo) ||
104 (preg_match('#^\d{4}-\d{4}$#', $p) && intval(substr($p, 0, 4)) <= $promo && intval(substr($p, 5)) >= $promo)) {
105 return true;
106 }
8fe81c50 107 }
5c6e38d7 108 return false;
8fe81c50 109 }
110
5c6e38d7 111 public function isValid()
8fe81c50 112 {
5c6e38d7 113 return $this->valid;
8fe81c50 114 }
115
5c6e38d7 116 public function isEnded()
8fe81c50 117 {
5c6e38d7 118 return (strtotime($this->end) - time() <= 0);
8fe81c50 119 }
8fe81c50 120
5c6e38d7 121 public function getTitle()
8fe81c50 122 {
5c6e38d7 123 return $this->title;
8fe81c50 124 }
125 // }}}
126
9f01f40b 127 // {{{ function toArray() : converts a question (or the whole survey) to array, with results if the survey is ended
5c6e38d7 128 public function toArray($i = 'all')
8fe81c50 129 {
9f01f40b 130 if ($i != 'all' && $i != 'root') { // if a specific question is requested, then just returns this question converted to array
5c6e38d7 131 $i = intval($i);
132 if (array_key_exists($i, $this->questions)) {
133 return $this->questions[$i]->toArray();
8fe81c50 134 } else {
5c6e38d7 135 return null;
8fe81c50 136 }
9f01f40b 137 } else { // else returns the root converted to array in any case
5c6e38d7 138 $a = array('title' => $this->title,
139 'description' => $this->description,
140 'end' => $this->end,
141 'mode' => $this->mode,
142 'promos' => $this->promos,
143 'valid' => $this->valid,
144 'type' => 'root');
145 if ($this->id != -1) {
146 $a['id'] = $this->id;
147 }
9f01f40b 148 if ($this->isEnded()) { // if the survey is ended, then adds here the number of votes
149 $sql = 'SELECT COUNT(id)
150 FROM survey_votes
151 WHERE survey_id={?};';
152 $tot = XDB::query($sql, $this->id);
153 $a['votes'] = $tot->fetchOneCell();
154 }
155 if ($i == 'all' && count($this->questions) > 0) { // if the whole survey is requested, then returns all the questions converted to array
5c6e38d7 156 $qArr = array();
157 for ($k = 0; $k < count($this->questions); $k++) {
158 $q = $this->questions[$k]->toArray();
159 $q['id'] = $k;
9f01f40b 160 if ($this->isEnded()) { // if the survey is ended, then adds here the results of this question
161 $sql = $this->questions[$k]->buildResultRequest();
162 if ($sql != '') {
163 $q['result'] = XDB::iterator($sql, $this->id, $k);
164 }
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');
185 for ($qid = 0; $qid < $nbq; $qid++) {
186 $line[] = $this->questions[$qid]->getCSVColumn(); // the fist line contains the questions
187 }
188 $users = array();
189 if ($this->isMode(self::MODE_XIDENT)) { // if the mode is non anonymous
190 $sql = 'SELECT v.id AS vid, a.nom, a.prenom, a.promo
191 FROM survey_votes AS v
192 INNER JOIN auth_user_md5 AS a
193 ON a.user_id=v.user_id
194 WHERE v.survey_id={?}
195 ORDER BY vid ASC;';
196 $res = XDB::iterator($sql, $this->id); // retrieves all users data
197 for ($u = $res->next(); $u != null; $u = $res->next()) {
198 $users[$u['vid']] = array('nom' => $u['nom'], 'prenom' => $u['prenom'], 'promo' => $u['promo']);
199 }
200 }
201 $sql = 'SELECT v.id AS vid, a.question_id AS qid, a.answer
202 FROM survey_votes AS v
203 LEFT JOIN survey_answers AS a
204 ON a.vote_id=v.id
205 WHERE v.survey_id={?}
206 ORDER BY vid ASC, qid ASC;';
207 $res = XDB::iterator($sql, $this->id); // retrieves all answers from database
208 $cur = $res->next();
209 $vid = -1;
210 $vid_ = 0;
211 $first = ($this->isMode(self::MODE_XIDENT))? 4 : 1;
212 while ($cur != null) {
213 if ($vid != $cur['vid']) { // if the vote id changes, then starts a new line
214 fputcsv($csv, $line, $sep, $enc); // stores the former line into $csv_output
215 $vid = $cur['vid'];
216 $line = array_fill(0, $first + $nbq, ''); // creates an array full of empty string
217 $line[0] = $vid_; // the first field is a 'clean' vote id (not the one stored in database)
218 if ($this->isMode(self::MODE_XIDENT)) { // if the mode is non anonymous
219 if (array_key_exists($vid, $users) && is_array($users[$vid])) { // and if the user data can be found
220 $line[1] = $users[$vid]['nom']; // adds the user data (in the first fields of the line)
221 $line[2] = $users[$vid]['prenom'];
222 $line[3] = $users[$vid]['promo'];
223 }
224 }
225 $vid_++;
226 }
227 $fid = $first + $cur['qid']; // computes the field id
228 if ($line[$fid] != '') { // if this field already contains something
229 $line[$fid] .= $asep; // then adds a separator before adding the new answer
230 }
231 $line[$fid] .= $cur['answer']; // adds the current answer to the correct field
232 $cur = $res->next(); // gets next answer
233 }
234 fputcsv($csv, $line, $sep, $enc); // stores the last line into $csv_output
235 return $csv_output;
236 }
237 // }}}
238
5c6e38d7 239 // {{{ function factory($type, $args) : builds a question according to the given type
240 public function factory($t, $args)
8fe81c50 241 {
5c6e38d7 242 switch ($t) {
243 case 'text':
244 return new SurveyText($args);
245 case 'textarea':
246 return new SurveyTextarea($args);
247 case 'num':
248 return new SurveyNum($args);
249 case 'radio':
250 return new SurveyRadio($args);
251 case 'checkbox':
252 return new SurveyCheckbox($args);
253 case 'personal':
254 return new SurveyPersonal($args);
255 default:
256 return null;
8fe81c50 257 }
5c6e38d7 258 }
259 // }}}
260
261 // {{{ questions manipulation functions
262 public function addQuestion($i, $c)
263 {
cd129064 264 $i = intval($i);
5c6e38d7 265 if ($this->valid || $i > count($this->questions)) {
266 return false;
267 } else {
268 array_splice($this->questions, $i, 0, array($c));
8fe81c50 269 return true;
270 }
8fe81c50 271 }
272
5c6e38d7 273 public function delQuestion($i)
8fe81c50 274 {
cd129064 275 $i = intval($i);
5c6e38d7 276 if ($this->valid || !array_key_exists($i, $this->questions)) {
277 return false;
278 } else {
279 array_splice($this->questions, $i, 1);
8fe81c50 280 return true;
281 }
8fe81c50 282 }
8fe81c50 283
5c6e38d7 284 public function editQuestion($i, $a)
8fe81c50 285 {
5c6e38d7 286 if ($i == 'root') {
8fe81c50 287 $this->update($a);
8fe81c50 288 } else {
5c6e38d7 289 $i = intval($i);
290 if ($this->valid ||!array_key_exists($i, $this->questions)) {
291 return false;
292 } else {
293 $this->questions[$i]->update($a);
8fe81c50 294 }
8fe81c50 295 }
5c6e38d7 296 return true;
8fe81c50 297 }
298 // }}}
299
5c6e38d7 300 // {{{ function checkSyntax() : checks syntax of the questions (currently the root only) before storing the survey in database
301 private static $errorMessages = array(
5c6e38d7 302 "datepassed" => "la date de fin de sondage est d&#233;j&#224; d&#233;pass&#233;e : vous devez pr&#233;ciser une date future",
303 "promoformat" => "les restrictions &#224; certaines promotions sont mal formatt&#233;es"
304 );
305
306 public function checkSyntax()
8fe81c50 307 {
5c6e38d7 308 $rArr = array();
0f396347 309 // checks that the end date given is not already passed
310 // (unless the survey has already been validated : an admin can have a validated survey expired)
311 if (!$this->valid && $this->isEnded()) {
312 $rArr[] = array('question' => 'root', 'error' => self::$errorMessages["datepassed"]);
5c6e38d7 313 }
314 if ($this->promos != '' && !preg_match('#^(\d{4}-?|(\d{4})?-\d{4})(,(\d{4}-?|(\d{4})?-\d{4}))*$#', $this->promos)) {
315 $rArr[] = array('question' => 'root', 'error' => self::$errorMessages["promoformat"]);
8fe81c50 316 }
5c6e38d7 317 return (empty($rArr))? null : $rArr;
8fe81c50 318 }
5c6e38d7 319 // }}}
8fe81c50 320
9f01f40b 321 // {{{ functions that manipulate surveys in database
5c6e38d7 322 // {{{ static function retrieveList() : gets the list of available survey (current, old and not validated surveys)
323 public static function retrieveList($type, $tpl = true)
8fe81c50 324 {
5c6e38d7 325 switch ($type) {
5c6e38d7 326 case 'c':
327 case 'current':
797d27db 328 $where = 'end > NOW()';
5c6e38d7 329 break;
330 case 'o':
331 case 'old':
797d27db 332 $where = 'end <= NOW()';
5c6e38d7 333 break;
334 default:
8fe81c50 335 return null;
336 }
5c6e38d7 337 $sql = 'SELECT id, title, end, mode
338 FROM survey_surveys
339 WHERE '.$where.';';
340 if ($tpl) {
341 return XDB::iterator($sql);
342 } else {
343 return XDB::iterRow($sql);
344 }
8fe81c50 345 }
346 // }}}
347
5c6e38d7 348 // {{{ static function retrieveSurvey() : gets a survey in database (and unserialize the survey object structure)
349 public static function retrieveSurvey($sid)
8fe81c50 350 {
797d27db 351 $sql = 'SELECT questions, title, description, end, mode, promos
5c6e38d7 352 FROM survey_surveys
353 WHERE id={?}';
354 $res = XDB::query($sql, $sid);
355 $data = $res->fetchOneAssoc();
356 if (is_null($data) || !is_array($data)) {
357 return null;
8fe81c50 358 }
797d27db 359 $survey = new Survey($data, $sid, true, unserialize($data['questions']));
5c6e38d7 360 return $survey;
8fe81c50 361 }
362 // }}}
363
5c6e38d7 364 // {{{ static function retrieveSurveyInfo() : gets information about a survey (title, description, end date, restrictions) but does not unserialize the survey object structure
365 public static function retrieveSurveyInfo($sid)
8fe81c50 366 {
797d27db 367 $sql = 'SELECT title, description, end, mode, promos
5c6e38d7 368 FROM survey_surveys
369 WHERE id={?}';
370 $res = XDB::query($sql, $sid);
371 return $res->fetchOneAssoc();
8fe81c50 372 }
373 // }}}
8fe81c50 374
797d27db 375 // {{{ static function retrieveSurveyReq() : gets a survey request to validate
376 public static function retrieveSurveyReq($id)
377 {
378 require_once 'validations.inc.php';
379 $surveyreq = Validate::get_request_by_id($id);
380 if ($surveyreq == null) {
381 return null;
382 }
383 $data = array('title' => $surveyreq->title,
384 'description' => $surveyreq->description,
385 'end' => $surveyreq->end,
386 'mode' => $surveyreq->mode,
387 'promos' => $surveyreq->promos);
388 $survey = new Survey($data, $id, false, $surveyreq->questions);
389 return $survey;
390 }
391 // }}}
392
5c6e38d7 393 // {{{ function proposeSurvey() : stores a proposition of survey in database (before validation)
394 public function proposeSurvey()
8fe81c50 395 {
797d27db 396 require_once 'validations.inc.php';
397 $surveyreq = new SurveyReq($this->title, $this->description, $this->end, $this->mode, $this->promos, $this->questions, S::v('uid'));
398 return $surveyreq->submit();
8fe81c50 399 }
5c6e38d7 400 // }}}
8fe81c50 401
5c6e38d7 402 // {{{ function updateSurvey() : updates a survey in database (before validation)
403 public function updateSurvey()
8fe81c50 404 {
797d27db 405 if ($this->valid) {
406 $sql = 'UPDATE survey_surveys
407 SET questions={?},
408 title={?},
409 description={?},
410 end={?},
411 mode={?},
412 promos={?}
413 WHERE id={?};';
414 return XDB::execute($sql, serialize($this->questions), $this->title, $this->description, $this->end, $this->mode, $this->promos, $this->id);
415 } else {
416 require_once 'validations.inc.php';
417 $surveyreq = Validate::get_request_by_id($this->id);
418 if ($surveyreq == null) {
419 return false;
420 }
421 return $surveyreq->updateReq($this->title, $this->description, $this->end, $this->mode, $this->promos, $this->questions);
8fe81c50 422 }
8fe81c50 423 }
5c6e38d7 424 // }}}
8fe81c50 425
5c6e38d7 426 // {{{ functions vote() and hasVoted() : handles vote to a survey
427 public function vote($uid, $args)
428 {
429 XDB::execute('INSERT INTO survey_votes
430 SET survey_id={?}, user_id={?};', $this->id, $uid); // notes the user as having voted
431 $vid = XDB::insertId();
432 for ($i = 0; $i < count($this->questions); $i++) {
433 $ans = $this->questions[$i]->checkAnswer($args[$i]);
9f01f40b 434 if (!is_null($ans) && is_array($ans)) {
435 foreach ($ans as $a) {
436 XDB::execute('INSERT INTO survey_answers
437 SET vote_id = {?},
438 question_id = {?},
439 answer = {?}', $vid, $i, $a);
440 }
5c6e38d7 441 }
442 }
8fe81c50 443 }
444
5c6e38d7 445 public function hasVoted($uid)
8fe81c50 446 {
5c6e38d7 447 $res = XDB::query('SELECT id
448 FROM survey_votes
449 WHERE survey_id={?} AND user_id={?};', $this->id, $uid); // checks whether the user has already voted
450 return ($res->numRows() != 0);
8fe81c50 451 }
5c6e38d7 452 // }}}
8fe81c50 453
5c6e38d7 454 // {{{ static function deleteSurvey() : deletes a survey (and all its votes)
455 public static function deleteSurvey($sid)
8fe81c50 456 {
5c6e38d7 457 $sql = 'DELETE s.*, v.*, a.*
458 FROM survey_surveys AS s
459 LEFT JOIN survey_votes AS v
460 ON v.survey_id=s.id
461 LEFT JOIN survey_answers AS a
462 ON a.vote_id=v.id
463 WHERE s.id={?};';
464 return XDB::execute($sql, $sid);
8fe81c50 465 }
466 // }}}
467
5c6e38d7 468 // {{{ static function purgeVotes() : clears all votes concerning a survey (I'm not sure whether it's really useful)
469 public static function purgeVotes($sid)
8fe81c50 470 {
5c6e38d7 471 $sql = 'DELETE v.*, a.*
472 FROM survey_votes AS v
473 LEFT JOIN survey_answers AS a
474 ON a.vote_id=v.id
475 WHERE v.survey_id={?};';
476 return XDB::execute($sql, $sid);
8fe81c50 477 }
478 // }}}
479
5c6e38d7 480 // }}}
481}
482// }}}
8fe81c50 483
5c6e38d7 484// {{{ abstract class SurveyQuestion
485abstract class SurveyQuestion
486{
487 // {{{ common properties, constructor, and basic methods
488 private $question;
489 private $comment;
8fe81c50 490
5c6e38d7 491 public function __construct($args)
8fe81c50 492 {
5c6e38d7 493 $this->update($args);
8fe81c50 494 }
495
5c6e38d7 496 public function update($a)
8fe81c50 497 {
5c6e38d7 498 $this->question = $a['question'];
499 $this->comment = $a['comment'];
8fe81c50 500 }
501
5c6e38d7 502 abstract protected function getQuestionType();
503 // }}}
504
505 // {{{ function toArray() : converts to array
8fe81c50 506 public function toArray()
507 {
5c6e38d7 508 return array('type' => $this->getQuestionType(), 'question' => $this->question, 'comment' => $this->comment);
8fe81c50 509 }
5c6e38d7 510 // }}}
8fe81c50 511
5c6e38d7 512 // {{{ function checkSyntax() : checks question elements (before storing into database), not currently needed (with new structure)
513 protected function checkSyntax()
8fe81c50 514 {
5c6e38d7 515 return null;
8fe81c50 516 }
517 // }}}
518
5c6e38d7 519 // {{{ function checkAnswer : returns a correctly formatted answer (or nothing empty string if error)
520 public function checkAnswer($ans)
8fe81c50 521 {
9f01f40b 522 return null;
8fe81c50 523 }
524 // }}}
525
4b8c8634 526 // {{{ functions regarding the results of a survey
9f01f40b 527 public function buildResultRequest()
528 {
529 return '';
530 }
4b8c8634 531
532 public function getCSVColumn()
533 {
534 return $this->question;
535 }
8fe81c50 536 // }}}
537}
538// }}}
539
fd0084aa 540// {{{ abstract class SurveySimple and its derived classes : "open" questions
9f01f40b 541// {{{ abstract class SurveySimple extends SurveyQuestion
8fe81c50 542abstract class SurveySimple extends SurveyQuestion
543{
5c6e38d7 544 public function checkAnswer($ans)
8fe81c50 545 {
9f01f40b 546 return array($ans);
547 }
548
549 public function buildResultRequest()
550 {
551 $sql = 'SELECT answer
552 FROM survey_answers
553 WHERE vote_id IN (SELECT id FROM survey_votes WHERE survey_id={?})
554 AND question_id={?}
555 ORDER BY RAND()
556 LIMIT 5;';
557 return $sql;
8fe81c50 558 }
559}
9f01f40b 560// }}}
8fe81c50 561
562// {{{ class SurveyText extends SurveySimple : simple text field, allowing a few words
563class SurveyText extends SurveySimple
564{
5c6e38d7 565 public function getQuestionType()
8fe81c50 566 {
567 return "text";
568 }
569}
570// }}}
571
572// {{{ class SurveyTextarea extends SurveySimple : textarea field, allowing longer comments
573class SurveyTextarea extends SurveySimple
574{
5c6e38d7 575 public function getQuestionType()
8fe81c50 576 {
577 return "textarea";
578 }
579}
580// }}}
581
582// {{{ class SurveyNum extends SurveySimple : allows numerical answers
583class SurveyNum extends SurveySimple
584{
5c6e38d7 585 public function checkAnswer($ans)
8fe81c50 586 {
9f01f40b 587 return array(intval($ans));
8fe81c50 588 }
589
590 protected function getQuestionType()
591 {
592 return "num";
593 }
594}
595// }}}
596// }}}
597
9f01f40b 598// {{{ abstract class SurveyList and its derived classes : restricted questions that allows only a list of possible answers
599// {{{ abstract class SurveyList extends SurveyQuestion
5c6e38d7 600abstract class SurveyList extends SurveyQuestion
8fe81c50 601{
5c6e38d7 602 protected $choices;
8fe81c50 603
5c6e38d7 604 public function update($args)
8fe81c50 605 {
606 parent::update($args);
0f396347 607 $this->choices = array();
608 foreach ($args['options'] as $val) {
609 if (trim($val)) {
610 $this->choices[] = $val;
611 }
612 }
8fe81c50 613 }
614
5c6e38d7 615 public function toArray()
8fe81c50 616 {
5c6e38d7 617 $rArr = parent::toArray();
8fe81c50 618 $rArr['choices'] = $this->choices;
8fe81c50 619 return $rArr;
620 }
621
9f01f40b 622 public function buildResultRequest()
623 {
624 $sql = 'SELECT answer, COUNT(id) AS count
625 FROM survey_answers
626 WHERE vote_id IN (SELECT id FROM survey_votes WHERE survey_id={?})
627 AND question_id={?}
628 GROUP BY answer ASC';
629 return $sql;
630 }
4b8c8634 631
632 public function getCSVColumn()
633 {
634 $r = parent::getCSVColumn();
635 if (empty($this->choices)) {
636 return $r;
637 }
638 $r .= ' [0 => '.$this->choices[0];
639 for ($k = 1; $k < count($this->choices); $k++) {
640 $r .= ', '.$k.' => '.$this->choices[$k];
641 }
642 $r .= ']';
643 return $r;
644 }
8fe81c50 645}
9f01f40b 646// }}}
8fe81c50 647
648// {{{ class SurveyRadio extends SurveyList : radio question, allows one answer among the list offered
649class SurveyRadio extends SurveyList
650{
5c6e38d7 651 public function checkAnswer($ans)
8fe81c50 652 {
9f01f40b 653 return (array_key_exists($ans, $this->choices))? array($ans) : null;
8fe81c50 654 }
655
656 protected function getQuestionType()
657 {
658 return "radio";
659 }
660}
661// }}}
662
663// {{{ class SurveyCheckbox extends SurveyList : checkbox question, allows any number of answers among the list offered
664class SurveyCheckbox extends SurveyList
665{
5c6e38d7 666 public function checkAnswer($ans)
8fe81c50 667 {
9f01f40b 668 $rep = array();
5c6e38d7 669 foreach ($ans as $a) {
9f01f40b 670 $a = intval($a);
671 if (array_key_exists($a, $this->choices)) {
672 $rep[] = $a;
8fe81c50 673 }
674 }
9f01f40b 675 return (count($rep) == 0)? null : $rep;
8fe81c50 676 }
677
678 protected function getQuestionType()
679 {
680 return "checkbox";
681 }
682}
683// }}}
684// }}}
685
8fe81c50 686// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
687?>