Commit | Line | Data |
---|---|---|
8fe81c50 | 1 | <?php |
2 | /*************************************************************************** | |
9f5bd98e | 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
8fe81c50 | 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( | |
eb5a266d SJ |
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'), | |
8fe81c50 | 35 | 'survey/admin/valid' => $this->make_hook('adminValidate', AUTH_MDP, 'admin'), |
eb5a266d | 36 | 'survey/admin/del' => $this->make_hook('adminDelete', AUTH_MDP, 'admin'), |
8fe81c50 | 37 | ); |
38 | } | |
39 | // }}} | |
40 | ||
41 | // {{{ function handler_index() : lists all available surveys | |
42 | function handler_index(&$page, $action = null) | |
43 | { | |
460d8f55 | 44 | $this->load('survey.inc.php'); |
8fe81c50 | 45 | $page->changeTpl('survey/index.tpl'); |
5c6e38d7 | 46 | $page->assign('survey_current', Survey::retrieveList('c')); |
47 | $page->assign('survey_old', Survey::retrieveList('o')); | |
48 | $page->assign('survey_modes', Survey::getModes(false)); | |
49 | } | |
50 | // }}} | |
51 | ||
9f01f40b | 52 | // {{{ function handler_vote() : handles the vote to a survey |
5c6e38d7 | 53 | function handler_vote(&$page, $id = -1) |
54 | { | |
55 | if (Post::has('survey_cancel')) { // if the user cancels, returns to index | |
56 | return $this->handler_index(&$page); | |
57 | } | |
58 | $id = intval($id); | |
59 | if ($id == -1) { | |
cd723c19 | 60 | return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey'); |
5c6e38d7 | 61 | } |
460d8f55 | 62 | $this->load('survey.inc.php'); |
5c6e38d7 | 63 | $survey = Survey::retrieveSurvey($id); // retrieves the survey object structure |
64 | if ($survey == null || !$survey->isValid()) { | |
797d27db | 65 | return $this->show_error($page, "Sondage ".$id." introuvable.", 'survey'); |
5c6e38d7 | 66 | } elseif ($survey->isEnded()) { |
cd723c19 | 67 | return $this->show_error($page, "Le sondage ".$survey->getTitle()." est terminé.", 'survey'); |
5c6e38d7 | 68 | } |
9f01f40b | 69 | if (!$this->check_surveyPerms($page, $survey)) { |
6997b099 | 70 | return PL_DO_AUTH; |
5c6e38d7 | 71 | } |
72 | if (Post::has('survey_submit')) { // checks if the survey has already been filled in | |
73 | $uid = 0; | |
74 | if (!$survey->isMode(Survey::MODE_ALL)) { // if survey is restriced to alumni | |
75 | $uid = S::v('uid'); | |
76 | if ($survey->hasVoted($uid)) { // checks whether the user has already voted | |
cd723c19 | 77 | return $this->show_error($page, "Tu as déjà voté à ce sondage.", 'survey'); |
5c6e38d7 | 78 | } |
79 | } | |
80 | $survey->vote($uid, Post::v('survey'.$id)); // performs vote | |
cd723c19 | 81 | $this->show_success($page, "Ta réponse a bien été prise en compte. Merci d'avoir participé à ce sondage.", 'survey'); |
5c6e38d7 | 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); | |
85 | } else { | |
cd723c19 | 86 | $page->assign('survey_warning', "Tu as déjà voté à ce sondage."); |
5c6e38d7 | 87 | } |
88 | //$page->assign('survey_id', $id); | |
89 | $this->show_survey($page, $survey); | |
90 | } | |
8fe81c50 | 91 | } |
92 | // }}} | |
93 | ||
9f01f40b | 94 | // {{{ function handler_result() : show the results of the votes to a survey |
4b8c8634 | 95 | function handler_result($page, $id = -1, $show = 'all') |
9f01f40b | 96 | { |
97 | $id = intval($id); | |
98 | if ($id == -1) { | |
cd723c19 | 99 | return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey'); |
9f01f40b | 100 | } |
460d8f55 | 101 | $this->load('survey.inc.php'); |
9f01f40b | 102 | $survey = Survey::retrieveSurvey($id); // retrieves the survey object structure |
103 | if ($survey == null || !$survey->isValid()) { | |
797d27db | 104 | return $this->show_error($page, "Sondage ".$id." introuvable.", 'survey'); |
9f01f40b | 105 | } elseif (!$survey->isEnded()) { |
cd723c19 | 106 | return $this->show_error($page, "Le sondage ".$survey->getTitle()." n'est pas encore terminé.", 'survey'); |
9f01f40b | 107 | } |
108 | if (!$this->check_surveyPerms($page, $survey)) { | |
6997b099 | 109 | return PL_DO_AUTH; |
9f01f40b | 110 | } |
4b8c8634 | 111 | if ($show == 'csv') { |
3cb500d5 | 112 | pl_content_headers("text/csv"); |
3cf01f9e | 113 | header('Content-Disposition: attachment; filename="'.addslashes($survey->getTitle()).'.csv"'); |
4b8c8634 | 114 | echo $survey->toCSV(); |
115 | exit; | |
116 | } else { | |
117 | $page->assign('survey_resultmode', true); | |
118 | $this->show_survey($page, $survey); | |
119 | } | |
9f01f40b | 120 | } |
121 | // }}} | |
122 | ||
8fe81c50 | 123 | // {{{ function handler_admin() : index of admin mode |
56c6950a | 124 | function handler_admin(&$page, $id = -1) |
8fe81c50 | 125 | { |
460d8f55 | 126 | $this->load('survey.inc.php'); |
56c6950a | 127 | $this->clear_session(); |
128 | if ($id == -1) { | |
129 | $page->changeTpl('survey/admin.tpl'); | |
5c6e38d7 | 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)); | |
56c6950a | 134 | } else { |
cd129064 | 135 | $id = intval($id); |
5c6e38d7 | 136 | $survey = Survey::retrieveSurvey($id); // retrieves all survey object structure |
56c6950a | 137 | if ($survey == null) { |
797d27db | 138 | $this->show_error($page, "Sondage ".$id." introuvable.", 'survey/admin'); |
56c6950a | 139 | } |
56c6950a | 140 | $page->assign('survey_adminmode', true); |
56c6950a | 141 | $this->show_survey($page, $survey); |
142 | } | |
8fe81c50 | 143 | } |
144 | // }}} | |
145 | ||
146 | // {{{ function handler_adminEdit() : edits a survey in admin mode | |
797d27db | 147 | function handler_adminEdit(&$page, $id = -1, $req = -1) |
8fe81c50 | 148 | { |
797d27db | 149 | if ($id == -1 || ($id == 'req' && $req == -1)) { |
cd723c19 | 150 | return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey/admin'); |
56c6950a | 151 | } |
460d8f55 | 152 | $this->load('survey.inc.php'); |
56c6950a | 153 | $this->clear_session(); // cleans session (in case there would have been a problem before) |
797d27db | 154 | if ($id == 'req') { |
155 | $survey = Survey::retrieveSurveyReq($req); | |
156 | if ($survey == null) { | |
157 | return $this->show_error($page, "Sondage introuvable.", 'survey/admin'); | |
158 | } | |
159 | $this->store_session($survey, $req, true); | |
160 | } else { | |
161 | $id = intval($id); | |
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'); | |
165 | } | |
166 | $this->store_session($survey, $id); | |
167 | } | |
8fe81c50 | 168 | $this->handler_edit($page, 'show'); // calls handler_edit, but in admin mode since 'survey_id' is in session |
169 | } | |
170 | // }}} | |
171 | ||
172 | // {{{ function handler_adminValidate() : validates a survey (admin mode) | |
173 | function handler_adminValidate(&$page, $id = -1) | |
174 | { | |
5c6e38d7 | 175 | $id = Post::i('survey_id', $id); |
8fe81c50 | 176 | if (Post::has('survey_cancel')) { // if the admin cancels the validation, returns to the admin index |
56c6950a | 177 | $this->clear_session(); |
178 | return $this->handler_admin(&$page, $id); | |
8fe81c50 | 179 | } |
8fe81c50 | 180 | if ($id == -1) { |
cd723c19 | 181 | return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey/admin'); |
8fe81c50 | 182 | } |
cd129064 | 183 | $id = intval($id); |
460d8f55 | 184 | $this->load('survey.inc.php'); |
5c6e38d7 | 185 | $surveyInfo = Survey::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure) |
8fe81c50 | 186 | if ($surveyInfo == null) { |
797d27db | 187 | return $this->show_error($page, "Sondage ".$id." introuvable.", 'survey/admin'); |
8fe81c50 | 188 | } |
189 | if (Post::has('survey_submit')) { // needs a confirmation before validation | |
5c6e38d7 | 190 | if (Survey::validateSurvey($id)) { // validates the survey (in the database) |
cd723c19 | 191 | $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien été validé, les votes sont maintenant ouverts.", 'survey/admin'); |
8fe81c50 | 192 | } else { |
797d27db | 193 | $this->show_error($page, '', 'survey/admin'); |
8fe81c50 | 194 | } |
195 | } else { // asks for a confirmation | |
cd723c19 | 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)); | |
8fe81c50 | 198 | } |
199 | } | |
200 | // }}} | |
201 | ||
202 | // {{{ function handler_adminDelete() : deletes a survey (admin mode) | |
203 | function handler_adminDelete(&$page, $id = -1) | |
204 | { | |
5c6e38d7 | 205 | $id = Post::i('survey_id', $id); |
8fe81c50 | 206 | if (Post::has('survey_cancel')) { // if the admin cancels the suppression, returns to the admin index |
56c6950a | 207 | return $this->handler_admin(&$page, $id); |
8fe81c50 | 208 | } |
8fe81c50 | 209 | if ($id == -1) { |
cd723c19 | 210 | return $this->show_error($page, "Un identifiant de sondage doit être précisé.", 'survey/admin'); |
8fe81c50 | 211 | } |
cd129064 | 212 | $id = intval($id); |
460d8f55 | 213 | $this->load('survey.inc.php'); |
5c6e38d7 | 214 | $surveyInfo = Survey::retrieveSurveyInfo($id); // retrieves information about the survey (does not retrieve and unserialize the object structure) |
8fe81c50 | 215 | if ($surveyInfo == null) { |
797d27db | 216 | return $this->show_error($page, "Sondage ".$id." introuvable.", 'survey/admin'); |
8fe81c50 | 217 | } |
218 | if (Post::has('survey_submit')) { // needs a confirmation before suppression | |
5c6e38d7 | 219 | if (Survey::deleteSurvey($id)) { // deletes survey in database |
cd723c19 | 220 | $this->show_success($page, "Le sondage \"".$surveyInfo['title']."\" a bien été supprimé, ainsi que tous les votes le concernant.", 'survey/admin'); |
8fe81c50 | 221 | } else { |
797d27db | 222 | $this->show_error($page, '', 'survey/admin'); |
8fe81c50 | 223 | } |
224 | } else { // asks for a confirmation | |
cd723c19 | 225 | $this->show_confirm($page, "Êtes-vous certain de vouloir supprimer le sondage \"".$surveyInfo['title']."\" ?", 'admin/del', array('id' => $id)); |
8fe81c50 | 226 | } |
227 | } | |
228 | // }}} | |
229 | ||
230 | // {{{ function handler_edit() : edits a survey (in normal mode unless called by handler_adminEdit() ) | |
5c6e38d7 | 231 | function handler_edit(&$page, $action = 'show', $qid = 'root') |
8fe81c50 | 232 | { |
460d8f55 | 233 | $this->load('survey.inc.php'); |
8fe81c50 | 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')) { | |
238 | $action = 'show'; | |
239 | } else { // unless no editing has been done at all (shows to the surveys index page) | |
240 | return $this->handler_index($page); | |
241 | } | |
242 | } | |
56c6950a | 243 | $page->assign('survey_editmode', true); |
8fe81c50 | 244 | if (S::has('survey_id')) { // if 'survey_id' is in session, it means we are modifying a survey in admin mode |
56c6950a | 245 | $page->assign('survey_updatemode', true); |
8fe81c50 | 246 | } |
247 | if ($action == 'show' && !S::has('survey')) { | |
248 | $action = 'new'; | |
249 | } | |
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'); | |
5c6e38d7 | 254 | if (!$survey->editQuestion($qid, $args)) { // update the survey object structure |
797d27db | 255 | return $this->show_error($page, '', 'survey/edit'); |
8fe81c50 | 256 | } |
257 | $this->show_survey($page, $survey); | |
56c6950a | 258 | $this->store_session($survey); |
8fe81c50 | 259 | } else { // if a form has not been submitted, shows modification form |
260 | $survey = unserialize(S::v('survey')); | |
5c6e38d7 | 261 | $current = $survey->toArray($qid); // gets the current parameters of the question |
8fe81c50 | 262 | if ($current == null) { |
797d27db | 263 | return $this->show_error($page, '', 'survey/edit'); |
8fe81c50 | 264 | } |
265 | $this->show_form($page, $action, $qid, $current['type'], $current); | |
266 | } // }}} | |
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 | |
56c6950a | 269 | $this->clear_session(); |
5c6e38d7 | 270 | $survey = new Survey(Post::v('survey_question')); // creates the object structure |
8fe81c50 | 271 | $this->show_survey($page, $survey); |
56c6950a | 272 | $this->store_session($survey); |
8fe81c50 | 273 | } else { |
56c6950a | 274 | $this->clear_session(); |
8602c852 | 275 | $this->show_form($page, $action, 'root', 'newsurvey'); |
8fe81c50 | 276 | } // }}} |
5c6e38d7 | 277 | } elseif ($action == 'add') { // {{{ adds a new question |
8fe81c50 | 278 | if (Post::has('survey_submit')) { // if the form has been submitted, adds the question |
279 | $survey = unserialize(S::v('survey')); | |
5c6e38d7 | 280 | if (!$survey->addQuestion($qid, $survey->factory(Post::v('survey_type'), Post::v('survey_question')))) { |
797d27db | 281 | return $this->show_error($page, '', 'survey/edit'); |
8fe81c50 | 282 | } |
283 | $this->show_survey($page, $survey); | |
56c6950a | 284 | $this->store_session($survey); |
8fe81c50 | 285 | } else { |
286 | $this->show_form($page, $action, $qid); | |
287 | } // }}} | |
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')); | |
5c6e38d7 | 291 | if (!$survey->delQuestion(Post::v('survey_qid'))) { // deletes the node in the survey object structure |
797d27db | 292 | return $this->show_error($page, '', 'survey/edit'); |
8fe81c50 | 293 | } |
294 | $this->show_survey($page, $survey); | |
56c6950a | 295 | $this->store_session($survey); |
8fe81c50 | 296 | } else { // if user has not confirmed, shows a confirmation form |
297 | $survey = unserialize(S::v('survey')); | |
5c6e38d7 | 298 | $current = $survey->toArray($qid); // needed to get the title of the question to delete (more user-friendly than an id) |
8fe81c50 | 299 | if ($current == null) { |
797d27db | 300 | return $this->show_error($page, '', 'survey/edit'); |
8fe81c50 | 301 | } |
cd723c19 | 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.', | |
8fe81c50 | 304 | 'edit', array('action' => 'del', 'qid' => $qid)); |
305 | } // }}} | |
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 | |
797d27db | 313 | $link = (S::has('survey_validate'))? 'admin/validate' : 'survey/admin'; |
5c6e38d7 | 314 | if ($survey->updateSurvey()) { // updates the database according the new survey object structure |
cd723c19 | 315 | $this->show_success($page, "Les modifications sur le sondage ont bien été enregistrées.", $link); |
8fe81c50 | 316 | } else { |
797d27db | 317 | $this->show_error($page, '', $link); |
8fe81c50 | 318 | } |
319 | } else { // if no 'survey_id' is in session, we are indeed proposing a new survey | |
5c6e38d7 | 320 | if ($survey->proposeSurvey()) { // stores the survey object structure in database |
cd723c19 | 321 | $this->show_success($page, "Votre proposition de sondage a bien été enregistrée, |
d1fa9d16 | 322 | elle est en attente de validation par un administrateur du site.", 'survey'); |
8fe81c50 | 323 | } else { |
797d27db | 324 | $this->show_error($page, '', 'survey'); |
8fe81c50 | 325 | } |
326 | } | |
56c6950a | 327 | $this->clear_session(); |
8fe81c50 | 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)) { | |
797d27db | 332 | $this->show_error($page, "", 'survey/edit', $errors); |
8fe81c50 | 333 | } else { |
334 | if (S::has('survey_id')) { | |
88339f38 | 335 | $this->show_confirm($page, "Veuillez confirmer l'enregistrement des modifications apportées à ce sondage.", 'edit', array('action' => 'valid')); |
8fe81c50 | 336 | } else { |
337 | $this->show_confirm($page, "Veuillez confirmer l'envoi de cette proposition de sondage.", 'edit', array('action' => 'valid')); | |
338 | } | |
339 | } | |
340 | } // }}} | |
341 | } elseif ($action == 'cancel') { // {{{ cancels the creation/modification of a survey | |
342 | if (Post::has('survey_submit')) { // needs a confirmation | |
797d27db | 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(); | |
cd723c19 | 346 | return $this->show_success($page, "Les modifications effectuées ont été annulées", 'admin/validate'); |
797d27db | 347 | } else { // else shows the admin index |
348 | $this->clear_session(); | |
349 | return $this->handler_admin($page); | |
350 | } | |
8fe81c50 | 351 | } else { |
797d27db | 352 | $this->clear_session(); |
8fe81c50 | 353 | return $this->handler_index($page); // else shows the 'normal' index |
354 | } | |
355 | } else { // asks for a confirmation if it has not been sent | |
cd723c19 | 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.", | |
8fe81c50 | 358 | 'edit', array('action' => $action)); |
359 | } | |
360 | } // }}} | |
361 | } | |
362 | // }}} | |
eaf30d86 | 363 | |
8fe81c50 | 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) | |
366 | { | |
460d8f55 | 367 | $this->load('survey.inc.php'); |
3cb500d5 | 368 | pl_content_headers("text/html"); |
5c6e38d7 | 369 | if (Survey::isType($type)) { // when type has been chosen, the form is updated to fit exactly the type of question chosen |
8fe81c50 | 370 | $page->changeTpl('survey/edit_new.tpl', NO_SKIN); |
5c6e38d7 | 371 | $page->assign('survey_types', Survey::getTypes()); |
8fe81c50 | 372 | $page->assign('survey_type', $type); |
373 | } | |
374 | } | |
375 | // }}} | |
376 | ||
56c6950a | 377 | // {{{ function clear_session() : clears the data stored in session |
378 | function clear_session() | |
379 | { | |
380 | S::kill('survey'); | |
381 | S::kill('survey_id'); | |
797d27db | 382 | S::kill('survey_validate'); |
56c6950a | 383 | } |
384 | // }}} | |
385 | ||
386 | // {{{ function store_session() : serializes and stores survey (and survey_id) in session | |
797d27db | 387 | function store_session($survey, $survey_id = -1, $survey_validate = false) |
56c6950a | 388 | { |
389 | $_SESSION['survey'] = serialize($survey); | |
390 | if ($survey_id != -1) { | |
391 | $_SESSION['survey_id'] = $survey_id; | |
392 | } | |
797d27db | 393 | if ($survey_validate) { |
394 | $_SESSION['survey_validate'] = true; | |
395 | } | |
56c6950a | 396 | } |
397 | // }}} | |
398 | ||
9f01f40b | 399 | // {{{ function check_surveyPerms() : checks the particular surveys access permissions |
12678e90 | 400 | function check_surveyPerms(&$page, $survey, $silent = false) |
9f01f40b | 401 | { |
460d8f55 | 402 | $this->load('survey.inc.php'); |
12678e90 PC |
403 | if ($survey->isMode(Survey::MODE_ALL)) { // if the survey is not reserved to alumni |
404 | return true; | |
405 | } | |
406 | if (!S::logged()) { | |
407 | return false; | |
408 | } | |
409 | $profile = S::user()->profile(); | |
410 | if (!$profile) { | |
411 | return false; | |
412 | } | |
413 | // checks promotion | |
414 | $allowed = false; | |
415 | foreach ($profile->yearspromo() as $p) { | |
416 | if ($survey->checkPromo($p)) { | |
417 | $allowed = true; | |
418 | break; | |
9f01f40b | 419 | } |
420 | } | |
12678e90 PC |
421 | if ($allowed) { |
422 | return true; | |
423 | } | |
424 | if (!$silent) { | |
425 | $page->kill("Tu n'as pas accès à ce sondage car il est réservé à d'autres promotions."); | |
426 | } | |
427 | return false; | |
9f01f40b | 428 | } |
429 | // }}} | |
430 | ||
8fe81c50 | 431 | // {{{ function show_survey() : calls the template to display a survey, for editing, voting, or consulting the results |
432 | function show_survey(&$page, $survey) | |
433 | { | |
56c6950a | 434 | $page->changeTpl('survey/show_root.tpl'); |
8fe81c50 | 435 | $page->assign('survey', $survey->toArray()); |
5c6e38d7 | 436 | $page->assign('survey_modes', Survey::getModes()); |
8fe81c50 | 437 | } |
438 | // }}} | |
439 | ||
440 | // {{{ function show_form() : calls the template to display the editing form | |
441 | function show_form(&$page, $action, $qid, $type = 'new', $current = null) | |
442 | { | |
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'); | |
5c6e38d7 | 452 | $page->assign('survey_types', Survey::getTypes()); |
453 | } | |
8602c852 | 454 | if ($type == 'root' || $type == 'newsurvey') { |
5c6e38d7 | 455 | $page->assign('survey_modes', Survey::getModes()); |
8fe81c50 | 456 | } |
457 | } | |
458 | // }}} | |
eaf30d86 | 459 | |
8fe81c50 | 460 | // {{{ function show_confirm() : calls the template to display a confirm form |
56c6950a | 461 | function show_confirm(&$page, $message, $formaction, $formhidden = null) |
8fe81c50 | 462 | { |
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); | |
467 | } | |
468 | // }}} | |
469 | ||
470 | // {{{ function show_error() : calls the template to display an error message | |
471 | function show_error(&$page, $message, $link = "", $errArray = null) | |
472 | { | |
473 | $page->changeTpl('survey/error.tpl'); | |
474 | $page->assign('survey_message', $message); | |
797d27db | 475 | $page->assign('survey_link', $link); // 'return' link to let the user leave the page |
8fe81c50 | 476 | if (!is_null($errArray)) { |
477 | $page->assign('survey_errors', $errArray); | |
478 | } | |
6997b099 | 479 | |
8fe81c50 | 480 | } |
481 | // }}} | |
482 | ||
483 | // {{{ function show_success() : calls the template to display a success message | |
484 | function show_success(&$page, $message = "", $link = "") | |
485 | { | |
486 | $page->changeTpl('survey/success.tpl'); | |
487 | $page->assign('survey_message', $message); | |
797d27db | 488 | $page->assign('survey_link', $link); // 'return' link to let the user leave the page |
8fe81c50 | 489 | } |
490 | // }}} | |
491 | } | |
492 | ||
8602c852 | 493 | // vim:set et sw=4 sts=4 ts=4 foldmethod=marker enc=utf-8: |
8fe81c50 | 494 | ?> |