<?php
/***************************************************************************
- * Copyright (C) 2003-2007 Polytechnique.org *
+ * Copyright (C) 2003-2010 Polytechnique.org *
* http://opensource.polytechnique.org/ *
* *
* This program is free software; you can redistribute it and/or modify *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
***************************************************************************/
-// {{{ class Survey : static database managing functions
-class SurveyDB
+class Survey extends PlDBTableEntry implements SurveyQuestionContainer
{
- // {{{ static function retrieveList() : gets the list of available survey (current, old and not validated surveys)
- public static function retrieveList($type, $tpl = true)
- {
- switch ($type) {
- case 'w':
- case 'waiting' :
- $where = 'valid=0';
- break;
- case 'c':
- case 'current':
- $where = 'valid=1 AND end > NOW()';
- break;
- case 'o':
- case 'old':
- $where = 'valid=1 AND end <= NOW()';
- break;
- default:
- return null;
- }
- $sql = 'SELECT survey_id, title, end
- FROM survey_questions
- WHERE '.$where.';';
- if ($tpl) {
- return XDB::iterator($sql);
- } else {
- return XDB::iterRow($sql);
- }
- }
- // }}}
-
- // {{{ static function proposeSurvey() : stores a proposition of survey in database (before validation)
- public static function proposeSurvey($survey)
- {
- $sql = 'INSERT INTO survey_questions
- SET questions={?},
- title={?},
- description={?},
- author_id={?},
- end={?},
- promos={?},
- valid=0;';
- $data = $survey->storeArray();
- return XDB::execute($sql, serialize($survey), $data['question'], $data['comment'], S::v('uid'), $data['end'], $data['promos']);
- }
- // }}}
-
- // {{{ static function updateSurvey() : updates a survey in database (before validation)
- public static function updateSurvey($survey, $sid)
- {
- $sql = 'UPDATE survey_questions
- SET questions={?},
- title={?},
- description={?},
- end={?},
- promos={?}
- WHERE survey_id={?};';
- $data = $survey->storeArray();
- return XDB::execute($sql, serialize($survey), $data['question'], $data['comment'], $data['end'], $data['promos'], $sid);
- }
- // }}}
-
- // {{{ static function retrieveSurvey() : gets a survey in database (and unserialize the survey object structure)
- public static function retrieveSurvey($sid)
- {
- $sql = 'SELECT questions, title, description, end, promos, valid
- FROM survey_questions
- WHERE survey_id={?}';
- $res = XDB::query($sql, $sid);
- $data = $res->fetchOneAssoc();
- $survey = unserialize($data['questions']);
- if (isset($data['end'])) {
- $data['end'] = preg_replace('#^(\d{4})-(\d{2})-(\d{2})$#', '\3/\2/\1', $data['end']);
- }
- $survey->update(array('question' => $data['title'], 'comment' => $data['description'], 'end' => $data['end'], 'promos' => $data['promos']));
- $survey->setValid($data['valid']);
- return $survey;
- }
- // }}}
-
- // {{{ static function retrieveSurveyInfo() : gets information about a survey (title, description, end date, restrictions) but does not unserialize the survey object structure
- public static function retrieveSurveyInfo($sid)
- {
- $sql = 'SELECT title, description, end, promos, valid
- FROM survey_questions
- WHERE survey_id={?}';
- $res = XDB::query($sql, $sid);
- return $res->fetchOneAssoc();
- }
- // }}}
+ private $fetchQuestions = true;
+ public $questions = array();
- // {{{ static function validateSurvey() : validates a survey
- public static function validateSurvey($sid)
+ public function __construct()
{
- $sql = 'UPDATE survey_questions
- SET valid=1
- WHERE survey_id={?};';
- return XDB::execute($sql, $sid);
- }
- // }}}
-
- // {{{ static function deleteSurvey() : deletes a survey (and all its votes)
- public static function deleteSurvey($sid)
- {
- $sql1 = 'DELETE FROM survey_questions
- WHERE survey_id={?};';
- $sql2 = 'DELETE FROM survey_answers
- WHERE survey_id={?};';
- $sql3 = 'DELETE FROM survey_votes
- WHERE survey_id={?};';
- return (XDB::execute($sql1, $sid) && XDB::execute($sql2, $sid) && XDB::execute($sql3, $sid));
- }
- // }}}
-}
-// }}}
-
-// {{{ abstract class SurveyQuestion
-abstract class SurveyQuestion
-{
- // {{{ static properties and methods regarding question types
- private static $types = array('text' => 'texte court',
- 'textarea' => 'texte long',
- 'num' => 'numérique',
- 'radio' => 'radio',
- 'checkbox' => 'checkbox',
- 'personal' => 'informations personnelles');
-
- public static function getTypes()
- {
- return self::$types;
- }
-
- public static function isType($t)
- {
- return array_key_exists($t, self::$types);
- }
- // }}}
-
- // {{{ common properties, constructor, and basic methods
- private $survey_id;
- private $id;
- private $question;
- private $comment;
-
- protected function __construct($i, $args)
- {
- $this->id = $i;
- $this->update($args);
- }
-
- protected function update($a)
- {
- $this->question = $a['question'];
- $this->comment = $a['comment'];
- }
-
- protected function getId()
- {
- return $this->id;
- }
-
- abstract protected function getQuestionType();
- // }}}
-
- // {{{ tree manipulation methods : not implemented here (but definition needed)
- protected function addChildNested($i, $c)
- {
- return false;
- }
-
- protected function addChildAfter($i, $c)
- {
- return false;
+ parent::__construct('surveys');
+ $this->registerFieldValidator('shortname', 'ShortNameFieldValidator');
+ $this->registerFieldFormatter('voters', 'JSonFieldFormatter');
+ $this->registerFieldFormatter('viewers', 'JSonFieldFormatter');
}
- protected function delChild($i)
+ protected function postFetch()
{
- return false;
- }
- // }}}
-
- // {{{ function edit($i, $a) : searches and edits question $i
- protected function edit($i, $a)
- {
- if ($this->id == $i) {
- $this->update($a);
+ if (!$this->fetchQuestions) {
return true;
- } else {
- return false;
}
- }
- // }}}
-
- // {{{ functions toArray() and searchToArray($i) : (searches and) converts to array
- protected function toArray()
- {
- return $this->storeArray();
- }
-
- protected function searchToArray($i)
- {
- if ($this->id == $i) {
- return $this->storeArray();
- } else {
- return null;
+ $selector = new SurveyQuestion($this);
+ $selector->sid = $this->id;
+
+ $stack = array();
+ foreach ($selector as $question) {
+ $question = $question->typedInstance();
+ if (is_null($question->parent)) {
+ $this->addQuestion($question);
+ } else {
+ $pos = count($stack) - 1;
+ while ($stack[$pos]->qid != $question->parent) {
+ --$pos;
+ array_pop($stack);
+ }
+ Platal::assert(count($stack) > 0, "Invalid question structure");
+ Platal::assert($stack[$pos] instanceof SurveyQuestionContainer, "Invalid question type");
+ $stack[$pos]->addQuestion($question);
+ }
+ array_push($stack, $question);
}
+ return true;
}
- protected function storeArray()
- {
- return array('type' => $this->getQuestionType(), 'id' => $this->id, 'question' => $this->question, 'comment' => $this->comment);
- }
- // }}}
-
- // {{{ function checkSyntax() : checks question elements (before storing into database)
- protected function checkSyntax()
+ protected function postSave()
{
- return null;
- }
- // }}}
-
- // {{{ function vote() : handles vote
- protected function checkAnswer($ans)
- {
- return "";
- }
+ $questions = array();
+ $selector = new SurveyQuestion($this);
+ $selector->sid = $this->id;
+ $selector->delete();
- function vote($sid, $vid, $a)
- {
- $ans = $this->checkAnswer($a[$this->getId]);
- if ($ans != "") {
- XDB::execute(
- 'INSERT INTO survey_answers
- SET survey_id = {?},
- vote_id = {?},
- question_id = {?},
- answer = "{?}"', $sid, $vid, $id, $ans);
+ $this->reassignQuestionIds();
+ foreach ($this->questions as $question) {
+ $question->sid = $this->id;
+ $question->insert();
}
}
- // }}}
-}
-// }}}
-// {{{ abstract class SurveyTreeable extends SurveyQuestion : questions that allow nested ones
-abstract class SurveyTreeable extends SurveyQuestion
-{
- // {{{ common properties, constructor
- private $children;
-
- protected function __construct($i, $args)
+ public function addQuestion(SurveyQuestion $question, $pos = null)
{
- parent::__construct($i, $args);
- $this->children = array();
+ $question->parent = null;
+ if (is_null($pos)) {
+ $this->questions[] = $question;
+ } else {
+ array_splice($this->questions, $pos, 0, $question);
+ }
}
- // }}}
- // {{{ tree manipulation functions : actual implementation
- protected function hasChild()
+ public function newQuestion($type, $pos = null)
{
- return !is_null($this->children) && is_array($this->children);
+ $question = SurveyQuestion::instanceForType($this, $type);
+ $this->addQuestion($question, $pos);
+ return $question;
}
- protected function addChildNested($i, $c)
+ public function reassignQuestionIds()
{
- if ($this->getId() == $i) {
- if ($this->hasChild()) {
- array_unshift($this->children, $c);
+ $id = 0;
+ foreach ($this->questions as $question) {
+ $question->qid = $id;
+ if ($question instanceof SurveyQuestionContainer) {
+ $id = $question->reassignQuestionIds();
} else {
- $this->children = array($c);
+ $id++;
}
- return true;
- } else {
- foreach ($this->children as $child) {
- if ($child->addChildNested($i, $c)) {
- return true;
- }
- }
- return false;
}
+ return $id;
}
- protected function addChildAfter($i, $c)
+ public function export()
{
- $found = false;
- for ($k = 0; $k < count($this->children); $k++) {
- if ($this->children[$k]->getId() == $i) {
- $found = true;
- break;
- } else {
- if ($this->children[$k]->addChildAfter($i, $c)) {
- return true;
- }
- }
- }
- if ($found) {
- array_splice($this->children, $k+1, 0, array($c));
- return true;
+ $export = parent::export();
+ $export['questions'] = array();
+ foreach ($this->questions as $question) {
+ $export['questions'][] = $question->export();
}
- return false;
+ return $export;
}
- protected function delChild($i)
+ public function canSee(User $user)
{
- $found = false;
- for ($k = 0; $k < count($this->children); $k++) {
- if ($this->children[$k]->getId() == $i) {
- $found = true;
- break;
- } else {
- if ($this->children[$k]->delChild($i)) {
- return true;
- }
- }
- }
- if ($found) {
- array_splice($this->children, $k, 1);
+ if ($this->uid == $user->id() || $user->hasFlag('admin')) {
return true;
}
return false;
}
- // }}}
- // {{{ function edit() with tree support
- protected function edit($i, $a)
+ public static function get($name, $fetchQuestions = true)
{
- if ($this->getId() == $i) {
- $this->update($a);
- return true;
- } else {
- foreach ($this->children as $child) {
- if ($child->edit($i, $a)) {
- return true;
- }
- }
- return false;
+ if (is_array($name)) {
+ $name = $name[0];
}
- }
- // }}}
-
- // {{{ functions toArray() and searchToArray() with tree support
- protected function toArray()
- {
- if ($this->hasChild()) {
- $cArr = array();
- foreach ($this->children as $child) {
- $cArr[] = $child->toArray();
- }
- $a = $this->storeArray();
- $a['children'] = $cArr;
- return $a;
+ $survey = new Survey();
+ $survey->fetchQuestions = $fetchQuestions;
+ if (can_convert_to_integer($name)) {
+ $survey->id = $name;
} else {
- return $this->storeArray();
+ $survey->shortname = $name;
}
- }
-
- protected function searchToArray($i)
- {
- if ($this->getId() == $i) {
- return $this->storeArray();
- } else {
- foreach ($this->children as $child) {
- $a = $child->searchToArray($i);
- if (!is_null($a) && is_array($a)) {
- return $a;
- }
- }
+ if (!$survey->fetch()) {
return null;
}
+ return $survey;
}
- // }}}
-
- // {{{ function checkSyntax()
- protected function checkSyntax()
- {
- $rArr = array();
- foreach ($this->children as $child) {
- $a = $child->checkSyntax();
- if ($a != null) {
- $rArr[] = $a;
- }
- }
- return (empty($rArr))? null : $rArr;
- }
- // }}}
- // {{{ function vote()
- function vote($sid, $vid, $a)
+ public static function iterActive()
{
- parent::vote($sid, $vid, $a);
- if ($this->hasChild()) {
- foreach ($this->children as $c) {
- $c->vote($sid, $vid, $a);
- }
- }
+ $survey = new Survey();
+ $survey->fetchQuestions = false;
+ return $survey->iterateOnCondition('begin <= CURDATE() AND end >= CURDATE()
+ AND FIND_IN_SET(\'validated\', flags)');
}
- // }}}
}
-// }}}
-// {{{ class SurveyRoot extends SurveyTreeable : root of any survey, actually the only entry point (no public methods outside this class)
-class SurveyRoot extends SurveyTreeable
+class ShortNameFieldValidator implements PlDBTableFieldValidator
{
- // {{{ properties, constructor and basic methods
- private $last_id;
- private $beginning;
- private $end;
- private $promos;
- private $valid;
-
- public function __construct($args)
- {
- parent::__construct(0, $args);
- $this->last_id = 0;
- }
-
- public function update($args)
+ public function __construct(PlDBTableField $field, $value)
{
- parent::update($args);
- //$this->beginning = $args['beginning_year'] . "-" . $args['beginning_month'] . "-" . $args['beginning_day'];
- //$this->end = $args['end_year'] . "-" . $args['end_year'] . "-" . $args['end_day'];
- if (preg_match('#^\d{2}/\d{2}/\d{4}$#', $args['end'])) {
- $this->end = preg_replace('#^(\d{2})/(\d{2})/(\d{4})$#', '\3-\2-\1', $args['end']);
- } else {
- $this->end = (preg_match('#^\d{4}-\d{2}-\d{2}$#', $args['end']))? $args['end'] : '#';
+ if (can_convert_to_integer($value) || !preg_match('/^[a-z0-9]+[-_\.a-z0-9]*[a-z0-9]+$/i', $value)) {
+ throw new PlDBBadValueException($value, $field,
+ 'The shortname can only contain alphanumerical caracters, dashes, underscores and dots');
}
- $this->promos = ($args['promos'] == '' || preg_match('#^(\d{4}-?|(\d{4})?-\d{4})(,(\d{4}-?|(\d{4})?-\d{4}))*$#', $args['promos']))? $args['promos'] : '#';
- }
-
- private function getNextId()
- {
- $this->last_id++;
- return $this->last_id;
}
-
- public function setValid($v)
- {
- $this->valid = (intval($v) != 0);
- }
-
- public function isValid()
- {
- return $this->valid;
- }
-
- protected function getQuestionType()
- {
- return "root";
- }
- // }}}
-
- // {{{ function factory($type, $args) : builds a question according to the given type
- public function factory($t, $args)
- {
- $i = $this->getNextId();
- switch ($t) {
- case 'text':
- return new SurveyText($i, $args);
- case 'textarea':
- return new SurveyTextarea($i, $args);
- case 'num':
- return new SurveyNum($i, $args);
- case 'radio':
- return new SurveyRadio($i, $args);
- case 'checkbox':
- return new SurveyCheckbox($i, $args);
- case 'personal':
- return new SurveyPersonal($i, $args);
- default:
- return null;
- }
- }
- // }}}
-
- // {{{ methods needing public access
- public function addChildNested($i, $c)
- {
- return parent::addChildNested($i, $c);
- }
-
- public function addChildAfter($i, $c)
- {
- return parent::addChildAfter($i, $c);
- }
-
- public function delChild($i)
- {
- return parent::delChild($i);
- }
-
- public function edit($i, $a)
- {
- return parent::edit($i, $a);
- }
-
- public function toArray()
- {
- return parent::toArray();
- }
-
- public function searchToArray($i)
- {
- return parent::searchToArray($i);
- }
- // }}}
-
- // {{{ function storeArray()
- public function storeArray()
- {
- $rArr = parent::storeArray();
- $rArr['beginning'] = $this->beginning;
- $rArr['end'] = $this->end;
- $rArr['promos'] = $this->promos;
- return $rArr;
- }
- // }}}
-
- // {{{ function checkSyntax()
- private static $errorMessages = array(
- "dateformat" => "la date de fin de sondage est mal formattée : elle doit respecter la syntaxe dd/mm/aaaa",
- "datepassed" => "la date de fin de sondage est déjà dépassée : vous devez préciser une date future",
- "promoformat" => "les restrictions à certaines promotions sont mal formattées"
- );
-
- public function checkSyntax()
- {
- $rArr = parent::checkSyntax();
- if (!preg_match('#^\d{4}-\d{2}-\d{2}$#', $this->end)) {
- $rArr[] = array('question' => $this->getId(), 'error' => self::$errorMessages["dateformat"]);
- } else {
- if (strtotime($this->end) - time() <= 0) {
- $rArr[] = array('question' => $this->getId(), 'error' => self::$errorMessages["datepassed"]);
- }
- }
- if ($this->promos != '' && !preg_match('#^(\d{4}-?|(\d{4})?-\d{4})(,(\d{4}-?|(\d{4})?-\d{4}))*$#', $this->promos)) {
- $rArr[] = array('question' => $this->getId(), 'error' => self::$errorMessages["promoformat"]);
- }
- return (empty($rArr))? null : $rArr;
- }
- // }}}
}
-// }}}
-// {{{ abstract class SurveySimple extends SurveyQuestion : "opened" questions
-abstract class SurveySimple extends SurveyQuestion
+interface SurveyQuestionContainer
{
- protected function checkAnswer($ans)
- {
- return $ans;
- }
+ public function addQuestion(SurveyQuestion $question, $pos = null);
+ public function newQuestion($type, $pos = null);
+ public function reassignQuestionIds();
}
-// {{{ class SurveyText extends SurveySimple : simple text field, allowing a few words
-class SurveyText extends SurveySimple
+class SurveyQuestion extends PlDBTableEntry
{
- protected function getQuestionType()
- {
- return "text";
- }
-}
-// }}}
+ protected $survey;
+ protected $parentQuestion;
-// {{{ class SurveyTextarea extends SurveySimple : textarea field, allowing longer comments
-class SurveyTextarea extends SurveySimple
-{
- protected function getQuestionType()
+ public function __construct(Survey $survey)
{
- return "textarea";
+ parent::__construct('survey_questions');
+ $this->registerFieldFormatter('parameters', 'JSonFieldFormatter');
+ $this->survey = $survey;
}
-}
-// }}}
-// {{{ class SurveyNum extends SurveySimple : allows numerical answers
-class SurveyNum extends SurveySimple
-{
- protected function checkAnswer($ans)
+ public function typedInstance()
{
- return intval($ans);
+ $instance = self::instanceForType($this->survey, $this->type);
+ $instance->copy($this);
+ return $instance;
}
- protected function getQuestionType()
+ public static function instanceForType(Survey $survey, $type)
{
- return "num";
+ require_once dirname(__FILE__) . '/' . $type . '.inc.php';
+ $class = 'SurveyQuestion' . $type;
+ return new $class($survey);
}
}
-// }}}
-// }}}
-// {{{ abstract class SurveyList extends SurveyTreeable : restricted questions that allows only a list of possible answers
-abstract class SurveyList extends SurveyTreeable
+class SurveyQuestionGroup extends SurveyQuestion implements SurveyQuestionContainer
{
- private $choices;
-
- protected function update($args)
- {
- parent::update($args);
- $this->choices = explode('|', $args['options']);
- }
+ public $children = array();
- protected function storeArray()
+ public function __construct(Survey $survey)
{
- $rArr = parent::storeArray();
- $rArr['choices'] = $this->choices;
- $rArr['options'] = implode('|', $this->choices);
- return $rArr;
+ parent::__construct($survey);
}
-}
-
-// {{{ class SurveyRadio extends SurveyList : radio question, allows one answer among the list offered
-class SurveyRadio extends SurveyList
-{
- protected function checkAnswer($ans)
+ public function addQuestion(SurveyQuestion $question, $pos = null)
{
- return (in_array($ans, $this->choices)) ? $ans : "";
+ $question->parentQuestion = $this;
+ if (is_null($pos)) {
+ $this->children[] = $question;
+ } else {
+ array_splice($this->children, $pos, 0, $question);
+ }
}
- protected function getQuestionType()
+ public function newQuestion($type, $pos = null)
{
- return "radio";
+ $question = SurveyQuestion::instanceForType($this->survey, $type);
+ $this->addQuestion($question, $pos);
+ return $question;
}
-}
-// }}}
-// {{{ class SurveyCheckbox extends SurveyList : checkbox question, allows any number of answers among the list offered
-class SurveyCheckbox extends SurveyList
-{
- protected function checkAnswer($ans)
+ public function reassignQuestionIds()
{
- $rep = "";
- foreach ($this->choices as $key => $value) {
- if (array_key_exists($key,$v[$id]) && $v[$id][$key]) {
- $rep .= "|" . $key;
+ $id = $this->qid + 1;
+ foreach ($this->children as $question) {
+ $question->qid = $id;
+ if ($question instanceof SurveyQuestionContainer) {
+ $id = $question->reassignQuestionIds();
+ } else {
+ $id++;
}
}
- $rep = (strlen($rep) >= 4) ? substr($rep, 4) : "";
- return $rep;
+ return $id;
}
- protected function getQuestionType()
+ protected function postSave()
{
- return "checkbox";
- }
-}
-// }}}
-// }}}
-
-// {{{ class SurveyPersonal extends SurveyQuestion : allows easy and verified access to user's personal data (promotion, name...)
-class SurveyPersonal extends SurveyQuestion
-{
- private $perm;
-
- protected function update($args)
- {
- $args['question'] = "Informations personnelles";
- parent::update($args);
- $this->perm['promo'] = isset($args['promo'])? 1 : 0;
- $this->perm['name'] = isset($args['name'])? 1 : 0;
- }
-
- protected function checkAnswer($ans)
- {
- if (intval($ans) == 1) {
- // requete mysql qvb
- return "";
- } else {
- return "";
+ foreach ($this->children as $question) {
+ $question->sid = $this->sid;
+ $question->parent = $this->qid;
+ $question->insert();
}
}
- protected function getQuestionType()
- {
- return "personal";
- }
-
- protected function storeArray()
+ public function export()
{
- $a = parent::storeArray();
- $a['promo'] = $this->perm['promo'];
- $a['name'] = $this->perm['name'];
- return $a;
+ $export = parent::export();
+ $export['children'] = array();
+ foreach ($this->children as $child) {
+ $export['children'][] = $child->export();
+ }
+ return $export;
}
}
-// }}}
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 ts=4 foldmethod=marker enc=utf-8:
?>