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