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