<?php
/*-------------------------------------------------------+
| <span style="border-bottom: 1px dotted black;">PHP</span>-Fusion Content Management System
| Copyright (C) 2002 - 2010 Nick Jones
| <a href='http://www.php-fusion.co.uk/' target='_blank'><span style='color:005C5B'>http://www.php-fusion.co.uk/</span></a>
+--------------------------------------------------------+
| Filename: ss_star_rating_include.php
| Author: SiteMaster style
+--------------------------------------------------------+
| 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 <a href='http://www.gnu.org/licenses/agpl.html.' target='_blank'><span style='color:005C5B'>www.gnu.org/licenses/agpl.html.</span></a> Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/
include "../../maincore.php";
if (!defined("IN_FUSION")) { die("Access Denied"); }
sleep(0);
// Check, if we need to proccess the FORM submission (or AJAX call that pretends POST method)
if($_SERVER["REQUEST_METHOD"] == 'POST'){
// veriffy user input!
$vote = in_range($_POST['rate'], 1, 5);
// update statistic and save to db
$db = save_rating($_POST['rate'], $_POST['rating_item_id'], $_POST['rating_type']);
// For AJAX requests we'll return JSON object with current vote statistics
if($_SERVER['HTTP_X_REQUESTED_WITH']) {
header('Cache-Control: no-cache');
echo json_encode($db); // requires: <span style="border-bottom: 1px dotted black;">PHP</span> >= 5.2.0, PECL json >= 1.2.0
// For non-AJAX requests we are going to echo {$post_message} variable in main script
} else {
$avg = round($db['avg']);
foreach($options as $id => $val) {
$options[$id]['disabled'] = 'disabled="disabled"';
$options[$id]['checked'] = $id==$avg ? 'checked="checked"' : '';
}
}
}
if(isset($_GET['fake']) && $_GET['fake'] == 1){
// For AJAX requests we'll return JSON object with current vote statistics
if($_SERVER['HTTP_X_REQUESTED_WITH']){
header('Cache-Control: no-cache');
echo json_encode(get_votes($_GET['rating_type'], $_GET['rating_item_id'])); // requires: <span style="border-bottom: 1px dotted black;">PHP</span> >= 5.2.0, PECL json >= 1.2.0
}else{
$db = get_votes($_GET['rating_type'], $_GET['rating_item_id']);
$avg = round($db['avg']);
foreach($options as $id => $val) {
$options[$id]['disabled'] = 'disabled="disabled"';
$options[$id]['checked'] = $id==$avg ? 'checked="checked"' : '';
}
}
}
function in_range($val, $from=0, $to=100) {
return min($to, max($from, (int)$val));
}
function get_votes($rating_type, $rating_item_id) {
$votes = dbcount("(rating_item_id)", DB_RATINGS, "rating_item_id='".$rating_item_id."' AND rating_type='".$rating_type."'");
$sum = dbarray(dbquery("SELECT SUM(rating_vote) AS sum FROM ".DB_RATINGS." WHERE rating_item_id='".$rating_item_id."' AND rating_type='".$rating_type."'"));
if ($sum != "" && $votes['rating_item_id']){
$db = array('votes' => $votes, 'sum' => $sum['sum'], 'avg' => (round($sum['sum'] / $votes, 2)));
}else{
$db = array('votes' => 0, 'sum' => 0, 'avg' => 0);
}
return $db;
}
function save_rating($vote, $rating_item_id, $rating_type) {
global $userdata;
$db['votes']++;
$db['sum'] += $vote;
$db['avg'] = round($db['sum'] / $db['votes'], 2);
if (iMEMBER) {
if (isset($_POST['rate']) && $_POST['rate'] == 0){
$result = dbquery("DELETE FROM ".DB_RATINGS." WHERE rating_item_id='".$rating_item_id."' AND rating_type='".$rating_type."' AND rating_user='".$userdata['user_id']."'");
}elseif (isset($_POST['rate']) && $_POST['rate'] > 0 && $_POST['rate'] < 6){
$d_rating = dbarray(dbquery("SELECT rating_vote, rating_datestamp FROM ".DB_RATINGS." WHERE rating_item_id='".$rating_item_id."' AND rating_type='".$rating_type."' AND rating_user='".$userdata['user_id']."'"));
if (!isset($d_rating['rating_vote'])){
$result = dbquery("INSERT INTO ".DB_RATINGS." (rating_item_id, rating_type, rating_user, rating_vote, rating_datestamp, rating_ip) VALUES ('".$rating_item_id."', '".$rating_type."', '".$userdata['user_id']."', '".$vote."', '".time()."', '".USER_IP."')");
} else {
$result = dbquery("UPDATE ".DB_RATINGS." SET rating_item_id='".$rating_item_id."', rating_type='".$rating_type."', rating_user='".$userdata['user_id']."', rating_vote='".$vote."', rating_datestamp='".time()."', rating_ip='".USER_IP."' WHERE rating_item_id='".$rating_item_id."' AND rating_type='".$rating_type."' AND rating_user='".$userdata['user_id']."'");
}
}
}
$db = get_votes($rating_type, $rating_item_id);
return $db;
}
?>