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 SurveyModule
extends PLModule
24 // {{{ function handlers() : registers the different handlers
28 'survey' => $this->make_hook('index', AUTH_PUBLIC
),
29 'survey/vote' => $this->make_hook('vote', AUTH_PUBLIC
),
30 'survey/edit' => $this->make_hook('edit', AUTH_COOKIE
),
31 'survey/ajax' => $this->make_hook('ajax', AUTH_COOKIE
),
32 'survey/admin' => $this->make_hook('admin', AUTH_MDP
, 'admin'),
33 'survey/admin/edit' => $this->make_hook('adminEdit', AUTH_MDP
, 'admin'),
34 'survey/admin/valid' => $this->make_hook('adminValidate', AUTH_MDP
, 'admin'),
35 'survey/admin/del' => $this->make_hook('adminDelete', AUTH_MDP
, 'admin'),
40 // {{{ function handler_index() : lists all available surveys
41 function handler_index(&$page, $action = null
)
43 require_once dirname(__FILE__
).'/survey/survey.inc.php';
44 $page->changeTpl('survey/index.tpl');
45 $page->assign('survey_current', Survey
::retrieveList('c'));
46 $page->assign('survey_old', Survey
::retrieveList('o'));
47 $page->assign('survey_modes', Survey
::getModes(false
));
51 // {{{ function handler_vote()
52 function handler_vote(&$page, $id = -1)
54 if (Post
::has('survey_cancel')) { // if the user cancels, returns to index
55 return $this->handler_index(&$page);
59 return $this->show_error($page, "Un identifiant de sondage doit être précisé.", '');
61 require_once dirname(__FILE__
).'/survey/survey.inc.php';
62 $survey = Survey
::retrieveSurvey($id); // retrieves the survey object structure
63 if ($survey == null ||
!$survey->isValid()) {
64 return $this->show_error($page, "Sondage ".$id." introuvable.", '');
65 } elseif ($survey->isEnded()) {
66 return $this->show_error($page, "Le sondage ".$survey->getTitle()." est terminé.");
68 if (!$survey->isMode(Survey
::MODE_ALL
)) { // if the survey is reserved to alumni
70 if (!call_user_func(array($globals->session
, 'doAuth'))) { // checks authentification
72 $platal->force_login($page);
74 if ($survey->isMode(Survey
::MODE_XIDENT
) && !$survey->checkPromo(S
::v('promo'))) { // checks promotion
75 return $this->show_error($page, "Tu n'as pas accès à ce sondage car il est réservé à d'autres promotions.");
78 if (Post
::has('survey_submit')) { // checks if the survey has already been filled in
80 if (!$survey->isMode(Survey
::MODE_ALL
)) { // if survey is restriced to alumni
82 if ($survey->hasVoted($uid)) { // checks whether the user has already voted
83 return $this->show_error($page, "Tu as déjà voté à ce sondage.");
86 $survey->vote($uid, Post
::v('survey'.$id)); // performs vote
87 $this->show_success($page, "Ta réponse a bien été prise en compte. Merci d'avoir participé à ce sondage.", '');
88 } else { // offers to fill in the survey
89 if ($survey->isMode(Survey
::MODE_ALL
) ||
!$survey->hasVoted(S
::v('uid'))) {
90 $page->assign('survey_votemode', true
);
92 $page->assign('survey_warning', "Tu as déjà voté à ce sondage.");
94 //$page->assign('survey_id', $id);
95 $this->show_survey($page, $survey);
100 // {{{ function handler_admin() : index of admin mode
101 function handler_admin(&$page, $id = -1)
103 require_once dirname(__FILE__
).'/survey/survey.inc.php';
104 $this->clear_session();
106 $page->changeTpl('survey/admin.tpl');
107 $page->assign('survey_waiting', Survey
::retrieveList('w'));
108 $page->assign('survey_current', Survey
::retrieveList('c'));
109 $page->assign('survey_old', Survey
::retrieveList('o'));
110 $page->assign('survey_modes', Survey
::getModes(false
));
112 $survey = Survey
::retrieveSurvey($id); // retrieves all survey object structure
113 if ($survey == null
) {
114 $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
116 $page->assign('survey_adminmode', true
);
117 $this->show_survey($page, $survey);
122 // {{{ function handler_adminEdit() : edits a survey in admin mode
123 function handler_adminEdit(&$page, $id = -1)
126 return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin');
128 require_once dirname(__FILE__
).'/survey/survey.inc.php';
129 $survey = Survey
::retrieveSurvey($id); // retrieves the survey in database
130 $this->clear_session(); // cleans session (in case there would have been a problem before)
131 $this->store_session($survey, $id);
132 $this->handler_edit($page, 'show'); // calls handler_edit, but in admin mode since 'survey_id' is in session
136 // {{{ function handler_adminValidate() : validates a survey (admin mode)
137 function handler_adminValidate(&$page, $id = -1)
139 $id = Post
::i('survey_id', $id);
140 if (Post
::has('survey_cancel')) { // if the admin cancels the validation, returns to the admin index
141 $this->clear_session();
142 return $this->handler_admin(&$page, $id);
145 return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin');
147 require_once dirname(__FILE__
).'/survey/survey.inc.php';
148 $surveyInfo = Survey
::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure)
149 if ($surveyInfo == null
) {
150 return $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
152 if (Post
::has('survey_submit')) { // needs a confirmation before validation
153 if (Survey
::validateSurvey($id)) { // validates the survey (in the database)
154 $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien été validé, les votes sont maintenant ouverts.", 'admin');
156 $this->show_error($page, '', 'admin');
158 } else { // asks for a confirmation
159 $this->show_confirm($page, "Êtes-vous certain de vouloir valider le sondage \"".$surveyInfo['title']."\" ? "
160 ."Les votes seront immédiatement ouverts.", 'admin/valid', array('id' => $id));
165 // {{{ function handler_adminDelete() : deletes a survey (admin mode)
166 function handler_adminDelete(&$page, $id = -1)
168 $id = Post
::i('survey_id', $id);
169 if (Post
::has('survey_cancel')) { // if the admin cancels the suppression, returns to the admin index
170 return $this->handler_admin(&$page, $id);
173 return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin');
175 require_once dirname(__FILE__
).'/survey/survey.inc.php';
176 $surveyInfo = Survey
::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure)
177 if ($surveyInfo == null
) {
178 return $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
180 if (Post
::has('survey_submit')) { // needs a confirmation before suppression
181 if (Survey
::deleteSurvey($id)) { // deletes survey in database
182 $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien été supprimé, ainsi que tous les votes le concernant.", 'admin');
184 $this->show_error($page, '', 'admin');
186 } else { // asks for a confirmation
187 $this->show_confirm($page, "Êtes-vous certain de vouloir supprimer le sondage \"".$surveyInfo['title']."\" ?", 'admin/del', array('id' => $id));
192 // {{{ function handler_edit() : edits a survey (in normal mode unless called by handler_adminEdit() )
193 function handler_edit(&$page, $action = 'show', $qid = 'root')
195 require_once dirname(__FILE__
).'/survey/survey.inc.php';
196 $action = Post
::v('survey_action', $action);
197 $qid = Post
::v('survey_qid', $qid);
198 if (Post
::has('survey_cancel')) { // after cancelling changes, shows the survey
199 if (S
::has('survey')) {
201 } else { // unless no editing has been done at all (shows to the surveys index page)
202 return $this->handler_index($page);
205 $page->assign('survey_editmode', true
);
206 if (S
::has('survey_id')) { // if 'survey_id' is in session, it means we are modifying a survey in admin mode
207 $page->assign('survey_updatemode', true
);
209 if ($action == 'show' && !S
::has('survey')) {
212 if ($action == 'question') { // {{{ modifies an existing question
213 if (Post
::has('survey_submit')) { // if the form has been submitted, makes the modifications
214 $survey = unserialize(S
::v('survey'));
215 $args = Post
::v('survey_question');
216 if (!$survey->editQuestion($qid, $args)) { // update the survey object structure
217 return $this->show_error($page, '', 'edit');
219 $this->show_survey($page, $survey);
220 $this->store_session($survey);
221 } else { // if a form has not been submitted, shows modification form
222 $survey = unserialize(S
::v('survey'));
223 $current = $survey->toArray($qid); // gets the current parameters of the question
224 if ($current == null
) {
225 return $this->show_error($page, '', 'edit');
227 $this->show_form($page, $action, $qid, $current['type'], $current);
229 } elseif ($action == 'new') { // {{{ create a new survey : actually store the root question
230 if (Post
::has('survey_submit')) { // if the form has been submitted, creates the survey
231 $this->clear_session();
232 $survey = new Survey(Post
::v('survey_question')); // creates the object structure
233 $this->show_survey($page, $survey);
234 $this->store_session($survey);
236 $this->clear_session();
237 $this->show_form($page, $action, 'root', 'root');
239 } elseif ($action == 'add') { // {{{ adds a new question
240 if (Post
::has('survey_submit')) { // if the form has been submitted, adds the question
241 $survey = unserialize(S
::v('survey'));
242 if (!$survey->addQuestion($qid, $survey->factory(Post
::v('survey_type'), Post
::v('survey_question')))) {
243 return $this->show_error($page, '', 'edit');
245 $this->show_survey($page, $survey);
246 $this->store_session($survey);
248 $this->show_form($page, $action, $qid);
250 } elseif ($action == 'del') { // {{{ deletes a question
251 if (Post
::has('survey_submit')) { // if a confirmation has been sent, deletes the question
252 $survey = unserialize(S
::v('survey'));
253 if (!$survey->delQuestion(Post
::v('survey_qid'))) { // deletes the node in the survey object structure
254 return $this->show_error($page, '', 'edit');
256 $this->show_survey($page, $survey);
257 $this->store_session($survey);
258 } else { // if user has not confirmed, shows a confirmation form
259 $survey = unserialize(S
::v('survey'));
260 $current = $survey->toArray($qid); // needed to get the title of the question to delete (more user-friendly than an id)
261 if ($current == null
) {
262 return $this->show_error($page, '', 'edit');
264 $this->show_confirm($page, 'Êtes-vous certain de vouloir supprimer la question intitulé "'.$current['question'].'" ? '
265 .'Attention, cela supprimera en même temps toutes les questions qui dépendent de celle-ci.',
266 'edit', array('action' => 'del', 'qid' => $qid));
268 } elseif ($action == 'show') { // {{{ simply shows the survey in its current state
269 $this->show_survey($page, unserialize(S
::v('survey'))); // }}}
270 } elseif ($action == 'valid') { // {{{ validates the proposition, i.e stores the proposition in the database
271 // but an admin will still need to validate the survey before it is activated
272 if (Post
::has('survey_submit')) { // needs a confirmation before storing the proposition
273 $survey = unserialize(S
::v('survey'));
274 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
275 if ($survey->updateSurvey()) { // updates the database according the new survey object structure
276 $this->show_success($page, "Les modifications sur le sondage ont bien été enregistrées.", 'admin');
278 $this->show_error($page, '', 'admin');
280 } else { // if no 'survey_id' is in session, we are indeed proposing a new survey
281 if ($survey->proposeSurvey()) { // stores the survey object structure in database
282 $this->show_success($page, "Votre proposition de sondage a bien été enregistrée,
283 elle est en attent de validation par un administrateur du site.", '');
285 $this->show_error($page);
288 $this->clear_session();
289 } else { // asks for a confirmation if it has not been sent
290 $survey = unserialize(S
::v('survey'));
291 $errors = $survey->checkSyntax();
292 if (!is_null($errors)) {
293 $this->show_error($page, "", 'edit', $errors);
295 if (S
::has('survey_id')) {
296 $this->show_confirm($page, "Veuillez confirmer l'enregistrement des modifications apportées à ce sondage", 'edit', array('action' => 'valid'));
298 $this->show_confirm($page, "Veuillez confirmer l'envoi de cette proposition de sondage.", 'edit', array('action' => 'valid'));
302 } elseif ($action == 'cancel') { // {{{ cancels the creation/modification of a survey
303 if (Post
::has('survey_submit')) { // needs a confirmation
304 if (S
::has('survey_id')) { // only possible when modifying a survey in admin mode, still this should be considered again,
305 $this->clear_session(); // maybe some name with "admin" in it, "survey_adminid" or anything that might not be confusing.
306 return $this->handler_admin($page); // in this case, shows the admin index
308 $this->clear_session(); // cleans session
309 return $this->handler_index($page); // else shows the 'normal' index
311 } else { // asks for a confirmation if it has not been sent
312 $this->show_confirm(&$page, "Êtes-vous certain de vouloir annuler totalement l'édition de ce sondage ? Attention, "
313 ."toutes les données éditées jusque lâ seront définitivement perdues.",
314 'edit', array('action' => $action));
320 // {{{ function handler_ajax() : some ajax in editing a new question (for now, there may be a little more later)
321 function handler_ajax(&$page, $type)
323 require_once dirname(__FILE__
).'/survey/survey.inc.php';
324 header('Content-Type: text/html; charset="UTF-8"');
325 if (Survey
::isType($type)) { // when type has been chosen, the form is updated to fit exactly the type of question chosen
326 $page->changeTpl('survey/edit_new.tpl', NO_SKIN
);
327 $page->assign('survey_types', Survey
::getTypes());
328 $page->assign('survey_type', $type);
333 // {{{ function clear_session() : clears the data stored in session
334 function clear_session()
337 S
::kill('survey_id');
341 // {{{ function store_session() : serializes and stores survey (and survey_id) in session
342 function store_session($survey, $survey_id = -1)
344 $_SESSION['survey'] = serialize($survey);
345 if ($survey_id != -1) {
346 $_SESSION['survey_id'] = $survey_id;
351 // {{{ function show_survey() : calls the template to display a survey, for editing, voting, or consulting the results
352 function show_survey(&$page, $survey)
354 $page->changeTpl('survey/show_root.tpl');
355 $page->assign('survey', $survey->toArray());
356 $page->assign('survey_modes', Survey
::getModes());
360 // {{{ function show_form() : calls the template to display the editing form
361 function show_form(&$page, $action, $qid, $type = 'new', $current = null
)
363 $page->changeTpl('survey/edit_survey.tpl');
364 $page->assign('survey_action', $action);
365 $page->assign('survey_qid', $qid);
366 $page->assign('survey_formaction', './survey/edit');
367 $page->assign('survey_type', $type);
368 if (!is_null($current) && is_array($current)) {
369 $page->assign('survey_current', $current);
370 } elseif ($type == 'new') {
371 $page->addJsLink('ajax.js');
372 $page->assign('survey_types', Survey
::getTypes());
374 if ($type == 'root') {
375 $page->assign('survey_modes', Survey
::getModes());
380 // {{{ function show_confirm() : calls the template to display a confirm form
381 function show_confirm(&$page, $message, $formaction, $formhidden = null
)
383 $page->changeTpl('survey/confirm.tpl');
384 $page->assign('survey_message', $message);
385 $page->assign('survey_formaction', './survey/'.$formaction);
386 $page->assign('survey_formhidden', $formhidden);
390 // {{{ function show_error() : calls the template to display an error message
391 function show_error(&$page, $message, $link = "", $errArray = null
)
393 $page->changeTpl('survey/error.tpl');
394 $page->assign('survey_message', $message);
395 $page->assign('survey_link', './survey/'.$link); // 'return' link to let the user leave the page
396 if (!is_null($errArray)) {
397 $page->assign('survey_errors', $errArray);
402 // {{{ function show_success() : calls the template to display a success message
403 function show_success(&$page, $message = "", $link = "")
405 $page->changeTpl('survey/success.tpl');
406 $page->assign('survey_message', $message);
407 $page->assign('survey_link', './survey/'.$link); // 'return' link to let the user leave the page
412 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: