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