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 |
50 | function handler_admin(&$page) |
51 | { |
52 | require_once dirname(__FILE__).'/survey/survey.inc.php'; |
53 | $page->changeTpl('survey/admin.tpl'); |
54 | $page->assign('survey_waiting', SurveyDB::retrieveList('w')); |
55 | $page->assign('survey_current', SurveyDB::retrieveList('c')); |
56 | $page->assign('survey_old', SurveyDB::retrieveList('o')); |
57 | } |
58 | // }}} |
59 | |
60 | // {{{ function handler_adminEdit() : edits a survey in admin mode |
61 | function handler_adminEdit(&$page, $id) |
62 | { |
63 | require_once dirname(__FILE__).'/survey/survey.inc.php'; |
64 | $survey = SurveyDB::retrieveSurvey($id); // retrieves the survey in database |
65 | S::kill('survey'); // cleans session (in case there would have been a problem before) |
66 | S::kill('survey_id'); |
67 | if ($survey->isValid()) { |
68 | return $this->show_error($page, "Il est impossible de modifier un sondage déjà validé", 'admin'); |
69 | } |
70 | $_SESSION['survey'] = serialize($survey); |
71 | $_SESSION['survey_id'] = $id; |
72 | $this->handler_edit($page, 'show'); // calls handler_edit, but in admin mode since 'survey_id' is in session |
73 | } |
74 | // }}} |
75 | |
76 | // {{{ function handler_adminValidate() : validates a survey (admin mode) |
77 | function handler_adminValidate(&$page, $id = -1) |
78 | { |
79 | if (Post::has('survey_cancel')) { // if the admin cancels the validation, returns to the admin index |
80 | return $this->handler_admin(&$page); |
81 | } |
82 | $id = Post::v('survey_id', $id); |
83 | if ($id == -1) { |
84 | return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin'); |
85 | } |
86 | require_once dirname(__FILE__).'/survey/survey.inc.php'; |
87 | $surveyInfo = SurveyDB::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure) |
88 | if ($surveyInfo == null) { |
89 | return $this->show_error($page, "Sondage ".$id." introuvable.", 'admin'); |
90 | } |
91 | if (Post::has('survey_submit')) { // needs a confirmation before validation |
92 | if (SurveyDB::validateSurvey($id)) { // validates the survey (in the database) |
93 | $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien été validé, les votes sont maintenant ouverts.", 'admin'); |
94 | } else { |
95 | $this->show_error($page, '', 'admin'); |
96 | } |
97 | } else { // asks for a confirmation |
98 | $this->show_confirm($page, "Êtes-vous certain de vouloir valider le sondage \"".$surveyInfo['title']."\" ? " |
99 | ."Les votes seront immédiatement ouverts.", 'admin/valid', array('id' => $id)); |
100 | } |
101 | } |
102 | // }}} |
103 | |
104 | // {{{ function handler_adminDelete() : deletes a survey (admin mode) |
105 | function handler_adminDelete(&$page, $id = -1) |
106 | { |
107 | if (Post::has('survey_cancel')) { // if the admin cancels the suppression, returns to the admin index |
108 | return $this->handler_admin(&$page); |
109 | } |
110 | $id = Post::v('survey_id', $id); |
111 | if ($id == -1) { |
112 | return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'admin'); |
113 | } |
114 | require_once dirname(__FILE__).'/survey/survey.inc.php'; |
115 | $surveyInfo = SurveyDB::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure) |
116 | if ($surveyInfo == null) { |
117 | return $this->show_error($page, "Sondage ".$id." introuvable.", 'admin'); |
118 | } |
119 | if (Post::has('survey_submit')) { // needs a confirmation before suppression |
120 | if (SurveyDB::deleteSurvey($id)) { // deletes survey in database |
121 | $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien été supprimé, ainsi que tous les votes le concernant.", 'admin'); |
122 | } else { |
123 | $this->show_error($page, '', 'admin'); |
124 | } |
125 | } else { // asks for a confirmation |
126 | $this->show_confirm($page, "Êtes-vous certain de vouloir supprimer le sondage \"".$surveyInfo['title']."\" ?", 'admin/delete', array('id' => $id)); |
127 | } |
128 | } |
129 | // }}} |
130 | |
131 | // {{{ function handler_edit() : edits a survey (in normal mode unless called by handler_adminEdit() ) |
132 | function handler_edit(&$page, $action = 'show', $qid = 0) |
133 | { |
134 | require_once dirname(__FILE__).'/survey/survey.inc.php'; |
135 | $action = Post::v('survey_action', $action); |
136 | $qid = Post::v('survey_qid', $qid); |
137 | if (Post::has('survey_cancel')) { // after cancelling changes, shows the survey |
138 | if (S::has('survey')) { |
139 | $action = 'show'; |
140 | } else { // unless no editing has been done at all (shows to the surveys index page) |
141 | return $this->handler_index($page); |
142 | } |
143 | } |
144 | if (S::has('survey_id')) { // if 'survey_id' is in session, it means we are modifying a survey in admin mode |
145 | $page->assign('survey_adminmode', true); |
146 | } |
147 | if ($action == 'show' && !S::has('survey')) { |
148 | $action = 'new'; |
149 | } |
150 | if ($action == 'question') { // {{{ modifies an existing question |
151 | if (Post::has('survey_submit')) { // if the form has been submitted, makes the modifications |
152 | $survey = unserialize(S::v('survey')); |
153 | $args = Post::v('survey_question'); |
154 | if (!$survey->edit($qid, $args)) { // update the survey object structure |
155 | return $this->show_error($page, '', 'edit'); |
156 | } |
157 | $this->show_survey($page, $survey); |
158 | $_SESSION['survey'] = serialize($survey); |
159 | } else { // if a form has not been submitted, shows modification form |
160 | $survey = unserialize(S::v('survey')); |
161 | $current = $survey->searchToArray($qid); // gets the current parameters of the question |
162 | if ($current == null) { |
163 | return $this->show_error($page, '', 'edit'); |
164 | } |
165 | $this->show_form($page, $action, $qid, $current['type'], $current); |
166 | } // }}} |
167 | } elseif ($action == 'new') { // {{{ create a new survey : actually store the root question |
168 | if (Post::has('survey_submit')) { // if the form has been submitted, creates the survey |
169 | S::kill('survey'); |
170 | S::kill('survey_id'); |
171 | $survey = new SurveyRoot(Post::v('survey_question')); // creates the object structure |
172 | $this->show_survey($page, $survey); |
173 | $_SESSION['survey'] = serialize($survey); |
174 | } else { |
175 | S::kill('survey'); |
176 | S::kill('survey_id'); |
177 | $this->show_form($page, $action, 0, 'root'); |
178 | } // }}} |
179 | } elseif ($action == 'nested' || $action == 'after') { // {{{ adds a new question, nested in the current node, or on the same level after it |
180 | if (Post::has('survey_submit')) { // if the form has been submitted, adds the question |
181 | $survey = unserialize(S::v('survey')); |
182 | $question = $survey->factory(Post::v('survey_type'), Post::v('survey_question')); // creates the question object, with a sort of 'factory' method |
183 | if ($action == 'nested') { |
184 | if (!$survey->addChildNested($qid, $question)) { |
185 | return $this->show_error($page, '', 'edit'); |
186 | } |
187 | } else { |
188 | if (!$survey->addChildAfter($qid, $question)) { |
189 | return $this->show_error($page, '', 'edit'); |
190 | } |
191 | } |
192 | $this->show_survey($page, $survey); |
193 | $_SESSION['survey'] = serialize($survey); |
194 | } else { |
195 | $this->show_form($page, $action, $qid); |
196 | } // }}} |
197 | } elseif ($action == 'del') { // {{{ deletes a question |
198 | if (Post::has('survey_submit')) { // if a confirmation has been sent, deletes the question |
199 | $survey = unserialize(S::v('survey')); |
200 | if (!$survey->delChild(Post::v('survey_qid'))) { // deletes the node in the survey object structure |
201 | return $this->show_error($page, '', 'edit'); |
202 | } |
203 | $this->show_survey($page, $survey); |
204 | $_SESSION['survey'] = serialize($survey); |
205 | } else { // if user has not confirmed, shows a confirmation form |
206 | $survey = unserialize(S::v('survey')); |
207 | $current = $survey->searchToArray($qid); // needed to get the title of the question to delete (more user-friendly than an id) |
208 | if ($current == null) { |
209 | return $this->show_error($page, '', 'edit'); |
210 | } |
211 | $this->show_confirm($page, 'Êtes-vous certain de vouloir supprimer la question intitulé "'.$current['question'].'" ? ' |
212 | .'Attention, cela supprimera en même temps toutes les questions qui dépendent de celle-ci.', |
213 | 'edit', array('action' => 'del', 'qid' => $qid)); |
214 | } // }}} |
215 | } elseif ($action == 'show') { // {{{ simply shows the survey in its current state |
216 | $this->show_survey($page, unserialize(S::v('survey'))); // }}} |
217 | } elseif ($action == 'valid') { // {{{ validates the proposition, i.e stores the proposition in the database |
218 | // but an admin will still need to validate the survey before it is activated |
219 | if (Post::has('survey_submit')) { // needs a confirmation before storing the proposition |
220 | $survey = unserialize(S::v('survey')); |
221 | 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 |
222 | if (SurveyDB::updateSurvey($survey, S::v('survey_id'))) { // updates the database according the new survey object structure |
223 | $this->show_success($page, "Les modifications sur le sondage ont bien été enregistrées.", 'admin'); |
224 | } else { |
225 | $this->show_error($page, '', 'admin'); |
226 | } |
227 | } else { // if no 'survey_id' is in session, we are indeed proposing a new survey |
228 | if (SurveyDB::proposeSurvey($survey)) { // stores the survey object structure in database |
229 | $this->show_success($page, "Votre proposition de sondage a bien été enregistrée, |
230 | elle est en attent de validation par un administrateur du site.", ''); |
231 | } else { |
232 | $this->show_error($page); |
233 | } |
234 | } |
235 | S::kill('survey'); // cleans session |
236 | S::kill('survey_id'); |
237 | } else { // asks for a confirmation if it has not been sent |
238 | $survey = unserialize(S::v('survey')); |
239 | $errors = $survey->checkSyntax(); |
240 | if (!is_null($errors)) { |
241 | $this->show_error($page, "", 'edit', $errors); |
242 | } else { |
243 | if (S::has('survey_id')) { |
244 | $this->show_confirm($page, "Veuillez confirmer l'enregistrement des modifications apportées à ce sondage", 'edit', array('action' => 'valid')); |
245 | } else { |
246 | $this->show_confirm($page, "Veuillez confirmer l'envoi de cette proposition de sondage.", 'edit', array('action' => 'valid')); |
247 | } |
248 | } |
249 | } // }}} |
250 | } elseif ($action == 'cancel') { // {{{ cancels the creation/modification of a survey |
251 | if (Post::has('survey_submit')) { // needs a confirmation |
252 | S::kill('survey'); // cleans session |
253 | if (S::has('survey_id')) { // only possible when modifying a survey in admin mode, still this should be considered again, |
254 | S::kill('survey_id'); // maybe some name with "admin" in it, "survey_adminid" or anything that might not be confusing. |
255 | return $this->handler_admin($page); // in this case, shows the admin index |
256 | } else { |
257 | return $this->handler_index($page); // else shows the 'normal' index |
258 | } |
259 | } else { // asks for a confirmation if it has not been sent |
260 | $this->show_confirm(&$page, "Êtes-vous certain de vouloir annuler totalement l'édition de ce sondage ? Attention, " |
261 | ."toutes les données éditées jusque lâ seront définitivement perdues.", |
262 | 'edit', array('action' => $action)); |
263 | } |
264 | } // }}} |
265 | } |
266 | // }}} |
267 | |
268 | // {{{ function handler_ajax() : some ajax in editing a new question (for now, there may be a little more later) |
269 | function handler_ajax(&$page, $type) |
270 | { |
271 | require_once dirname(__FILE__).'/survey/survey.inc.php'; |
272 | header('Content-Type: text/html; charset="UTF-8"'); |
273 | if (SurveyQuestion::isType($type)) { // when type has been chosen, the form is updated to fit exactly the type of question chosen |
274 | $page->changeTpl('survey/edit_new.tpl', NO_SKIN); |
275 | $page->assign('survey_types', SurveyQuestion::getTypes()); |
276 | $page->assign('survey_type', $type); |
277 | } |
278 | } |
279 | // }}} |
280 | |
281 | // {{{ function show_survey() : calls the template to display a survey, for editing, voting, or consulting the results |
282 | function show_survey(&$page, $survey) |
283 | { |
284 | $page->changeTpl('survey/show_survey.tpl'); |
285 | $page->assign('survey_mode', 'edit'); // for now, only editing has been completely implemented |
286 | $page->assign('survey', $survey->toArray()); |
287 | } |
288 | // }}} |
289 | |
290 | // {{{ function show_form() : calls the template to display the editing form |
291 | function show_form(&$page, $action, $qid, $type = 'new', $current = null) |
292 | { |
293 | $page->changeTpl('survey/edit_survey.tpl'); |
294 | $page->assign('survey_action', $action); |
295 | $page->assign('survey_qid', $qid); |
296 | $page->assign('survey_formaction', './survey/edit'); |
297 | $page->assign('survey_type', $type); |
298 | if (!is_null($current) && is_array($current)) { |
299 | $page->assign('survey_current', $current); |
300 | } elseif ($type == 'new') { |
301 | $page->addJsLink('ajax.js'); |
302 | $page->assign('survey_types', SurveyQuestion::getTypes()); |
303 | } |
304 | } |
305 | // }}} |
306 | |
307 | // {{{ function show_confirm() : calls the template to display a confirm form |
308 | function show_confirm(&$page, $message, $formaction, $formhidden) |
309 | { |
310 | $page->changeTpl('survey/confirm.tpl'); |
311 | $page->assign('survey_message', $message); |
312 | $page->assign('survey_formaction', './survey/'.$formaction); |
313 | $page->assign('survey_formhidden', $formhidden); |
314 | } |
315 | // }}} |
316 | |
317 | // {{{ function show_error() : calls the template to display an error message |
318 | function show_error(&$page, $message, $link = "", $errArray = null) |
319 | { |
320 | $page->changeTpl('survey/error.tpl'); |
321 | $page->assign('survey_message', $message); |
322 | $page->assign('survey_link', './survey/'.$link); // 'return' link to let the user leave the page |
323 | if (!is_null($errArray)) { |
324 | $page->assign('survey_errors', $errArray); |
325 | } |
326 | } |
327 | // }}} |
328 | |
329 | // {{{ function show_success() : calls the template to display a success message |
330 | function show_success(&$page, $message = "", $link = "") |
331 | { |
332 | $page->changeTpl('survey/success.tpl'); |
333 | $page->assign('survey_message', $message); |
334 | $page->assign('survey_link', './survey/'.$link); // 'return' link to let the user leave the page |
335 | } |
336 | // }}} |
337 | } |
338 | |
339 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
340 | ?> |