2 /***************************************************************************
3 * Copyright (C) 2003-2010 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/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'),
41 // {{{ function handler_index() : lists all available surveys
42 function handler_index(&$page, $action = null
)
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
));
52 // {{{ function handler_vote() : handles the vote to a survey
53 function handler_vote(&$page, $id = -1)
55 if (Post
::has('survey_cancel')) { // if the user cancels, returns to index
56 return $this->handler_index(&$page);
60 return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey');
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');
69 if (!$this->check_surveyPerms($page, $survey)) {
72 if (Post
::has('survey_submit')) { // checks if the survey has already been filled in
74 if (!$survey->isMode(Survey
::MODE_ALL
)) { // if survey is restriced to alumni
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');
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
);
86 $page->assign('survey_warning', "Tu as déjà voté à ce sondage.");
88 //$page->assign('survey_id', $id);
89 $this->show_survey($page, $survey);
94 // {{{ function handler_result() : show the results of the votes to a survey
95 function handler_result($page, $id = -1, $show = 'all')
99 return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey');
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');
108 if (!$this->check_surveyPerms($page, $survey)) {
111 if ($show == 'csv') {
112 pl_content_headers("text/csv");
113 header('Content-Disposition: attachment; filename="'.addslashes($survey->getTitle()).'.csv"');
114 echo $survey->toCSV();
117 $page->assign('survey_resultmode', true
);
118 $this->show_survey($page, $survey);
123 // {{{ function handler_admin() : index of admin mode
124 function handler_admin(&$page, $id = -1)
126 $this->load('survey.inc.php');
127 $this->clear_session();
129 $page->changeTpl('survey/admin.tpl');
130 $page->assign('survey_waiting', Survey
::retrieveList('w'));
131 $page->assign('survey_current', Survey
::retrieveList('c'));
132 $page->assign('survey_old', Survey
::retrieveList('o'));
133 $page->assign('survey_modes', Survey
::getModes(false
));
136 $survey = Survey
::retrieveSurvey($id); // retrieves all survey object structure
137 if ($survey == null
) {
138 $this->show_error($page, "Sondage ".$id." introuvable.", 'survey/admin');
140 $page->assign('survey_adminmode', true
);
141 $this->show_survey($page, $survey);
146 // {{{ function handler_adminEdit() : edits a survey in admin mode
147 function handler_adminEdit(&$page, $id = -1, $req = -1)
149 if ($id == -1 ||
($id == 'req' && $req == -1)) {
150 return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey/admin');
152 $this->load('survey.inc.php');
153 $this->clear_session(); // cleans session (in case there would have been a problem before)
155 $survey = Survey
::retrieveSurveyReq($req);
156 if ($survey == null
) {
157 return $this->show_error($page, "Sondage introuvable.", 'survey/admin');
159 $this->store_session($survey, $req, true
);
162 $survey = Survey
::retrieveSurvey($id); // retrieves the survey in database
163 if ($survey == null
) {
164 return $this->show_error($page, "Sondage ".$id." introuvable.", 'survey/admin');
166 $this->store_session($survey, $id);
168 $this->handler_edit($page, 'show'); // calls handler_edit, but in admin mode since 'survey_id' is in session
172 // {{{ function handler_adminValidate() : validates a survey (admin mode)
173 function handler_adminValidate(&$page, $id = -1)
175 $id = Post
::i('survey_id', $id);
176 if (Post
::has('survey_cancel')) { // if the admin cancels the validation, returns to the admin index
177 $this->clear_session();
178 return $this->handler_admin(&$page, $id);
181 return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey/admin');
184 $this->load('survey.inc.php');
185 $surveyInfo = Survey
::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure)
186 if ($surveyInfo == null
) {
187 return $this->show_error($page, "Sondage ".$id." introuvable.", 'survey/admin');
189 if (Post
::has('survey_submit')) { // needs a confirmation before validation
190 if (Survey
::validateSurvey($id)) { // validates the survey (in the database)
191 $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien été validé, les votes sont maintenant ouverts.", 'survey/admin');
193 $this->show_error($page, '', 'survey/admin');
195 } else { // asks for a confirmation
196 $this->show_confirm($page, "Êtes-vous certain de vouloir valider le sondage \"".$surveyInfo['title']."\" ? "
197 ."Les votes seront immédiatement ouverts.", 'admin/valid', array('id' => $id));
202 // {{{ function handler_adminDelete() : deletes a survey (admin mode)
203 function handler_adminDelete(&$page, $id = -1)
205 $id = Post
::i('survey_id', $id);
206 if (Post
::has('survey_cancel')) { // if the admin cancels the suppression, returns to the admin index
207 return $this->handler_admin(&$page, $id);
210 return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey/admin');
213 $this->load('survey.inc.php');
214 $surveyInfo = Survey
::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure)
215 if ($surveyInfo == null
) {
216 return $this->show_error($page, "Sondage ".$id." introuvable.", 'survey/admin');
218 if (Post
::has('survey_submit')) { // needs a confirmation before suppression
219 if (Survey
::deleteSurvey($id)) { // deletes survey in database
220 $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien été supprimé, ainsi que tous les votes le concernant.", 'survey/admin');
222 $this->show_error($page, '', 'survey/admin');
224 } else { // asks for a confirmation
225 $this->show_confirm($page, "Êtes-vous certain de vouloir supprimer le sondage \"".$surveyInfo['title']."\" ?", 'admin/del', array('id' => $id));
230 // {{{ function handler_edit() : edits a survey (in normal mode unless called by handler_adminEdit() )
231 function handler_edit(&$page, $action = 'show', $qid = 'root')
233 $this->load('survey.inc.php');
234 $action = Post
::v('survey_action', $action);
235 $qid = Post
::v('survey_qid', $qid);
236 if (Post
::has('survey_cancel')) { // after cancelling changes, shows the survey
237 if (S
::has('survey')) {
239 } else { // unless no editing has been done at all (shows to the surveys index page)
240 return $this->handler_index($page);
243 $page->assign('survey_editmode', true
);
244 if (S
::has('survey_id')) { // if 'survey_id' is in session, it means we are modifying a survey in admin mode
245 $page->assign('survey_updatemode', true
);
247 if ($action == 'show' && !S
::has('survey')) {
250 if ($action == 'question') { // {{{ modifies an existing question
251 if (Post
::has('survey_submit')) { // if the form has been submitted, makes the modifications
252 $survey = unserialize(S
::v('survey'));
253 $args = Post
::v('survey_question');
254 if (!$survey->editQuestion($qid, $args)) { // update the survey object structure
255 return $this->show_error($page, '', 'survey/edit');
257 $this->show_survey($page, $survey);
258 $this->store_session($survey);
259 } else { // if a form has not been submitted, shows modification form
260 $survey = unserialize(S
::v('survey'));
261 $current = $survey->toArray($qid); // gets the current parameters of the question
262 if ($current == null
) {
263 return $this->show_error($page, '', 'survey/edit');
265 $this->show_form($page, $action, $qid, $current['type'], $current);
267 } elseif ($action == 'new') { // {{{ create a new survey : actually store the root question
268 if (Post
::has('survey_submit')) { // if the form has been submitted, creates the survey
269 $this->clear_session();
270 $survey = new Survey(Post
::v('survey_question')); // creates the object structure
271 $this->show_survey($page, $survey);
272 $this->store_session($survey);
274 $this->clear_session();
275 $this->show_form($page, $action, 'root', 'newsurvey');
277 } elseif ($action == 'add') { // {{{ adds a new question
278 if (Post
::has('survey_submit')) { // if the form has been submitted, adds the question
279 $survey = unserialize(S
::v('survey'));
280 if (!$survey->addQuestion($qid, $survey->factory(Post
::v('survey_type'), Post
::v('survey_question')))) {
281 return $this->show_error($page, '', 'survey/edit');
283 $this->show_survey($page, $survey);
284 $this->store_session($survey);
286 $this->show_form($page, $action, $qid);
288 } elseif ($action == 'del') { // {{{ deletes a question
289 if (Post
::has('survey_submit')) { // if a confirmation has been sent, deletes the question
290 $survey = unserialize(S
::v('survey'));
291 if (!$survey->delQuestion(Post
::v('survey_qid'))) { // deletes the node in the survey object structure
292 return $this->show_error($page, '', 'survey/edit');
294 $this->show_survey($page, $survey);
295 $this->store_session($survey);
296 } else { // if user has not confirmed, shows a confirmation form
297 $survey = unserialize(S
::v('survey'));
298 $current = $survey->toArray($qid); // needed to get the title of the question to delete (more user-friendly than an id)
299 if ($current == null
) {
300 return $this->show_error($page, '', 'survey/edit');
302 $this->show_confirm($page, 'Êtes-vous certain de vouloir supprimer la question intitulé "'.$current['question'].'" ? '
303 .'Attention, cela supprimera en même temps toutes les questions qui dépendent de celle-ci.',
304 'edit', array('action' => 'del', 'qid' => $qid));
306 } elseif ($action == 'show') { // {{{ simply shows the survey in its current state
307 $this->show_survey($page, unserialize(S
::v('survey'))); // }}}
308 } elseif ($action == 'valid') { // {{{ validates the proposition, i.e stores the proposition in the database
309 // but an admin will still need to validate the survey before it is activated
310 if (Post
::has('survey_submit')) { // needs a confirmation before storing the proposition
311 $survey = unserialize(S
::v('survey'));
312 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
313 $link = (S
::has('survey_validate'))?
'admin/validate' : 'survey/admin';
314 if ($survey->updateSurvey()) { // updates the database according the new survey object structure
315 $this->show_success($page, "Les modifications sur le sondage ont bien été enregistrées.", $link);
317 $this->show_error($page, '', $link);
319 } else { // if no 'survey_id' is in session, we are indeed proposing a new survey
320 if ($survey->proposeSurvey()) { // stores the survey object structure in database
321 $this->show_success($page, "Votre proposition de sondage a bien été enregistrée,
322 elle est en attente de validation par un administrateur du site.", 'survey');
324 $this->show_error($page, '', 'survey');
327 $this->clear_session();
328 } else { // asks for a confirmation if it has not been sent
329 $survey = unserialize(S
::v('survey'));
330 $errors = $survey->checkSyntax();
331 if (!is_null($errors)) {
332 $this->show_error($page, "", 'survey/edit', $errors);
334 if (S
::has('survey_id')) {
335 $this->show_confirm($page, "Veuillez confirmer l'enregistrement des modifications apportées à ce sondage.", 'edit', array('action' => 'valid'));
337 $this->show_confirm($page, "Veuillez confirmer l'envoi de cette proposition de sondage.", 'edit', array('action' => 'valid'));
341 } elseif ($action == 'cancel') { // {{{ cancels the creation/modification of a survey
342 if (Post
::has('survey_submit')) { // needs a confirmation
343 if (S
::has('survey_id')) { // only possible when modifying a survey in admin mode
344 if (S
::has('survey_validate')) { // if a link has been supplied, uses it
345 $this->clear_session();
346 return $this->show_success($page, "Les modifications effectuées ont été annulées", 'admin/validate');
347 } else { // else shows the admin index
348 $this->clear_session();
349 return $this->handler_admin($page);
352 $this->clear_session();
353 return $this->handler_index($page); // else shows the 'normal' index
355 } else { // asks for a confirmation if it has not been sent
356 $this->show_confirm(&$page, "Êtes-vous certain de vouloir annuler totalement l'édition de ce sondage ? Attention, "
357 ."toutes les données éditées jusque là seront définitivement perdues.",
358 'edit', array('action' => $action));
364 // {{{ function handler_ajax() : some ajax in editing a new question (for now, there may be a little more later)
365 function handler_ajax(&$page, $type)
367 $this->load('survey.inc.php');
368 pl_content_headers("text/html");
369 if (Survey
::isType($type)) { // when type has been chosen, the form is updated to fit exactly the type of question chosen
370 $page->changeTpl('survey/edit_new.tpl', NO_SKIN
);
371 $page->assign('survey_types', Survey
::getTypes());
372 $page->assign('survey_type', $type);
377 // {{{ function clear_session() : clears the data stored in session
378 function clear_session()
381 S
::kill('survey_id');
382 S
::kill('survey_validate');
386 // {{{ function store_session() : serializes and stores survey (and survey_id) in session
387 function store_session($survey, $survey_id = -1, $survey_validate = false
)
389 $_SESSION['survey'] = serialize($survey);
390 if ($survey_id != -1) {
391 $_SESSION['survey_id'] = $survey_id;
393 if ($survey_validate) {
394 $_SESSION['survey_validate'] = true
;
399 // {{{ function check_surveyPerms() : checks the particular surveys access permissions
400 function check_surveyPerms(&$page, $survey, $silent = false
)
402 $this->load('survey.inc.php');
403 if ($survey->isMode(Survey
::MODE_ALL
)) { // if the survey is not reserved to alumni
409 $profile = S
::user()->profile();
415 foreach ($profile->yearspromo() as $p) {
416 if ($survey->checkPromo($p)) {
425 $page->kill("Tu n'as pas accès à ce sondage car il est réservé à d'autres promotions.");
431 // {{{ function show_survey() : calls the template to display a survey, for editing, voting, or consulting the results
432 function show_survey(&$page, $survey)
434 $page->changeTpl('survey/show_root.tpl');
435 $page->assign('survey', $survey->toArray());
436 $page->assign('survey_modes', Survey
::getModes());
440 // {{{ function show_form() : calls the template to display the editing form
441 function show_form(&$page, $action, $qid, $type = 'new', $current = null
)
443 $page->changeTpl('survey/edit_survey.tpl');
444 $page->assign('survey_action', $action);
445 $page->assign('survey_qid', $qid);
446 $page->assign('survey_formaction', './survey/edit');
447 $page->assign('survey_type', $type);
448 if (!is_null($current) && is_array($current)) {
449 $page->assign('survey_current', $current);
450 } elseif ($type == 'new') {
451 $page->addJsLink('ajax.js');
452 $page->assign('survey_types', Survey
::getTypes());
454 if ($type == 'root' ||
$type == 'newsurvey') {
455 $page->assign('survey_modes', Survey
::getModes());
460 // {{{ function show_confirm() : calls the template to display a confirm form
461 function show_confirm(&$page, $message, $formaction, $formhidden = null
)
463 $page->changeTpl('survey/confirm.tpl');
464 $page->assign('survey_message', $message);
465 $page->assign('survey_formaction', './survey/'.$formaction);
466 $page->assign('survey_formhidden', $formhidden);
470 // {{{ function show_error() : calls the template to display an error message
471 function show_error(&$page, $message, $link = "", $errArray = null
)
473 $page->changeTpl('survey/error.tpl');
474 $page->assign('survey_message', $message);
475 $page->assign('survey_link', $link); // 'return' link to let the user leave the page
476 if (!is_null($errArray)) {
477 $page->assign('survey_errors', $errArray);
483 // {{{ function show_success() : calls the template to display a success message
484 function show_success(&$page, $message = "", $link = "")
486 $page->changeTpl('survey/success.tpl');
487 $page->assign('survey_message', $message);
488 $page->assign('survey_link', $link); // 'return' link to let the user leave the page
493 // vim:set et sw=4 sts=4 ts=4 foldmethod=marker enc=utf-8: