Location: PHPKode > scripts > Poll Engine > polleng.php
<?php

/***************************************************************************/
/*  Author: Jure Merhar (hide@address.com)                         */
/*	Name: 	PollEng                                                        */
/* 	Desc: 	Poll engine.                                                   */
/*                                                                         */
/* !! IMPORTANT !!                                                         */
/*                                                                         */
/*  This class requires the ezSQL class by Justin Vincent. You can get it  */
/*  here: http://www.jvmultimedia.com/home/articles.php?articleId=2        */
/*                                                                         */
/*  It also requires the Template Engine class by Jonas Lasauskas. Get it  */
/*  here: http://www.phpclasses.org/browse/package/1216.html               */
/*  (the site requires a free registration)                                */
/***************************************************************************/

class PollEng {

	/*************************/
	/*     MAIN SETTINGS     */
	/*************************/
	
	/* The name of the template file that contains the poll blocks */
	var $pollTemplate = 'poll.tpl';

	/* The name of the polls table in database */
	var $pollsTable = 'polls';

	/* The name of the answers table in database */
	var $answersTable = 'answers';

	/* The names of the fields of the polls table in database */
	var $pollsFields = array('id' => 'id', 'question' => 'question');

	/* The names of the fields of the answers table in database */
	var $answersFields = array('id' => 'id', 'value' => 'value', 'score' => 'score', 'poll_id' => 'poll_id');
	
	/*****************************/
	/*     PRIVATE VARIABLES     */
	/*****************************/
	var $db;
	var $pollId;
	var $pollQuestion;
	var $pollAnswers = array();
	
	/**************************************/
	/*  Method: constructor               */
	/*  Desc: Instantiates the class      */
	/*  Params:                           */
	/*    $db - instantiated ezSQL class  */
	/*  Return value: class instance      */
	/**************************************/
	function PollEng($db)
	{
		$this->db = $db;
	}
	
	/*************************************************/
	/*  Method: getPoll()                            */
	/*  Desc: Reads a poll from the database         */
	/*  Params:                                      */
	/*    $pollWidth - max width of the results bar  */
	/*    $nr - poll number starting from last poll  */
	/*  Return value: none                           */
	/*************************************************/
	function getPoll($pollWidth, $nr = 1)
	{
		$nr--;
		$poll = $this->db->get_row("SELECT * FROM $this->pollsTable ORDER BY id DESC LIMIT $nr, 1", ARRAY_A);
		$this->pollId = $poll[$this->pollsFields['id']];
		$this->pollQuestion = htmlspecialchars($poll[$this->pollsFields['question']]);
		$this->pollAnswers = $this->db->get_results("SELECT " . $this->answersFields['id'] . " AS id, " . $this->answersFields['value'] . " AS value, " . $this->answersFields['score'] . " AS count FROM $this->answersTable WHERE " . $this->answersFields['poll_id'] . " = $this->pollId ORDER BY " . $this->answersFields['id'], ARRAY_A);
		$pollSum = $this->db->get_var("SELECT SUM(" . $this->answersFields['score'] . ") FROM $this->answersTable WHERE " . $this->answersFields['poll_id'] . " = $this->pollId");
		foreach($this->pollAnswers as $key => $pollAnswer) {
			$pollAnswer['value'] = htmlspecialchars($pollAnswer['value']);
			$pollAnswer['percent'] = round(100 * $pollAnswer['count'] / $pollSum);
			$pollAnswer['width'] = round($pollAnswer['percent'] * $pollWidth / 100);
			$this->pollAnswers[$key] = $pollAnswer;
		}
	}
	
	/**************************************************************/
	/*  Method: assignPoll()                                      */
	/*  Desc: Assigns the poll block to a template variable       */
	/*  Params:                                                   */
	/*    $tpl - instantiated template engine class               */
	/*    $varName - the name of the variable to assign to        */
	/*    $showResults - if true, the results will be shown,      */
	/*                   else the voting form will be shown       */
	/*    $formAction - the URL to go to after voting             */
	/*    $formMethod - selects method ('post' or 'get')          */
	/*    $buttonValue - the text displayed on the 'vote' button  */
	/*    $buttonName - the name of the 'vote' button             */
	/*    $formName - the name of the form                        */
	/*  Return value: none                                        */
	/**************************************************************/
	function assignPoll(&$tpl, $varName, $showResults = false, $formAction = '', $formMethod = 'post', $buttonValue = 'Vote', $buttonName = 'submit_vote', $formName = 'pollform')
	{
		if (empty($formAction)) $formAction = $_SERVER['PHP_SELF'];
		$tpl->set_file('poll_blocks', $this->pollTemplate);
		if ($showResults) {
			$tpl->set_var('poll_results', array('question' => $this->pollQuestion));
			$tpl->set_loop('results_answer', $this->pollAnswers);
			$tpl->set_block($varName, 'poll_results');
		} else {
			$tpl->set_var('poll_vote', array('form_name' => $formName,
																			 'form_action' => $formAction,
																			 'form_method' => $formMethod,
																			 'question' => $this->pollQuestion,
																			 'button_name' => $buttonName,
																			 'button_value' => $buttonValue)
																			 );
			$tpl->set_loop('vote_answer', $this->pollAnswers);
			$tpl->set_block($varName, 'poll_vote');
		}
	}
	
	/**************************************************/
	/*  Method: checkVote()                           */
	/*  Desc: Checks whether a vote was submitted     */
	/*  Params:                                       */
	/*    $method - selects method ('post' or 'get')  */
	/*  Return value: poll number or false            */
	/**************************************************/
	function checkVote($method = 'post')
	{
		if ($method == 'post') {
			$vars = $_POST;
		} else {
			$vars = $_GET;
		}
		if (isset($vars['answer'])) {
			$answerId = $vars['answer'];
			$this->db->query("UPDATE $this->answersTable SET " . $this->answersFields['score'] . "=" . $this->answersFields['score'] . "+1 WHERE " . $this->answersFields['id'] . "=$answerId");
			$pollId = $this->db->get_var("SELECT " . $this->answersFields['poll_id'] . " FROM $this->answersTable WHERE " . $this->answersFields['id'] . "=$answerId LIMIT 1");
			return $this->db->get_var("SELECT COUNT(*) FROM $this->pollsTable WHERE " . $this->pollsFields['id'] . ">=$pollId");
		} else {
			return false;
		}
	}

}

?>
Return current item: Poll Engine