====== vote plugin ====== ---- plugin ---- description: Similar to poll plugin(previous authors: Gina Häußge, Michael Klier) author : Norihiro Tobo email : norihiro.tobo@gmail.com type : syntax lastupdate : 2008-06-09 compatible : depends : conflicts : similar : poll 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 ---- ==Sample Image== {{http://half.luck.jp/toeic/vote-sample.png}} ===== Syntax ===== Use this [[plugins|plugin]] to add a vote to a wiki page. The syntax looks like this: [question] * [option] * [option] * ... That means, you can simply put ''%%%%'' tags around regular bulleted lists to get a radio button. ===== Example ===== == No usercheck == No limit to vote. question * option A * option B * option C == Usercheck with IP Address == User can vote only once. question * option A * option B * option C == Usercheck with DokuWiki username == User can vote only once. question * option A * option B * option C ----- ===== Discussion ===== \\ ---------------- \\ unfortunately the bars in my wiki are colorless, how can I change this? \\ ---------------- \\ in **lib/plugins/vote/style.css**, there is a line like following. >background-color: _missing_; Please try to change like... >>background-color: #00FF00; Or >>background-color: red; With your favorite color. It follows _ missing_ and _text_ of your template. (As configured in **lib/tpl/[YOUR TEMPLATE]/style.ini**) \\ ------ \\ Thank you very much, that solves the problem :-) \\ ------ \\ A good idea is to add a user limitation, if you have the time, check=user is for one vote, but if i want to purpose 3 vote per person check=user3 or check=3user not work. and a new vote in the same page don't work for multiple vote. But for it's good it doesn't allow a user to check twice the same answer or three ------ \\ How I can close the poll and removing the botton to vote? ------ \\ check=user will allow one anonymous vote. A dirty fix is to add to line 135 of syntax.php: if ($user == "") { print "Please log in first"; die; } 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// * @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( '.+?', $mode, 'plugin_vote' ); } function handle( $match, $state, $pos, &$handler ){ $data = $match; //$this->_debug_out( 'vote_debug', $data ); // Extract Title and Param preg_match( '/]/s', $data, $regx_result ); $title = htmlspecialchars( $regx_result[1] ); if( ! isset( $title ) ) { return NULL; } preg_match( '//s', $data, $regx_result ); $param = htmlspecialchars( $regx_result[1] ); preg_match( '/\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 .= '
'.''.$title.''; $output .= "\n"; if ( $question ) { $output .= '
'.$question.'

'; } $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 .= '
'; $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 = '
'. ''. ''; $ret .= "\n"; $ret .= ''; $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 .= ''; $ret .= ''. ''. ''; //Form $ret .= ''; $ret .= ''; $ret .= "\n"; } $ret .= "
'.$options[$i].'
'; if ( $percent ) { $ret .= '
 
'; } //Result $ret .= '
'.$percent.'%('.$absolute.')
\n"; if($vote_allowed) { $ret .= ''; } else { $ret .= "
"; } $ret .= "
\n
\n"; return $ret; } } // Class Definition //Setup VIM: ex: et ts=4 enc=utf-8 : \\ ------ \\ There's no "security token", as DokuWiki calls it, to prevent CSRF attack. DokuWiki has functions to help against this security issue: http://xref.dokuwiki.org/reference/dokuwiki/nav.html?inc/common.php.source.html#l62