Wiki4Games:RatingBar/recordajax.php

From Wiki4Games
Jump to: navigation, search
<?php

/****************************************************************************
**
** This file is part of the Rating Bar extension for MediaWiki
** Copyright (C)2009 Franck Dernoncourt <[email protected]>
**
** Home Page : https://wiki4games.com
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either
** version 3 of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
** <http://www.gnu.org/licenses/>
*********************************************************************/

// Get some variables
require_once ('config.php');

header("Cache-Control: no-cache");
header("Pragma: nocache");

//Get the parameters
$vote_sent              = preg_replace("/[^0-9]/","",$_REQUEST['vote']);
$uid                    = preg_replace("/[^0-9]/","",$_REQUEST['uid']);
$page_id                = preg_replace("/[^0-9a-zA-Z]/","",$_REQUEST['pid']);
$units                  = preg_replace("/[^0-9]/","",$_REQUEST['units']);

// Convert the vote into a base-100 indice
$vote_sent = $vote_sent * 100 / $units;

// Kill the script if the user try to cheat
if ($vote_sent > 100 or $vote_sent < 0) die("Sorry, vote appears to be invalid."); 

// Check if the user exists
$query = mysql_query("SELECT user_id FROM $ratingbar_dbname.$user_tablename WHERE user_id=$uid") or die(" Error: ".mysql_error());
$line = mysql_fetch_array($query);
if ( count($line) <= 1 ) die  ("You must log in to vote!");

// Check if the user do not try to vote more than once. If yes, update the existing entry. If no, create a new entry.
$query = mysql_query("SELECT user_id FROM $ratingbar_dbname.$ratingbar_tablename WHERE page_id='$page_id' AND user_id=$uid") or die(" Error: ".mysql_error());
$line = mysql_fetch_array($query);
if ( count($line) > 1 ) {
        $update = "UPDATE $ratingbar_dbname.$ratingbar_tablename SET rating = $vote_sent WHERE page_id='$page_id' AND user_id=$uid";
} else {
        $update = "INSERT INTO $ratingbar_dbname.$ratingbar_tablename VALUES($uid, $vote_sent, '$page_id')";
}
$result = mysql_query($update) or die(" Error: ".mysql_error());


// What's the new global rating?
$query = mysql_query("SELECT AVG(rating) FROM $ratingbar_dbname.$ratingbar_tablename WHERE page_id='$page_id'") or die(" Error: ".mysql_error());
$line = mysql_fetch_array($query);
$score = intval($line[0]); // Round figures
$response .= 'Current user rating: <b>'.$score.'% </b>';

// How many users voted?
$query = mysql_query("SELECT count(*) FROM $ratingbar_dbname.$ratingbar_tablename WHERE page_id='$page_id'") or die(" Error: ".mysql_error());
$line = mysql_fetch_array($query);
$response .= '('.$line[0].' votes) <br/>';

// Display the new vote
$response .= "You voted <b>".$vote_sent.'%</b>';

$blocksdisplay = $vote_sent / 100 * 10;
$linkvote = $linkvoteloggedin; // If the user voted, it means he was logged in

// Displa y rating bar
$response .= '<div id="bar"><div id="blocks" class="rating'.$blocksdisplay.'">';

for ($ncount = 1; $ncount <= $units; $ncount++) { // loop from 1 to the number of units
        $response .= '<a href="'.$linkvote.'" onclick="ajaxFunction('.$ncount.','.$uid.',\''.$page_id.'\','.$units.')" id="block'.$ncount.'" title="Give this game rating '.$ncount.'" rel="nofollow"><span>'.$ncount.'</span></a>';
}
$response .= '</div></div>';


// Output the response
echo $response;


?>