diff --git a/syntax.php b/syntax.php index ff8c2c8..e85f72d 100644 --- a/syntax.php +++ b/syntax.php @@ -15,159 +15,158 @@ * need to inherit from this class */ class syntax_plugin_poll extends DokuWiki_Syntax_Plugin { - - /** - * return some info - */ - function getInfo(){ - return confToHash(dirname(__FILE__).'/INFO'); - } - - function getType(){ return 'substition';} - function getPType(){ return 'block';} - function getSort(){ return 167; } - - /** - * Connect pattern to lexer - */ - function connectTo($mode){ - $this->Lexer->addSpecialPattern('.+?', $mode, 'plugin_poll'); - } - - /** - * Handle the match - */ - function handle($match, $state, $pos, &$handler){ - $match = substr($match, 6, -7); // strip markup - list($title, $options) = preg_split('/>/u', $match, 2); - if (!$options){ - $options = $title; - $title = NULL; + + /** + * return some info + */ + function getInfo() { + return confToHash(dirname(__FILE__).'/INFO'); } - $options = explode('*', $options); - - $c = count($options); - for ($i = 0; $i < $c; $i++){ - $options[$i] = trim($options[$i]); + + function getType() { return 'substition';} + function getPType() { return 'block';} + function getSort() { return 167; } + + /** + * Connect pattern to lexer + */ + function connectTo($mode) { + $this->Lexer->addSpecialPattern('.+?', $mode, 'plugin_poll'); } - - return array(trim($title), $options); - } - - /** - * Create output - */ - function render($mode, &$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 .= '
'. - ''.$title.''; - $more = trim(array_shift($options)); - if ($more){ - $renderer->doc .= '
'.$renderer->_xmlEntities($more).'
'; - } - - // 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 + + /** + * Handle the match + */ + function handle($match, $state, $pos, &$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++){ - $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; - } + for ($i = 0; $i < $c; $i++) { + $options[$i] = trim($options[$i]); } - $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 .= '
'; - - return true; + + return array(trim($title), $options); } - return false; - } - - function _pollResults($poll){ - $total = $poll['votes']; - if ($total == 0) return ''; - - $ret = ''; - $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 .= ''. - ''; + + /** + * Create output + */ + function render($mode, &$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 .= '
'. + ''.$title.''; + $more = trim(array_shift($options)); + if ($more) { + $renderer->doc .= '
'.$renderer->_xmlEntities($more).'
'; + } + + // 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 .= '
'; + + return true; + } + return false; } - $ret .= '
'.$options[$i].'
'; - if ($percent) $ret .= '
 
'; - $ret .= '
'.$percent.'%('.$absolute.')
'; - - return $ret; - } - - function _pollForm($options, &$renderer){ - global $lang; - global $ID; - - $i = 0; - $ret = '
'. - ''. - ''; - foreach ($options as $option){ - $i++; - $option = $renderer->_xmlEntities($option); - $ret.= ''; + + function _pollResults($poll) { + $total = $poll['votes']; + if ($total == 0) return ''; + + $ret = ''; + $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 .= ''. + ''; + } + $ret .= '
'.$options[$i].'
'; + if ($percent) $ret .= '
 
'; + $ret .= '
'.$percent.'%('.$absolute.')
'; + + return $ret; + } + + function _pollForm($options, &$renderer) { + global $lang; + global $ID; + + $i = 0; + $ret = '
'. + ''. + ''; + foreach ($options as $option) { + $i++; + $option = $renderer->_xmlEntities($option); + $ret.= ''; + } + $ret .= ''. + '
'; + + return $ret; } - $ret .= ''. - '
'; - - return $ret; - } } - -//Setup VIM: ex: et ts=4 enc=utf-8 : +//vim:ts=4:sw=4:et:enc=utf-8: