<?php
/**
* @version		$Id: extravote.php 2008 vargas $
* @package		Joomla
* @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// no direct access
defined( '_JEXEC' ) or  die('Restricted access');

class plgsimpleratingExtraVote 
{

	function plgsimpleratingExtraVote()
	{
        parent::__construct();
	}
	
	// GET STARS TO DISPLAY
	// $id = user id
	// $xid= my id
	function simpleratingExtraVote($id, $xid, $what='stars', $single='')
	{ 		
		jimport('joomla.filesystem.file');
		$rating_count=0;
		$html='';
      if ($single) $single = "user_id=$xid AND";
			$db	=& JFactory::getDBO();
			$query="SELECT AVG(rating_sum) AS rating_avg, SUM(rating_count) AS votes FROM #__simplerating_extravote WHERE $single `option`='com_comprofiler' AND published=1 AND extra_id=". $id;
			$db->setQuery($query);
			$vote=$db->loadObject();

			if(!empty($vote)) {
				$rating_sum = $vote->rating_avg;
				$rating_count = intval($vote->votes);
			}
			if (empty($rating_sum)) $rating_sum = 0;
		
			$html .= plgsimpleratingExtraVote::plgsimpleratingExtraDisplayStars( $id, $rating_sum, $rating_count, $xid );

		return $html;
 	}
	
  
 	function plgsimpleratingExtraDisplayStars( $id, $rating_sum, $rating_count, $xid )
	{
			//JHTML::stylesheet('extravote.css',dirname(__FILE__).DS.'extravote'.DS,false);
     	//JHTML::script('extravote.js',dirname(__FILE__).DS.'extravote'.DS,false);
        //JPlugin::loadLanguage('plg_videogal_extravote', JPATH_SITE);
	
        $live_path = JURI::base();

     	global $plgsimpleratingExtraVoteAddScript;
		
		$counter = 1;
		$unrated = 1;
		$percent = 0;
		$stars = '';
		
	 	if(!$plgsimpleratingExtraVoteAddScript){ 
         	echo "
  <script type='text/javascript' language='javascript'>
  var sfolder = '".JURI::base(true)."';
  var extravote_text=Array('"._SR_NOT_SUPPORT_AJAX."','"._SR_LOADING."','"._SR_THANK_YOU."','"._SR_NEED_LOGIN."','"._SR_ALREADY."','"._SR_VOTE."','"._SR_VOTES."');
  </script>";
       	$plgsimpleratingExtraVoteAddScript = 1;
  	 	}
  		
  		if($rating_count!=0) {
  			$percent = number_format(($rating_sum)*20,2);
  		} elseif ($unrated == 0) {
  			$counter = -1;
  		}
  		
  		if ( (int)$xid ) { 
  			$stars = '-small';
  			if ( $counter == 2 ) $counter = 0;
  		} else {
  			if ( $counter == 3 ) $counter = 0;
  		}
  								
  	 	$html="<div class='extravote-container".$stars."' ".( $xid != 'x' ? "" : "style='margin-top:5px; display:inline;'" ).">
    <ul class='extravote-stars".$stars."'>
      <li id='rated_".$id."_".$xid."' class='current-rating' style='width:".(int)$percent."%;'></li>
    </ul></div>";
    
    return $html;
  }
  
}
?>
