From: x2004laborde Date: Thu, 15 Mar 2007 19:23:56 +0000 (+0000) Subject: Survey module : core components, basic editing and administration X-Git-Tag: xorg/0.9.14~210 X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=8fe81c50aa8db4f632c380a8510f38717b1fc808;p=platal.git Survey module : core components, basic editing and administration git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@1575 839d8a87-29fc-0310-9880-83ba4fa771e5 --- diff --git a/modules/survey.php b/modules/survey.php new file mode 100644 index 0000000..c3ed63c --- /dev/null +++ b/modules/survey.php @@ -0,0 +1,340 @@ + $this->make_hook('index', AUTH_COOKIE), + 'survey/edit' => $this->make_hook('edit', AUTH_COOKIE), + 'survey/ajax' => $this->make_hook('ajax', AUTH_COOKIE), + 'survey/admin' => $this->make_hook('admin', AUTH_MDP, 'admin'), + 'survey/admin/edit' => $this->make_hook('adminEdit', AUTH_MDP, 'admin'), + 'survey/admin/valid' => $this->make_hook('adminValidate', AUTH_MDP, 'admin'), + 'survey/admin/del' => $this->make_hook('adminDelete', AUTH_MDP, 'admin'), + ); + } + // }}} + + // {{{ function handler_index() : lists all available surveys + function handler_index(&$page, $action = null) + { + require_once dirname(__FILE__).'/survey/survey.inc.php'; + $page->changeTpl('survey/index.tpl'); + $page->assign('survey_current', SurveyDB::retrieveList('c')); + $page->assign('survey_old', SurveyDB::retrieveList('o')); + } + // }}} + + // {{{ function handler_admin() : index of admin mode + function handler_admin(&$page) + { + require_once dirname(__FILE__).'/survey/survey.inc.php'; + $page->changeTpl('survey/admin.tpl'); + $page->assign('survey_waiting', SurveyDB::retrieveList('w')); + $page->assign('survey_current', SurveyDB::retrieveList('c')); + $page->assign('survey_old', SurveyDB::retrieveList('o')); + } + // }}} + + // {{{ function handler_adminEdit() : edits a survey in admin mode + function handler_adminEdit(&$page, $id) + { + require_once dirname(__FILE__).'/survey/survey.inc.php'; + $survey = SurveyDB::retrieveSurvey($id); // retrieves the survey in database + S::kill('survey'); // cleans session (in case there would have been a problem before) + S::kill('survey_id'); + if ($survey->isValid()) { + return $this->show_error($page, "Il est impossible de modifier un sondage déjà validé", 'admin'); + } + $_SESSION['survey'] = serialize($survey); + $_SESSION['survey_id'] = $id; + $this->handler_edit($page, 'show'); // calls handler_edit, but in admin mode since 'survey_id' is in session + } + // }}} + + // {{{ function handler_adminValidate() : validates a survey (admin mode) + function handler_adminValidate(&$page, $id = -1) + { + if (Post::has('survey_cancel')) { // if the admin cancels the validation, returns to the admin index + return $this->handler_admin(&$page); + } + $id = Post::v('survey_id', $id); + if ($id == -1) { + return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin'); + } + require_once dirname(__FILE__).'/survey/survey.inc.php'; + $surveyInfo = SurveyDB::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure) + if ($surveyInfo == null) { + return $this->show_error($page, "Sondage ".$id." introuvable.", 'admin'); + } + if (Post::has('survey_submit')) { // needs a confirmation before validation + if (SurveyDB::validateSurvey($id)) { // validates the survey (in the database) + $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien été validé, les votes sont maintenant ouverts.", 'admin'); + } else { + $this->show_error($page, '', 'admin'); + } + } else { // asks for a confirmation + $this->show_confirm($page, "Êtes-vous certain de vouloir valider le sondage \"".$surveyInfo['title']."\" ? " + ."Les votes seront immédiatement ouverts.", 'admin/valid', array('id' => $id)); + } + } + // }}} + + // {{{ function handler_adminDelete() : deletes a survey (admin mode) + function handler_adminDelete(&$page, $id = -1) + { + if (Post::has('survey_cancel')) { // if the admin cancels the suppression, returns to the admin index + return $this->handler_admin(&$page); + } + $id = Post::v('survey_id', $id); + if ($id == -1) { + return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin'); + } + require_once dirname(__FILE__).'/survey/survey.inc.php'; + $surveyInfo = SurveyDB::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure) + if ($surveyInfo == null) { + return $this->show_error($page, "Sondage ".$id." introuvable.", 'admin'); + } + if (Post::has('survey_submit')) { // needs a confirmation before suppression + if (SurveyDB::deleteSurvey($id)) { // deletes survey in database + $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien été supprimé, ainsi que tous les votes le concernant.", 'admin'); + } else { + $this->show_error($page, '', 'admin'); + } + } else { // asks for a confirmation + $this->show_confirm($page, "Êtes-vous certain de vouloir supprimer le sondage \"".$surveyInfo['title']."\" ?", 'admin/delete', array('id' => $id)); + } + } + // }}} + + // {{{ function handler_edit() : edits a survey (in normal mode unless called by handler_adminEdit() ) + function handler_edit(&$page, $action = 'show', $qid = 0) + { + require_once dirname(__FILE__).'/survey/survey.inc.php'; + $action = Post::v('survey_action', $action); + $qid = Post::v('survey_qid', $qid); + if (Post::has('survey_cancel')) { // after cancelling changes, shows the survey + if (S::has('survey')) { + $action = 'show'; + } else { // unless no editing has been done at all (shows to the surveys index page) + return $this->handler_index($page); + } + } + if (S::has('survey_id')) { // if 'survey_id' is in session, it means we are modifying a survey in admin mode + $page->assign('survey_adminmode', true); + } + if ($action == 'show' && !S::has('survey')) { + $action = 'new'; + } + if ($action == 'question') { // {{{ modifies an existing question + if (Post::has('survey_submit')) { // if the form has been submitted, makes the modifications + $survey = unserialize(S::v('survey')); + $args = Post::v('survey_question'); + if (!$survey->edit($qid, $args)) { // update the survey object structure + return $this->show_error($page, '', 'edit'); + } + $this->show_survey($page, $survey); + $_SESSION['survey'] = serialize($survey); + } else { // if a form has not been submitted, shows modification form + $survey = unserialize(S::v('survey')); + $current = $survey->searchToArray($qid); // gets the current parameters of the question + if ($current == null) { + return $this->show_error($page, '', 'edit'); + } + $this->show_form($page, $action, $qid, $current['type'], $current); + } // }}} + } elseif ($action == 'new') { // {{{ create a new survey : actually store the root question + if (Post::has('survey_submit')) { // if the form has been submitted, creates the survey + S::kill('survey'); + S::kill('survey_id'); + $survey = new SurveyRoot(Post::v('survey_question')); // creates the object structure + $this->show_survey($page, $survey); + $_SESSION['survey'] = serialize($survey); + } else { + S::kill('survey'); + S::kill('survey_id'); + $this->show_form($page, $action, 0, 'root'); + } // }}} + } elseif ($action == 'nested' || $action == 'after') { // {{{ adds a new question, nested in the current node, or on the same level after it + if (Post::has('survey_submit')) { // if the form has been submitted, adds the question + $survey = unserialize(S::v('survey')); + $question = $survey->factory(Post::v('survey_type'), Post::v('survey_question')); // creates the question object, with a sort of 'factory' method + if ($action == 'nested') { + if (!$survey->addChildNested($qid, $question)) { + return $this->show_error($page, '', 'edit'); + } + } else { + if (!$survey->addChildAfter($qid, $question)) { + return $this->show_error($page, '', 'edit'); + } + } + $this->show_survey($page, $survey); + $_SESSION['survey'] = serialize($survey); + } else { + $this->show_form($page, $action, $qid); + } // }}} + } elseif ($action == 'del') { // {{{ deletes a question + if (Post::has('survey_submit')) { // if a confirmation has been sent, deletes the question + $survey = unserialize(S::v('survey')); + if (!$survey->delChild(Post::v('survey_qid'))) { // deletes the node in the survey object structure + return $this->show_error($page, '', 'edit'); + } + $this->show_survey($page, $survey); + $_SESSION['survey'] = serialize($survey); + } else { // if user has not confirmed, shows a confirmation form + $survey = unserialize(S::v('survey')); + $current = $survey->searchToArray($qid); // needed to get the title of the question to delete (more user-friendly than an id) + if ($current == null) { + return $this->show_error($page, '', 'edit'); + } + $this->show_confirm($page, 'Êtes-vous certain de vouloir supprimer la question intitulé "'.$current['question'].'" ? ' + .'Attention, cela supprimera en même temps toutes les questions qui dépendent de celle-ci.', + 'edit', array('action' => 'del', 'qid' => $qid)); + } // }}} + } elseif ($action == 'show') { // {{{ simply shows the survey in its current state + $this->show_survey($page, unserialize(S::v('survey'))); // }}} + } elseif ($action == 'valid') { // {{{ validates the proposition, i.e stores the proposition in the database + // but an admin will still need to validate the survey before it is activated + if (Post::has('survey_submit')) { // needs a confirmation before storing the proposition + $survey = unserialize(S::v('survey')); + if (S::has('survey_id')) { // if 'survey_id' is in session, we are modifying an existing survey (in admin mode) instead of proposing a new one + if (SurveyDB::updateSurvey($survey, S::v('survey_id'))) { // updates the database according the new survey object structure + $this->show_success($page, "Les modifications sur le sondage ont bien été enregistrées.", 'admin'); + } else { + $this->show_error($page, '', 'admin'); + } + } else { // if no 'survey_id' is in session, we are indeed proposing a new survey + if (SurveyDB::proposeSurvey($survey)) { // stores the survey object structure in database + $this->show_success($page, "Votre proposition de sondage a bien été enregistrée, + elle est en attent de validation par un administrateur du site.", ''); + } else { + $this->show_error($page); + } + } + S::kill('survey'); // cleans session + S::kill('survey_id'); + } else { // asks for a confirmation if it has not been sent + $survey = unserialize(S::v('survey')); + $errors = $survey->checkSyntax(); + if (!is_null($errors)) { + $this->show_error($page, "", 'edit', $errors); + } else { + if (S::has('survey_id')) { + $this->show_confirm($page, "Veuillez confirmer l'enregistrement des modifications apportées à ce sondage", 'edit', array('action' => 'valid')); + } else { + $this->show_confirm($page, "Veuillez confirmer l'envoi de cette proposition de sondage.", 'edit', array('action' => 'valid')); + } + } + } // }}} + } elseif ($action == 'cancel') { // {{{ cancels the creation/modification of a survey + if (Post::has('survey_submit')) { // needs a confirmation + S::kill('survey'); // cleans session + if (S::has('survey_id')) { // only possible when modifying a survey in admin mode, still this should be considered again, + S::kill('survey_id'); // maybe some name with "admin" in it, "survey_adminid" or anything that might not be confusing. + return $this->handler_admin($page); // in this case, shows the admin index + } else { + return $this->handler_index($page); // else shows the 'normal' index + } + } else { // asks for a confirmation if it has not been sent + $this->show_confirm(&$page, "Êtes-vous certain de vouloir annuler totalement l'édition de ce sondage ? Attention, " + ."toutes les données éditées jusque lâ seront définitivement perdues.", + 'edit', array('action' => $action)); + } + } // }}} + } + // }}} + + // {{{ function handler_ajax() : some ajax in editing a new question (for now, there may be a little more later) + function handler_ajax(&$page, $type) + { + require_once dirname(__FILE__).'/survey/survey.inc.php'; + header('Content-Type: text/html; charset="UTF-8"'); + if (SurveyQuestion::isType($type)) { // when type has been chosen, the form is updated to fit exactly the type of question chosen + $page->changeTpl('survey/edit_new.tpl', NO_SKIN); + $page->assign('survey_types', SurveyQuestion::getTypes()); + $page->assign('survey_type', $type); + } + } + // }}} + + // {{{ function show_survey() : calls the template to display a survey, for editing, voting, or consulting the results + function show_survey(&$page, $survey) + { + $page->changeTpl('survey/show_survey.tpl'); + $page->assign('survey_mode', 'edit'); // for now, only editing has been completely implemented + $page->assign('survey', $survey->toArray()); + } + // }}} + + // {{{ function show_form() : calls the template to display the editing form + function show_form(&$page, $action, $qid, $type = 'new', $current = null) + { + $page->changeTpl('survey/edit_survey.tpl'); + $page->assign('survey_action', $action); + $page->assign('survey_qid', $qid); + $page->assign('survey_formaction', './survey/edit'); + $page->assign('survey_type', $type); + if (!is_null($current) && is_array($current)) { + $page->assign('survey_current', $current); + } elseif ($type == 'new') { + $page->addJsLink('ajax.js'); + $page->assign('survey_types', SurveyQuestion::getTypes()); + } + } + // }}} + + // {{{ function show_confirm() : calls the template to display a confirm form + function show_confirm(&$page, $message, $formaction, $formhidden) + { + $page->changeTpl('survey/confirm.tpl'); + $page->assign('survey_message', $message); + $page->assign('survey_formaction', './survey/'.$formaction); + $page->assign('survey_formhidden', $formhidden); + } + // }}} + + // {{{ function show_error() : calls the template to display an error message + function show_error(&$page, $message, $link = "", $errArray = null) + { + $page->changeTpl('survey/error.tpl'); + $page->assign('survey_message', $message); + $page->assign('survey_link', './survey/'.$link); // 'return' link to let the user leave the page + if (!is_null($errArray)) { + $page->assign('survey_errors', $errArray); + } + } + // }}} + + // {{{ function show_success() : calls the template to display a success message + function show_success(&$page, $message = "", $link = "") + { + $page->changeTpl('survey/success.tpl'); + $page->assign('survey_message', $message); + $page->assign('survey_link', './survey/'.$link); // 'return' link to let the user leave the page + } + // }}} +} + +// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: +?> diff --git a/modules/survey/survey.inc.php b/modules/survey/survey.inc.php new file mode 100644 index 0000000..5386efd --- /dev/null +++ b/modules/survey/survey.inc.php @@ -0,0 +1,709 @@ + 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(); + } + // }}} + + // {{{ static function validateSurvey() : validates a survey + public static function validateSurvey($sid) + { + $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 SurveyQuestion($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; + } + + protected function delChild($i) + { + return false; + } + // }}} + + // {{{ function edit($i, $a) : searches and edits question $i + protected function edit($i, $a) + { + if ($this->id == $i) { + $this->update($a); + 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; + } + } + + 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() + { + return null; + } + // }}} + + // {{{ function vote() : handles vote + protected function checkAnswer($ans) + { + return ""; + } + + 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); + } + } + // }}} +} +// }}} + +// {{{ abstract class SurveyTreeable extends SurveyQuestion : questions that allow nested ones +abstract class SurveyTreeable extends SurveyQuestion +{ + // {{{ common properties, constructor + private $children; + + protected function SurveyTreeable($i, $args) + { + parent::__construct($i, $args); + $this->children = array(); + } + // }}} + + // {{{ tree manipulation functions : actual implementation + protected function hasChild() + { + return !is_null($this->children) && is_array($this->children); + } + + protected function addChildNested($i, $c) + { + if ($this->getId() == $i) { + if ($this->hasChild()) { + array_unshift($this->children, $c); + } else { + $this->children = array($c); + } + return true; + } else { + foreach ($this->children as $child) { + if ($child->addChildNested($i, $c)) { + return true; + } + } + return false; + } + } + + protected function addChildAfter($i, $c) + { + $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; + } + return false; + } + + protected function delChild($i) + { + $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); + return true; + } + return false; + } + // }}} + + // {{{ function edit() with tree support + protected function edit($i, $a) + { + if ($this->getId() == $i) { + $this->update($a); + return true; + } else { + foreach ($this->children as $child) { + if ($child->edit($i, $a)) { + return true; + } + } + return false; + } + } + // }}} + + // {{{ 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; + } else { + return $this->storeArray(); + } + } + + 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; + } + } + return null; + } + } + // }}} + + // {{{ 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) + { + parent::vote($sid, $vid, $a); + if ($this->hasChild()) { + foreach ($this->children as $c) { + $c->vote($sid, $vid, $a); + } + } + } + // }}} +} +// }}} + +// {{{ class SurveyRoot extends SurveyTreeable : root of any survey, actually the only entry point (no public methods outside this class) +class SurveyRoot extends SurveyTreeable +{ + // {{{ properties, constructor and basic methods + private $last_id; + private $beginning; + private $end; + private $promos; + private $valid; + + public function SurveyRoot($args) + { + parent::__construct(0, $args); + $this->last_id = 0; + } + + public function update($args) + { + 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'] : '#'; + } + $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 +{ + protected function checkAnswer($ans) + { + return $ans; + } +} + +// {{{ class SurveyText extends SurveySimple : simple text field, allowing a few words +class SurveyText extends SurveySimple +{ + protected function getQuestionType() + { + return "text"; + } +} +// }}} + +// {{{ class SurveyTextarea extends SurveySimple : textarea field, allowing longer comments +class SurveyTextarea extends SurveySimple +{ + protected function getQuestionType() + { + return "textarea"; + } +} +// }}} + +// {{{ class SurveyNum extends SurveySimple : allows numerical answers +class SurveyNum extends SurveySimple +{ + protected function checkAnswer($ans) + { + return intval($ans); + } + + protected function getQuestionType() + { + return "num"; + } +} +// }}} +// }}} + +// {{{ abstract class SurveyList extends SurveyTreeable : restricted questions that allows only a list of possible answers +abstract class SurveyList extends SurveyTreeable +{ + private $choices; + + protected function update($args) + { + parent::update($args); + $this->choices = explode('|', $args['options']); + } + + protected function storeArray() + { + $rArr = parent::storeArray(); + $rArr['choices'] = $this->choices; + $rArr['options'] = implode('|', $this->choices); + return $rArr; + } + +} + +// {{{ class SurveyRadio extends SurveyList : radio question, allows one answer among the list offered +class SurveyRadio extends SurveyList +{ + protected function checkAnswer($ans) + { + return (in_array($ans, $this->choices)) ? $ans : ""; + } + + protected function getQuestionType() + { + return "radio"; + } +} +// }}} + +// {{{ class SurveyCheckbox extends SurveyList : checkbox question, allows any number of answers among the list offered +class SurveyCheckbox extends SurveyList +{ + protected function checkAnswer($ans) + { + $rep = ""; + foreach ($this->choices as $key => $value) { + if (array_key_exists($key,$v[$id]) && $v[$id][$key]) { + $rep .= "|" . $key; + } + } + $rep = (strlen($rep) >= 4) ? substr($rep, 4) : ""; + return $rep; + } + + protected function getQuestionType() + { + 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 ""; + } + } + + protected function getQuestionType() + { + return "personal"; + } + + protected function storeArray() + { + $a = parent::storeArray(); + $a['promo'] = $this->perm['promo']; + $a['name'] = $this->perm['name']; + return $a; + } +} +// }}} + +// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: +?>