Promotion restrictions explanation and correction
[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) {
326 case 'w':
327 case 'waiting' :
328 $where = 'valid=0';
329 break;
330 case 'c':
331 case 'current':
332 $where = 'valid=1 AND end > NOW()';
333 break;
334 case 'o':
335 case 'old':
336 $where = 'valid=1 AND end <= NOW()';
337 break;
338 default:
8fe81c50 339 return null;
340 }
5c6e38d7 341 $sql = 'SELECT id, title, end, mode
342 FROM survey_surveys
343 WHERE '.$where.';';
344 if ($tpl) {
345 return XDB::iterator($sql);
346 } else {
347 return XDB::iterRow($sql);
348 }
8fe81c50 349 }
350 // }}}
351
5c6e38d7 352 // {{{ static function retrieveSurvey() : gets a survey in database (and unserialize the survey object structure)
353 public static function retrieveSurvey($sid)
8fe81c50 354 {
5c6e38d7 355 $sql = 'SELECT questions, title, description, end, mode, promos, valid
356 FROM survey_surveys
357 WHERE id={?}';
358 $res = XDB::query($sql, $sid);
359 $data = $res->fetchOneAssoc();
360 if (is_null($data) || !is_array($data)) {
361 return null;
8fe81c50 362 }
5c6e38d7 363 $survey = new Survey($data, $sid, (boolean) $data['valid'], unserialize($data['questions']));
364 return $survey;
8fe81c50 365 }
366 // }}}
367
5c6e38d7 368 // {{{ static function retrieveSurveyInfo() : gets information about a survey (title, description, end date, restrictions) but does not unserialize the survey object structure
369 public static function retrieveSurveyInfo($sid)
8fe81c50 370 {
5c6e38d7 371 $sql = 'SELECT title, description, end, mode, promos, valid
372 FROM survey_surveys
373 WHERE id={?}';
374 $res = XDB::query($sql, $sid);
375 return $res->fetchOneAssoc();
8fe81c50 376 }
377 // }}}
8fe81c50 378
5c6e38d7 379 // {{{ function proposeSurvey() : stores a proposition of survey in database (before validation)
380 public function proposeSurvey()
8fe81c50 381 {
5c6e38d7 382 $sql = 'INSERT INTO survey_surveys
383 SET questions={?},
384 title={?},
385 description={?},
386 author_id={?},
387 end={?},
388 mode={?},
389 promos={?},
390 valid=0;';
391 return XDB::execute($sql, serialize($this->questions), $this->title, $this->description, S::v('uid'), $this->end, $this->mode, $this->promos);
8fe81c50 392 }
5c6e38d7 393 // }}}
8fe81c50 394
5c6e38d7 395 // {{{ function updateSurvey() : updates a survey in database (before validation)
396 public function updateSurvey()
8fe81c50 397 {
5c6e38d7 398 if ($this->id == -1) {
399 return false;
8fe81c50 400 }
5c6e38d7 401 $sql = 'UPDATE survey_surveys
402 SET questions={?},
403 title={?},
404 description={?},
405 end={?},
406 mode={?},
407 promos={?}
408 WHERE id={?};';
409 return XDB::execute($sql, serialize($this->questions), $this->title, $this->description, $this->end, $this->mode, $this->promos, $this->id);
8fe81c50 410 }
5c6e38d7 411 // }}}
8fe81c50 412
5c6e38d7 413 // {{{ static function validateSurvey() : validates a survey
414 public static function validateSurvey($sid)
8fe81c50 415 {
5c6e38d7 416 $sql = 'UPDATE survey_surveys
417 SET valid=1
418 WHERE id={?};';
419 return XDB::execute($sql, $sid);
8fe81c50 420 }
5c6e38d7 421 // }}}
8fe81c50 422
5c6e38d7 423 // {{{ functions vote() and hasVoted() : handles vote to a survey
424 public function vote($uid, $args)
425 {
426 XDB::execute('INSERT INTO survey_votes
427 SET survey_id={?}, user_id={?};', $this->id, $uid); // notes the user as having voted
428 $vid = XDB::insertId();
429 for ($i = 0; $i < count($this->questions); $i++) {
430 $ans = $this->questions[$i]->checkAnswer($args[$i]);
9f01f40b 431 if (!is_null($ans) && is_array($ans)) {
432 foreach ($ans as $a) {
433 XDB::execute('INSERT INTO survey_answers
434 SET vote_id = {?},
435 question_id = {?},
436 answer = {?}', $vid, $i, $a);
437 }
5c6e38d7 438 }
439 }
8fe81c50 440 }
441
5c6e38d7 442 public function hasVoted($uid)
8fe81c50 443 {
5c6e38d7 444 $res = XDB::query('SELECT id
445 FROM survey_votes
446 WHERE survey_id={?} AND user_id={?};', $this->id, $uid); // checks whether the user has already voted
447 return ($res->numRows() != 0);
8fe81c50 448 }
5c6e38d7 449 // }}}
8fe81c50 450
5c6e38d7 451 // {{{ static function deleteSurvey() : deletes a survey (and all its votes)
452 public static function deleteSurvey($sid)
8fe81c50 453 {
5c6e38d7 454 $sql = 'DELETE s.*, v.*, a.*
455 FROM survey_surveys AS s
456 LEFT JOIN survey_votes AS v
457 ON v.survey_id=s.id
458 LEFT JOIN survey_answers AS a
459 ON a.vote_id=v.id
460 WHERE s.id={?};';
461 return XDB::execute($sql, $sid);
8fe81c50 462 }
463 // }}}
464
5c6e38d7 465 // {{{ static function purgeVotes() : clears all votes concerning a survey (I'm not sure whether it's really useful)
466 public static function purgeVotes($sid)
8fe81c50 467 {
5c6e38d7 468 $sql = 'DELETE v.*, a.*
469 FROM survey_votes AS v
470 LEFT JOIN survey_answers AS a
471 ON a.vote_id=v.id
472 WHERE v.survey_id={?};';
473 return XDB::execute($sql, $sid);
8fe81c50 474 }
475 // }}}
476
5c6e38d7 477 // }}}
478}
479// }}}
8fe81c50 480
5c6e38d7 481// {{{ abstract class SurveyQuestion
482abstract class SurveyQuestion
483{
484 // {{{ common properties, constructor, and basic methods
485 private $question;
486 private $comment;
8fe81c50 487
5c6e38d7 488 public function __construct($args)
8fe81c50 489 {
5c6e38d7 490 $this->update($args);
8fe81c50 491 }
492
5c6e38d7 493 public function update($a)
8fe81c50 494 {
5c6e38d7 495 $this->question = $a['question'];
496 $this->comment = $a['comment'];
8fe81c50 497 }
498
5c6e38d7 499 abstract protected function getQuestionType();
500 // }}}
501
502 // {{{ function toArray() : converts to array
8fe81c50 503 public function toArray()
504 {
5c6e38d7 505 return array('type' => $this->getQuestionType(), 'question' => $this->question, 'comment' => $this->comment);
8fe81c50 506 }
5c6e38d7 507 // }}}
8fe81c50 508
5c6e38d7 509 // {{{ function checkSyntax() : checks question elements (before storing into database), not currently needed (with new structure)
510 protected function checkSyntax()
8fe81c50 511 {
5c6e38d7 512 return null;
8fe81c50 513 }
514 // }}}
515
5c6e38d7 516 // {{{ function checkAnswer : returns a correctly formatted answer (or nothing empty string if error)
517 public function checkAnswer($ans)
8fe81c50 518 {
9f01f40b 519 return null;
8fe81c50 520 }
521 // }}}
522
4b8c8634 523 // {{{ functions regarding the results of a survey
9f01f40b 524 public function buildResultRequest()
525 {
526 return '';
527 }
4b8c8634 528
529 public function getCSVColumn()
530 {
531 return $this->question;
532 }
8fe81c50 533 // }}}
534}
535// }}}
536
fd0084aa 537// {{{ abstract class SurveySimple and its derived classes : "open" questions
9f01f40b 538// {{{ abstract class SurveySimple extends SurveyQuestion
8fe81c50 539abstract class SurveySimple extends SurveyQuestion
540{
5c6e38d7 541 public function checkAnswer($ans)
8fe81c50 542 {
9f01f40b 543 return array($ans);
544 }
545
546 public function buildResultRequest()
547 {
548 $sql = 'SELECT answer
549 FROM survey_answers
550 WHERE vote_id IN (SELECT id FROM survey_votes WHERE survey_id={?})
551 AND question_id={?}
552 ORDER BY RAND()
553 LIMIT 5;';
554 return $sql;
8fe81c50 555 }
556}
9f01f40b 557// }}}
8fe81c50 558
559// {{{ class SurveyText extends SurveySimple : simple text field, allowing a few words
560class SurveyText extends SurveySimple
561{
5c6e38d7 562 public function getQuestionType()
8fe81c50 563 {
564 return "text";
565 }
566}
567// }}}
568
569// {{{ class SurveyTextarea extends SurveySimple : textarea field, allowing longer comments
570class SurveyTextarea extends SurveySimple
571{
5c6e38d7 572 public function getQuestionType()
8fe81c50 573 {
574 return "textarea";
575 }
576}
577// }}}
578
579// {{{ class SurveyNum extends SurveySimple : allows numerical answers
580class SurveyNum extends SurveySimple
581{
5c6e38d7 582 public function checkAnswer($ans)
8fe81c50 583 {
9f01f40b 584 return array(intval($ans));
8fe81c50 585 }
586
587 protected function getQuestionType()
588 {
589 return "num";
590 }
591}
592// }}}
593// }}}
594
9f01f40b 595// {{{ abstract class SurveyList and its derived classes : restricted questions that allows only a list of possible answers
596// {{{ abstract class SurveyList extends SurveyQuestion
5c6e38d7 597abstract class SurveyList extends SurveyQuestion
8fe81c50 598{
5c6e38d7 599 protected $choices;
8fe81c50 600
5c6e38d7 601 public function update($args)
8fe81c50 602 {
603 parent::update($args);
0f396347 604 $this->choices = array();
605 foreach ($args['options'] as $val) {
606 if (trim($val)) {
607 $this->choices[] = $val;
608 }
609 }
8fe81c50 610 }
611
5c6e38d7 612 public function toArray()
8fe81c50 613 {
5c6e38d7 614 $rArr = parent::toArray();
8fe81c50 615 $rArr['choices'] = $this->choices;
8fe81c50 616 return $rArr;
617 }
618
9f01f40b 619 public function buildResultRequest()
620 {
621 $sql = 'SELECT answer, COUNT(id) AS count
622 FROM survey_answers
623 WHERE vote_id IN (SELECT id FROM survey_votes WHERE survey_id={?})
624 AND question_id={?}
625 GROUP BY answer ASC';
626 return $sql;
627 }
4b8c8634 628
629 public function getCSVColumn()
630 {
631 $r = parent::getCSVColumn();
632 if (empty($this->choices)) {
633 return $r;
634 }
635 $r .= ' [0 => '.$this->choices[0];
636 for ($k = 1; $k < count($this->choices); $k++) {
637 $r .= ', '.$k.' => '.$this->choices[$k];
638 }
639 $r .= ']';
640 return $r;
641 }
8fe81c50 642}
9f01f40b 643// }}}
8fe81c50 644
645// {{{ class SurveyRadio extends SurveyList : radio question, allows one answer among the list offered
646class SurveyRadio extends SurveyList
647{
5c6e38d7 648 public function checkAnswer($ans)
8fe81c50 649 {
9f01f40b 650 return (array_key_exists($ans, $this->choices))? array($ans) : null;
8fe81c50 651 }
652
653 protected function getQuestionType()
654 {
655 return "radio";
656 }
657}
658// }}}
659
660// {{{ class SurveyCheckbox extends SurveyList : checkbox question, allows any number of answers among the list offered
661class SurveyCheckbox extends SurveyList
662{
5c6e38d7 663 public function checkAnswer($ans)
8fe81c50 664 {
9f01f40b 665 $rep = array();
5c6e38d7 666 foreach ($ans as $a) {
9f01f40b 667 $a = intval($a);
668 if (array_key_exists($a, $this->choices)) {
669 $rep[] = $a;
8fe81c50 670 }
671 }
9f01f40b 672 return (count($rep) == 0)? null : $rep;
8fe81c50 673 }
674
675 protected function getQuestionType()
676 {
677 return "checkbox";
678 }
679}
680// }}}
681// }}}
682
8fe81c50 683// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
684?>