DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:vote

vote Plugin

Compatible with DokuWiki

No compatibility info given!

plugin Similar to poll plugin

Last updated on
2008-06-09
Provides
Syntax

This extension has not been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues.

Similar to doodle2, doodle3, doodle4, poll

Tagged with !experimental, poll

Syntax

Use this plugin to add a vote to a wiki page. The syntax looks like this:

<vote [title] [usercheck option]>
[question]
* [option]
* [option]
* ...
</vote>

That means, you can simply put <vote> tags around regular bulleted lists to get a radio button.

Example

No usercheck

No limit to vote.

<vote title>
question
* option A
* option B
* option C
</vote>

Usercheck with IP Address

User can vote only once.

<vote title check=ip>
question
* option A
* option B
* option C
</vote>

Usercheck with DokuWiki username

User can vote only once.

<vote title check=user>
question
* option A
* option B
* option C
</vote>

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.phpKaTe 21.06.2011 12:18

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 :




There's no “security token”, as DokuWiki calls it, to prevent CSRF attack. DokuWiki has functions to help against this security issue: getSecurityToken() and checkSecurityToken($token).

plugin/vote.txt · Last modified: 2023-12-21 15:42 by Aleksandr

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