-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsyntax.php
165 lines (135 loc) · 5.28 KB
/
syntax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* Poll Plugin: allows to create simple polls
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Esther Brunner <[email protected]>
*/
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
/**
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class
*/
class syntax_plugin_poll extends DokuWiki_Syntax_Plugin {
function getType() { return 'substition';}
function getPType() { return 'block';}
function getSort() { return 167; }
/**
* Connect pattern to lexer
*/
function connectTo($mode) {
$this->Lexer->addSpecialPattern('<poll.*?>.+?</poll>', $mode, 'plugin_poll');
}
/**
* Handle the match
*/
function handle($match, $state, $pos, Doku_Handler $handler) {
$match = substr($match, 6, -7); // strip markup
list($title, $options) = preg_split('/>/u', $match, 2);
if (!$options) {
$options = $title;
$title = NULL;
}
$options = explode('*', $options);
$c = count($options);
for ($i = 0; $i < $c; $i++) {
$options[$i] = trim($options[$i]);
}
return array(trim($title), $options);
}
/**
* Create output
*/
function render($mode, Doku_Renderer $renderer, $data) {
if ($mode == 'xhtml') {
global $ID;
$options = $data[1];
$title = $renderer->_xmlEntities($data[0]);
// prevent caching to ensure the poll results are fresh
$renderer->info['cache'] = false;
// get poll file contents
$pfile = metaFN(md5($title), '.poll');
$poll = unserialize(@file_get_contents($pfile));
// output the poll
$renderer->doc .= '<fieldset class="poll">'.
'<legend>'.$title.'</legend>';
$more = trim(array_shift($options));
if ($more) {
$renderer->doc .= '<div>'.$renderer->_xmlEntities($more).'</div>';
}
// check if user has voted already
$ip = clientIP(true);
if (isset($poll['ips']) && in_array($ip, $poll['ips'])) {
// display results
$renderer->doc .= $this->_pollResults($poll);
} elseif ($vote = $_REQUEST['vote']) {
// user has just voted -> update results
$c = count($options);
for ($i = 0; $i < $c; $i++) {
$opt = $renderer->_xmlEntities($options[$i]);
if ($vote == $opt) {
$poll['results'][$opt] += 1;
$poll['votes'] += 1;
$poll['ips'][] = $ip;
} elseif (!isset($poll['results'][$opt])) {
$poll['results'][$opt] = 0;
}
}
$fh = fopen($pfile, 'w');
fwrite($fh, serialize($poll));
fclose($fh);
// display results
$renderer->doc .= $this->_pollResults($poll);
} elseif (count($options) > 0) {
// display poll form
$renderer->doc .= $this->_pollForm($options, $renderer);
} else {
// display results
$renderer->doc .= $this->_pollResults($poll);
}
$renderer->doc .= '</fieldset>';
return true;
}
return false;
}
function _pollResults($poll) {
$total = $poll['votes'];
if ($total == 0) return '';
$ret = '<table class="blind">';
$c = count($poll['results']);
$options = array_keys($poll['results']);
$votes = array_values($poll['results']);
for ($i = 0; $i < $c; $i++) {
$absolute = $votes[$i];
$percent = round(($absolute*100)/$total);
$ret .= '<tr><td>'.$options[$i].'</td><td><div class="poll_bar">';
if ($percent) $ret .= '<div class="poll_full" style="width:'.($percent*2).'px"> </div>';
$ret .= '</div></td><td class="rightalign">'.$percent.'%</td>'.
'<td class="rightalign">('.$absolute.')</td></tr>';
}
$ret .= '</table>';
return $ret;
}
function _pollForm($options, &$renderer) {
global $lang;
global $ID;
$i = 0;
$ret = '<form id="poll__form" method="post" action="'.script().'" accept-charset="'.$lang['encoding'].'"><div class="no">'.
'<input type="hidden" name="do" value="show" />'.
'<input type="hidden" name="id" value="'.$ID.'" />';
foreach ($options as $option) {
$i++;
$option = $renderer->_xmlEntities($option);
$ret.= '<label class="simple" for="poll__option'.$i.'">'.
'<input type="radio" name="vote" id="poll__option'.$i.'" '.
'value="'.$option.'" /> <span>'.$option.'</span></label>';
}
$ret .= '<input class="button" type="submit" '.
'value="'.$this->getLang('btn_vote').'" />'.
'</div></form>';
return $ret;
}
}
// vim:ts=4:sw=4:et:enc=utf-8: