X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=modules%2Fsurvey.php;h=ff3bb05ed21f64007b1e00432ac31829a1aa05e8;hb=7cc98bf7d74816b40738b3965dc19ada26e55e10;hp=c3ed63c93213a3ee7823a6e3449bda85958ea1bd;hpb=8fe81c50aa8db4f632c380a8510f38717b1fc808;p=platal.git diff --git a/modules/survey.php b/modules/survey.php index c3ed63c..ff3bb05 100644 --- a/modules/survey.php +++ b/modules/survey.php @@ -1,6 +1,6 @@ $this->make_hook('index', AUTH_COOKIE), + 'survey' => $this->make_hook('index', AUTH_PUBLIC), + 'survey/vote' => $this->make_hook('vote', AUTH_PUBLIC), + 'survey/result' => $this->make_hook('result', AUTH_PUBLIC), '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'), @@ -41,34 +43,127 @@ class SurveyModule extends PLModule { 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')); + $page->assign('survey_current', Survey::retrieveList('c')); + $page->assign('survey_old', Survey::retrieveList('o')); + $page->assign('survey_modes', Survey::getModes(false)); + } + // }}} + + // {{{ function handler_vote() : handles the vote to a survey + function handler_vote(&$page, $id = -1) + { + if (Post::has('survey_cancel')) { // if the user cancels, returns to index + return $this->handler_index(&$page); + } + $id = intval($id); + if ($id == -1) { + return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey'); + } + require_once dirname(__FILE__).'/survey/survey.inc.php'; + $survey = Survey::retrieveSurvey($id); // retrieves the survey object structure + if ($survey == null || !$survey->isValid()) { + return $this->show_error($page, "Sondage ".$id." introuvable.", 'survey'); + } elseif ($survey->isEnded()) { + return $this->show_error($page, "Le sondage ".$survey->getTitle()." est terminé.", 'survey'); + } + if (!$this->check_surveyPerms($page, $survey)) { + return; + } + if (Post::has('survey_submit')) { // checks if the survey has already been filled in + $uid = 0; + if (!$survey->isMode(Survey::MODE_ALL)) { // if survey is restriced to alumni + $uid = S::v('uid'); + if ($survey->hasVoted($uid)) { // checks whether the user has already voted + return $this->show_error($page, "Tu as déjà voté à ce sondage.", 'survey'); + } + } + $survey->vote($uid, Post::v('survey'.$id)); // performs vote + $this->show_success($page, "Ta réponse a bien été prise en compte. Merci d'avoir participé à ce sondage.", 'survey'); + } else { // offers to fill in the survey + if ($survey->isMode(Survey::MODE_ALL) || !$survey->hasVoted(S::v('uid'))) { + $page->assign('survey_votemode', true); + } else { + $page->assign('survey_warning', "Tu as déjà voté à ce sondage."); + } + //$page->assign('survey_id', $id); + $this->show_survey($page, $survey); + } + } + // }}} + + // {{{ function handler_result() : show the results of the votes to a survey + function handler_result($page, $id = -1, $show = 'all') + { + $id = intval($id); + if ($id == -1) { + return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey'); + } + require_once dirname(__FILE__).'/survey/survey.inc.php'; + $survey = Survey::retrieveSurvey($id); // retrieves the survey object structure + if ($survey == null || !$survey->isValid()) { + return $this->show_error($page, "Sondage ".$id." introuvable.", 'survey'); + } elseif (!$survey->isEnded()) { + return $this->show_error($page, "Le sondage ".$survey->getTitle()." n'est pas encore terminé.", 'survey'); + } + if (!$this->check_surveyPerms($page, $survey)) { + return; + } + if ($show == 'csv') { + header('Content-Type: text/csv; charset="UTF-8"'); + echo $survey->toCSV(); + exit; + } else { + $page->assign('survey_resultmode', true); + $this->show_survey($page, $survey); + } } // }}} // {{{ function handler_admin() : index of admin mode - function handler_admin(&$page) + function handler_admin(&$page, $id = -1) { 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')); + $this->clear_session(); + if ($id == -1) { + $page->changeTpl('survey/admin.tpl'); + $page->assign('survey_waiting', Survey::retrieveList('w')); + $page->assign('survey_current', Survey::retrieveList('c')); + $page->assign('survey_old', Survey::retrieveList('o')); + $page->assign('survey_modes', Survey::getModes(false)); + } else { + $id = intval($id); + $survey = Survey::retrieveSurvey($id); // retrieves all survey object structure + if ($survey == null) { + $this->show_error($page, "Sondage ".$id." introuvable.", 'survey/admin'); + } + $page->assign('survey_adminmode', true); + $this->show_survey($page, $survey); + } } // }}} // {{{ function handler_adminEdit() : edits a survey in admin mode - function handler_adminEdit(&$page, $id) + function handler_adminEdit(&$page, $id = -1, $req = -1) { + if ($id == -1 || ($id == 'req' && $req == -1)) { + return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey/admin'); + } 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'); + $this->clear_session(); // cleans session (in case there would have been a problem before) + if ($id == 'req') { + $survey = Survey::retrieveSurveyReq($req); + if ($survey == null) { + return $this->show_error($page, "Sondage introuvable.", 'survey/admin'); + } + $this->store_session($survey, $req, true); + } else { + $id = intval($id); + $survey = Survey::retrieveSurvey($id); // retrieves the survey in database + if ($survey == null) { + return $this->show_error($page, "Sondage ".$id." introuvable.", 'survey/admin'); + } + $this->store_session($survey, $id); } - $_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 } // }}} @@ -76,27 +171,29 @@ class SurveyModule extends PLModule // {{{ function handler_adminValidate() : validates a survey (admin mode) function handler_adminValidate(&$page, $id = -1) { + $id = Post::i('survey_id', $id); if (Post::has('survey_cancel')) { // if the admin cancels the validation, returns to the admin index - return $this->handler_admin(&$page); + $this->clear_session(); + return $this->handler_admin(&$page, $id); } - $id = Post::v('survey_id', $id); if ($id == -1) { - return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin'); + return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey/admin'); } + $id = intval($id); 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) + $surveyInfo = Survey::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'); + return $this->show_error($page, "Sondage ".$id." introuvable.", 'survey/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'); + if (Survey::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.", 'survey/admin'); } else { - $this->show_error($page, '', 'admin'); + $this->show_error($page, '', 'survey/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)); + $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)); } } // }}} @@ -104,32 +201,33 @@ class SurveyModule extends PLModule // {{{ function handler_adminDelete() : deletes a survey (admin mode) function handler_adminDelete(&$page, $id = -1) { + $id = Post::i('survey_id', $id); if (Post::has('survey_cancel')) { // if the admin cancels the suppression, returns to the admin index - return $this->handler_admin(&$page); + return $this->handler_admin(&$page, $id); } - $id = Post::v('survey_id', $id); if ($id == -1) { - return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin'); + return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey/admin'); } + $id = intval($id); 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) + $surveyInfo = Survey::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'); + return $this->show_error($page, "Sondage ".$id." introuvable.", 'survey/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'); + if (Survey::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.", 'survey/admin'); } else { - $this->show_error($page, '', 'admin'); + $this->show_error($page, '', 'survey/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)); + $this->show_confirm($page, "Êtes-vous certain de vouloir supprimer le sondage \"".$surveyInfo['title']."\" ?", 'admin/del', 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) + function handler_edit(&$page, $action = 'show', $qid = 'root') { require_once dirname(__FILE__).'/survey/survey.inc.php'; $action = Post::v('survey_action', $action); @@ -141,8 +239,9 @@ class SurveyModule extends PLModule return $this->handler_index($page); } } + $page->assign('survey_editmode', true); 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); + $page->assign('survey_updatemode', true); } if ($action == 'show' && !S::has('survey')) { $action = 'new'; @@ -151,65 +250,56 @@ class SurveyModule extends PLModule 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'); + if (!$survey->editQuestion($qid, $args)) { // update the survey object structure + return $this->show_error($page, '', 'survey/edit'); } $this->show_survey($page, $survey); - $_SESSION['survey'] = serialize($survey); + $this->store_session($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 + $current = $survey->toArray($qid); // gets the current parameters of the question if ($current == null) { - return $this->show_error($page, '', 'edit'); + return $this->show_error($page, '', 'survey/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->clear_session(); + $survey = new Survey(Post::v('survey_question')); // creates the object structure $this->show_survey($page, $survey); - $_SESSION['survey'] = serialize($survey); + $this->store_session($survey); } else { - S::kill('survey'); - S::kill('survey_id'); - $this->show_form($page, $action, 0, 'root'); + $this->clear_session(); + $this->show_form($page, $action, 'root', 'newsurvey'); } // }}} - } elseif ($action == 'nested' || $action == 'after') { // {{{ adds a new question, nested in the current node, or on the same level after it + } elseif ($action == 'add') { // {{{ adds a new question 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'); - } + if (!$survey->addQuestion($qid, $survey->factory(Post::v('survey_type'), Post::v('survey_question')))) { + return $this->show_error($page, '', 'survey/edit'); } $this->show_survey($page, $survey); - $_SESSION['survey'] = serialize($survey); + $this->store_session($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'); + if (!$survey->delQuestion(Post::v('survey_qid'))) { // deletes the node in the survey object structure + return $this->show_error($page, '', 'survey/edit'); } $this->show_survey($page, $survey); - $_SESSION['survey'] = serialize($survey); + $this->store_session($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) + $current = $survey->toArray($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'); + return $this->show_error($page, '', 'survey/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.', + $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 @@ -219,29 +309,29 @@ class SurveyModule extends PLModule 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'); + $link = (S::has('survey_validate'))? 'admin/validate' : 'survey/admin'; + if ($survey->updateSurvey()) { // updates the database according the new survey object structure + $this->show_success($page, "Les modifications sur le sondage ont bien été enregistrées.", $link); } else { - $this->show_error($page, '', 'admin'); + $this->show_error($page, '', $link); } } 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.", ''); + if ($survey->proposeSurvey()) { // stores the survey object structure in database + $this->show_success($page, "Votre proposition de sondage a bien été enregistrée, + elle est en attente de validation par un administrateur du site.", 'survey'); } else { - $this->show_error($page); + $this->show_error($page, '', 'survey'); } } - S::kill('survey'); // cleans session - S::kill('survey_id'); + $this->clear_session(); } 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); + $this->show_error($page, "", 'survey/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')); + $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')); } @@ -249,41 +339,87 @@ class SurveyModule extends PLModule } // }}} } 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 + if (S::has('survey_id')) { // only possible when modifying a survey in admin mode + if (S::has('survey_validate')) { // if a link has been supplied, uses it + $this->clear_session(); + return $this->show_success($page, "Les modifications effectuées ont été annulées", 'admin/validate'); + } else { // else shows the admin index + $this->clear_session(); + return $this->handler_admin($page); + } } else { + $this->clear_session(); 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.", + $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 + if (Survey::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_types', Survey::getTypes()); $page->assign('survey_type', $type); } } // }}} + // {{{ function clear_session() : clears the data stored in session + function clear_session() + { + S::kill('survey'); + S::kill('survey_id'); + S::kill('survey_validate'); + } + // }}} + + // {{{ function store_session() : serializes and stores survey (and survey_id) in session + function store_session($survey, $survey_id = -1, $survey_validate = false) + { + $_SESSION['survey'] = serialize($survey); + if ($survey_id != -1) { + $_SESSION['survey_id'] = $survey_id; + } + if ($survey_validate) { + $_SESSION['survey_validate'] = true; + } + } + // }}} + + // {{{ function check_surveyPerms() : checks the particular surveys access permissions + function check_surveyPerms(&$page, $survey) + { + require_once dirname(__FILE__).'/survey/survey.inc.php'; + if (!$survey->isMode(Survey::MODE_ALL)) { // if the survey is reserved to alumni + global $globals; + if (!call_user_func(array($globals->session, 'doAuth'))) { // checks authentification + global $platal; + $platal->force_login($page); + } + if (!$survey->checkPromo(S::v('promo'))) { // checks promotion + $this->show_error($page, "Tu n'as pas accès à ce sondage car il est réservé à d'autres promotions.", 'survey'); + return false; + } + } + return true; + } + // }}} + // {{{ 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->changeTpl('survey/show_root.tpl'); $page->assign('survey', $survey->toArray()); + $page->assign('survey_modes', Survey::getModes()); } // }}} @@ -299,13 +435,16 @@ class SurveyModule extends PLModule $page->assign('survey_current', $current); } elseif ($type == 'new') { $page->addJsLink('ajax.js'); - $page->assign('survey_types', SurveyQuestion::getTypes()); + $page->assign('survey_types', Survey::getTypes()); + } + if ($type == 'root' || $type == 'newsurvey') { + $page->assign('survey_modes', Survey::getModes()); } } // }}} - + // {{{ function show_confirm() : calls the template to display a confirm form - function show_confirm(&$page, $message, $formaction, $formhidden) + function show_confirm(&$page, $message, $formaction, $formhidden = null) { $page->changeTpl('survey/confirm.tpl'); $page->assign('survey_message', $message); @@ -319,7 +458,7 @@ class SurveyModule extends PLModule { $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 + $page->assign('survey_link', $link); // 'return' link to let the user leave the page if (!is_null($errArray)) { $page->assign('survey_errors', $errArray); } @@ -331,10 +470,10 @@ class SurveyModule extends PLModule { $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 + $page->assign('survey_link', $link); // 'return' link to let the user leave the page } // }}} } -// 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: ?>