4 * Copyright 2004 $ThePhpWikiProgrammingTeam
5 * Copyright 2008 Marc-Etienne Vargenau, Alcatel-lucent
7 * This file is part of PhpWiki.
9 * PhpWiki is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * PhpWiki is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * This plugin provides configurable polls.
27 <<WikiPoll require_all=0 require_least=2
28 question[1]="Do you like PhpWiki?"
29 answer[1][1]="Yes" answer[1][2]="Do not know" answer[1][3]="No"
30 question[2]="Do you have PhpWiki installed by your own?"
31 answer[2][1]="Yes" answer[2][2]="No"
32 question[3]="Did you install any other wiki engine?"
33 answer[3][1]="Yes" answer[3][2]="No"
34 question[4]="What wiki engine do you like most?"
35 answer[4][1]="c2Wiki" answer[4][2]="MoinMoin" answer[4][3]="PhpWiki"
36 answer[4][4]="usemod" answer[4][5]="Twiki" answer[4][5]="guiki"
37 answer[4][6]="Mediawiki" answer[4][7]="Other"
38 question[5]="Which PhpWiki version do you use?"
39 answer[5][1]="1.2.x" answer[5][2]="1.3.1-12" answer[5][3]="1.3.13"
44 * <<WikiPoll page=PhpWikiPoll admin=1 >>
45 * and protect this page properly (e.g. PhpWikiPoll/Admin)
48 * admin page (view and reset statistics)
49 * for now only radio, support checkboxes (multiple selections) also?
54 class WikiPlugin_WikiPoll
63 function getDescription () {
64 return _("Enable configurable polls");
67 function getDefaultArguments() {
68 return array('page' => '[pagename]',
70 'require_all' => 1, // 1 if all questions must be answered
71 'require_least' => 0, // how many at least
75 function getArgs($argstr, $request=false, $defaults = false) {
76 if ($defaults === false)
77 $defaults = $this->getDefaultArguments();
78 //Fixme: on POST argstr is empty
80 list ($argstr_args, $argstr_defaults) = $this->parseArgStr($argstr);
81 if (isset($argstr_args["question_1"])) {
82 $args['question'] = $this->str2array("question",$argstr_args);
83 $args['answer'] = array();
84 for ($i = 0; $i <= count($args['question']); $i++) {
85 if ($array = $this->str2array(sprintf("%s_%d","answer",$i),$argstr_args))
86 $args['answer'][$i] = $array;
90 if (!empty($defaults))
91 foreach ($defaults as $arg => $default_val) {
92 if (isset($argstr_args[$arg]))
93 $args[$arg] = $argstr_args[$arg];
94 elseif ( $request and ($argval = $request->getArg($arg)) !== false )
95 $args[$arg] = $argval;
96 elseif (isset($argstr_defaults[$arg]))
97 $args[$arg] = (string) $argstr_defaults[$arg];
99 $args[$arg] = $default_val;
102 $args[$arg] = $this->expandArg($args[$arg], $request);
104 unset($argstr_args[$arg]);
105 unset($argstr_defaults[$arg]);
108 foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
109 if (!preg_match("/^(answer_|question_)/",$arg))
110 trigger_error(sprintf(_("argument '%s' not declared by plugin"),
111 $arg), E_USER_NOTICE);
117 function handle_plugin_args_cruft($argstr, $args) {
118 $argstr = str_replace("\n"," ",$argstr);
119 $argstr = str_replace(array("[","]"),array("_",""),$argstr);
120 $this->_args = $this->getArgs($argstr, $GLOBALS['request']);
124 function str2array($var, $obarray=false) {
125 if (!$obarray) $obarray = $GLOBALS;
126 $i = 0; $array = array();
127 $name = sprintf("%s_%d",$var,$i);
128 if (isset($obarray[$name])) $array[$i] = $obarray[$name];
131 $name = sprintf("%s_%d",$var,$i);
132 if (isset($obarray[$name])) $array[$i] = $obarray[$name];
133 } while (isset($obarray[$name]));
137 function run($dbi, $argstr, &$request, $basepage) {
138 if (!isset($_SERVER))
139 $_SERVER =& $GLOBALS['HTTP_SERVER_VARS'];
140 $request->setArg('nocache','purge');
141 $args = $this->getArgs($argstr, $request);
142 if (!$args['page']) {
143 return $this->error(sprintf(_("A required argument '%s' is missing."), 'page'));
145 if (!empty($args['admin']) and $request->_user->isAdmin()) {
147 return $this->doPollAdmin($dbi, $request, $page);
149 extract($this->_args);
150 $page = $dbi->getPage($args['page']);
151 // check ip and last visit
152 $poll = $page->get("poll");
153 $ip = $_SERVER['REMOTE_ADDR'];
154 $disable_submit = false;
155 if (isset($poll['ip'][$ip]) and ((time() - $poll['ip'][$ip]) < 20*60)) {
156 //view at least the result or disable the Go button
157 $html = HTML(HTML::strong(
158 _("Sorry! You must wait at least 20 minutes until you can vote again!")));
159 $html->pushContent($this->doPoll($page, $request, $request->getArg('answer'),true));
163 $poll['ip'][$ip] = time();
165 foreach ($poll['ip'] as $ip => $time) {
166 if ((time() - $time) > 21*60)
167 unset($poll['ip'][$ip]);
169 $html = HTML::form(array('action' => $request->getPostURL(),
170 'method' => 'post'));
172 if ($request->isPost()) {
173 // checkme: check if all answers are answered
174 if ($request->getArg('answer') and (
175 ($args['require_all'] and
176 count($request->getArg('answer')) == count($question))
178 ($args['require_least'] and
179 count($request->getArg('answer')) >= $args['require_least']))) {
180 $page->set("poll", $poll);
181 // update statistics and present them the user
182 return $this->doPoll($page, $request, $request->getArg('answer'));
184 $html->pushContent(HTML::p(HTML::strong(_("Not enough questions answered!"))));
188 $init = isset($question[0]) ? 0 : 1;
189 for ($i = $init; $i <= count($question); $i++) {
190 if (!isset($question[$i])) break;
192 if (!isset($answer[$i]))
193 trigger_error(fmt("Missing %s for %s","answer"."[$i]","question"."[$i]"),
196 if (! is_array($a)) {
198 $html->pushContent(HTML::p(HTML::strong($q)));
199 $html->pushContent(HTML::div(
200 HTML::input(array('type' => 'checkbox',
201 'name' => "answer[$i]",
203 HTML::raw(" "), $a));
206 for ($j=0; $j <= count($a); $j++) {
208 $row->pushContent(HTML::div(
209 HTML::input(array('type' => 'radio',
210 'name' => "answer[$i]",
212 HTML::raw(" "), $a[$j]));
214 $html->pushContent(HTML::p(HTML::strong($q)),$row);
217 if (!$disable_submit)
218 $html->pushContent(HTML::p(
219 HTML::input(array('type' => 'submit',
220 'name' => "WikiPoll",
221 'value' => _("OK"))),
222 HTML::input(array('type' => 'reset',
224 'value' => _("Reset")))));
226 $html->pushContent(HTML::p(),HTML::strong(
227 _("Sorry! You must wait at least 20 minutes until you can vote again!")));
231 function bar($percent) {
233 return HTML(HTML::img(array('src' => $WikiTheme->getImageUrl('leftbar'),
235 HTML::img(array('src' => $WikiTheme->getImageUrl('mainbar'),
237 'width' => sprintf("%02d",$percent),
239 HTML::img(array('src' => $WikiTheme->getImageUrl('rightbar'),
243 function doPoll($page, $request, $answers, $readonly = false) {
244 $question = $this->_args['question'];
245 $answer = $this->_args['answer'];
246 $html = HTML::table(array('cellspacing' => 2));
247 $init = isset($question[0]) ? 0 : 1;
248 for ($i = $init; $i <= count($question); $i++) {
249 if (!isset($question[$i])) break;
250 $poll = $page->get('poll');
251 @$poll['data']['all'][$i]++;
253 if (!isset($answer[$i]))
254 trigger_error(fmt("Missing %s for %s","answer"."[$i]","question"."[$i]"),
257 $page->set('poll',$poll);
259 $result = (isset($answers[$i])) ? $answers[$i] : -1;
260 if (! is_array($a) ) {
261 $checkbox = HTML::input(array('type' => 'checkbox',
262 'name' => "answer[$i]",
265 $checkbox->setAttr('checked', "checked");
267 list($percent,$count,$all) = $this->storeResult($page, $i, $result ? 1 : 0);
269 list($percent,$count,$all) = $this->getResult($page, $i, 1);
270 $print = sprintf(_(" %d%% (%d/%d)"), $percent, $count, $all);
271 $html->pushContent(HTML::tr(HTML::th(array('colspan' => 4,'align'=>'left'),$q)));
272 $html->pushContent(HTML::tr(HTML::td($checkbox),
274 HTML::td($this->bar($percent)),
277 $html->pushContent(HTML::tr(HTML::th(array('colspan' => 4,'align'=>'left'),$q)));
280 $this->storeResult($page, $i, $answers[$i]);
281 for ($j=0; $j <= count($a); $j++) {
283 list($percent,$count,$all) = $this->getResult($page,$i,$j);
284 $print = sprintf(_(" %d%% (%d/%d)"), $percent, $count, $all);
285 $radio = HTML::input(array('type' => 'radio',
286 'name' => "answer[$i]",
289 $radio->setAttr('checked', "checked");
290 $row->pushContent(HTML::tr(HTML::td($radio),
292 HTML::td($this->bar($percent)),
296 $html->pushContent($row);
300 return HTML(HTML::h3(_("The result of this poll so far:")),$html,HTML::p(_("Thanks for participating!")));
302 return HTML(HTML::h3(_("The result of this poll so far:")),$html);
306 function getResult($page,$i,$j) {
307 $poll = $page->get("poll");
308 @$count = $poll['data']['count'][$i][$j];
309 @$all = $poll['data']['all'][$i];
310 $percent = sprintf("%d", $count * 100.0 / $all);
311 return array($percent,$count,$all);
314 function storeResult($page, $i, $j) {
315 $poll = $page->get("poll");
317 $poll = array('data' => array('count' => array(),
320 @$poll['data']['count'][$i][$j]++;
321 //@$poll['data']['all'][$i];
322 $page->set("poll",$poll);
323 $percent = sprintf("%d", $poll['data']['count'][$i][$j] * 100.0 / $poll['data']['all'][$i]);
324 return array($percent,$poll['data']['count'][$i][$j],$poll['data']['all'][$i]);
333 // c-hanging-comment-ender-p: nil
334 // indent-tabs-mode: nil