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