<?php
/*-------------------------------------------------------+
| Dynamic Star Rating Redux
| Developed by Jordan Boesch
| www.boedesign.com
| Licensed under Creative Commons - http://creativecommons.org/licenses/by-nc-nd/2.5/ca/
| Used CSS from komodomedia.com.
+--------------------------------------------------------+
| Ajax Rating System mSF For PHP-Fusion 7.xx v.1.1
| Copyright © 2008-2009 Powered by .:MeF:.
| URL: http://x-iweb.ru/
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html. Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/


function getRating($id, $rtype) {

	$total = 0;
	$rows = 0;
	
	$sel = dbquery("SELECT rating_vote FROM ".DB_RATINGS." WHERE rating_item_id = '$id' AND rating_type = '".$rtype."'");
	if(dbrows($sel) != 0){
	
		while($data = dbarray($sel)) {	
			$total = $total + $data['rating_vote'];
			$rows++;
    	}
		
		$perc = ($total/$rows) * 20;
		
		//$newPerc = round($perc/5)*5;
		//return $newPerc.'%';
		
		$newPerc = round($perc,2);
		return $newPerc.'%';
	
	} else { return '0%'; }
	
	return '0%';
}

function outOfFive($id, $rtype) {

	$total = 0;
	$rows = 0;
	
	$sel = dbquery("SELECT rating_vote FROM ".DB_RATINGS." WHERE rating_item_id = '".$id."' AND rating_type = '".$rtype."'");
	if(dbrows($sel) != 0) {
	
		while($data = dbarray($sel)){	
			$total = $total + $data['rating_vote'];
			$rows++;
		}
		
		$perc = ($total/$rows);
		
		return round($perc,2);
		//return round(($perc*2), 0)/2; // 3.5
	
	} else { return '0'; }
	
	return '0';
}

function getVotes($id, $rtype){

	$sel = dbquery("SELECT rating_vote FROM ".DB_RATINGS." WHERE rating_item_id = '$id' AND rating_type = '".$rtype."'");
	$rows = mysql_num_rows($sel);
	if ($rows == 0) {
		$votes = "0 balsu";
	} else if($rows == 1) {
		$votes = "1 balsas";
	} else if($rows > 1 && $rows < 5) {
		$votes = $rows." balsai";
	} else {
		$votes = $rows." balsu";
	}
	return $votes;
}

function showratings($rtype, $id, $rating_link = true) {
	
	global $show5, $showPerc, $showVotes, $static;
	
	$show5 = false; $showPerc = false; $showVotes = false; $static = NULL;
	
	// Check if they have already voted...
	$text = '';
	
	$sel = dbquery("SELECT rating_item_id FROM ".DB_RATINGS." WHERE rating_ip = '".USER_IP."' AND rating_item_id = '$id' AND rating_type='".$rtype."'");
	
	if(dbrows($sel) != 0 || $static == 'novote') {
	
	    
	
	echo '<div class="rated_text">';
	echo 'Vartotoj&#371; &#303;vertinimas: <span id="outOfFive_'.$id.'" class="out5Class">'.outOfFive($id, $rtype).'</span>/5';
	echo ' (<span id="percentage_'.$id.'" class="percentClass">'.getRating($id, $rtype).'</span>)';
	echo ' (Viso: <span id="showvotes_'.$id.'" class="votesClass">'.getVotes($id, $rtype).'</span>)';
	echo '</div>';
		
	echo '<ul class="star-rating2" id="rater_'.$id.'">';
	echo '<li class="current-rating" style="width:'.getRating($id, $rtype).';" id="ul_'.$id.'"></li>';
	echo '<li><a onclick="return true;" href="#" title="1 i&#353; 5" class="one-star" >1</a></li>';
	echo '<li><a onclick="return true;" href="#" title="2 i&#353; 5" class="two-stars">2</a></li>';
	echo '<li><a onclick="return true;" href="#" title="3 i&#353; 5" class="three-stars">3</a></li>';
	echo '<li><a onclick="return true;" href="#" title="4 i&#353; 5" class="four-stars">4</a></li>';
	echo '<li><a onclick="return true;" href="#" title="5 i&#353; 5" class="five-stars">5</a></li>';
	echo '</ul>';
	echo '<div id="loading_'.$id.'"></div>';
	
	    
		
	} else {
	
	    
		
	echo '<div class="rated_text">';
			$show5bool = 'true';
	echo 'Vartotoj&#371; &#303;vertinimas: <span id="outOfFive_'.$id.'" class="out5Class">'.outOfFive($id, $rtype).'</span>/5';
			$showPercbool = 'true';
	echo ' (<span id="percentage_'.$id.'" class="percentClass">'.getRating($id, $rtype).'</span>)';
			$showVotesbool = 'true';
	echo ' (<span id="showvotes_'.$id.'" class="votesClass">'.getVotes($id, $rtype).'</span>)';
	echo '</div>';

	echo '<ul class="star-rating" id="rater_'.$id.'">';		
    echo '<li class="current-rating" style="width:'.getRating($id, $rtype).';" id="ul_'.$id.'"></li>';
    echo '<li><a onclick="rate(\'1\',\''.$id.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.',\''.$rtype.'\'); return false;" href="'.INCLUDES.'rating_process.php?id='.$id.'&rating=1&rtype='.$rtype.'" title="1 i&#353; 5" class="one-star" >1</a></li>';
    echo '<li><a onclick="rate(\'2\',\''.$id.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.',\''.$rtype.'\'); return false;" href="'.INCLUDES.'rating_process.php?id='.$id.'&rating=2&rtype='.$rtype.'" title="2 i&#353; 5" class="two-stars">2</a></li>';
    echo '<li><a onclick="rate(\'3\',\''.$id.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.',\''.$rtype.'\'); return false;" href="'.INCLUDES.'rating_process.php?id='.$id.'&rating=3&rtype='.$rtype.'" title="3 i&#353; 5" class="three-stars">3</a></li>';
    echo '<li><a onclick="rate(\'4\',\''.$id.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.',\''.$rtype.'\'); return false;" href="'.INCLUDES.'rating_process.php?id='.$id.'&rating=4&rtype='.$rtype.'" title="4 i&#353; 5" class="four-stars">4</a></li>';
    echo '<li><a onclick="rate(\'5\',\''.$id.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.',\''.$rtype.'\'); return false;" href="'.INCLUDES.'rating_process.php?id='.$id.'&rating=5&rtype='.$rtype.'" title="5 i&#353; 5" class="five-stars">5</a></li>';
	echo '</ul>';
	echo '<div id="loading_'.$id.'"></div>';
	
	    
		
	}
}

/*
// Added in version 1.5
// Fixed sort in version 1.7
function getTopRated($limit, $table, $idfield, $namefield){
	
	$result = '';
	
	$sql = "
	SELECT COUNT(ratings.rating_id) as rates,ratings.rating_item_id,".DB_RATINGS.".".$namefield." as thenamefield,
	ROUND(AVG(ratings.rating_vote),2) as rating 
	FROM ratings,".DB_RATINGS." WHERE ".DB_RATINGS.".".$idfield." = ratings.rating_item_id GROUP BY rating_id 
	ORDER BY rates DESC,rating DESC LIMIT ".$limit."
	";
			
	$sel = dbquery($sql);
	
	$result .= '<ul class="topRatedList">'."\n";
	
	while($data = dbarray($sel)){
		$result .= '<li>'.$data['thenamefield'].' ('.$data['rating'].')</li>'."\n";
	}
	
	$result .= '</ul>'."\n";
	
	return $result;	
}
*/

?>