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