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 | |
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_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'), |
35 | ); |
36 | } |
37 | // }}} |
38 | |
39 | // {{{ function handler_index() : lists all available surveys |
40 | function handler_index(&$page, $action = null) |
41 | { |
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')); |
46 | } |
47 | // }}} |
48 | |
49 | // {{{ function handler_admin() : index of admin mode |
56c6950a |
50 | function handler_admin(&$page, $id = -1) |
8fe81c50 |
51 | { |
52 | require_once dirname(__FILE__).'/survey/survey.inc.php'; |
56c6950a |
53 | $this->clear_session(); |
54 | if ($id == -1) { |
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')); |
59 | } else { |
60 | $survey = SurveyDB::retrieveSurvey($id); // retrieves all survey object structure |
61 | if ($survey == null) { |
62 | $this->show_error($page, "Sondage ".$id." introuvable.", 'admin'); |
63 | } |
64 | //$this->store_session($survey, $id); |
65 | $page->assign('survey_adminmode', true); |
66 | $page->assign('survey_id', $id); |
67 | $this->show_survey($page, $survey); |
68 | } |
8fe81c50 |
69 | } |
70 | // }}} |
71 | |
72 | // {{{ function handler_adminEdit() : edits a survey in admin mode |
56c6950a |
73 | function handler_adminEdit(&$page, $id = -1) |
8fe81c50 |
74 | { |
56c6950a |
75 | if ($id == -1) { |
76 | return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin'); |
77 | } |
8fe81c50 |
78 | require_once dirname(__FILE__).'/survey/survey.inc.php'; |
79 | $survey = SurveyDB::retrieveSurvey($id); // retrieves the survey in database |
56c6950a |
80 | $this->clear_session(); // cleans session (in case there would have been a problem before) |
81 | /*if ($survey->isValid()) { |
8fe81c50 |
82 | return $this->show_error($page, "Il est impossible de modifier un sondage déjà validé", 'admin'); |
56c6950a |
83 | }*/ |
84 | $this->store_session($survey, $id); |
8fe81c50 |
85 | $this->handler_edit($page, 'show'); // calls handler_edit, but in admin mode since 'survey_id' is in session |
86 | } |
87 | // }}} |
88 | |
89 | // {{{ function handler_adminValidate() : validates a survey (admin mode) |
90 | function handler_adminValidate(&$page, $id = -1) |
91 | { |
56c6950a |
92 | $id = Post::v('survey_id', $id); |
8fe81c50 |
93 | if (Post::has('survey_cancel')) { // if the admin cancels the validation, returns to the admin index |
56c6950a |
94 | $this->clear_session(); |
95 | return $this->handler_admin(&$page, $id); |
8fe81c50 |
96 | } |
8fe81c50 |
97 | if ($id == -1) { |
98 | return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin'); |
99 | } |
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'); |
104 | } |
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'); |
108 | } else { |
109 | $this->show_error($page, '', 'admin'); |
110 | } |
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)); |
114 | } |
56c6950a |
115 | /* |
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'); |
118 | } |
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'); |
126 | } else { |
127 | $this->show_error($page, '', 'admin'); |
128 | } |
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'); |
132 | } |
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'); |
138 | } |
139 | $this->store_session($survey, $id); |
140 | $page->assign('survey_validmode', true); |
141 | $this->show_survey($page, $survey); |
142 | } |
143 | */ |
8fe81c50 |
144 | } |
145 | // }}} |
146 | |
147 | // {{{ function handler_adminDelete() : deletes a survey (admin mode) |
148 | function handler_adminDelete(&$page, $id = -1) |
149 | { |
56c6950a |
150 | $id = Post::v('survey_id', $id); |
8fe81c50 |
151 | if (Post::has('survey_cancel')) { // if the admin cancels the suppression, returns to the admin index |
56c6950a |
152 | return $this->handler_admin(&$page, $id); |
8fe81c50 |
153 | } |
8fe81c50 |
154 | if ($id == -1) { |
155 | return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin'); |
156 | } |
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'); |
161 | } |
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'); |
165 | } else { |
166 | $this->show_error($page, '', 'admin'); |
167 | } |
168 | } else { // asks for a confirmation |
56c6950a |
169 | $this->show_confirm($page, "Êtes-vous certain de vouloir supprimer le sondage \"".$surveyInfo['title']."\" ?", 'admin/del', array('id' => $id)); |
8fe81c50 |
170 | } |
171 | } |
172 | // }}} |
173 | |
174 | // {{{ function handler_edit() : edits a survey (in normal mode unless called by handler_adminEdit() ) |
175 | function handler_edit(&$page, $action = 'show', $qid = 0) |
176 | { |
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')) { |
182 | $action = 'show'; |
183 | } else { // unless no editing has been done at all (shows to the surveys index page) |
184 | return $this->handler_index($page); |
185 | } |
186 | } |
56c6950a |
187 | $page->assign('survey_editmode', true); |
8fe81c50 |
188 | if (S::has('survey_id')) { // if 'survey_id' is in session, it means we are modifying a survey in admin mode |
56c6950a |
189 | $page->assign('survey_updatemode', true); |
8fe81c50 |
190 | } |
191 | if ($action == 'show' && !S::has('survey')) { |
192 | $action = 'new'; |
193 | } |
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'); |
200 | } |
201 | $this->show_survey($page, $survey); |
56c6950a |
202 | $this->store_session($survey); |
8fe81c50 |
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'); |
208 | } |
209 | $this->show_form($page, $action, $qid, $current['type'], $current); |
210 | } // }}} |
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 |
56c6950a |
213 | $this->clear_session(); |
8fe81c50 |
214 | $survey = new SurveyRoot(Post::v('survey_question')); // creates the object structure |
215 | $this->show_survey($page, $survey); |
56c6950a |
216 | $this->store_session($survey); |
8fe81c50 |
217 | } else { |
56c6950a |
218 | $this->clear_session(); |
8fe81c50 |
219 | $this->show_form($page, $action, 0, 'root'); |
220 | } // }}} |
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'); |
228 | } |
229 | } else { |
230 | if (!$survey->addChildAfter($qid, $question)) { |
231 | return $this->show_error($page, '', 'edit'); |
232 | } |
233 | } |
234 | $this->show_survey($page, $survey); |
56c6950a |
235 | $this->store_session($survey); |
8fe81c50 |
236 | } else { |
237 | $this->show_form($page, $action, $qid); |
238 | } // }}} |
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'); |
244 | } |
245 | $this->show_survey($page, $survey); |
56c6950a |
246 | $this->store_session($survey); |
8fe81c50 |
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'); |
252 | } |
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)); |
256 | } // }}} |
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'); |
266 | } else { |
267 | $this->show_error($page, '', 'admin'); |
268 | } |
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.", ''); |
273 | } else { |
274 | $this->show_error($page); |
275 | } |
276 | } |
56c6950a |
277 | $this->clear_session(); |
8fe81c50 |
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); |
283 | } else { |
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')); |
286 | } else { |
287 | $this->show_confirm($page, "Veuillez confirmer l'envoi de cette proposition de sondage.", 'edit', array('action' => 'valid')); |
288 | } |
289 | } |
290 | } // }}} |
291 | } elseif ($action == 'cancel') { // {{{ cancels the creation/modification of a survey |
292 | if (Post::has('survey_submit')) { // needs a confirmation |
8fe81c50 |
293 | if (S::has('survey_id')) { // only possible when modifying a survey in admin mode, still this should be considered again, |
56c6950a |
294 | $this->clear_session(); // maybe some name with "admin" in it, "survey_adminid" or anything that might not be confusing. |
8fe81c50 |
295 | return $this->handler_admin($page); // in this case, shows the admin index |
296 | } else { |
56c6950a |
297 | $this->clear_session(); // cleans session |
8fe81c50 |
298 | return $this->handler_index($page); // else shows the 'normal' index |
299 | } |
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)); |
304 | } |
305 | } // }}} |
306 | } |
307 | // }}} |
308 | |
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) |
311 | { |
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); |
318 | } |
319 | } |
320 | // }}} |
321 | |
56c6950a |
322 | // {{{ function clear_session() : clears the data stored in session |
323 | function clear_session() |
324 | { |
325 | S::kill('survey'); |
326 | S::kill('survey_id'); |
327 | } |
328 | // }}} |
329 | |
330 | // {{{ function store_session() : serializes and stores survey (and survey_id) in session |
331 | function store_session($survey, $survey_id = -1) |
332 | { |
333 | $_SESSION['survey'] = serialize($survey); |
334 | if ($survey_id != -1) { |
335 | $_SESSION['survey_id'] = $survey_id; |
336 | } |
337 | } |
338 | // }}} |
339 | |
8fe81c50 |
340 | // {{{ function show_survey() : calls the template to display a survey, for editing, voting, or consulting the results |
341 | function show_survey(&$page, $survey) |
342 | { |
56c6950a |
343 | $page->changeTpl('survey/show_root.tpl'); |
8fe81c50 |
344 | $page->assign('survey', $survey->toArray()); |
345 | } |
346 | // }}} |
347 | |
348 | // {{{ function show_form() : calls the template to display the editing form |
349 | function show_form(&$page, $action, $qid, $type = 'new', $current = null) |
350 | { |
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()); |
361 | } |
362 | } |
363 | // }}} |
364 | |
365 | // {{{ function show_confirm() : calls the template to display a confirm form |
56c6950a |
366 | function show_confirm(&$page, $message, $formaction, $formhidden = null) |
8fe81c50 |
367 | { |
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); |
372 | } |
373 | // }}} |
374 | |
375 | // {{{ function show_error() : calls the template to display an error message |
376 | function show_error(&$page, $message, $link = "", $errArray = null) |
377 | { |
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); |
383 | } |
384 | } |
385 | // }}} |
386 | |
387 | // {{{ function show_success() : calls the template to display a success message |
388 | function show_success(&$page, $message = "", $link = "") |
389 | { |
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 |
393 | } |
394 | // }}} |
395 | } |
396 | |
397 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
398 | ?> |