70dad6ab070ad1162c2d98e60095fad93a54eb9a
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/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 require_once dirname(__FILE__
).'/survey/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é.", '');
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é.");
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.");
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.", '');
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é.", '');
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é.");
108 if (!$this->check_surveyPerms($page, $survey)) {
111 if ($show == 'csv') {
112 header('Content-Type: text/csv; charset="UTF-8"');
113 echo $survey->toCSV();
116 $page->assign('survey_resultmode', true
);
117 $this->show_survey($page, $survey);
122 // {{{ function handler_admin() : index of admin mode
123 function handler_admin(&$page, $id = -1)
125 require_once dirname(__FILE__
).'/survey/survey.inc.php';
126 $this->clear_session();
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
));
134 $survey = Survey
::retrieveSurvey($id); // retrieves all survey object structure
135 if ($survey == null
) {
136 $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
138 $page->assign('survey_adminmode', true
);
139 $this->show_survey($page, $survey);
144 // {{{ function handler_adminEdit() : edits a survey in admin mode
145 function handler_adminEdit(&$page, $id = -1)
148 return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin');
150 require_once dirname(__FILE__
).'/survey/survey.inc.php';
151 $survey = Survey
::retrieveSurvey($id); // retrieves the survey in database
152 $this->clear_session(); // cleans session (in case there would have been a problem before)
153 $this->store_session($survey, $id);
154 $this->handler_edit($page, 'show'); // calls handler_edit, but in admin mode since 'survey_id' is in session
158 // {{{ function handler_adminValidate() : validates a survey (admin mode)
159 function handler_adminValidate(&$page, $id = -1)
161 $id = Post
::i('survey_id', $id);
162 if (Post
::has('survey_cancel')) { // if the admin cancels the validation, returns to the admin index
163 $this->clear_session();
164 return $this->handler_admin(&$page, $id);
167 return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin');
169 require_once dirname(__FILE__
).'/survey/survey.inc.php';
170 $surveyInfo = Survey
::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure)
171 if ($surveyInfo == null
) {
172 return $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
174 if (Post
::has('survey_submit')) { // needs a confirmation before validation
175 if (Survey
::validateSurvey($id)) { // validates the survey (in the database)
176 $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien été validé, les votes sont maintenant ouverts.", 'admin');
178 $this->show_error($page, '', 'admin');
180 } else { // asks for a confirmation
181 $this->show_confirm($page, "Êtes-vous certain de vouloir valider le sondage \"".$surveyInfo['title']."\" ? "
182 ."Les votes seront immédiatement ouverts.", 'admin/valid', array('id' => $id));
187 // {{{ function handler_adminDelete() : deletes a survey (admin mode)
188 function handler_adminDelete(&$page, $id = -1)
190 $id = Post
::i('survey_id', $id);
191 if (Post
::has('survey_cancel')) { // if the admin cancels the suppression, returns to the admin index
192 return $this->handler_admin(&$page, $id);
195 return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin');
197 require_once dirname(__FILE__
).'/survey/survey.inc.php';
198 $surveyInfo = Survey
::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure)
199 if ($surveyInfo == null
) {
200 return $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
202 if (Post
::has('survey_submit')) { // needs a confirmation before suppression
203 if (Survey
::deleteSurvey($id)) { // deletes survey in database
204 $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien été supprimé, ainsi que tous les votes le concernant.", 'admin');
206 $this->show_error($page, '', 'admin');
208 } else { // asks for a confirmation
209 $this->show_confirm($page, "Êtes-vous certain de vouloir supprimer le sondage \"".$surveyInfo['title']."\" ?", 'admin/del', array('id' => $id));
214 // {{{ function handler_edit() : edits a survey (in normal mode unless called by handler_adminEdit() )
215 function handler_edit(&$page, $action = 'show', $qid = 'root')
217 require_once dirname(__FILE__
).'/survey/survey.inc.php';
218 $action = Post
::v('survey_action', $action);
219 $qid = Post
::v('survey_qid', $qid);
220 if (Post
::has('survey_cancel')) { // after cancelling changes, shows the survey
221 if (S
::has('survey')) {
223 } else { // unless no editing has been done at all (shows to the surveys index page)
224 return $this->handler_index($page);
227 $page->assign('survey_editmode', true
);
228 if (S
::has('survey_id')) { // if 'survey_id' is in session, it means we are modifying a survey in admin mode
229 $page->assign('survey_updatemode', true
);
231 if ($action == 'show' && !S
::has('survey')) {
234 if ($action == 'question') { // {{{ modifies an existing question
235 if (Post
::has('survey_submit')) { // if the form has been submitted, makes the modifications
236 $survey = unserialize(S
::v('survey'));
237 $args = Post
::v('survey_question');
238 if (!$survey->editQuestion($qid, $args)) { // update the survey object structure
239 return $this->show_error($page, '', 'edit');
241 $this->show_survey($page, $survey);
242 $this->store_session($survey);
243 } else { // if a form has not been submitted, shows modification form
244 $survey = unserialize(S
::v('survey'));
245 $current = $survey->toArray($qid); // gets the current parameters of the question
246 if ($current == null
) {
247 return $this->show_error($page, '', 'edit');
249 $this->show_form($page, $action, $qid, $current['type'], $current);
251 } elseif ($action == 'new') { // {{{ create a new survey : actually store the root question
252 if (Post
::has('survey_submit')) { // if the form has been submitted, creates the survey
253 $this->clear_session();
254 $survey = new Survey(Post
::v('survey_question')); // creates the object structure
255 $this->show_survey($page, $survey);
256 $this->store_session($survey);
258 $this->clear_session();
259 $this->show_form($page, $action, 'root', 'root');
261 } elseif ($action == 'add') { // {{{ adds a new question
262 if (Post
::has('survey_submit')) { // if the form has been submitted, adds the question
263 $survey = unserialize(S
::v('survey'));
264 if (!$survey->addQuestion($qid, $survey->factory(Post
::v('survey_type'), Post
::v('survey_question')))) {
265 return $this->show_error($page, '', 'edit');
267 $this->show_survey($page, $survey);
268 $this->store_session($survey);
270 $this->show_form($page, $action, $qid);
272 } elseif ($action == 'del') { // {{{ deletes a question
273 if (Post
::has('survey_submit')) { // if a confirmation has been sent, deletes the question
274 $survey = unserialize(S
::v('survey'));
275 if (!$survey->delQuestion(Post
::v('survey_qid'))) { // deletes the node in the survey object structure
276 return $this->show_error($page, '', 'edit');
278 $this->show_survey($page, $survey);
279 $this->store_session($survey);
280 } else { // if user has not confirmed, shows a confirmation form
281 $survey = unserialize(S
::v('survey'));
282 $current = $survey->toArray($qid); // needed to get the title of the question to delete (more user-friendly than an id)
283 if ($current == null
) {
284 return $this->show_error($page, '', 'edit');
286 $this->show_confirm($page, 'Êtes-vous certain de vouloir supprimer la question intitulé "'.$current['question'].'" ? '
287 .'Attention, cela supprimera en même temps toutes les questions qui dépendent de celle-ci.',
288 'edit', array('action' => 'del', 'qid' => $qid));
290 } elseif ($action == 'show') { // {{{ simply shows the survey in its current state
291 $this->show_survey($page, unserialize(S
::v('survey'))); // }}}
292 } elseif ($action == 'valid') { // {{{ validates the proposition, i.e stores the proposition in the database
293 // but an admin will still need to validate the survey before it is activated
294 if (Post
::has('survey_submit')) { // needs a confirmation before storing the proposition
295 $survey = unserialize(S
::v('survey'));
296 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
297 if ($survey->updateSurvey()) { // updates the database according the new survey object structure
298 $this->show_success($page, "Les modifications sur le sondage ont bien été enregistrées.", 'admin');
300 $this->show_error($page, '', 'admin');
302 } else { // if no 'survey_id' is in session, we are indeed proposing a new survey
303 if ($survey->proposeSurvey()) { // stores the survey object structure in database
304 $this->show_success($page, "Votre proposition de sondage a bien été enregistrée,
305 elle est en attent de validation par un administrateur du site.", '');
307 $this->show_error($page);
310 $this->clear_session();
311 } else { // asks for a confirmation if it has not been sent
312 $survey = unserialize(S
::v('survey'));
313 $errors = $survey->checkSyntax();
314 if (!is_null($errors)) {
315 $this->show_error($page, "", 'edit', $errors);
317 if (S
::has('survey_id')) {
318 $this->show_confirm($page, "Veuillez confirmer l'enregistrement des modifications apportées à ce sondage", 'edit', array('action' => 'valid'));
320 $this->show_confirm($page, "Veuillez confirmer l'envoi de cette proposition de sondage.", 'edit', array('action' => 'valid'));
324 } elseif ($action == 'cancel') { // {{{ cancels the creation/modification of a survey
325 if (Post
::has('survey_submit')) { // needs a confirmation
326 if (S
::has('survey_id')) { // only possible when modifying a survey in admin mode, still this should be considered again,
327 $this->clear_session(); // maybe some name with "admin" in it, "survey_adminid" or anything that might not be confusing.
328 return $this->handler_admin($page); // in this case, shows the admin index
330 $this->clear_session(); // cleans session
331 return $this->handler_index($page); // else shows the 'normal' index
333 } else { // asks for a confirmation if it has not been sent
334 $this->show_confirm(&$page, "Êtes-vous certain de vouloir annuler totalement l'édition de ce sondage ? Attention, "
335 ."toutes les données éditées jusque lâ seront définitivement perdues.",
336 'edit', array('action' => $action));
342 // {{{ function handler_ajax() : some ajax in editing a new question (for now, there may be a little more later)
343 function handler_ajax(&$page, $type)
345 require_once dirname(__FILE__
).'/survey/survey.inc.php';
346 header('Content-Type: text/html; charset="UTF-8"');
347 if (Survey
::isType($type)) { // when type has been chosen, the form is updated to fit exactly the type of question chosen
348 $page->changeTpl('survey/edit_new.tpl', NO_SKIN
);
349 $page->assign('survey_types', Survey
::getTypes());
350 $page->assign('survey_type', $type);
355 // {{{ function clear_session() : clears the data stored in session
356 function clear_session()
359 S
::kill('survey_id');
363 // {{{ function store_session() : serializes and stores survey (and survey_id) in session
364 function store_session($survey, $survey_id = -1)
366 $_SESSION['survey'] = serialize($survey);
367 if ($survey_id != -1) {
368 $_SESSION['survey_id'] = $survey_id;
373 // {{{ function check_surveyPerms() : checks the particular surveys access permissions
374 function check_surveyPerms(&$page, $survey)
376 require_once dirname(__FILE__
).'/survey/survey.inc.php';
377 if (!$survey->isMode(Survey
::MODE_ALL
)) { // if the survey is reserved to alumni
379 if (!call_user_func(array($globals->session
, 'doAuth'))) { // checks authentification
381 $platal->force_login($page);
383 if (!$survey->checkPromo(S
::v('promo'))) { // checks promotion
384 $this->show_error($page, "Tu n'as pas accès à ce sondage car il est réservé à d'autres promotions.");
392 // {{{ function show_survey() : calls the template to display a survey, for editing, voting, or consulting the results
393 function show_survey(&$page, $survey)
395 $page->changeTpl('survey/show_root.tpl');
396 $page->assign('survey', $survey->toArray());
397 $page->assign('survey_modes', Survey
::getModes());
401 // {{{ function show_form() : calls the template to display the editing form
402 function show_form(&$page, $action, $qid, $type = 'new', $current = null
)
404 $page->changeTpl('survey/edit_survey.tpl');
405 $page->assign('survey_action', $action);
406 $page->assign('survey_qid', $qid);
407 $page->assign('survey_formaction', './survey/edit');
408 $page->assign('survey_type', $type);
409 if (!is_null($current) && is_array($current)) {
410 $page->assign('survey_current', $current);
411 } elseif ($type == 'new') {
412 $page->addJsLink('ajax.js');
413 $page->assign('survey_types', Survey
::getTypes());
415 if ($type == 'root') {
416 $page->assign('survey_modes', Survey
::getModes());
421 // {{{ function show_confirm() : calls the template to display a confirm form
422 function show_confirm(&$page, $message, $formaction, $formhidden = null
)
424 $page->changeTpl('survey/confirm.tpl');
425 $page->assign('survey_message', $message);
426 $page->assign('survey_formaction', './survey/'.$formaction);
427 $page->assign('survey_formhidden', $formhidden);
431 // {{{ function show_error() : calls the template to display an error message
432 function show_error(&$page, $message, $link = "", $errArray = null
)
434 $page->changeTpl('survey/error.tpl');
435 $page->assign('survey_message', $message);
436 $page->assign('survey_link', './survey/'.$link); // 'return' link to let the user leave the page
437 if (!is_null($errArray)) {
438 $page->assign('survey_errors', $errArray);
443 // {{{ function show_success() : calls the template to display a success message
444 function show_success(&$page, $message = "", $link = "")
446 $page->changeTpl('survey/success.tpl');
447 $page->assign('survey_message', $message);
448 $page->assign('survey_link', './survey/'.$link); // 'return' link to let the user leave the page
453 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: