<?php
/* =============================================================== *\
|		Module name:      Poll										|
|																	|
\* =============================================================== */

if (!defined('IN_SITE')){
     die('Hacking attempt!');
}
//Module language
//$Func->import_module_language("home/lang_poll". PHP_EX);

$Poll = new Poll;

class Poll
{
	function Poll(){
		global $Func, $Template, $Lang;

		$act	= $Func->get_vars('act', 1);
		$Template->set_vars(array(
			'BROWSER_NAVIGATOR'	=> ' - '. $Lang->data['poll'],
		));

		switch ($act){
			case "vote":
				$this->vote_poll();
				break;
			default:
				$this->view_poll();
		}
	}

	function view_poll(){
		global $DB, $Info, $Template, $Lang, $Func;

		$id		= intval($Func->get_vars('id', 2));

		$Info->tpl_header		= "";
		$Info->tpl_footer		= "";
		$Info->tpl_main			= "poll_view";

		//Get poll
		$DB->query("SELECT * FROM ". $DB->prefix ."poll WHERE poll_id=$id");
		if ( !$DB->num_rows() ){
			$Template->message_die($Lang->data['poll_not_exist']);
			return false;
		}
		$poll_info	= $DB->fetch_array();
		$DB->free_result();

		//Get poll option
		$DB->query("SELECT * FROM ". $DB->prefix ."poll_option WHERE poll_id=$id");
		$option_count		= $DB->num_rows();
		$option_data		= $DB->fetch_all_array();
		$DB->free_result();

		$total_hits = 0;
		for ($i=0; $i<$option_count; $i++){
			$total_hits += $option_data[$i]['option_hits'];
		}

		for ($i=0; $i<$option_count; $i++){
			$percent	= !$total_hits ? '0%' : round(($option_data[$i]['option_hits'] * 100) / $total_hits,1) .'%';
			$Template->set_block_vars("optionrow", array(
				"TITLE"			=> $option_data[$i]["option_title"],
				"HITS"			=> $option_data[$i]["option_hits"],
				"PERCENT"		=> $percent,
			));
		}

		$Template->set_vars(array(
			"QUESTION"				=> $poll_info['question'],
			"TOTAL_HITS"			=> $total_hits,
			"L_TOTAL_HITS"			=> $Lang->data["poll_hits"],
		));
		return true;
	}

	function vote_poll(){
		global $DB, $Template, $Lang, $Info, $Func;

		$id		= intval($Func->get_vars('id', 2));

		$optionid	= isset($_POST['optionid']) ? $_POST['optionid'] : 0;

		//Check poll
		$DB->query('SELECT * FROM '. $DB->prefix .'poll WHERE enabled='. SYS_ENABLED .' AND start_date<='. CURRENT_TIME .' AND (end_date=0 OR end_date>'. CURRENT_TIME .') AND poll_id='. $id);
		if ( !$DB->num_rows() ){
			$Template->message_die($Lang->data['poll_error_not_exist']);
			return false;
		}
		$poll_info		= $DB->fetch_array();

		//Check user has voted
		$DB->query("SELECT hit_time FROM ". $DB->prefix ."poll_ip WHERE poll_id=$id AND client_ip='". $Info->client_ip ."' AND hit_time>=". (CURRENT_TIME - $Info->option['time_revote']));
		if ( !$DB->num_rows() ){
			if ( $poll_info['multiple'] ){
				$option_id		= $optionid;
			}
			else{
				$option_id[]	= $optionid;
			}
			unset($optionid);

			$where_sql		= " WHERE poll_id=$id AND (option_id=0";
			reset($option_id);
			while (list(, $oid) = each($option_id)){
				$where_sql	.= " OR option_id=". intval($oid);
			}
			$where_sql		.= ")";

			//Update hits
			$DB->query('UPDATE '. $DB->prefix .'poll_option SET option_hits=option_hits+1 '. $where_sql);
			$DB->query('UPDATE '. $DB->prefix .'poll SET total_hits=total_hits+1 WHERE poll_id='. $id);

			//Delete expired vote time
			$DB->query("DELETE FROM ". $DB->prefix ."poll_ip WHERE hit_time<". ( CURRENT_TIME - $Info->option['time_revote']) );

			//Insert vote time
			$DB->query("INSERT INTO ". $DB->prefix ."poll_ip(poll_id, client_ip, hit_time) VALUES($id, '". $Info->client_ip ."', ". CURRENT_TIME .")");
		}

		$this->view_poll();
		return true;
	}
}

?>