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_COOKIE
),
29 'survey/edit' => $this->make_hook('edit', AUTH_COOKIE
),
30 'survey/ajax' => $this->make_hook('ajax', AUTH_COOKIE
),
31 'survey/admin' => $this->make_hook('admin', AUTH_MDP
, 'admin'),
32 'survey/admin/edit' => $this->make_hook('adminEdit', AUTH_MDP
, 'admin'),
33 'survey/admin/valid' => $this->make_hook('adminValidate', AUTH_MDP
, 'admin'),
34 'survey/admin/del' => $this->make_hook('adminDelete', AUTH_MDP
, 'admin'),
39 // {{{ function handler_index() : lists all available surveys
40 function handler_index(&$page, $action = null
)
42 require_once dirname(__FILE__
).'/survey/survey.inc.php';
43 $page->changeTpl('survey/index.tpl');
44 $page->assign('survey_current', SurveyDB
::retrieveList('c'));
45 $page->assign('survey_old', SurveyDB
::retrieveList('o'));
49 // {{{ function handler_admin() : index of admin mode
50 function handler_admin(&$page, $id = -1)
52 require_once dirname(__FILE__
).'/survey/survey.inc.php';
53 $this->clear_session();
55 $page->changeTpl('survey/admin.tpl');
56 $page->assign('survey_waiting', SurveyDB
::retrieveList('w'));
57 $page->assign('survey_current', SurveyDB
::retrieveList('c'));
58 $page->assign('survey_old', SurveyDB
::retrieveList('o'));
60 $survey = SurveyDB
::retrieveSurvey($id); // retrieves all survey object structure
61 if ($survey == null
) {
62 $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
64 //$this->store_session($survey, $id);
65 $page->assign('survey_adminmode', true
);
66 $page->assign('survey_id', $id);
67 $this->show_survey($page, $survey);
72 // {{{ function handler_adminEdit() : edits a survey in admin mode
73 function handler_adminEdit(&$page, $id = -1)
76 return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin');
78 require_once dirname(__FILE__
).'/survey/survey.inc.php';
79 $survey = SurveyDB
::retrieveSurvey($id); // retrieves the survey in database
80 $this->clear_session(); // cleans session (in case there would have been a problem before)
81 /*if ($survey->isValid()) {
82 return $this->show_error($page, "Il est impossible de modifier un sondage déjà validé", 'admin');
84 $this->store_session($survey, $id);
85 $this->handler_edit($page, 'show'); // calls handler_edit, but in admin mode since 'survey_id' is in session
89 // {{{ function handler_adminValidate() : validates a survey (admin mode)
90 function handler_adminValidate(&$page, $id = -1)
92 $id = Post
::v('survey_id', $id);
93 if (Post
::has('survey_cancel')) { // if the admin cancels the validation, returns to the admin index
94 $this->clear_session();
95 return $this->handler_admin(&$page, $id);
98 return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin');
100 require_once dirname(__FILE__
).'/survey/survey.inc.php';
101 $surveyInfo = SurveyDB
::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure)
102 if ($surveyInfo == null
) {
103 return $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
105 if (Post
::has('survey_submit')) { // needs a confirmation before validation
106 if (SurveyDB
::validateSurvey($id)) { // validates the survey (in the database)
107 $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien été validé, les votes sont maintenant ouverts.", 'admin');
109 $this->show_error($page, '', 'admin');
111 } else { // asks for a confirmation
112 $this->show_confirm($page, "Êtes-vous certain de vouloir valider le sondage \"".$surveyInfo['title']."\" ? "
113 ."Les votes seront immédiatement ouverts.", 'admin/valid', array('id' => $id));
116 if ($id == -1 && !(S::has('survey') && S::has('survey_id'))) {
117 return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin');
119 require_once dirname(__FILE__).'/survey/survey.inc.php';
120 if ($id == -1) { // the survey and survey_id are in session, performs the validation
121 $survey = unserialize(S::v('survey'));
122 $surveyInfo = $survey->storeArray();
123 if (Post::has('survey_submit')) { // needs a confirmation before validation
124 if (SurveyDB::validateSurvey(S::v('survey_id'))) { // validates the survey (in the database)
125 $this->show_success($page, "Le sondage \"".$surveyInfo['question']."\" a bien été validé, les votes sont maintenant ouverts.", 'admin');
127 $this->show_error($page, '', 'admin');
129 } else { // asks for a confirmation
130 $this->show_confirm($page, "Êtes-vous certain de vouloir valider le sondage \"".$surveyInfo['question']."\" ? "
131 ."Les votes seront immédiatement ouverts.", 'admin/valid');
133 } else { // a survey id is given, loads this survey for validation
134 $this->clear_session();
135 $survey = SurveyDB::retrieveSurvey($id); // retrieves all survey object structure
136 if ($survey == null) {
137 $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
139 $this->store_session($survey, $id);
140 $page->assign('survey_validmode', true);
141 $this->show_survey($page, $survey);
147 // {{{ function handler_adminDelete() : deletes a survey (admin mode)
148 function handler_adminDelete(&$page, $id = -1)
150 $id = Post
::v('survey_id', $id);
151 if (Post
::has('survey_cancel')) { // if the admin cancels the suppression, returns to the admin index
152 return $this->handler_admin(&$page, $id);
155 return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin');
157 require_once dirname(__FILE__
).'/survey/survey.inc.php';
158 $surveyInfo = SurveyDB
::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure)
159 if ($surveyInfo == null
) {
160 return $this->show_error($page, "Sondage ".$id." introuvable.", 'admin');
162 if (Post
::has('survey_submit')) { // needs a confirmation before suppression
163 if (SurveyDB
::deleteSurvey($id)) { // deletes survey in database
164 $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien été supprimé, ainsi que tous les votes le concernant.", 'admin');
166 $this->show_error($page, '', 'admin');
168 } else { // asks for a confirmation
169 $this->show_confirm($page, "Êtes-vous certain de vouloir supprimer le sondage \"".$surveyInfo['title']."\" ?", 'admin/del', array('id' => $id));
174 // {{{ function handler_edit() : edits a survey (in normal mode unless called by handler_adminEdit() )
175 function handler_edit(&$page, $action = 'show', $qid = 0)
177 require_once dirname(__FILE__
).'/survey/survey.inc.php';
178 $action = Post
::v('survey_action', $action);
179 $qid = Post
::v('survey_qid', $qid);
180 if (Post
::has('survey_cancel')) { // after cancelling changes, shows the survey
181 if (S
::has('survey')) {
183 } else { // unless no editing has been done at all (shows to the surveys index page)
184 return $this->handler_index($page);
187 $page->assign('survey_editmode', true
);
188 if (S
::has('survey_id')) { // if 'survey_id' is in session, it means we are modifying a survey in admin mode
189 $page->assign('survey_updatemode', true
);
191 if ($action == 'show' && !S
::has('survey')) {
194 if ($action == 'question') { // {{{ modifies an existing question
195 if (Post
::has('survey_submit')) { // if the form has been submitted, makes the modifications
196 $survey = unserialize(S
::v('survey'));
197 $args = Post
::v('survey_question');
198 if (!$survey->edit($qid, $args)) { // update the survey object structure
199 return $this->show_error($page, '', 'edit');
201 $this->show_survey($page, $survey);
202 $this->store_session($survey);
203 } else { // if a form has not been submitted, shows modification form
204 $survey = unserialize(S
::v('survey'));
205 $current = $survey->searchToArray($qid); // gets the current parameters of the question
206 if ($current == null
) {
207 return $this->show_error($page, '', 'edit');
209 $this->show_form($page, $action, $qid, $current['type'], $current);
211 } elseif ($action == 'new') { // {{{ create a new survey : actually store the root question
212 if (Post
::has('survey_submit')) { // if the form has been submitted, creates the survey
213 $this->clear_session();
214 $survey = new SurveyRoot(Post
::v('survey_question')); // creates the object structure
215 $this->show_survey($page, $survey);
216 $this->store_session($survey);
218 $this->clear_session();
219 $this->show_form($page, $action, 0, 'root');
221 } elseif ($action == 'nested' ||
$action == 'after') { // {{{ adds a new question, nested in the current node, or on the same level after it
222 if (Post
::has('survey_submit')) { // if the form has been submitted, adds the question
223 $survey = unserialize(S
::v('survey'));
224 $question = $survey->factory(Post
::v('survey_type'), Post
::v('survey_question')); // creates the question object, with a sort of 'factory' method
225 if ($action == 'nested') {
226 if (!$survey->addChildNested($qid, $question)) {
227 return $this->show_error($page, '', 'edit');
230 if (!$survey->addChildAfter($qid, $question)) {
231 return $this->show_error($page, '', 'edit');
234 $this->show_survey($page, $survey);
235 $this->store_session($survey);
237 $this->show_form($page, $action, $qid);
239 } elseif ($action == 'del') { // {{{ deletes a question
240 if (Post
::has('survey_submit')) { // if a confirmation has been sent, deletes the question
241 $survey = unserialize(S
::v('survey'));
242 if (!$survey->delChild(Post
::v('survey_qid'))) { // deletes the node in the survey object structure
243 return $this->show_error($page, '', 'edit');
245 $this->show_survey($page, $survey);
246 $this->store_session($survey);
247 } else { // if user has not confirmed, shows a confirmation form
248 $survey = unserialize(S
::v('survey'));
249 $current = $survey->searchToArray($qid); // needed to get the title of the question to delete (more user-friendly than an id)
250 if ($current == null
) {
251 return $this->show_error($page, '', 'edit');
253 $this->show_confirm($page, 'Êtes-vous certain de vouloir supprimer la question intitulé "'.$current['question'].'" ? '
254 .'Attention, cela supprimera en même temps toutes les questions qui dépendent de celle-ci.',
255 'edit', array('action' => 'del', 'qid' => $qid));
257 } elseif ($action == 'show') { // {{{ simply shows the survey in its current state
258 $this->show_survey($page, unserialize(S
::v('survey'))); // }}}
259 } elseif ($action == 'valid') { // {{{ validates the proposition, i.e stores the proposition in the database
260 // but an admin will still need to validate the survey before it is activated
261 if (Post
::has('survey_submit')) { // needs a confirmation before storing the proposition
262 $survey = unserialize(S
::v('survey'));
263 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
264 if (SurveyDB
::updateSurvey($survey, S
::v('survey_id'))) { // updates the database according the new survey object structure
265 $this->show_success($page, "Les modifications sur le sondage ont bien été enregistrées.", 'admin');
267 $this->show_error($page, '', 'admin');
269 } else { // if no 'survey_id' is in session, we are indeed proposing a new survey
270 if (SurveyDB
::proposeSurvey($survey)) { // stores the survey object structure in database
271 $this->show_success($page, "Votre proposition de sondage a bien été enregistrée,
272 elle est en attent de validation par un administrateur du site.", '');
274 $this->show_error($page);
277 $this->clear_session();
278 } else { // asks for a confirmation if it has not been sent
279 $survey = unserialize(S
::v('survey'));
280 $errors = $survey->checkSyntax();
281 if (!is_null($errors)) {
282 $this->show_error($page, "", 'edit', $errors);
284 if (S
::has('survey_id')) {
285 $this->show_confirm($page, "Veuillez confirmer l'enregistrement des modifications apportées à ce sondage", 'edit', array('action' => 'valid'));
287 $this->show_confirm($page, "Veuillez confirmer l'envoi de cette proposition de sondage.", 'edit', array('action' => 'valid'));
291 } elseif ($action == 'cancel') { // {{{ cancels the creation/modification of a survey
292 if (Post
::has('survey_submit')) { // needs a confirmation
293 if (S
::has('survey_id')) { // only possible when modifying a survey in admin mode, still this should be considered again,
294 $this->clear_session(); // maybe some name with "admin" in it, "survey_adminid" or anything that might not be confusing.
295 return $this->handler_admin($page); // in this case, shows the admin index
297 $this->clear_session(); // cleans session
298 return $this->handler_index($page); // else shows the 'normal' index
300 } else { // asks for a confirmation if it has not been sent
301 $this->show_confirm(&$page, "Êtes-vous certain de vouloir annuler totalement l'édition de ce sondage ? Attention, "
302 ."toutes les données éditées jusque lâ seront définitivement perdues.",
303 'edit', array('action' => $action));
309 // {{{ function handler_ajax() : some ajax in editing a new question (for now, there may be a little more later)
310 function handler_ajax(&$page, $type)
312 require_once dirname(__FILE__
).'/survey/survey.inc.php';
313 header('Content-Type: text/html; charset="UTF-8"');
314 if (SurveyQuestion
::isType($type)) { // when type has been chosen, the form is updated to fit exactly the type of question chosen
315 $page->changeTpl('survey/edit_new.tpl', NO_SKIN
);
316 $page->assign('survey_types', SurveyQuestion
::getTypes());
317 $page->assign('survey_type', $type);
322 // {{{ function clear_session() : clears the data stored in session
323 function clear_session()
326 S
::kill('survey_id');
330 // {{{ function store_session() : serializes and stores survey (and survey_id) in session
331 function store_session($survey, $survey_id = -1)
333 $_SESSION['survey'] = serialize($survey);
334 if ($survey_id != -1) {
335 $_SESSION['survey_id'] = $survey_id;
340 // {{{ function show_survey() : calls the template to display a survey, for editing, voting, or consulting the results
341 function show_survey(&$page, $survey)
343 $page->changeTpl('survey/show_root.tpl');
344 $page->assign('survey', $survey->toArray());
348 // {{{ function show_form() : calls the template to display the editing form
349 function show_form(&$page, $action, $qid, $type = 'new', $current = null
)
351 $page->changeTpl('survey/edit_survey.tpl');
352 $page->assign('survey_action', $action);
353 $page->assign('survey_qid', $qid);
354 $page->assign('survey_formaction', './survey/edit');
355 $page->assign('survey_type', $type);
356 if (!is_null($current) && is_array($current)) {
357 $page->assign('survey_current', $current);
358 } elseif ($type == 'new') {
359 $page->addJsLink('ajax.js');
360 $page->assign('survey_types', SurveyQuestion
::getTypes());
365 // {{{ function show_confirm() : calls the template to display a confirm form
366 function show_confirm(&$page, $message, $formaction, $formhidden = null
)
368 $page->changeTpl('survey/confirm.tpl');
369 $page->assign('survey_message', $message);
370 $page->assign('survey_formaction', './survey/'.$formaction);
371 $page->assign('survey_formhidden', $formhidden);
375 // {{{ function show_error() : calls the template to display an error message
376 function show_error(&$page, $message, $link = "", $errArray = null
)
378 $page->changeTpl('survey/error.tpl');
379 $page->assign('survey_message', $message);
380 $page->assign('survey_link', './survey/'.$link); // 'return' link to let the user leave the page
381 if (!is_null($errArray)) {
382 $page->assign('survey_errors', $errArray);
387 // {{{ function show_success() : calls the template to display a success message
388 function show_success(&$page, $message = "", $link = "")
390 $page->changeTpl('survey/success.tpl');
391 $page->assign('survey_message', $message);
392 $page->assign('survey_link', './survey/'.$link); // 'return' link to let the user leave the page
397 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: