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