first view of the results of a survey
[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),
9f01f40b 30 'survey/result' => $this->make_hook('result', AUTH_PUBLIC),
8fe81c50 31 'survey/edit' => $this->make_hook('edit', AUTH_COOKIE),
32 'survey/ajax' => $this->make_hook('ajax', AUTH_COOKIE),
33 'survey/admin' => $this->make_hook('admin', AUTH_MDP, 'admin'),
34 'survey/admin/edit' => $this->make_hook('adminEdit', AUTH_MDP, 'admin'),
35 'survey/admin/valid' => $this->make_hook('adminValidate', AUTH_MDP, 'admin'),
36 'survey/admin/del' => $this->make_hook('adminDelete', AUTH_MDP, 'admin'),
37 );
38 }
39 // }}}
40
41 // {{{ function handler_index() : lists all available surveys
42 function handler_index(&$page, $action = null)
43 {
44 require_once dirname(__FILE__).'/survey/survey.inc.php';
45 $page->changeTpl('survey/index.tpl');
5c6e38d7 46 $page->assign('survey_current', Survey::retrieveList('c'));
47 $page->assign('survey_old', Survey::retrieveList('o'));
48 $page->assign('survey_modes', Survey::getModes(false));
49 }
50 // }}}
51
9f01f40b 52 // {{{ function handler_vote() : handles the vote to a survey
5c6e38d7 53 function handler_vote(&$page, $id = -1)
54 {
55 if (Post::has('survey_cancel')) { // if the user cancels, returns to index
56 return $this->handler_index(&$page);
57 }
58 $id = intval($id);
59 if ($id == -1) {
60 return $this->show_error($page, "Un identifiant de sondage doit &#234;tre pr&#233;cis&#233;.", '');
61 }
62 require_once dirname(__FILE__).'/survey/survey.inc.php';
63 $survey = Survey::retrieveSurvey($id); // retrieves the survey object structure
64 if ($survey == null || !$survey->isValid()) {
65 return $this->show_error($page, "Sondage ".$id." introuvable.", '');
66 } elseif ($survey->isEnded()) {
67 return $this->show_error($page, "Le sondage ".$survey->getTitle()." est termin&#233;.");
68 }
9f01f40b 69 if (!$this->check_surveyPerms($page, $survey)) {
70 return;
5c6e38d7 71 }
72 if (Post::has('survey_submit')) { // checks if the survey has already been filled in
73 $uid = 0;
74 if (!$survey->isMode(Survey::MODE_ALL)) { // if survey is restriced to alumni
75 $uid = S::v('uid');
76 if ($survey->hasVoted($uid)) { // checks whether the user has already voted
77 return $this->show_error($page, "Tu as d&#233;j&#224; vot&#233; &#224; ce sondage.");
78 }
79 }
80 $survey->vote($uid, Post::v('survey'.$id)); // performs vote
81 $this->show_success($page, "Ta r&#233;ponse a bien &#233;t&#233; prise en compte. Merci d'avoir particip&#233; &#224; ce sondage.", '');
82 } else { // offers to fill in the survey
83 if ($survey->isMode(Survey::MODE_ALL) || !$survey->hasVoted(S::v('uid'))) {
84 $page->assign('survey_votemode', true);
85 } else {
86 $page->assign('survey_warning', "Tu as d&#233;j&#224; vot&#233; &#224; ce sondage.");
87 }
88 //$page->assign('survey_id', $id);
89 $this->show_survey($page, $survey);
90 }
8fe81c50 91 }
92 // }}}
93
9f01f40b 94 // {{{ function handler_result() : show the results of the votes to a survey
95 function handler_result($page, $id = -1, $qid = 'all')
96 {
97 $id = intval($id);
98 if ($id == -1) {
99 return $this->show_error($page, "Un identifiant de sondage doit &#234;tre pr&#233;cis&#233;.", '');
100 }
101 require_once dirname(__FILE__).'/survey/survey.inc.php';
102 $survey = Survey::retrieveSurvey($id); // retrieves the survey object structure
103 if ($survey == null || !$survey->isValid()) {
104 return $this->show_error($page, "Sondage ".$id." introuvable.", '');
105 } elseif (!$survey->isEnded()) {
106 return $this->show_error($page, "Le sondage ".$survey->getTitle()." n'est pas encore termin&#233;.");
107 }
108 if (!$this->check_surveyPerms($page, $survey)) {
109 return;
110 }
111 $page->assign('survey_resultmode', true);
112 $this->show_survey($page, $survey);
113 }
114 // }}}
115
8fe81c50 116 // {{{ function handler_admin() : index of admin mode
56c6950a 117 function handler_admin(&$page, $id = -1)
8fe81c50 118 {
119 require_once dirname(__FILE__).'/survey/survey.inc.php';
56c6950a 120 $this->clear_session();
121 if ($id == -1) {
122 $page->changeTpl('survey/admin.tpl');
5c6e38d7 123 $page->assign('survey_waiting', Survey::retrieveList('w'));
124 $page->assign('survey_current', Survey::retrieveList('c'));
125 $page->assign('survey_old', Survey::retrieveList('o'));
126 $page->assign('survey_modes', Survey::getModes(false));
56c6950a 127 } else {
5c6e38d7 128 $survey = Survey::retrieveSurvey($id); // retrieves all survey object structure
56c6950a 129 if ($survey == null) {
130 $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
131 }
56c6950a 132 $page->assign('survey_adminmode', true);
56c6950a 133 $this->show_survey($page, $survey);
134 }
8fe81c50 135 }
136 // }}}
137
138 // {{{ function handler_adminEdit() : edits a survey in admin mode
56c6950a 139 function handler_adminEdit(&$page, $id = -1)
8fe81c50 140 {
56c6950a 141 if ($id == -1) {
142 return $this->show_error($page, "Un identifiant de sondage doit &#234;tre pr&#233;cis&#233;.", 'admin');
143 }
8fe81c50 144 require_once dirname(__FILE__).'/survey/survey.inc.php';
5c6e38d7 145 $survey = Survey::retrieveSurvey($id); // retrieves the survey in database
56c6950a 146 $this->clear_session(); // cleans session (in case there would have been a problem before)
56c6950a 147 $this->store_session($survey, $id);
8fe81c50 148 $this->handler_edit($page, 'show'); // calls handler_edit, but in admin mode since 'survey_id' is in session
149 }
150 // }}}
151
152 // {{{ function handler_adminValidate() : validates a survey (admin mode)
153 function handler_adminValidate(&$page, $id = -1)
154 {
5c6e38d7 155 $id = Post::i('survey_id', $id);
8fe81c50 156 if (Post::has('survey_cancel')) { // if the admin cancels the validation, returns to the admin index
56c6950a 157 $this->clear_session();
158 return $this->handler_admin(&$page, $id);
8fe81c50 159 }
8fe81c50 160 if ($id == -1) {
161 return $this->show_error($page, "Un identifiant de sondage doit &#234;tre pr&#233;cis&#233;.", 'admin');
162 }
163 require_once dirname(__FILE__).'/survey/survey.inc.php';
5c6e38d7 164 $surveyInfo = Survey::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure)
8fe81c50 165 if ($surveyInfo == null) {
166 return $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
167 }
168 if (Post::has('survey_submit')) { // needs a confirmation before validation
5c6e38d7 169 if (Survey::validateSurvey($id)) { // validates the survey (in the database)
8fe81c50 170 $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien &#233;t&#233; valid&#233;, les votes sont maintenant ouverts.", 'admin');
171 } else {
172 $this->show_error($page, '', 'admin');
173 }
174 } else { // asks for a confirmation
175 $this->show_confirm($page, "&#202;tes-vous certain de vouloir valider le sondage \"".$surveyInfo['title']."\" ? "
176 ."Les votes seront imm&#233;diatement ouverts.", 'admin/valid', array('id' => $id));
177 }
178 }
179 // }}}
180
181 // {{{ function handler_adminDelete() : deletes a survey (admin mode)
182 function handler_adminDelete(&$page, $id = -1)
183 {
5c6e38d7 184 $id = Post::i('survey_id', $id);
8fe81c50 185 if (Post::has('survey_cancel')) { // if the admin cancels the suppression, returns to the admin index
56c6950a 186 return $this->handler_admin(&$page, $id);
8fe81c50 187 }
8fe81c50 188 if ($id == -1) {
189 return $this->show_error($page, "Un identifiant de sondage doit &#234;tre pr&#233;cis&#233;.", 'admin');
190 }
191 require_once dirname(__FILE__).'/survey/survey.inc.php';
5c6e38d7 192 $surveyInfo = Survey::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure)
8fe81c50 193 if ($surveyInfo == null) {
194 return $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
195 }
196 if (Post::has('survey_submit')) { // needs a confirmation before suppression
5c6e38d7 197 if (Survey::deleteSurvey($id)) { // deletes survey in database
8fe81c50 198 $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien &#233;t&#233; supprim&#233;, ainsi que tous les votes le concernant.", 'admin');
199 } else {
200 $this->show_error($page, '', 'admin');
201 }
202 } else { // asks for a confirmation
56c6950a 203 $this->show_confirm($page, "&#202;tes-vous certain de vouloir supprimer le sondage \"".$surveyInfo['title']."\" ?", 'admin/del', array('id' => $id));
8fe81c50 204 }
205 }
206 // }}}
207
208 // {{{ function handler_edit() : edits a survey (in normal mode unless called by handler_adminEdit() )
5c6e38d7 209 function handler_edit(&$page, $action = 'show', $qid = 'root')
8fe81c50 210 {
211 require_once dirname(__FILE__).'/survey/survey.inc.php';
212 $action = Post::v('survey_action', $action);
213 $qid = Post::v('survey_qid', $qid);
214 if (Post::has('survey_cancel')) { // after cancelling changes, shows the survey
215 if (S::has('survey')) {
216 $action = 'show';
217 } else { // unless no editing has been done at all (shows to the surveys index page)
218 return $this->handler_index($page);
219 }
220 }
56c6950a 221 $page->assign('survey_editmode', true);
8fe81c50 222 if (S::has('survey_id')) { // if 'survey_id' is in session, it means we are modifying a survey in admin mode
56c6950a 223 $page->assign('survey_updatemode', true);
8fe81c50 224 }
225 if ($action == 'show' && !S::has('survey')) {
226 $action = 'new';
227 }
228 if ($action == 'question') { // {{{ modifies an existing question
229 if (Post::has('survey_submit')) { // if the form has been submitted, makes the modifications
230 $survey = unserialize(S::v('survey'));
231 $args = Post::v('survey_question');
5c6e38d7 232 if (!$survey->editQuestion($qid, $args)) { // update the survey object structure
8fe81c50 233 return $this->show_error($page, '', 'edit');
234 }
235 $this->show_survey($page, $survey);
56c6950a 236 $this->store_session($survey);
8fe81c50 237 } else { // if a form has not been submitted, shows modification form
238 $survey = unserialize(S::v('survey'));
5c6e38d7 239 $current = $survey->toArray($qid); // gets the current parameters of the question
8fe81c50 240 if ($current == null) {
241 return $this->show_error($page, '', 'edit');
242 }
243 $this->show_form($page, $action, $qid, $current['type'], $current);
244 } // }}}
245 } elseif ($action == 'new') { // {{{ create a new survey : actually store the root question
246 if (Post::has('survey_submit')) { // if the form has been submitted, creates the survey
56c6950a 247 $this->clear_session();
5c6e38d7 248 $survey = new Survey(Post::v('survey_question')); // creates the object structure
8fe81c50 249 $this->show_survey($page, $survey);
56c6950a 250 $this->store_session($survey);
8fe81c50 251 } else {
56c6950a 252 $this->clear_session();
5c6e38d7 253 $this->show_form($page, $action, 'root', 'root');
8fe81c50 254 } // }}}
5c6e38d7 255 } elseif ($action == 'add') { // {{{ adds a new question
8fe81c50 256 if (Post::has('survey_submit')) { // if the form has been submitted, adds the question
257 $survey = unserialize(S::v('survey'));
5c6e38d7 258 if (!$survey->addQuestion($qid, $survey->factory(Post::v('survey_type'), Post::v('survey_question')))) {
259 return $this->show_error($page, '', 'edit');
8fe81c50 260 }
261 $this->show_survey($page, $survey);
56c6950a 262 $this->store_session($survey);
8fe81c50 263 } else {
264 $this->show_form($page, $action, $qid);
265 } // }}}
266 } elseif ($action == 'del') { // {{{ deletes a question
267 if (Post::has('survey_submit')) { // if a confirmation has been sent, deletes the question
268 $survey = unserialize(S::v('survey'));
5c6e38d7 269 if (!$survey->delQuestion(Post::v('survey_qid'))) { // deletes the node in the survey object structure
8fe81c50 270 return $this->show_error($page, '', 'edit');
271 }
272 $this->show_survey($page, $survey);
56c6950a 273 $this->store_session($survey);
8fe81c50 274 } else { // if user has not confirmed, shows a confirmation form
275 $survey = unserialize(S::v('survey'));
5c6e38d7 276 $current = $survey->toArray($qid); // needed to get the title of the question to delete (more user-friendly than an id)
8fe81c50 277 if ($current == null) {
278 return $this->show_error($page, '', 'edit');
279 }
280 $this->show_confirm($page, '&#202;tes-vous certain de vouloir supprimer la question intitul&#233; "'.$current['question'].'" ? '
281 .'Attention, cela supprimera en m&#234;me temps toutes les questions qui d&#233;pendent de celle-ci.',
282 'edit', array('action' => 'del', 'qid' => $qid));
283 } // }}}
284 } elseif ($action == 'show') { // {{{ simply shows the survey in its current state
285 $this->show_survey($page, unserialize(S::v('survey'))); // }}}
286 } elseif ($action == 'valid') { // {{{ validates the proposition, i.e stores the proposition in the database
287 // but an admin will still need to validate the survey before it is activated
288 if (Post::has('survey_submit')) { // needs a confirmation before storing the proposition
289 $survey = unserialize(S::v('survey'));
290 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 291 if ($survey->updateSurvey()) { // updates the database according the new survey object structure
8fe81c50 292 $this->show_success($page, "Les modifications sur le sondage ont bien &#233;t&#233; enregistr&#233;es.", 'admin');
293 } else {
294 $this->show_error($page, '', 'admin');
295 }
296 } else { // if no 'survey_id' is in session, we are indeed proposing a new survey
5c6e38d7 297 if ($survey->proposeSurvey()) { // stores the survey object structure in database
8fe81c50 298 $this->show_success($page, "Votre proposition de sondage a bien &#233;t&#233; enregistr&#233;e,
299 elle est en attent de validation par un administrateur du site.", '');
300 } else {
301 $this->show_error($page);
302 }
303 }
56c6950a 304 $this->clear_session();
8fe81c50 305 } else { // asks for a confirmation if it has not been sent
306 $survey = unserialize(S::v('survey'));
307 $errors = $survey->checkSyntax();
308 if (!is_null($errors)) {
309 $this->show_error($page, "", 'edit', $errors);
310 } else {
311 if (S::has('survey_id')) {
312 $this->show_confirm($page, "Veuillez confirmer l'enregistrement des modifications apport&#233;es &#224; ce sondage", 'edit', array('action' => 'valid'));
313 } else {
314 $this->show_confirm($page, "Veuillez confirmer l'envoi de cette proposition de sondage.", 'edit', array('action' => 'valid'));
315 }
316 }
317 } // }}}
318 } elseif ($action == 'cancel') { // {{{ cancels the creation/modification of a survey
319 if (Post::has('survey_submit')) { // needs a confirmation
8fe81c50 320 if (S::has('survey_id')) { // only possible when modifying a survey in admin mode, still this should be considered again,
56c6950a 321 $this->clear_session(); // maybe some name with "admin" in it, "survey_adminid" or anything that might not be confusing.
8fe81c50 322 return $this->handler_admin($page); // in this case, shows the admin index
323 } else {
56c6950a 324 $this->clear_session(); // cleans session
8fe81c50 325 return $this->handler_index($page); // else shows the 'normal' index
326 }
327 } else { // asks for a confirmation if it has not been sent
328 $this->show_confirm(&$page, "&#202;tes-vous certain de vouloir annuler totalement l'&#233;dition de ce sondage ? Attention, "
329 ."toutes les donn&#233;es &#233;dit&#233;es jusque l&#226; seront d&#233;finitivement perdues.",
330 'edit', array('action' => $action));
331 }
332 } // }}}
333 }
334 // }}}
335
336 // {{{ function handler_ajax() : some ajax in editing a new question (for now, there may be a little more later)
337 function handler_ajax(&$page, $type)
338 {
339 require_once dirname(__FILE__).'/survey/survey.inc.php';
340 header('Content-Type: text/html; charset="UTF-8"');
5c6e38d7 341 if (Survey::isType($type)) { // when type has been chosen, the form is updated to fit exactly the type of question chosen
8fe81c50 342 $page->changeTpl('survey/edit_new.tpl', NO_SKIN);
5c6e38d7 343 $page->assign('survey_types', Survey::getTypes());
8fe81c50 344 $page->assign('survey_type', $type);
345 }
346 }
347 // }}}
348
56c6950a 349 // {{{ function clear_session() : clears the data stored in session
350 function clear_session()
351 {
352 S::kill('survey');
353 S::kill('survey_id');
354 }
355 // }}}
356
357 // {{{ function store_session() : serializes and stores survey (and survey_id) in session
358 function store_session($survey, $survey_id = -1)
359 {
360 $_SESSION['survey'] = serialize($survey);
361 if ($survey_id != -1) {
362 $_SESSION['survey_id'] = $survey_id;
363 }
364 }
365 // }}}
366
9f01f40b 367 // {{{ function check_surveyPerms() : checks the particular surveys access permissions
368 function check_surveyPerms(&$page, $survey)
369 {
370 require_once dirname(__FILE__).'/survey/survey.inc.php';
371 if (!$survey->isMode(Survey::MODE_ALL)) { // if the survey is reserved to alumni
372 global $globals;
373 if (!call_user_func(array($globals->session, 'doAuth'))) { // checks authentification
374 global $platal;
375 $platal->force_login($page);
376 }
377 if (!$survey->checkPromo(S::v('promo'))) { // checks promotion
378 $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.");
379 return false;
380 }
381 }
382 return true;
383 }
384 // }}}
385
8fe81c50 386 // {{{ function show_survey() : calls the template to display a survey, for editing, voting, or consulting the results
387 function show_survey(&$page, $survey)
388 {
56c6950a 389 $page->changeTpl('survey/show_root.tpl');
8fe81c50 390 $page->assign('survey', $survey->toArray());
5c6e38d7 391 $page->assign('survey_modes', Survey::getModes());
8fe81c50 392 }
393 // }}}
394
395 // {{{ function show_form() : calls the template to display the editing form
396 function show_form(&$page, $action, $qid, $type = 'new', $current = null)
397 {
398 $page->changeTpl('survey/edit_survey.tpl');
399 $page->assign('survey_action', $action);
400 $page->assign('survey_qid', $qid);
401 $page->assign('survey_formaction', './survey/edit');
402 $page->assign('survey_type', $type);
403 if (!is_null($current) && is_array($current)) {
404 $page->assign('survey_current', $current);
405 } elseif ($type == 'new') {
406 $page->addJsLink('ajax.js');
5c6e38d7 407 $page->assign('survey_types', Survey::getTypes());
408 }
409 if ($type == 'root') {
410 $page->assign('survey_modes', Survey::getModes());
8fe81c50 411 }
412 }
413 // }}}
414
415 // {{{ function show_confirm() : calls the template to display a confirm form
56c6950a 416 function show_confirm(&$page, $message, $formaction, $formhidden = null)
8fe81c50 417 {
418 $page->changeTpl('survey/confirm.tpl');
419 $page->assign('survey_message', $message);
420 $page->assign('survey_formaction', './survey/'.$formaction);
421 $page->assign('survey_formhidden', $formhidden);
422 }
423 // }}}
424
425 // {{{ function show_error() : calls the template to display an error message
426 function show_error(&$page, $message, $link = "", $errArray = null)
427 {
428 $page->changeTpl('survey/error.tpl');
429 $page->assign('survey_message', $message);
430 $page->assign('survey_link', './survey/'.$link); // 'return' link to let the user leave the page
431 if (!is_null($errArray)) {
432 $page->assign('survey_errors', $errArray);
433 }
434 }
435 // }}}
436
437 // {{{ function show_success() : calls the template to display a success message
438 function show_success(&$page, $message = "", $link = "")
439 {
440 $page->changeTpl('survey/success.tpl');
441 $page->assign('survey_message', $message);
442 $page->assign('survey_link', './survey/'.$link); // 'return' link to let the user leave the page
443 }
444 // }}}
445}
446
447// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
448?>