Commit | Line | Data |
---|---|---|
8fe81c50 | 1 | <?php |
2 | /*************************************************************************** | |
5e1513f6 | 3 | * Copyright (C) 2003-2011 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 | { | |
8fe81c50 | 24 | function handlers() |
25 | { | |
26 | return array( | |
2f4b93be FB |
27 | 'survey' => $this->make_hook('index', AUTH_COOKIE), |
28 | 'survey/vote' => $this->make_hook('vote', AUTH_COOKIE), | |
eb5a266d | 29 | 'survey/edit' => $this->make_hook('edit', AUTH_COOKIE), |
d0b836c9 FB |
30 | /* |
31 | 'survey/result' => $this->make_hook('result', AUTH_COOKIE), | |
eb5a266d SJ |
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'), |
2f4b93be | 37 | */ ); |
8fe81c50 | 38 | } |
8fe81c50 | 39 | |
22264cd4 | 40 | private function setup_page(PlPage $page) |
8fe81c50 | 41 | { |
460d8f55 | 42 | $this->load('survey.inc.php'); |
22264cd4 FB |
43 | $page->addJsLink('jquery.ui.core.js'); |
44 | $page->addJsLink('jquery.ui.widget.js'); | |
45 | $page->addJsLink('jquery.ui.datepicker.js'); | |
46 | $page->addJsLink('jquery.ui.datepicker-fr.js'); | |
47 | $page->addJsLink('jquery.tmpl.js'); | |
48 | $page->addJsLink('survey.js'); | |
49 | } | |
50 | ||
51 | function handler_index(&$page, $action = null) | |
52 | { | |
53 | $this->setup_page($page); | |
5c6e38d7 | 54 | |
2f4b93be FB |
55 | $page->changeTpl('survey/index.tpl'); |
56 | $page->assign('active', Survey::iterActive()); | |
56c6950a | 57 | } |
56c6950a | 58 | |
2f4b93be | 59 | function handler_vote(PlPage $page, $name) |
9f01f40b | 60 | { |
22264cd4 | 61 | $this->setup_page($page); |
2f4b93be | 62 | $page->changeTpl('survey/vote.tpl'); |
22264cd4 | 63 | |
2f4b93be FB |
64 | $survey = Survey::get($name); |
65 | if (is_null($survey)) { | |
66 | return PL_NOT_FOUND; | |
12678e90 | 67 | } |
2f4b93be FB |
68 | if (!$survey->canSee(S::user())) { |
69 | return PL_FORBIDDEN; | |
95fb3c74 | 70 | } |
90343d1e FB |
71 | if (Post::has('vote')) { |
72 | $answers = Post::v('qid'); | |
73 | $vote = $survey->vote(S::user(), $answers); | |
74 | if (is_null($vote)) { | |
75 | $page->kill("Tu n'as pas le droit de voter à ce sondage."); | |
76 | } else if ($vote->inError()) { | |
77 | $page->trigError("Certaines réponses sont invalides et doivent être corrigées"); | |
78 | } else { | |
79 | $vote->insert(true); | |
80 | $page->trigSuccess("Ton vote a été enregistré"); | |
81 | } | |
82 | } | |
2f4b93be | 83 | $page->assign('survey', $survey); |
8fe81c50 | 84 | } |
d0b836c9 FB |
85 | |
86 | function handler_edit(PlPage $page, $name = null) | |
87 | { | |
22264cd4 | 88 | $this->setup_page($page); |
d0b836c9 FB |
89 | $page->changeTpl('survey/edit.tpl'); |
90 | ||
91 | if (!is_null($name)) { | |
92 | $survey = Survey::get($name); | |
93 | } else { | |
94 | $survey = new Survey(); | |
95 | $survey->id = null; | |
96 | $survey->uid = S::user()->id(); | |
97 | } | |
99ec711f FB |
98 | if (Post::has('valid')) { |
99 | $survey->title = Post::t('title'); | |
100 | $survey->shortname = Post::t('shortname'); | |
101 | $survey->description = Post::t('description'); | |
102 | $survey->begin = Post::t('begin'); | |
103 | $survey->end = Post::t('end'); | |
104 | $survey->flags = 'validated'; | |
22264cd4 | 105 | $survey->flags->addFlag('anonymous', Post::b('anonymous')); |
99ec711f FB |
106 | |
107 | $q_edit = Post::v('q_edit'); | |
108 | $qs = array(); | |
3ddf2584 | 109 | $survey->clearQuestions(); |
99ec711f FB |
110 | foreach ($q_edit as $qid => $q_desc) { |
111 | if (isset($q_desc['parent'])) { | |
112 | $parent = $qs[$q_desc['parent']]; | |
113 | } else { | |
114 | $parent = $survey; | |
115 | } | |
116 | $question = $parent->newQuestion($q_desc['type']); | |
117 | $question->label = $q_desc['label']; | |
118 | unset($q_desc['type']); | |
119 | unset($q_desc['parent']); | |
120 | unset($q_desc['label']); | |
121 | $question->parameters = $q_desc; | |
122 | $qs[$qid] = $question; | |
123 | } | |
124 | $survey->insert('true'); | |
d0b836c9 FB |
125 | } |
126 | $page->assign('survey', $survey); | |
127 | } | |
8fe81c50 | 128 | } |
129 | ||
8602c852 | 130 | // vim:set et sw=4 sts=4 ts=4 foldmethod=marker enc=utf-8: |
8fe81c50 | 131 | ?> |