<?php
class ControllerModuleArticlePoll extends Controller {
	protected function index($setting){
		if($this->config->get('config_news_installed')==1){
			
		
		if(VERSION=='1.5.5'||VERSION=='1.5.5.1'){
			$this->document->addScript('catalog/view/javascript/jquery/colorbox/jquery.colorbox-min.js');
			$this->document->addStyle('catalog/view/javascript/jquery/colorbox/colorbox.css');			
		}	
		static $module = 0;
		$this->language->load('news/poll');
			$this->load->model('news/poll');
			$this->data['heading_title']=$this->language->get('heading_title');
			$this->data['text_vote']=$this->language->get('text_vote');
			$this->data['text_total_votes']=$this->language->get('text_total_votes');
			$this->data['text_no_votes']=$this->language->get('text_no_votes');
			$this->data['text_no_poll']=$this->language->get('text_no_poll');
			$this->data['poll_id']=$setting['poll_id'];
			
		if($setting['poll_id']){ 
		$poll_id=$setting['poll_id'];				
		}
		else{ 
		$poll_id=0;
		}

		if(isset($this->request->cookie['poll_answered'.$setting['poll_id']])){
			if($this->request->cookie['poll_answered'.$setting['poll_id']]==$poll_id){ 
			$this->data['answered']=TRUE;
			}
		}
		$this->data['poll_id']=$poll_id;
			$this->data['poll_data']=$this->model_news_poll->getPollData($poll_id);
			$this->data['text_poll_results']=$this->language->get('text_poll_results');
			$this->data['poll_results']=$this->url->link('news/poll&poll_id='.$setting['poll_id']);
			$this->data['action']=$this->url->link('news/poll');
			$reactions=$this->model_news_poll->getPollResults($poll_id);
			$total_votes=$this->model_news_poll->getTotalResults($poll_id);
				if($reactions){ 
		$this->data['reactions']=TRUE;
			$percent=array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
			$totals=array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
		foreach($reactions as $reaction){ 
		$totals[$reaction['answer'] - 1]++;
			}

			for($i=0;
			$i < 15;
			$i++){ 
		$percent[$i]=round(100 * ($totals[$i]/$total_votes));
		$vote[$i]=$totals[$i];
			}
		$this->data['percent']=$percent;
		$this->data['vote']=$vote;
			$this->data['total_votes']=$total_votes;
			$this->data['text_percent']=$this->language->get('text_percent');
			$this->data['text_answer']=$this->language->get('text_answer');
		}
		
		$this->data['module'] = $module++;
		$this->data['template']=$this->config->get('config_template');
				if(file_exists(DIR_TEMPLATE.$this->config->get('config_template').'/news/poll.tpl')){ 
		$this->template=$this->config->get('config_template').'/news/poll.tpl';}
		else{$this->template='default/template/news/poll.tpl';
		}
		$this->render();
		}
		}
}
?>
