Location: PHPKode > projects > Content*Builder > contentbuilder/lib/patPollsRenderer.php
<?php

/**
* patPolls renderer base class - used as base module for your own rendering extensions. The basic poll renderer is the patPollsBarRenderer
* and is really quite simple. There is an amount of functions that you can use to make retrieving poll data easy so you can process it as you like.
*
* @package		patPolls
* @access		public	
* @author		Stephan Schmidt <hide@address.com>, Sebastian Mordziol <hide@address.com>
* @version		0.01
*/
class	patPollsRenderer
{
	var $poll;
	var	$options	=	array();
/**
* acquire reference to poll object
*
* @access		private
* @param		object patPoll &$patPoll the reference to the patPoll object
*/
	function	setPollReference( &$patPoll )
	{
		$this->poll	=	&$patPoll;
	}

/**
* set a patPoll Option
*
* @access		public
* @param		string	$option		option to be set
* @param		mixed	$value		value of the option
*/
	function	setOption( $option, $value )
	{
		$this->options[$option]		=	$value;
	}

/**
* get a patPoll Option
*
* @access		public
* @param		string	$option		option to be set
* @return		mixed	$value		value of the option
*/
	function	getOption( $option )
	{
		return	$this->options[$option];
	}
	
/**
* render and return the poll results
*
* @access		private
*/
	function	start()
	{
		$this->fetchData();
		return	$this->render();
	}

/**
* fetch all data from patPoll object
*
* @access		private
*/
	function	fetchData()
	{
		$this->querydata	=	$this->poll->getQueryData();
		$this->answerdata	=	$this->poll->getAnswerData();
		$this->writeindata	=	$this->poll->getWriteinData();
	}

/**
* get the total amount of users for this poll
*
* @access		public
* @return		int	$users	amount of users
*/
	function	getTotalUsers()
	{
		return	$this->querydata[users];
	}
								
/**
* get the total amount of votes for default answers
*
* @access		public
* @return		int	$votes	amount of votes for default answers
*/
	function	getAnswerVotes()
	{
		$votes	=	0;
		for( $i=0; $i<count( $this->answerdata ); $i++ )
			$votes	=	$votes + $this->answerdata[$i][count];

		return	$votes;
	}
								
/**
* get the total amount of votes for writeins
*
* @access		public
* @return		int	$votes	amount of votes for writeins
*/
	function	getWriteinVotes()
	{
		$votes	=	0;
		for( $i=0; $i<count( $this->writeindata ); $i++ )
			$votes	=	$votes + $this->writeindata[$i][count];

		return	$votes;
	}
								
/**
* get the total amount of votes
*
* @access		public
* @return		int	$votes	total amount of votes
*/
	function	getTotalVotes()
	{
		return	$this->getAnswerVotes() + $this->getWriteinVotes();
	}
								
/**
* calculate percentage for all votes and store them in the appropriate array
*
* @access		public
*/
	function	calculatePercentages()
	{
		$sum	=	$this->getTotalVotes();

		for( $i=0; $i<count( $this->answerdata ); $i++ )
			$this->answerdata[$i][percent]	=	$this->answerdata[$i][count] / $sum;

		for( $i=0; $i<count( $this->writeindata ); $i++ )
			$this->writeindata[$i][percent]	=	$this->writeindata[$i][count] / $sum;
	}

/**
* get the percentage of default answers in relation to all votes
*
* @access		public
* @return		int	$percentage	percentage of default answers in relation to all votes
*/
	function	getAnswersPercentage()
	{
		return	$this->getAnswerVotes() / $this->getTotalVotes();
	}								

/**
* get the percentage of writeins in relation to all votes
*
* @access		public
* @return		int	$percentage	percentage of writeins in relation to all votes
*/
	function	getWriteinsPercentage()
	{
		return	$this->getWriteinVotes() / $this->getTotalVotes();
	}								

/**
* get the answer which has gotten the most votes, and return that value
*
* @access		public
* @return		int	$maxvalue	the amount of votes for the most answers.
*/
	function	getMaxAnswerVotes()
	{
		$maxvalue	=	0;
		for( $i=0; $i<count( $this->answerdata ); $i++ )
			if( $maxvalue < $this->answerdata[$i][count] )
				$maxvalue	=	$this->answerdata[$i][count];
		
		return $maxvalue;
	}								

/**
* get the writein which has gotten the most votes, and return that value
*
* @access		public
* @return		int	$maxvalue	the amount of votes for the most used writein.
*/
	function	getMaxWriteinVotes()
	{
		$maxvalue	=	0;
		for( $i=0; $i<count( $this->writeindata ); $i++ )
			if( $maxvalue < $this->writeindata[$i][count] )
				$maxvalue	=	$this->writeindata[$i][count];
				
		return	$maxvalue;
	}								


/**
* get the answer that has gotten the most votes regardless of wether it is a writein or normal answer.
*
* @access		public
* @return		int	$max	the amount of votes for the most voted answer or writein.
*/
	function	getMaxVotes()
	{
		$answervotes	=	$this->getMaxAnswerVotes();
		$writeinvotes	=	$this->getMaxWriteinVotes();

		$max			=	$answervotes < $writeinvotes ? $writeinvotes : $answervotes;
		
		return $max;
	}								
	
	
/**
* return the rendered html of the result - overwrite this function to start rendering the poll results using your own template(s).
*
* @access		public
* @return		int	$html	rendered html
*/
	function	render()
	{
		return	true;
	}
}
?>
Return current item: Content*Builder