2 rcs_id('$Id: WikiPoll.php,v 1.3 2004-02-24 03:54:46 rurban Exp $');
4 Copyright 2004 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
8 PhpWiki is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 PhpWiki is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * This plugin provides configurable polls.
26 <?plugin WikiPoll require_all=0 require_least=2
27 question[1]="Do you like PhpWiki?"
28 answer[1][1]="Yes" answer[1][2]="Do not know" answer[1][3]="No"
29 question[2]="Do you have PhpWiki installed by your own?"
30 answer[2][1]="Yes" answer[2][2]="No"
31 question[3]="Did you install any other wiki engine?"
32 answer[3][1]="Yes" answer[3][2]="No"
33 question[4]="What wiki engine do you like most?"
34 answer[4][1]="c2Wiki" answer[4][2]="MoinMoin" answer[4][3]="PhpWiki"
35 answer[4][4]="usemod" answer[4][5]="Twiki" answer[4][5]="guiki"
37 question[5]="Which PhpWiki version do you use?"
38 answer[5][1]="1.2.x" answer[5][2]="1.3. 1-2" answer[5][3]="1.3.3-4"
39 answer[5][4]="1.3.5-8"
43 * <?plugin WikiPoll page=PhpWikiPoll admin=1 ?>
44 * and protect this page properly (e.g. PhpWikiPoll/Admin)
47 * admin page (view and reset statistics)
48 * for now only radio, support checkboxes (multiple selections) also?
53 class WikiPlugin_WikiPoll
62 function getDescription () {
63 return _("Enable configurable polls");
66 function getVersion() {
67 return preg_replace("/[Revision: $]/", '',
71 function getDefaultArguments() {
72 return array('page' => '[pagename]',
74 'require_all' => 1, // 1 if all questions must be answered
75 'require_least' => 0, // how many at least
79 function getArgs($argstr, $request=false, $defaults = false) {
80 if ($defaults === false)
81 $defaults = $this->getDefaultArguments();
82 //Fixme: on POST argstr is empty
84 list ($argstr_args, $argstr_defaults) = $this->parseArgStr($argstr);
85 if (isset($argstr_args["question_1"])) {
86 $args['question'] = $this->str2array("question",$argstr_args);
87 $args['answer'] = array();
88 for ($i = 0; $i <= count($args['question']); $i++) {
89 if ($array = $this->str2array(sprintf("%s_%d","answer",$i),$argstr_args))
90 $args['answer'][$i] = $array;
94 if (!empty($defaults))
95 foreach ($defaults as $arg => $default_val) {
96 if (isset($argstr_args[$arg]))
97 $args[$arg] = $argstr_args[$arg];
98 elseif ( $request and ($argval = $request->getArg($arg)) !== false )
99 $args[$arg] = $argval;
100 elseif (isset($argstr_defaults[$arg]))
101 $args[$arg] = (string) $argstr_defaults[$arg];
103 $args[$arg] = $default_val;
106 $args[$arg] = $this->expandArg($args[$arg], $request);
108 unset($argstr_args[$arg]);
109 unset($argstr_defaults[$arg]);
112 foreach (array_merge($argstr_args, $argstr_defaults) as $arg => $val) {
113 if (!preg_match("/^(answer_|question_)/",$arg))
114 trigger_error(sprintf(_("argument '%s' not declared by plugin"),
115 $arg), E_USER_NOTICE);
121 function handle_plugin_args_cruft($argstr, $args) {
122 $argstr = str_replace("\n"," ",$argstr);
123 $argstr = str_replace(array("[","]"),array("_",""),$argstr);
124 $this->_args = $this->getArgs($argstr, $GLOBALS['request']);
128 function str2array($var, $obarray=false) {
129 if (!$obarray) $obarray = $GLOBALS;
130 $i = 0; $array = array();
131 $name = sprintf("%s_%d",$var,$i);
132 if (isset($obarray[$name])) $array[$i] = $obarray[$name];
135 $name = sprintf("%s_%d",$var,$i);
136 if (isset($obarray[$name])) $array[$i] = $obarray[$name];
137 } while (isset($obarray[$name]));
141 function run($dbi, $argstr, &$request, $basepage) {
142 $args = $this->getArgs($argstr, $request);
144 return $this->error("No page specified");
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 doPoll($page, $request, $answers, $readonly = false) {
232 $question = $this->_args['question'];
233 $answer = $this->_args['answer'];
234 $html = HTML::table(array('cellspacing' => 2));
235 $init = isset($question[0]) ? 0 : 1;
236 for ($i = $init; $i <= count($question); $i++) {
237 if (!isset($question[$i])) break;
238 $poll = $page->get('poll');
239 @$poll['data']['all'][$i]++;
241 if (!isset($answer[$i]))
242 trigger_error(fmt("Missing %s for %s","answer"."[$i]","question"."[$i]"),
245 $page->set('poll',$poll);
247 if (! is_array($a) ) {
248 $checkbox = HTML::input(array('type' => 'checkbox',
249 'name' => "answer[$i]",
252 $checkbox->setAttr('checked',1);
254 list($percent,$count,$all) = $this->storeResult(&$page, $i, $answers[$i] ? 1 : 0);
256 list($percent,$count,$all) = $this->getResult(&$page, $i, 1);
257 $result = sprintf(_(" %d%% selected this (%d/%d)"),$percent,$count,$all);
258 $html->pushContent(HTML::tr(HTML::th(array('colspan' => 3,'align'=>'left'),$q)));
259 $html->pushContent(HTML::tr(HTML::td($checkbox),
263 $html->pushContent(HTML::tr(HTML::th(array('colspan' => 3,'align'=>'left'),$q)));
266 $this->storeResult(&$page,$i,$answers[$i]);
267 for ($j=0; $j <= count($a); $j++) {
269 list($percent,$count,$all) = $this->getResult(&$page,$i,$j);
270 $result = sprintf(_(" %d%% selected this (%d/%d)"),$percent,$count,$all);
271 $radio = HTML::input(array('type' => 'radio',
272 'name' => "answer[$i]",
274 if ($answers[$i] == $j)
275 $radio->setAttr('checked',1);
276 $row->pushContent(HTML::tr(HTML::td($radio),
281 $html->pushContent($row);
285 return HTML(HTML::h3(_("The result of this poll so far:")),$html,HTML::p(_("Thanks for participating!")));
287 return HTML(HTML::h3(_("The result of this poll so far:")),$html);
291 function getResult($page,$i,$j) {
292 $poll = $page->get("poll");
293 @$count = $poll['data']['count'][$i][$j];
294 @$all = $poll['data']['all'][$i];
295 $percent = sprintf("%d", $count * 100.0 / $all);
296 return array($percent,$count,$all);
299 function storeResult($page, $i, $j) {
300 $poll = $page->get("poll");
302 $poll = array('data' => array('count' => array(),
305 @$poll['data']['count'][$i][$j]++;
306 //@$poll['data']['all'][$i];
307 $page->set("poll",$poll);
308 $percent = sprintf("%d", $poll['data']['count'][$i][$j] * 100.0 / $poll['data']['all'][$i]);
309 return array($percent,$poll['data']['count'][$i][$j],$poll['data']['all'][$i]);
314 // $Log: not supported by cvs2svn $
315 // Revision 1.2 2004/02/24 03:21:46 rurban
316 // enabled require_all check in WikiPoll
317 // better handling of <20 min visiting client: display results so far
326 // c-hanging-comment-ender-p: nil
327 // indent-tabs-mode: nil