Commit | Line | Data |
---|---|---|
8fe81c50 | 1 | <?php |
2 | /*************************************************************************** | |
9f5bd98e | 3 | * Copyright (C) 2003-2010 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 |
23 | class 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 | } |
0dc8c5a4 | 86 | $args['promos'] = str_replace(' ', '', $args['promos']); |
5c6e38d7 | 87 | $this->promos = ($args['promos'] == '' || preg_match('#^(\d{4}-?|(\d{4})?-\d{4})(,(\d{4}-?|(\d{4})?-\d{4}))*$#', $args['promos']))? $args['promos'] : '#'; |
8fe81c50 | 88 | } |
89 | // }}} | |
90 | ||
5c6e38d7 | 91 | // {{{ functions to access general information |
92 | public function isMode($mode) | |
8fe81c50 | 93 | { |
5c6e38d7 | 94 | return ($this->mode == $mode); |
8fe81c50 | 95 | } |
96 | ||
5c6e38d7 | 97 | public function checkPromo($promo) |
8fe81c50 | 98 | { |
4b8c8634 | 99 | if ($this->promos == '') { |
100 | return true; | |
101 | } | |
fd0084aa | 102 | $promos = explode(',', $this->promos); |
5c6e38d7 | 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) || | |
80bb82f6 PC |
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 ))) { | |
5c6e38d7 | 110 | return true; |
111 | } | |
8fe81c50 | 112 | } |
5c6e38d7 | 113 | return false; |
8fe81c50 | 114 | } |
115 | ||
5c6e38d7 | 116 | public function isValid() |
8fe81c50 | 117 | { |
5c6e38d7 | 118 | return $this->valid; |
8fe81c50 | 119 | } |
120 | ||
5c6e38d7 | 121 | public function isEnded() |
8fe81c50 | 122 | { |
5c6e38d7 | 123 | return (strtotime($this->end) - time() <= 0); |
8fe81c50 | 124 | } |
8fe81c50 | 125 | |
5c6e38d7 | 126 | public function getTitle() |
8fe81c50 | 127 | { |
5c6e38d7 | 128 | return $this->title; |
8fe81c50 | 129 | } |
130 | // }}} | |
131 | ||
9f01f40b | 132 | // {{{ function toArray() : converts a question (or the whole survey) to array, with results if the survey is ended |
5c6e38d7 | 133 | public function toArray($i = 'all') |
8fe81c50 | 134 | { |
9f01f40b | 135 | if ($i != 'all' && $i != 'root') { // if a specific question is requested, then just returns this question converted to array |
5c6e38d7 | 136 | $i = intval($i); |
137 | if (array_key_exists($i, $this->questions)) { | |
138 | return $this->questions[$i]->toArray(); | |
8fe81c50 | 139 | } else { |
5c6e38d7 | 140 | return null; |
8fe81c50 | 141 | } |
9f01f40b | 142 | } else { // else returns the root converted to array in any case |
5c6e38d7 | 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 | } | |
9f01f40b | 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 | |
5c6e38d7 | 161 | $qArr = array(); |
162 | for ($k = 0; $k < count($this->questions); $k++) { | |
163 | $q = $this->questions[$k]->toArray(); | |
164 | $q['id'] = $k; | |
9f01f40b | 165 | if ($this->isEnded()) { // if the survey is ended, then adds here the results of this question |
02d94468 | 166 | $q['result'] = $this->questions[$k]->getResultArray($this->id, $k); |
9f01f40b | 167 | } |
5c6e38d7 | 168 | $qArr[$k] = $q; |
8fe81c50 | 169 | } |
5c6e38d7 | 170 | $a['questions'] = $qArr; |
8fe81c50 | 171 | } |
5c6e38d7 | 172 | return $a; |
8fe81c50 | 173 | } |
174 | } | |
5c6e38d7 | 175 | // }}} |
8fe81c50 | 176 | |
4b8c8634 | 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'); | |
8e9f416e | 187 | $qids = array(); |
4b8c8634 | 188 | for ($qid = 0; $qid < $nbq; $qid++) { |
8e9f416e | 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 | |
4b8c8634 | 191 | } |
8e9f416e | 192 | $nbf = count($line); |
4b8c8634 | 193 | $users = array(); |
194 | if ($this->isMode(self::MODE_XIDENT)) { // if the mode is non anonymous | |
7f376ae0 | 195 | $users = User::getBulkUsersWithUIDs(XDB::fetchAllAssoc('vid', 'SELECT v.id AS vid, v.uid |
ee682285 FB |
196 | FROM survey_votes AS v |
197 | WHERE v.survey_id = {?} | |
198 | ORDER BY vid ASC', | |
199 | $this->id)); | |
4b8c8634 | 200 | } |
8e9f416e | 201 | $sql = 'SELECT v.id AS vid, a.question_id AS qid, a.answer AS answer |
4b8c8634 | 202 | FROM survey_votes AS v |
ee22a54b | 203 | INNER JOIN survey_answers AS a ON a.vote_id=v.id |
4b8c8634 | 204 | WHERE v.survey_id={?} |
ee22a54b | 205 | ORDER BY vid ASC, qid ASC, answer ASC'; |
4b8c8634 | 206 | $res = XDB::iterator($sql, $this->id); // retrieves all answers from database |
4b8c8634 | 207 | $vid = -1; |
208 | $vid_ = 0; | |
ee682285 | 209 | while (($cur = $res->next()) != null) { |
4b8c8634 | 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']; | |
8e9f416e | 213 | $line = array_fill(0, $nbf, ''); // creates an array full of empty string |
4b8c8634 | 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 | |
ee682285 FB |
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(); | |
4b8c8634 | 220 | } |
221 | } | |
222 | $vid_++; | |
223 | } | |
8e9f416e | 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 | |
4b8c8634 | 237 | } |
4b8c8634 | 238 | } |
239 | fputcsv($csv, $line, $sep, $enc); // stores the last line into $csv_output | |
240 | return $csv_output; | |
241 | } | |
242 | // }}} | |
243 | ||
5c6e38d7 | 244 | // {{{ function factory($type, $args) : builds a question according to the given type |
245 | public function factory($t, $args) | |
8fe81c50 | 246 | { |
5c6e38d7 | 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); | |
02d94468 | 258 | case 'radiotable': |
259 | return new SurveyRadioTable($args); | |
260 | case 'checkboxtable': | |
261 | return new SurveyCheckboxTable($args); | |
5c6e38d7 | 262 | default: |
263 | return null; | |
8fe81c50 | 264 | } |
5c6e38d7 | 265 | } |
266 | // }}} | |
267 | ||
268 | // {{{ questions manipulation functions | |
269 | public function addQuestion($i, $c) | |
270 | { | |
cd129064 | 271 | $i = intval($i); |
5c6e38d7 | 272 | if ($this->valid || $i > count($this->questions)) { |
273 | return false; | |
274 | } else { | |
275 | array_splice($this->questions, $i, 0, array($c)); | |
8fe81c50 | 276 | return true; |
277 | } | |
8fe81c50 | 278 | } |
279 | ||
5c6e38d7 | 280 | public function delQuestion($i) |
8fe81c50 | 281 | { |
cd129064 | 282 | $i = intval($i); |
5c6e38d7 | 283 | if ($this->valid || !array_key_exists($i, $this->questions)) { |
284 | return false; | |
285 | } else { | |
286 | array_splice($this->questions, $i, 1); | |
8fe81c50 | 287 | return true; |
288 | } | |
8fe81c50 | 289 | } |
8fe81c50 | 290 | |
5c6e38d7 | 291 | public function editQuestion($i, $a) |
8fe81c50 | 292 | { |
5c6e38d7 | 293 | if ($i == 'root') { |
8fe81c50 | 294 | $this->update($a); |
8fe81c50 | 295 | } else { |
5c6e38d7 | 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); | |
8fe81c50 | 301 | } |
8fe81c50 | 302 | } |
5c6e38d7 | 303 | return true; |
8fe81c50 | 304 | } |
305 | // }}} | |
306 | ||
5c6e38d7 | 307 | // {{{ function checkSyntax() : checks syntax of the questions (currently the root only) before storing the survey in database |
308 | private static $errorMessages = array( | |
cd723c19 | 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" | |
5c6e38d7 | 311 | ); |
312 | ||
313 | public function checkSyntax() | |
8fe81c50 | 314 | { |
5c6e38d7 | 315 | $rArr = array(); |
0f396347 | 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"]); | |
5c6e38d7 | 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"]); | |
8fe81c50 | 323 | } |
5c6e38d7 | 324 | return (empty($rArr))? null : $rArr; |
8fe81c50 | 325 | } |
5c6e38d7 | 326 | // }}} |
8fe81c50 | 327 | |
9f01f40b | 328 | // {{{ functions that manipulate surveys in database |
5c6e38d7 | 329 | // {{{ static function retrieveList() : gets the list of available survey (current, old and not validated surveys) |
330 | public static function retrieveList($type, $tpl = true) | |
8fe81c50 | 331 | { |
5c6e38d7 | 332 | switch ($type) { |
5c6e38d7 | 333 | case 'c': |
334 | case 'current': | |
797d27db | 335 | $where = 'end > NOW()'; |
5c6e38d7 | 336 | break; |
337 | case 'o': | |
338 | case 'old': | |
797d27db | 339 | $where = 'end <= NOW()'; |
5c6e38d7 | 340 | break; |
341 | default: | |
8fe81c50 | 342 | return null; |
343 | } | |
5c6e38d7 | 344 | $sql = 'SELECT id, title, end, mode |
06f4daf9 | 345 | FROM surveys |
8e9f416e | 346 | WHERE '.$where.' |
347 | ORDER BY end DESC;'; | |
5c6e38d7 | 348 | if ($tpl) { |
349 | return XDB::iterator($sql); | |
350 | } else { | |
351 | return XDB::iterRow($sql); | |
352 | } | |
8fe81c50 | 353 | } |
354 | // }}} | |
355 | ||
5c6e38d7 | 356 | // {{{ static function retrieveSurvey() : gets a survey in database (and unserialize the survey object structure) |
357 | public static function retrieveSurvey($sid) | |
8fe81c50 | 358 | { |
797d27db | 359 | $sql = 'SELECT questions, title, description, end, mode, promos |
06f4daf9 | 360 | FROM surveys |
5c6e38d7 | 361 | WHERE id={?}'; |
362 | $res = XDB::query($sql, $sid); | |
363 | $data = $res->fetchOneAssoc(); | |
364 | if (is_null($data) || !is_array($data)) { | |
365 | return null; | |
8fe81c50 | 366 | } |
797d27db | 367 | $survey = new Survey($data, $sid, true, unserialize($data['questions'])); |
5c6e38d7 | 368 | return $survey; |
8fe81c50 | 369 | } |
370 | // }}} | |
371 | ||
5c6e38d7 | 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) | |
8fe81c50 | 374 | { |
797d27db | 375 | $sql = 'SELECT title, description, end, mode, promos |
06f4daf9 | 376 | FROM surveys |
5c6e38d7 | 377 | WHERE id={?}'; |
378 | $res = XDB::query($sql, $sid); | |
379 | return $res->fetchOneAssoc(); | |
8fe81c50 | 380 | } |
381 | // }}} | |
8fe81c50 | 382 | |
797d27db | 383 | // {{{ static function retrieveSurveyReq() : gets a survey request to validate |
384 | public static function retrieveSurveyReq($id) | |
385 | { | |
797d27db | 386 | $surveyreq = Validate::get_request_by_id($id); |
387 | if ($surveyreq == null) { | |
388 | return null; | |
389 | } | |
390 | $data = array('title' => $surveyreq->title, | |
391 | 'description' => $surveyreq->description, | |
392 | 'end' => $surveyreq->end, | |
393 | 'mode' => $surveyreq->mode, | |
394 | 'promos' => $surveyreq->promos); | |
395 | $survey = new Survey($data, $id, false, $surveyreq->questions); | |
396 | return $survey; | |
397 | } | |
398 | // }}} | |
399 | ||
5c6e38d7 | 400 | // {{{ function proposeSurvey() : stores a proposition of survey in database (before validation) |
401 | public function proposeSurvey() | |
8fe81c50 | 402 | { |
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) { |
06f4daf9 | 412 | $sql = 'UPDATE surveys |
797d27db | 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 { | |
797d27db | 422 | $surveyreq = Validate::get_request_by_id($this->id); |
423 | if ($surveyreq == null) { | |
424 | return false; | |
425 | } | |
426 | return $surveyreq->updateReq($this->title, $this->description, $this->end, $this->mode, $this->promos, $this->questions); | |
8fe81c50 | 427 | } |
8fe81c50 | 428 | } |
5c6e38d7 | 429 | // }}} |
8fe81c50 | 430 | |
5c6e38d7 | 431 | // {{{ functions vote() and hasVoted() : handles vote to a survey |
432 | public function vote($uid, $args) | |
433 | { | |
1bf36cd1 | 434 | XDB::execute('INSERT INTO survey_votes |
7f376ae0 | 435 | SET survey_id = {?}, uid = {?};', $this->id, $uid); // notes the user as having voted |
5c6e38d7 | 436 | $vid = XDB::insertId(); |
437 | for ($i = 0; $i < count($this->questions); $i++) { | |
438 | $ans = $this->questions[$i]->checkAnswer($args[$i]); | |
9f01f40b | 439 | if (!is_null($ans) && is_array($ans)) { |
440 | foreach ($ans as $a) { | |
441 | XDB::execute('INSERT INTO survey_answers | |
442 | SET vote_id = {?}, | |
443 | question_id = {?}, | |
444 | answer = {?}', $vid, $i, $a); | |
445 | } | |
5c6e38d7 | 446 | } |
447 | } | |
8fe81c50 | 448 | } |
449 | ||
5c6e38d7 | 450 | public function hasVoted($uid) |
8fe81c50 | 451 | { |
1bf36cd1 SJ |
452 | $res = XDB::query('SELECT id |
453 | FROM survey_votes | |
7f376ae0 | 454 | WHERE survey_id = {?} AND uid = {?};', $this->id, $uid); // checks whether the user has already voted |
5c6e38d7 | 455 | return ($res->numRows() != 0); |
8fe81c50 | 456 | } |
5c6e38d7 | 457 | // }}} |
8fe81c50 | 458 | |
5c6e38d7 | 459 | // {{{ static function deleteSurvey() : deletes a survey (and all its votes) |
460 | public static function deleteSurvey($sid) | |
8fe81c50 | 461 | { |
5c6e38d7 | 462 | $sql = 'DELETE s.*, v.*, a.* |
06f4daf9 | 463 | FROM surveys AS s |
5c6e38d7 | 464 | LEFT JOIN survey_votes AS v |
465 | ON v.survey_id=s.id | |
466 | LEFT JOIN survey_answers AS a | |
467 | ON a.vote_id=v.id | |
468 | WHERE s.id={?};'; | |
469 | return XDB::execute($sql, $sid); | |
8fe81c50 | 470 | } |
471 | // }}} | |
472 | ||
5c6e38d7 | 473 | // {{{ static function purgeVotes() : clears all votes concerning a survey (I'm not sure whether it's really useful) |
474 | public static function purgeVotes($sid) | |
8fe81c50 | 475 | { |
5c6e38d7 | 476 | $sql = 'DELETE v.*, a.* |
477 | FROM survey_votes AS v | |
478 | LEFT JOIN survey_answers AS a | |
479 | ON a.vote_id=v.id | |
480 | WHERE v.survey_id={?};'; | |
481 | return XDB::execute($sql, $sid); | |
8fe81c50 | 482 | } |
483 | // }}} | |
484 | ||
5c6e38d7 | 485 | // }}} |
486 | } | |
487 | // }}} | |
8fe81c50 | 488 | |
5c6e38d7 | 489 | // {{{ abstract class SurveyQuestion |
490 | abstract class SurveyQuestion | |
491 | { | |
492 | // {{{ common properties, constructor, and basic methods | |
493 | private $question; | |
494 | private $comment; | |
8fe81c50 | 495 | |
5c6e38d7 | 496 | public function __construct($args) |
8fe81c50 | 497 | { |
5c6e38d7 | 498 | $this->update($args); |
8fe81c50 | 499 | } |
500 | ||
5c6e38d7 | 501 | public function update($a) |
8fe81c50 | 502 | { |
5c6e38d7 | 503 | $this->question = $a['question']; |
504 | $this->comment = $a['comment']; | |
8fe81c50 | 505 | } |
506 | ||
5c6e38d7 | 507 | abstract protected function getQuestionType(); |
508 | // }}} | |
509 | ||
510 | // {{{ function toArray() : converts to array | |
8fe81c50 | 511 | public function toArray() |
512 | { | |
5c6e38d7 | 513 | return array('type' => $this->getQuestionType(), 'question' => $this->question, 'comment' => $this->comment); |
8fe81c50 | 514 | } |
5c6e38d7 | 515 | // }}} |
8fe81c50 | 516 | |
5c6e38d7 | 517 | // {{{ function checkSyntax() : checks question elements (before storing into database), not currently needed (with new structure) |
518 | protected function checkSyntax() | |
8fe81c50 | 519 | { |
5c6e38d7 | 520 | return null; |
8fe81c50 | 521 | } |
522 | // }}} | |
523 | ||
02d94468 | 524 | // {{{ function checkAnswer : returns a correct answer (or a null value if error) |
5c6e38d7 | 525 | public function checkAnswer($ans) |
8fe81c50 | 526 | { |
9f01f40b | 527 | return null; |
8fe81c50 | 528 | } |
529 | // }}} | |
530 | ||
4b8c8634 | 531 | // {{{ functions regarding the results of a survey |
02d94468 | 532 | abstract public function getResultArray($sid, $qid); |
4b8c8634 | 533 | |
8e9f416e | 534 | public function formatAnswer($ans) |
535 | { | |
536 | return $ans; | |
537 | } | |
538 | ||
539 | public function getCSVColumns() | |
4b8c8634 | 540 | { |
541 | return $this->question; | |
542 | } | |
8fe81c50 | 543 | // }}} |
544 | } | |
545 | // }}} | |
546 | ||
fd0084aa | 547 | // {{{ abstract class SurveySimple and its derived classes : "open" questions |
9f01f40b | 548 | // {{{ abstract class SurveySimple extends SurveyQuestion |
8fe81c50 | 549 | abstract class SurveySimple extends SurveyQuestion |
550 | { | |
5c6e38d7 | 551 | public function checkAnswer($ans) |
8fe81c50 | 552 | { |
9f01f40b | 553 | return array($ans); |
554 | } | |
555 | ||
02d94468 | 556 | public function getResultArray($sid, $qid) |
9f01f40b | 557 | { |
558 | $sql = 'SELECT answer | |
559 | FROM survey_answers | |
560 | WHERE vote_id IN (SELECT id FROM survey_votes WHERE survey_id={?}) | |
561 | AND question_id={?} | |
562 | ORDER BY RAND() | |
563 | LIMIT 5;'; | |
02d94468 | 564 | $res = XDB::query($sql, $sid, $qid); |
565 | return $res->fetchAllAssoc(); | |
8fe81c50 | 566 | } |
567 | } | |
9f01f40b | 568 | // }}} |
8fe81c50 | 569 | |
570 | // {{{ class SurveyText extends SurveySimple : simple text field, allowing a few words | |
571 | class SurveyText extends SurveySimple | |
572 | { | |
5c6e38d7 | 573 | public function getQuestionType() |
8fe81c50 | 574 | { |
575 | return "text"; | |
576 | } | |
577 | } | |
578 | // }}} | |
579 | ||
580 | // {{{ class SurveyTextarea extends SurveySimple : textarea field, allowing longer comments | |
581 | class SurveyTextarea extends SurveySimple | |
582 | { | |
5c6e38d7 | 583 | public function getQuestionType() |
8fe81c50 | 584 | { |
585 | return "textarea"; | |
586 | } | |
587 | } | |
588 | // }}} | |
589 | ||
590 | // {{{ class SurveyNum extends SurveySimple : allows numerical answers | |
591 | class SurveyNum extends SurveySimple | |
592 | { | |
5c6e38d7 | 593 | public function checkAnswer($ans) |
8fe81c50 | 594 | { |
9f01f40b | 595 | return array(intval($ans)); |
8fe81c50 | 596 | } |
597 | ||
598 | protected function getQuestionType() | |
599 | { | |
600 | return "num"; | |
601 | } | |
602 | } | |
603 | // }}} | |
604 | // }}} | |
605 | ||
9f01f40b | 606 | // {{{ abstract class SurveyList and its derived classes : restricted questions that allows only a list of possible answers |
607 | // {{{ abstract class SurveyList extends SurveyQuestion | |
5c6e38d7 | 608 | abstract class SurveyList extends SurveyQuestion |
8fe81c50 | 609 | { |
5c6e38d7 | 610 | protected $choices; |
8fe81c50 | 611 | |
5c6e38d7 | 612 | public function update($args) |
8fe81c50 | 613 | { |
614 | parent::update($args); | |
0f396347 | 615 | $this->choices = array(); |
02d94468 | 616 | foreach ($args['choices'] as $val) { |
eadfa7ba | 617 | if (trim($val) || trim($val) == '0') { |
0f396347 | 618 | $this->choices[] = $val; |
619 | } | |
620 | } | |
8fe81c50 | 621 | } |
622 | ||
5c6e38d7 | 623 | public function toArray() |
8fe81c50 | 624 | { |
5c6e38d7 | 625 | $rArr = parent::toArray(); |
8fe81c50 | 626 | $rArr['choices'] = $this->choices; |
8fe81c50 | 627 | return $rArr; |
628 | } | |
629 | ||
02d94468 | 630 | public function getResultArray($sid, $qid) |
9f01f40b | 631 | { |
632 | $sql = 'SELECT answer, COUNT(id) AS count | |
633 | FROM survey_answers | |
634 | WHERE vote_id IN (SELECT id FROM survey_votes WHERE survey_id={?}) | |
635 | AND question_id={?} | |
636 | GROUP BY answer ASC'; | |
02d94468 | 637 | $res = XDB::query($sql, $sid, $qid); |
638 | return $res->fetchAllAssoc(); | |
9f01f40b | 639 | } |
4b8c8634 | 640 | |
8e9f416e | 641 | public function formatAnswer($ans) |
4b8c8634 | 642 | { |
8e9f416e | 643 | if (array_key_exists($ans, $this->choices)) { |
644 | return $this->choices[$ans]; | |
645 | } else { | |
646 | return null; | |
4b8c8634 | 647 | } |
4b8c8634 | 648 | } |
8fe81c50 | 649 | } |
9f01f40b | 650 | // }}} |
8fe81c50 | 651 | |
652 | // {{{ class SurveyRadio extends SurveyList : radio question, allows one answer among the list offered | |
653 | class SurveyRadio extends SurveyList | |
654 | { | |
5c6e38d7 | 655 | public function checkAnswer($ans) |
8fe81c50 | 656 | { |
02d94468 | 657 | $a = intval($ans); |
658 | return (array_key_exists($a, $this->choices))? array($a) : null; | |
8fe81c50 | 659 | } |
660 | ||
661 | protected function getQuestionType() | |
662 | { | |
663 | return "radio"; | |
664 | } | |
665 | } | |
666 | // }}} | |
667 | ||
668 | // {{{ class SurveyCheckbox extends SurveyList : checkbox question, allows any number of answers among the list offered | |
669 | class SurveyCheckbox extends SurveyList | |
670 | { | |
5c6e38d7 | 671 | public function checkAnswer($ans) |
8fe81c50 | 672 | { |
9f01f40b | 673 | $rep = array(); |
5c6e38d7 | 674 | foreach ($ans as $a) { |
9f01f40b | 675 | $a = intval($a); |
676 | if (array_key_exists($a, $this->choices)) { | |
677 | $rep[] = $a; | |
8fe81c50 | 678 | } |
679 | } | |
9f01f40b | 680 | return (count($rep) == 0)? null : $rep; |
8fe81c50 | 681 | } |
682 | ||
683 | protected function getQuestionType() | |
684 | { | |
685 | return "checkbox"; | |
686 | } | |
687 | } | |
688 | // }}} | |
689 | // }}} | |
690 | ||
02d94468 | 691 | // {{{ abstract class SurveyTable and its derived classes : table question, each column represents a choice, each line represents a question |
692 | // {{{ abstract class SurveyTable extends SurveyList | |
693 | abstract class SurveyTable extends SurveyList | |
694 | { | |
695 | protected $subquestions; | |
696 | ||
697 | public function update($args) | |
698 | { | |
699 | parent::update($args); | |
700 | $this->subquestions = array(); | |
701 | foreach ($args['subquestions'] as $val) { | |
702 | if (trim($val) || trim($val) == '0') { | |
703 | $this->subquestions[] = $val; | |
704 | } | |
705 | } | |
706 | } | |
707 | ||
708 | public function toArray() | |
709 | { | |
710 | $rArr = parent::toArray(); | |
711 | $rArr['subquestions'] = $this->subquestions; | |
712 | return $rArr; | |
713 | } | |
714 | ||
715 | public function getResultArray($sid, $qid) | |
716 | { | |
717 | $sql = 'SELECT answer, COUNT(id) AS count | |
718 | FROM survey_answers | |
719 | WHERE vote_id IN (SELECT id FROM survey_votes WHERE survey_id={?}) | |
720 | AND question_id={?} | |
721 | GROUP BY answer ASC'; | |
722 | $res = XDB::iterator($sql, $sid, $qid); | |
723 | $result = array(); | |
724 | for ($i = 0; $i < count($this->subquestions); $i++) { | |
725 | $result[$i] = array_fill(0, count($this->choices), 0); | |
726 | } | |
727 | while ($r = $res->next()) { | |
728 | list($i, $j) = explode(':', $r['answer']); | |
729 | $result[$i][$j] = $r['count']; | |
730 | } | |
731 | return $result; | |
732 | } | |
733 | ||
8e9f416e | 734 | public function formatAnswer($ans) |
02d94468 | 735 | { |
8e9f416e | 736 | list($q, $c) = explode(':', $ans); |
737 | if (array_key_exists($q, $this->subquestions) && array_key_exists($c, $this->choices)) { | |
738 | return array('id' => $q, 'answer' => $this->choices[$c]); | |
739 | } else { | |
740 | return null; | |
02d94468 | 741 | } |
8e9f416e | 742 | } |
743 | ||
744 | public function getCSVColumns() | |
745 | { | |
746 | $q = parent::getCSVColumns(); | |
747 | if (empty($this->subquestions)) { | |
748 | return $q; | |
749 | } | |
750 | $a = array(); | |
751 | for ($k = 0; $k < count($this->subquestions); $k++) { | |
752 | $a[$k] = $q.' : '.$this->subquestions[$k]; | |
02d94468 | 753 | } |
8e9f416e | 754 | return $a; |
02d94468 | 755 | } |
756 | } | |
757 | // }}} | |
758 | ||
759 | // {{{ class SurveyRadioTable extends SurveyTable : SurveyTable with radio type choices | |
760 | class SurveyRadioTable extends SurveyTable | |
761 | { | |
762 | public function checkAnswer($ans) | |
763 | { | |
764 | $rep = array(); | |
765 | foreach ($ans as $k => $a) { | |
766 | if (!array_key_exists($k, $this->subquestions)) { | |
767 | continue; | |
768 | } | |
769 | $a = intval($a); | |
770 | if (array_key_exists($a, $this->choices)) { | |
771 | $rep[] = $k . ':' . $a; | |
772 | } | |
773 | } | |
774 | return (count($rep) == 0)? null : $rep; | |
775 | } | |
776 | ||
777 | protected function getQuestionType() | |
778 | { | |
779 | return "radiotable"; | |
780 | } | |
781 | ||
782 | } | |
783 | // }}} | |
784 | ||
785 | // {{{ class SurveyCheckboxTable extends SurveyTable : SurveyTable with checkbox type choices | |
786 | class SurveyCheckboxTable extends SurveyTable | |
787 | { | |
788 | public function checkAnswer($ans) | |
789 | { | |
790 | $rep = array(); | |
791 | foreach ($ans as $k => $aa) { | |
792 | if (!array_key_exists($k, $this->subquestions)) { | |
793 | continue; | |
794 | } | |
795 | foreach ($aa as $a) { | |
796 | $a = intval($a); | |
797 | if (array_key_exists($a, $this->choices)) { | |
798 | $rep[] = $k . ':' . $a; | |
799 | } | |
800 | } | |
801 | } | |
802 | return (count($rep) == 0)? null : $rep; | |
803 | } | |
804 | ||
805 | protected function getQuestionType() | |
806 | { | |
807 | return "checkboxtable"; | |
808 | } | |
809 | ||
810 | } | |
811 | // }}} | |
812 | // }}} | |
813 | ||
8602c852 | 814 | // vim:set et sw=4 sts=4 ts=4 foldmethod=marker enc=utf-8: |
8fe81c50 | 815 | ?> |