Can disable email redirection
[platal.git] / modules / survey.php
CommitLineData
8fe81c50 1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22class SurveyModule extends PLModule
23{
24 // {{{ function handlers() : registers the different handlers
25 function handlers()
26 {
27 return array(
5c6e38d7 28 'survey' => $this->make_hook('index', AUTH_PUBLIC),
29 'survey/vote' => $this->make_hook('vote', AUTH_PUBLIC),
8fe81c50 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'),
36 );
37 }
38 // }}}
39
40 // {{{ function handler_index() : lists all available surveys
41 function handler_index(&$page, $action = null)
42 {
43 require_once dirname(__FILE__).'/survey/survey.inc.php';
44 $page->changeTpl('survey/index.tpl');
5c6e38d7 45 $page->assign('survey_current', Survey::retrieveList('c'));
46 $page->assign('survey_old', Survey::retrieveList('o'));
47 $page->assign('survey_modes', Survey::getModes(false));
48 }
49 // }}}
50
51 // {{{ function handler_vote()
52 function handler_vote(&$page, $id = -1)
53 {
54 if (Post::has('survey_cancel')) { // if the user cancels, returns to index
55 return $this->handler_index(&$page);
56 }
57 $id = intval($id);
58 if ($id == -1) {
59 return $this->show_error($page, "Un identifiant de sondage doit &#234;tre pr&#233;cis&#233;.", '');
60 }
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&#233;.");
67 }
68 if (!$survey->isMode(Survey::MODE_ALL)) { // if the survey is reserved to alumni
69 global $globals;
70 if (!call_user_func(array($globals->session, 'doAuth'))) { // checks authentification
71 global $platal;
72 $platal->force_login($page);
73 }
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&#232;s &#224; ce sondage car il est r&#233;serv&#233; &#224; d'autres promotions.");
76 }
77 }
78 if (Post::has('survey_submit')) { // checks if the survey has already been filled in
79 $uid = 0;
80 if (!$survey->isMode(Survey::MODE_ALL)) { // if survey is restriced to alumni
81 $uid = S::v('uid');
82 if ($survey->hasVoted($uid)) { // checks whether the user has already voted
83 return $this->show_error($page, "Tu as d&#233;j&#224; vot&#233; &#224; ce sondage.");
84 }
85 }
86 $survey->vote($uid, Post::v('survey'.$id)); // performs vote
87 $this->show_success($page, "Ta r&#233;ponse a bien &#233;t&#233; prise en compte. Merci d'avoir particip&#233; &#224; 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);
91 } else {
92 $page->assign('survey_warning', "Tu as d&#233;j&#224; vot&#233; &#224; ce sondage.");
93 }
94 //$page->assign('survey_id', $id);
95 $this->show_survey($page, $survey);
96 }
8fe81c50 97 }
98 // }}}
99
100 // {{{ function handler_admin() : index of admin mode
56c6950a 101 function handler_admin(&$page, $id = -1)
8fe81c50 102 {
103 require_once dirname(__FILE__).'/survey/survey.inc.php';
56c6950a 104 $this->clear_session();
105 if ($id == -1) {
106 $page->changeTpl('survey/admin.tpl');
5c6e38d7 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));
56c6950a 111 } else {
5c6e38d7 112 $survey = Survey::retrieveSurvey($id); // retrieves all survey object structure
56c6950a 113 if ($survey == null) {
114 $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
115 }
56c6950a 116 $page->assign('survey_adminmode', true);
56c6950a 117 $this->show_survey($page, $survey);
118 }
8fe81c50 119 }
120 // }}}
121
122 // {{{ function handler_adminEdit() : edits a survey in admin mode
56c6950a 123 function handler_adminEdit(&$page, $id = -1)
8fe81c50 124 {
56c6950a 125 if ($id == -1) {
126 return $this->show_error($page, "Un identifiant de sondage doit &#234;tre pr&#233;cis&#233;.", 'admin');
127 }
8fe81c50 128 require_once dirname(__FILE__).'/survey/survey.inc.php';
5c6e38d7 129 $survey = Survey::retrieveSurvey($id); // retrieves the survey in database
56c6950a 130 $this->clear_session(); // cleans session (in case there would have been a problem before)
56c6950a 131 $this->store_session($survey, $id);
8fe81c50 132 $this->handler_edit($page, 'show'); // calls handler_edit, but in admin mode since 'survey_id' is in session
133 }
134 // }}}
135
136 // {{{ function handler_adminValidate() : validates a survey (admin mode)
137 function handler_adminValidate(&$page, $id = -1)
138 {
5c6e38d7 139 $id = Post::i('survey_id', $id);
8fe81c50 140 if (Post::has('survey_cancel')) { // if the admin cancels the validation, returns to the admin index
56c6950a 141 $this->clear_session();
142 return $this->handler_admin(&$page, $id);
8fe81c50 143 }
8fe81c50 144 if ($id == -1) {
145 return $this->show_error($page, "Un identifiant de sondage doit &#234;tre pr&#233;cis&#233;.", 'admin');
146 }
147 require_once dirname(__FILE__).'/survey/survey.inc.php';
5c6e38d7 148 $surveyInfo = Survey::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure)
8fe81c50 149 if ($surveyInfo == null) {
150 return $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
151 }
152 if (Post::has('survey_submit')) { // needs a confirmation before validation
5c6e38d7 153 if (Survey::validateSurvey($id)) { // validates the survey (in the database)
8fe81c50 154 $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien &#233;t&#233; valid&#233;, les votes sont maintenant ouverts.", 'admin');
155 } else {
156 $this->show_error($page, '', 'admin');
157 }
158 } else { // asks for a confirmation
159 $this->show_confirm($page, "&#202;tes-vous certain de vouloir valider le sondage \"".$surveyInfo['title']."\" ? "
160 ."Les votes seront imm&#233;diatement ouverts.", 'admin/valid', array('id' => $id));
161 }
162 }
163 // }}}
164
165 // {{{ function handler_adminDelete() : deletes a survey (admin mode)
166 function handler_adminDelete(&$page, $id = -1)
167 {
5c6e38d7 168 $id = Post::i('survey_id', $id);
8fe81c50 169 if (Post::has('survey_cancel')) { // if the admin cancels the suppression, returns to the admin index
56c6950a 170 return $this->handler_admin(&$page, $id);
8fe81c50 171 }
8fe81c50 172 if ($id == -1) {
173 return $this->show_error($page, "Un identifiant de sondage doit &#234;tre pr&#233;cis&#233;.", 'admin');
174 }
175 require_once dirname(__FILE__).'/survey/survey.inc.php';
5c6e38d7 176 $surveyInfo = Survey::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure)
8fe81c50 177 if ($surveyInfo == null) {
178 return $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
179 }
180 if (Post::has('survey_submit')) { // needs a confirmation before suppression
5c6e38d7 181 if (Survey::deleteSurvey($id)) { // deletes survey in database
8fe81c50 182 $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien &#233;t&#233; supprim&#233;, ainsi que tous les votes le concernant.", 'admin');
183 } else {
184 $this->show_error($page, '', 'admin');
185 }
186 } else { // asks for a confirmation
56c6950a 187 $this->show_confirm($page, "&#202;tes-vous certain de vouloir supprimer le sondage \"".$surveyInfo['title']."\" ?", 'admin/del', array('id' => $id));
8fe81c50 188 }
189 }
190 // }}}
191
192 // {{{ function handler_edit() : edits a survey (in normal mode unless called by handler_adminEdit() )
5c6e38d7 193 function handler_edit(&$page, $action = 'show', $qid = 'root')
8fe81c50 194 {
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')) {
200 $action = 'show';
201 } else { // unless no editing has been done at all (shows to the surveys index page)
202 return $this->handler_index($page);
203 }
204 }
56c6950a 205 $page->assign('survey_editmode', true);
8fe81c50 206 if (S::has('survey_id')) { // if 'survey_id' is in session, it means we are modifying a survey in admin mode
56c6950a 207 $page->assign('survey_updatemode', true);
8fe81c50 208 }
209 if ($action == 'show' && !S::has('survey')) {
210 $action = 'new';
211 }
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');
5c6e38d7 216 if (!$survey->editQuestion($qid, $args)) { // update the survey object structure
8fe81c50 217 return $this->show_error($page, '', 'edit');
218 }
219 $this->show_survey($page, $survey);
56c6950a 220 $this->store_session($survey);
8fe81c50 221 } else { // if a form has not been submitted, shows modification form
222 $survey = unserialize(S::v('survey'));
5c6e38d7 223 $current = $survey->toArray($qid); // gets the current parameters of the question
8fe81c50 224 if ($current == null) {
225 return $this->show_error($page, '', 'edit');
226 }
227 $this->show_form($page, $action, $qid, $current['type'], $current);
228 } // }}}
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
56c6950a 231 $this->clear_session();
5c6e38d7 232 $survey = new Survey(Post::v('survey_question')); // creates the object structure
8fe81c50 233 $this->show_survey($page, $survey);
56c6950a 234 $this->store_session($survey);
8fe81c50 235 } else {
56c6950a 236 $this->clear_session();
5c6e38d7 237 $this->show_form($page, $action, 'root', 'root');
8fe81c50 238 } // }}}
5c6e38d7 239 } elseif ($action == 'add') { // {{{ adds a new question
8fe81c50 240 if (Post::has('survey_submit')) { // if the form has been submitted, adds the question
241 $survey = unserialize(S::v('survey'));
5c6e38d7 242 if (!$survey->addQuestion($qid, $survey->factory(Post::v('survey_type'), Post::v('survey_question')))) {
243 return $this->show_error($page, '', 'edit');
8fe81c50 244 }
245 $this->show_survey($page, $survey);
56c6950a 246 $this->store_session($survey);
8fe81c50 247 } else {
248 $this->show_form($page, $action, $qid);
249 } // }}}
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'));
5c6e38d7 253 if (!$survey->delQuestion(Post::v('survey_qid'))) { // deletes the node in the survey object structure
8fe81c50 254 return $this->show_error($page, '', 'edit');
255 }
256 $this->show_survey($page, $survey);
56c6950a 257 $this->store_session($survey);
8fe81c50 258 } else { // if user has not confirmed, shows a confirmation form
259 $survey = unserialize(S::v('survey'));
5c6e38d7 260 $current = $survey->toArray($qid); // needed to get the title of the question to delete (more user-friendly than an id)
8fe81c50 261 if ($current == null) {
262 return $this->show_error($page, '', 'edit');
263 }
264 $this->show_confirm($page, '&#202;tes-vous certain de vouloir supprimer la question intitul&#233; "'.$current['question'].'" ? '
265 .'Attention, cela supprimera en m&#234;me temps toutes les questions qui d&#233;pendent de celle-ci.',
266 'edit', array('action' => 'del', 'qid' => $qid));
267 } // }}}
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
5c6e38d7 275 if ($survey->updateSurvey()) { // updates the database according the new survey object structure
8fe81c50 276 $this->show_success($page, "Les modifications sur le sondage ont bien &#233;t&#233; enregistr&#233;es.", 'admin');
277 } else {
278 $this->show_error($page, '', 'admin');
279 }
280 } else { // if no 'survey_id' is in session, we are indeed proposing a new survey
5c6e38d7 281 if ($survey->proposeSurvey()) { // stores the survey object structure in database
8fe81c50 282 $this->show_success($page, "Votre proposition de sondage a bien &#233;t&#233; enregistr&#233;e,
283 elle est en attent de validation par un administrateur du site.", '');
284 } else {
285 $this->show_error($page);
286 }
287 }
56c6950a 288 $this->clear_session();
8fe81c50 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);
294 } else {
295 if (S::has('survey_id')) {
296 $this->show_confirm($page, "Veuillez confirmer l'enregistrement des modifications apport&#233;es &#224; ce sondage", 'edit', array('action' => 'valid'));
297 } else {
298 $this->show_confirm($page, "Veuillez confirmer l'envoi de cette proposition de sondage.", 'edit', array('action' => 'valid'));
299 }
300 }
301 } // }}}
302 } elseif ($action == 'cancel') { // {{{ cancels the creation/modification of a survey
303 if (Post::has('survey_submit')) { // needs a confirmation
8fe81c50 304 if (S::has('survey_id')) { // only possible when modifying a survey in admin mode, still this should be considered again,
56c6950a 305 $this->clear_session(); // maybe some name with "admin" in it, "survey_adminid" or anything that might not be confusing.
8fe81c50 306 return $this->handler_admin($page); // in this case, shows the admin index
307 } else {
56c6950a 308 $this->clear_session(); // cleans session
8fe81c50 309 return $this->handler_index($page); // else shows the 'normal' index
310 }
311 } else { // asks for a confirmation if it has not been sent
312 $this->show_confirm(&$page, "&#202;tes-vous certain de vouloir annuler totalement l'&#233;dition de ce sondage ? Attention, "
313 ."toutes les donn&#233;es &#233;dit&#233;es jusque l&#226; seront d&#233;finitivement perdues.",
314 'edit', array('action' => $action));
315 }
316 } // }}}
317 }
318 // }}}
319
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)
322 {
323 require_once dirname(__FILE__).'/survey/survey.inc.php';
324 header('Content-Type: text/html; charset="UTF-8"');
5c6e38d7 325 if (Survey::isType($type)) { // when type has been chosen, the form is updated to fit exactly the type of question chosen
8fe81c50 326 $page->changeTpl('survey/edit_new.tpl', NO_SKIN);
5c6e38d7 327 $page->assign('survey_types', Survey::getTypes());
8fe81c50 328 $page->assign('survey_type', $type);
329 }
330 }
331 // }}}
332
56c6950a 333 // {{{ function clear_session() : clears the data stored in session
334 function clear_session()
335 {
336 S::kill('survey');
337 S::kill('survey_id');
338 }
339 // }}}
340
341 // {{{ function store_session() : serializes and stores survey (and survey_id) in session
342 function store_session($survey, $survey_id = -1)
343 {
344 $_SESSION['survey'] = serialize($survey);
345 if ($survey_id != -1) {
346 $_SESSION['survey_id'] = $survey_id;
347 }
348 }
349 // }}}
350
8fe81c50 351 // {{{ function show_survey() : calls the template to display a survey, for editing, voting, or consulting the results
352 function show_survey(&$page, $survey)
353 {
56c6950a 354 $page->changeTpl('survey/show_root.tpl');
8fe81c50 355 $page->assign('survey', $survey->toArray());
5c6e38d7 356 $page->assign('survey_modes', Survey::getModes());
8fe81c50 357 }
358 // }}}
359
360 // {{{ function show_form() : calls the template to display the editing form
361 function show_form(&$page, $action, $qid, $type = 'new', $current = null)
362 {
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');
5c6e38d7 372 $page->assign('survey_types', Survey::getTypes());
373 }
374 if ($type == 'root') {
375 $page->assign('survey_modes', Survey::getModes());
8fe81c50 376 }
377 }
378 // }}}
379
380 // {{{ function show_confirm() : calls the template to display a confirm form
56c6950a 381 function show_confirm(&$page, $message, $formaction, $formhidden = null)
8fe81c50 382 {
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);
387 }
388 // }}}
389
390 // {{{ function show_error() : calls the template to display an error message
391 function show_error(&$page, $message, $link = "", $errArray = null)
392 {
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);
398 }
399 }
400 // }}}
401
402 // {{{ function show_success() : calls the template to display a success message
403 function show_success(&$page, $message = "", $link = "")
404 {
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
408 }
409 // }}}
410}
411
412// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
413?>