<?php defined('_JEXEC') or die;

/**
 * File       helper.php
 * Created    6/7/13 1:51 PM
 * Author     Matt Thomas | matt@betweenbrain.com | http://betweenbrain.com
 * Support    https://github.com/betweenbrain/
 * Copyright  Copyright (C) 2013 betweenbrain llc. All Rights Reserved.
 * License    GNU General Public License version 2, or later.
 */

class modSpSoccerPollHelper extends JHelper{

	public static function getAjax(){
		$input = JFactory::getApplication()->input;
		// Get data from ajax
		$data  = $input->get('data');
		// Update vote
		self::updateVote($data);


		$module = JModuleHelper::getModule('mod_sp_soccer_poll');
		$params = new JRegistry($module->params);  

		$poll_infos = self::getSoccerPolls($params->get('poll_id'));

		// total vote array
		$total_vote = array();
		foreach ($poll_infos->poptions as $poll_info) {
			$total_vote[] = $poll_info['poll_vote'];
		}

		$total_votes = array_sum($total_vote);


		// output
		$output = '<div class="ajax-poll-result">';
			$output .= '<div class="ajax-poll-result-wrap table-responsive">';
				$output .= '<table class="table">';

					$output .= '<thead>';
						$output .= '<tr>';
							$output .= '<th>Result</th><th>Total Vote ('. $total_votes .')</th>';
						$output .= '</tr>';
					$output .= '</thead>';
					$output .= '<tbody>';

					foreach ($poll_infos->poptions as $poll_info) {
						$percentage = ($poll_info['poll_vote']/$total_votes)*100;
						$output .= '<tr>';
							$output .= '<td class="poll-question">' . $poll_info['poll_name'] . '</td>';
							$output .= '<td class="poll-percent">'. round($percentage, 2) . ' %</td>';
							$output .= '<td class="poll-total"> ' . $poll_info['poll_vote'] . '</td>';
						$output .= '</tr>';

						$total_vote[] = $poll_info['poll_vote'];
					}
					$output .= '</tbody>';
				$output .= '</table>';
			$output .= '</div>';
		$output .= '</div>';

		// return to ajax
		return $output;
	}


	
	public static function updateVote($option_id){
		//Update Vote
		$db 	= JFactory::getDbo();

		$sql = "UPDATE #__spsoccer_polloptions SET vote = vote + 1 WHERE spsoccer_polloption_id =$option_id";

		$db->setQuery($sql);
		$db->execute();
	}
	

	// Get Soccer Polls
	public static function getSoccerPolls($poll_id) {

		$db = JFactory::getDbo();
		$query = $db->getQuery(true);
		$query->select($db->quoteName(array('spsoccer_poll_id', 'title', 'slug', 'list_options')));
		$query->from($db->quoteName('#__spsoccer_polls'));
		$query->where($db->quoteName('enabled')." = 1");
		$query->where($db->quoteName('spsoccer_poll_id') . " = " . $db->quote($poll_id));
		$query->order('ordering DESC'); 
		$db->setQuery($query);

		$result = $db->loadObject();
		
			//$group_team_ids = json_decode($result->spsoccer_gameteam_id);
			$poption_ids = json_decode($result->list_options)->spsoccer_polloption_id;
			$result->poptions = array();
			
			foreach ($poption_ids as $key => $poption_id) {
				$poption_infos = self::getPollOptionByID($poption_id);				

				$result->poptions[$key] = array(
					'poll_id'		=> $poption_infos->spsoccer_polloption_id,
					'poll_name' 	=> $poption_infos->title,
					'poll_vote'    	=> $poption_infos->vote,
				);
			}

		return $result;
	}



	// Get Poll option by ID
	public static function getPollOptionByID($option_id) {
		$db = JFactory::getDbo();
		$query = $db->getQuery(true);

		// Select all records from the user profile table where key begins with "custom.".
		$query->select($db->quoteName(array('spsoccer_polloption_id', 'title', 'slug', 'vote')));
		$query->from($db->quoteName('#__spsoccer_polloptions'));
		$query->where($db->quoteName('enabled')." = 1");
		$query->where($db->quoteName('spsoccer_polloption_id')." = ".$db->quote($option_id));
		//$query->where($db->quoteName('access')." IN (" . implode( ',', JFactory::getUser()->getAuthorisedViewLevels() ) . ")");
		$query->order('ordering ASC');

		$db->setQuery($query);
		
		$result = $db->loadObject();

		return $result;
	}

}
