2 /***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 // {{{ class Survey : root of any survey, contains all questions
25 // {{{ static properties and functions, regarding survey modes and question types
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");
36 public static function getModes($long = true
) {
37 return ($long)? self
::$longModes : self
::$shortModes;
40 private static $types = array('text' => 'texte court',
41 'textarea' => 'texte long',
42 'num' => 'numérique',
44 'checkbox' => 'checkbox',
45 'personal' => 'informations personnelles');
47 public static function getTypes()
52 public static function isType($t)
54 return array_key_exists($t, self
::$types);
58 // {{{ properties, constructor and basic methods
68 public function __construct($args, $id = -1, $valid = false
, $questions = null
)
72 $this->valid
= $valid;
73 $this->questions
= ($questions == null
)?
array() : $questions;
76 public function update($args)
78 $this->title
= $args['title'];
79 $this->description
= $args['description'];
80 if (preg_match('#^\d{2}/\d{2}/\d{4}$#', $args['end'])) {
81 $this->end
= preg_replace('#^(\d{2})/(\d{2})/(\d{4})$#', '\3-\2-\1', $args['end']);
83 $this->end
= (preg_match('#^\d{4}-\d{2}-\d{2}$#', $args['end']))?
$args['end'] : '#';
85 $this->mode
= $args['mode'];
86 if ($args['mode'] == 0) {
89 $this->promos
= ($args['promos'] == '' ||
preg_match('#^(\d{4}-?|(\d{4})?-\d{4})(,(\d{4}-?|(\d{4})?-\d{4}))*$#', $args['promos']))?
$args['promos'] : '#';
93 // {{{ functions to access general information
94 public function isMode($mode)
96 return ($this->mode
== $mode);
99 public function checkPromo($promo)
101 $promos = explode('|', $this->promos
);
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)) {
113 public function isValid()
118 public function isEnded()
120 return (strtotime($this->end
) - time() <= 0);
123 public function getTitle()
129 // {{{ function toArray() : converts a question (or the whole survey) to array
130 public function toArray($i = 'all')
132 if ($i != 'all' && $i != 'root') {
134 if (array_key_exists($i, $this->questions
)) {
135 return $this->questions
[$i]->toArray();
140 $a = array('title' => $this->title
,
141 'description' => $this->description
,
143 'mode' => $this->mode
,
144 'promos' => $this->promos
,
145 'valid' => $this->valid
,
147 if ($this->id
!= -1) {
148 $a['id'] = $this->id
;
150 if ($i == 'all' && count($this->questions
) > 0) {
152 for ($k = 0; $k < count($this->questions
); $k++
) {
153 $q = $this->questions
[$k]->toArray();
157 $a['questions'] = $qArr;
164 // {{{ function factory($type, $args) : builds a question according to the given type
165 public function factory($t, $args)
169 return new SurveyText($args);
171 return new SurveyTextarea($args);
173 return new SurveyNum($args);
175 return new SurveyRadio($args);
177 return new SurveyCheckbox($args);
179 return new SurveyPersonal($args);
186 // {{{ questions manipulation functions
187 public function addQuestion($i, $c)
189 if ($this->valid ||
$i > count($this->questions
)) {
192 array_splice($this->questions
, $i, 0, array($c));
197 public function delQuestion($i)
199 if ($this->valid ||
!array_key_exists($i, $this->questions
)) {
202 array_splice($this->questions
, $i, 1);
207 public function editQuestion($i, $a)
213 if ($this->valid ||
!array_key_exists($i, $this->questions
)) {
216 $this->questions
[$i]->update($a);
223 // {{{ function checkSyntax() : checks syntax of the questions (currently the root only) before storing the survey in database
224 private static $errorMessages = array(
225 "dateformat" => "la date de fin de sondage est mal formattée : elle doit respecter la syntaxe dd/mm/aaaa",
226 "datepassed" => "la date de fin de sondage est déjà dépassée : vous devez préciser une date future",
227 "promoformat" => "les restrictions à certaines promotions sont mal formattées"
230 public function checkSyntax()
233 if (!preg_match('#^\d{4}-\d{2}-\d{2}$#', $this->end
)) {
234 $rArr[] = array('question' => 'root', 'error' => self
::$errorMessages["dateformat"]);
236 // checks that the end date given is not already passed
237 // (unless the survey has already been validated : an admin can have a validated survey expired)
238 if (!$this->valid
&& $this->isEnded()) {
239 $rArr[] = array('question' => 'root', 'error' => self
::$errorMessages["datepassed"]);
242 if ($this->promos
!= '' && !preg_match('#^(\d{4}-?|(\d{4})?-\d{4})(,(\d{4}-?|(\d{4})?-\d{4}))*$#', $this->promos
)) {
243 $rArr[] = array('question' => 'root', 'error' => self
::$errorMessages["promoformat"]);
245 return (empty($rArr))? null
: $rArr;
249 // {{{ functions that manipulates surveys in database
250 // {{{ static function retrieveList() : gets the list of available survey (current, old and not validated surveys)
251 public static function retrieveList($type, $tpl = true
)
260 $where = 'valid=1 AND end > NOW()';
264 $where = 'valid=1 AND end <= NOW()';
269 $sql = 'SELECT id, title, end, mode
273 return XDB
::iterator($sql);
275 return XDB
::iterRow($sql);
280 // {{{ static function retrieveSurvey() : gets a survey in database (and unserialize the survey object structure)
281 public static function retrieveSurvey($sid)
283 $sql = 'SELECT questions, title, description, end, mode, promos, valid
286 $res = XDB
::query($sql, $sid);
287 $data = $res->fetchOneAssoc();
288 if (is_null($data) ||
!is_array($data)) {
291 $survey = new Survey($data, $sid, (boolean
) $data['valid'], unserialize($data['questions']));
296 // {{{ static function retrieveSurveyInfo() : gets information about a survey (title, description, end date, restrictions) but does not unserialize the survey object structure
297 public static function retrieveSurveyInfo($sid)
299 $sql = 'SELECT title, description, end, mode, promos, valid
302 $res = XDB
::query($sql, $sid);
303 return $res->fetchOneAssoc();
307 // {{{ function proposeSurvey() : stores a proposition of survey in database (before validation)
308 public function proposeSurvey()
310 $sql = 'INSERT INTO survey_surveys
319 return XDB
::execute($sql, serialize($this->questions
), $this->title
, $this->description
, S
::v('uid'), $this->end
, $this->mode
, $this->promos
);
323 // {{{ function updateSurvey() : updates a survey in database (before validation)
324 public function updateSurvey()
326 if ($this->id
== -1) {
329 $sql = 'UPDATE survey_surveys
337 return XDB
::execute($sql, serialize($this->questions
), $this->title
, $this->description
, $this->end
, $this->mode
, $this->promos
, $this->id
);
341 // {{{ static function validateSurvey() : validates a survey
342 public static function validateSurvey($sid)
344 $sql = 'UPDATE survey_surveys
347 return XDB
::execute($sql, $sid);
351 // {{{ functions vote() and hasVoted() : handles vote to a survey
352 public function vote($uid, $args)
354 XDB
::execute('INSERT INTO survey_votes
355 SET survey_id={?}, user_id={?};', $this->id
, $uid); // notes the user as having voted
356 $vid = XDB
::insertId();
357 for ($i = 0; $i < count($this->questions
); $i++
) {
358 $ans = $this->questions
[$i]->checkAnswer($args[$i]);
360 XDB
::execute('INSERT INTO survey_answers
363 answer = {?}', $vid, $i, $ans);
368 public function hasVoted($uid)
370 $res = XDB
::query('SELECT id
372 WHERE survey_id={?} AND user_id={?};', $this->id
, $uid); // checks whether the user has already voted
373 return ($res->numRows() != 0);
377 // {{{ static function deleteSurvey() : deletes a survey (and all its votes)
378 public static function deleteSurvey($sid)
380 $sql = 'DELETE s.*, v.*, a.*
381 FROM survey_surveys AS s
382 LEFT JOIN survey_votes AS v
384 LEFT JOIN survey_answers AS a
387 return XDB
::execute($sql, $sid);
391 // {{{ static function purgeVotes() : clears all votes concerning a survey (I'm not sure whether it's really useful)
392 public static function purgeVotes($sid)
394 $sql = 'DELETE v.*, a.*
395 FROM survey_votes AS v
396 LEFT JOIN survey_answers AS a
398 WHERE v.survey_id={?};';
399 return XDB
::execute($sql, $sid);
407 // {{{ abstract class SurveyQuestion
408 abstract class SurveyQuestion
410 // {{{ common properties, constructor, and basic methods
414 public function __construct($args)
416 $this->update($args);
419 public function update($a)
421 $this->question
= $a['question'];
422 $this->comment
= $a['comment'];
425 abstract protected function getQuestionType();
428 // {{{ function toArray() : converts to array
429 public function toArray()
431 return array('type' => $this->getQuestionType(), 'question' => $this->question
, 'comment' => $this->comment
);
435 // {{{ function checkSyntax() : checks question elements (before storing into database), not currently needed (with new structure)
436 protected function checkSyntax()
442 // {{{ function checkAnswer : returns a correctly formatted answer (or nothing empty string if error)
443 public function checkAnswer($ans)
449 // {{{ function resultArray() : statistics on the results of the survey
450 //abstract protected function resultArray($sid, $where);
455 // {{{ abstract class SurveySimple extends SurveyQuestion : "opened" questions
456 abstract class SurveySimple
extends SurveyQuestion
458 public function checkAnswer($ans)
464 // {{{ class SurveyText extends SurveySimple : simple text field, allowing a few words
465 class SurveyText
extends SurveySimple
467 public function getQuestionType()
474 // {{{ class SurveyTextarea extends SurveySimple : textarea field, allowing longer comments
475 class SurveyTextarea
extends SurveySimple
477 public function getQuestionType()
484 // {{{ class SurveyNum extends SurveySimple : allows numerical answers
485 class SurveyNum
extends SurveySimple
487 public function checkAnswer($ans)
492 protected function getQuestionType()
500 // {{{ abstract class SurveyList extends SurveyTreeable : restricted questions that allows only a list of possible answers
501 abstract class SurveyList
extends SurveyQuestion
505 public function update($args)
507 parent
::update($args);
508 $this->choices
= explode('|', $args['options']);
511 public function toArray()
513 $rArr = parent
::toArray();
514 $rArr['choices'] = $this->choices
;
515 $rArr['options'] = implode('|', $this->choices
);
521 // {{{ class SurveyRadio extends SurveyList : radio question, allows one answer among the list offered
522 class SurveyRadio
extends SurveyList
524 public function checkAnswer($ans)
526 return (array_key_exists($ans, $this->choices
)) ?
$ans : "";
529 protected function getQuestionType()
536 // {{{ class SurveyCheckbox extends SurveyList : checkbox question, allows any number of answers among the list offered
537 class SurveyCheckbox
extends SurveyList
539 public function checkAnswer($ans)
542 foreach ($ans as $a) {
543 if (array_key_exists($a,$this->choices
)) {
550 protected function getQuestionType()
558 // {{{ class SurveyPersonal extends SurveyQuestion : allows easy and verified access to user's personal data (promotion, name...)
559 // actually this type of question should be suppressed (non anonymous surveys are possible with survey modes)
560 // and anyway it is not finished (checkAnswer implementation) : currently it does not store anything when a user votes
561 class SurveyPersonal
extends SurveyQuestion
565 public function update($args)
567 $args['question'] = "Informations personnelles";
568 parent
::update($args);
569 $this->perm
['promo'] = isset($args['promo'])?
1 : 0;
570 $this->perm
['name'] = isset($args['name'])?
1 : 0;
573 public function checkAnswer($ans)
575 if (intval($ans) == 1) {
583 protected function getQuestionType()
588 public function toArray()
590 $a = parent
::toArray();
591 $a['promo'] = $this->perm
['promo'];
592 $a['name'] = $this->perm
['name'];
598 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: