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