DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:vote

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
plugin:vote [2010-10-03 15:40] – [vote plugin] HåkanSplugin:vote [2023-12-21 15:42] (current) – new download url Aleksandr
Line 1: Line 1:
-====== vote plugin ======+====== vote Plugin ======
  
 ---- plugin ---- ---- plugin ----
-description: Similar to poll plugin(previous authors: Gina Häußge, Michael Klier)+description: Similar to poll plugin #(previous authors: Gina Häußge, Michael Klier)
 author     : Norihiro Tobo author     : Norihiro Tobo
 email      : norihiro.tobo@gmail.com email      : norihiro.tobo@gmail.com
Line 13: Line 13:
 tags       : poll, !experimental tags       : poll, !experimental
  
-downloadurl: http://half.luck.jp/toeic/lib/exe/fetch.php?id=dokuwiki_original_tools&cache=cache&media=vote-1.09.tar.gz+downloadurl: https://trello.com/1/cards/5b06b6b2ee523a9b01e2fa1f/attachments/5b06b83df286afab5d52fa3f/download/vote-1.09.zip 
 +bugtracker :  
 +sourcerepo :  
 +donationurl:  
 + 
 +screenshot_img: https://trello.com/1/cards/5b06b6b2ee523a9b01e2fa1f/attachments/5b06b6f383e588976dc38b8d/download/vote-sample.png
 ---- ----
- 
-==Sample Image== 
- 
-{{http://half.luck.jp/toeic/vote-sample.png}} 
  
 ===== Syntax ===== ===== Syntax =====
  
-Use this [[plugins|plugin]] to add a vote to a wiki page. The syntax looks like this:+Use this plugin to add a vote to a wiki page. The syntax looks like this:
  
   <vote [title] [usercheck option]>   <vote [title] [usercheck option]>
Line 35: Line 36:
 ===== Example ===== ===== Example =====
  
-== No usercheck ==+==== No usercheck ==== 
 No limit to vote. No limit to vote.
-<code text> 
-<vote title> 
-question 
-* option A 
-* option B 
-* option C 
-</vote> 
-</code> 
  
-== Usercheck with IP Address ==+  <vote title> 
 +  question 
 +  * option A 
 +  * option B 
 +  * option C 
 +  </vote> 
 + 
 +==== Usercheck with IP Address ==== 
 User can vote only once. User can vote only once.
-<code text> 
-<vote title check=ip> 
-question 
-* option A 
-* option B 
-* option C 
-</vote> 
-</code> 
  
-== Usercheck with DokuWiki username ==+  <vote title check=ip> 
 +  question 
 +  * option A 
 +  * option B 
 +  * option C 
 +  </vote> 
 + 
 +==== Usercheck with DokuWiki username ==== 
 User can vote only once. User can vote only once.
-<code text> 
-<vote title check=user> 
-question 
-* option A 
-* option B 
-* option C 
-</vote> 
-</code> 
  
------+  <vote title check=user> 
 +  question 
 +  * option A 
 +  * option B 
 +  * option C 
 +  </vote> 
  
 ===== Discussion =====  ===== Discussion ===== 
Line 116: Line 116:
 Somebody with the skills should make a prettier fix. Somebody with the skills should make a prettier fix.
  
 +-----
 +\\
 +Here comes a suggestion how to remove the 'Vote'-button (and also the radiobuttons) if 
 +  * the user is not logged in (is 'anonymous') or
 +  * the user is logged in, but has already voted
  
 +Needs to edit **lib/plugins/vote/syntax.php**  --- [[mailto:kate-web@arcor.de|KaTe]] //21.06.2011 12:18//
 +<file php syntax.php>
 +<?php
 +/**
 + * Vote Plugin: allows to create simple votes
 + *
 + * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
 + * @author Esther Brunner <wikidesign@gmail.com>
 + * @author Gina Häußge, Michael Klier
 + * @author Norihiro Tobo
 + */
 + 
 +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');
 + 
 + 
 +class syntax_plugin_vote extends DokuWiki_Syntax_Plugin {
 + 
 + 
 +function getInfo(){
 + return confToHash(dirname(__FILE__).'/INFO');
 +}
 +function getType(){ return 'substition'; }
 +function getPType(){ return 'block'; }
 +function getSort(){ return 168; }
 +function connectTo( $mode ) {
 + $this->Lexer->addSpecialPattern( '<vote.*?>.+?</vote>',  $mode, 'plugin_vote' );
 +}
 + 
 + 
 + 
 +function handle( $match, $state, $pos, &$handler ){
 + 
 + $data = $match; 
 + //$this->_debug_out( 'vote_debug', $data );
 + 
 + // Extract Title and Param
 + preg_match( '/<vote\s+(.*?)[\s>]/s', $data, $regx_result );
 + $title = htmlspecialchars( $regx_result[1] );
 + if( ! isset( $title ) ) { return NULL; }
 +
 + preg_match( '/<vote\s+.*?\s+(.*?)>/s', $data, $regx_result );
 + $param = htmlspecialchars( $regx_result[1] );
 +
 + preg_match( '/<vote.*?>\n(.*?)\n\*/s', $data, $regx_result );
 + $question = htmlspecialchars( $regx_result[1] );
 +
 + //$this->_debug_out( 'vote_debug1', "\n:$title:$param:$question:\n" );
 +
 +
 + // Extract options
 + $data = strip_tags( $data );
 + preg_match_all( '/^\*\s*(.*?)\n/m', $data, $regex_result );
 + foreach( $regex_result[1] as $option ) {
 + $options[] = htmlspecialchars( trim( $option ) );
 + }
 + 
 + $this->_create_vote_log_file( $title, $options );
 + 
 + return array( $title, $param, $question, $options );
 +}
 + 
 + 
 + 
 +function render( $mode, &$renderer, $data ) {
 + 
 + 
 + $ouotput = "";
 + if ( $mode != 'xhtml' ){ return FALSE; }
 + 
 + // Parse data
 + list( $title, $param, $question, $options ) = $data;
 + $renderer->info['cache'] = false;
 + $vote_log =  $this->_read_vote_log( $title );
 + 
 + //Print header parts
 + $output .= '<fieldset class="vote">'.'<legend>'.$title.'</legend>';
 + $output .= "\n";
 + if ( $question ) {
 + $output .= '<div>'.$question.'<br /><br /></div>';
 + }
 +  $output .= "\n";
 +
 + $vote_allowed = $this->_user_check( $vote_log, $param );
 + //Update vote log
 + if ( $_REQUEST['vote'] && $vote_allowed ){
 +
 + $vote = $_REQUEST['vote'];
 +
 +    foreach( $options as $option ) {
 +
 + if ( $vote == $option ) { 
 + $vote_log[ 'results' ][ $option ] += 1;
 + $vote_log[ 'votes' ] += 1;
 + $vote_log[ 'ips' ][] = clientIP( true );
 + global $USERINFO;
 + $vote_log[ 'users' ][] = $USERINFO['name'];
 + $vote_allowed = false;
 + }
 + }
 + 
 + $this->_write_vote_log( $title, $vote_log );
 + }
 + 
 + // display vote form
 + $output .= $this->_print_vote_form( $vote_log, $vote_allowed );
 + $output .= '</fieldset>';
 + $output .= "\n";
 +
 + $renderer->doc .= $output;
 + return true;
 + 
 +}
 + 
 + 
 + 
 +function _user_check( $vote_log, $param ) {
 + 
 + if( preg_match( '/check=(\w+)/s', $param, $regex_result )  > 0 ) {
 + $check = $regex_result[1];
 + } else {
 + return TRUE;
 + }
 +
 + switch( $check ) {
 +
 + case "ip":
 + $ip = clientIP( true );
 + if ( isset( $vote_log['ips'] ) && in_array( $ip, $vote_log['ips'] ) ) {
 + return FALSE;
 + }
 + break;
 +
 + case "user":
 + global $USERINFO; 
 + $user = $USERINFO['name'];
 + if ( ( isset( $vote_log['users'] ) && in_array( $user, $vote_log['users'] ) ) || $user == '') {
 + return FALSE;
 + }
 + break;
 + }
 +
 + return TRUE;
 + 
 +}
 + 
 + 
 + 
 +function _create_vote_log_file( $title, $options ) {
 + 
 + $vote_log_file = metaFN( md5( $title ), '.vote' );
 + 
 + if( file_exists( $vote_log_file ) ){ return TRUE; }
 + 
 + foreach( $options as $option ) {
 + $vote_skelton[ 'results' ][ $option ] = 0;
 + }
 + $vote_skelton[ 'votes' ] = 0; 
 + 
 + $fh = fopen( $vote_log_file, 'w' );
 + fwrite( $fh, serialize( $vote_skelton ) );
 + fclose( $fh );
 + 
 + return TRUE;
 + 
 +}
 + 
 +
 +function _debug_out( $title, $data ) {
 + 
 + $file = metaFN( $title, '.dbg' );
 + $fh = fopen( $file, 'a' );
 + fwrite( $fh, $data );
 + fclose( $fh );
 + 
 + return TRUE;
 + 
 +}
 + 
 + 
 +function _read_vote_log( $title ) {
 + 
 + $vote_log = NULL;
 + 
 + $vote_log_file = metaFN( md5( $title ), '.vote' );
 + $vote_log  = unserialize( @file_get_contents( $vote_log_file ) );
 + 
 + return $vote_log;
 +}
 + 
 + 
 + 
 +function _write_vote_log( $title, $vote_log ) {
 + 
 + $vote_log_file = metaFN( md5( $title ), '.vote' );
 + 
 + $fh = fopen( $vote_log_file, 'w' );
 + fwrite( $fh, serialize( $vote_log ) );
 + fclose( $fh);
 + 
 + return TRUE;
 +}
 + 
 + 
 + 
 +function _print_vote_form( $vote_log, $vote_allowed ){
 + 
 + global $lang;
 + global $ID;
 +
 + $total = $vote_log['votes'];
 + if ( $total < 0 ) { return ''; }
 +
 + $option_count = count( $vote_log['results'] );
 + $options = array_keys( (array)( $vote_log[ 'results' ] ) );
 + $votes   = array_values( (array)( $vote_log[ 'results' ] ) );
 +
 + $ret = '<form id="vote__form" method="post" action="'.script().
 + '" accept-charset="'.$lang[ 'encoding' ].'"><div align="center" class="no">'.
 + '<input type="hidden" name="do" value="show" />'.
 + '<input type="hidden" name="id" value="'.$ID.'" />';
 + $ret .= "\n";
 +
 + $ret .= '<table class="blind" align="center">';
 + $ret .= "\n";
 +
 +
 + for ( $i = 0; $i < $option_count; $i++ ) {
 +
 + $absolute = $votes[ $i ];
 + if( $total == 0 ) {
 + $percent = 0;
 + } else {
 + $percent  = round( ( $absolute * 100 ) / $total );
 + }
 +
 + $ret .= "\t";
 + $ret .= '<tr><td align="left" colspan="3">'.$options[$i].'</td></tr>';
 + $ret .= '<tr><td><div class="vote_bar">';
 +
 + if ( $percent ) {
 + $ret .= '<div class="vote_full" style="width:'.( $percent * 2 ).'px">&nbsp;</div>';
 + }
 + //Result
 + $ret .= '</div></td>'.
 + '<td class="rightalign">'.$percent.'%</td>'.
 + '<td class="rightalign">('.$absolute.')</td>';
 + //Form
 + $ret .= '<td class="rightalign"><label class="simple" for="vote__option'.$i.'">'.
 + '<input type="radio" name="vote" id="vote__option'.$i.'"'.($vote_allowed ? ' ':' style=" visibility:hidden "').
 + 'value="'.$options[$i].'" /></label></td>';
 + $ret .= '</tr>';
 + $ret .= "\n";
 +
 + }
 + 
 + $ret .= "</table>\n"; 
 + if($vote_allowed) {
 + $ret .= '<input class="button" type="submit" '.
 + 'value="'.$this->getLang( 'btn_vote' ).'" />';
 + }
 + else {
 +   $ret .= "<br />";
 + }
 + $ret .= "</div>\n</form>\n";
 +
 + 
 + return $ret;
 +}
 + 
 +} // Class Definition
 + 
 +//Setup VIM: ex: et ts=4 enc=utf-8 :
 +</file>
 +
 +\\
 +------
 +\\
 +There's no "security token", as DokuWiki calls it, to prevent CSRF attack. DokuWiki has functions to help against this security issue: [[xref>getSecurityToken()]] and [[xref>checkSecurityToken($token)]].
plugin/vote.1286113259.txt.gz · Last modified: 2010-10-03 15:40 by HåkanS

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki