280 lines
12 KiB
PHP
280 lines
12 KiB
PHP
<?php
|
|
class Rating
|
|
{
|
|
## PRIVATE VARIABLES
|
|
## END PRIVATE VARIABLES
|
|
|
|
## PUBLIC METHODS
|
|
// Output the Rating information
|
|
// Returns a string of HTML
|
|
public static function OutputRating($varParent, $varItem)
|
|
{
|
|
// Verify $varItem was provided
|
|
if ($varItem != null && strlen(trim($varItem)) != 0 && $varParent != null && strlen(trim($varParent)) != 0)
|
|
{
|
|
// Check if Magic QUotes is ON
|
|
if (!get_magic_quotes_gpc())
|
|
{
|
|
$varItem = addslashes($varItem);
|
|
$varParent = addslashes($varParent);
|
|
}
|
|
|
|
// Information for the Output
|
|
$averageStars = Rating::CalculateAverageRating($varParent, $varItem);
|
|
|
|
// Check to see that the user has not already rated this item
|
|
if (Rating::CheckRatingsByIp($varParent, $varItem) == 0)
|
|
{
|
|
$classes = "rating " . Rating::ShowStars($averageStars);
|
|
$cat_info = Rating::FetchCategoryInfo($varItem);
|
|
|
|
// Write Output HTML for the Rating Data
|
|
$output = "\r\n";
|
|
$output .= "<div class=\"rating-table\"><div><strong>{$cat_info['rating_cat_name']}</strong></div>\r\n";
|
|
$output .= "<div>{$cat_info['rating_cat_min']}</div><div><div class=\"rated\"><ul class=\"{$classes}\" style='margin: 0px 0px 10px 0px;' id=\"{$varParent}_{$varItem}\">\r\n";
|
|
$output .= " <li class=\"one\"><a href=\"javascript:RateItem('{$varParent}','{$varItem}', 1);\" title=\"1 Star\">1</a></li>\r\n";
|
|
$output .= " <li class=\"two\"><a href=\"javascript:RateItem('{$varParent}','{$varItem}', 2);\" title=\"2 Stars\">2</a></li>\r\n";
|
|
$output .= " <li class=\"three\"><a href=\"javascript:RateItem('{$varParent}','{$varItem}', 3);\" title=\"3 Stars\">3</a></li>\r\n";
|
|
$output .= " <li class=\"four\"><a href=\"javascript:RateItem('{$varParent}','{$varItem}', 4);\" title=\"4 Stars\">4</a></li>\r\n";
|
|
$output .= " <li class=\"five\"><a href=\"javascript:RateItem('{$varParent}','{$varItem}', 5);\" title=\"5 Stars\">5</a></li>\r\n";
|
|
$output .= "</ul></div></div><div> {$cat_info['rating_cat_max']}</div></div>\r\n";
|
|
}
|
|
else
|
|
{
|
|
$classes = "rated " . Rating::ShowStars($averageStars);
|
|
$cat_info = Rating::FetchCategoryInfo($varItem);
|
|
|
|
// Write Output HTML for the Rating Data
|
|
$output = "\r\n";
|
|
$output .= "<div class=\"rating-table\"><div><strong>{$cat_info['rating_cat_name']}</strong></div>\r\n";
|
|
$output .= "<div>{$cat_info['rating_cat_min']}</div><div><div class=\"rated\"><ul class=\"{$classes}\" style='margin: 0px 0px 10px 0px;' id=\"{$varParent}_{$varItem}\">\r\n";
|
|
$output .= " <li class=\"one\">1</li>\r\n";
|
|
$output .= " <li class=\"two\">2</li>\r\n";
|
|
$output .= " <li class=\"three\">3</li>\r\n";
|
|
$output .= " <li class=\"four\">4</li>\r\n";
|
|
$output .= " <li class=\"five\">5</li>\r\n";
|
|
$output .= "</ul></div></div><div> {$cat_info['rating_cat_max']}</div></div>\r\n";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
$output = "";
|
|
// This is a major issue. NO information can be retrieve if an item name is not passed.
|
|
Error::LogError("Variable Missing", "You must provide the item name for this function to find the average.");
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
|
|
public static function OutputParentRating($varParent)
|
|
{
|
|
// Verify $varParent was provided
|
|
if ($varParent != null && strlen(trim($varParent)) != 0)
|
|
{
|
|
// Check if Magic QUotes is ON
|
|
if (!get_magic_quotes_gpc())
|
|
{
|
|
$varParent = addslashes($varParent);
|
|
}
|
|
|
|
// Information for the Output
|
|
$averageStars = Rating::CalculateAverageParentRating($varParent);
|
|
|
|
$classes = "rated " . Rating::ShowStars($averageStars);
|
|
//$parent_info = Rating::FetchParentInfo($varParent);
|
|
|
|
// Write Output HTML for the Rating Data
|
|
$output = "\r\n";
|
|
//$output .= "<div style='clear:both;'>Overall Rating</div>";
|
|
$output .= "<div style='height: 16px; width: 80px; position: relative;'><ul class=\"{$classes}\" id=\"{$varParent}\" style='margin: 0px 0px 10px 0px;'>\r\n";
|
|
$output .= " <li class=\"one\">1</li>\r\n";
|
|
$output .= " <li class=\"two\">2</li>\r\n";
|
|
$output .= " <li class=\"three\">3</li>\r\n";
|
|
$output .= " <li class=\"four\">4</li>\r\n";
|
|
$output .= " <li class=\"five\">5</li>\r\n";
|
|
$output .= "</ul></div>\r\n";
|
|
}
|
|
else
|
|
{
|
|
$output = "";
|
|
// This is a major issue. NO information can be retrieve if an item name is not passed.
|
|
Error::LogError("Variable Missing", "You must provide the parent name for this function to find the average.");
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
|
|
// Rate an Item
|
|
// Returns the name/value pair of new class names and the item name
|
|
public static function RateItem($varParent, $varItem, $varRating, $varClasses)
|
|
{
|
|
$newClassNames = $varClasses;
|
|
|
|
// Verify $varName was provided
|
|
if ($varParent != null && strlen(trim($varParent)) != 0
|
|
&& $varItem != null && strlen(trim($varItem)) != 0
|
|
&& $varRating != null && strlen(trim($varRating)) != 0 && is_numeric($varRating)
|
|
&& $varClasses != null && strlen(trim($varClasses)) != 0)
|
|
{
|
|
// Check if Magic Quotes is ON
|
|
if (!get_magic_quotes_gpc())
|
|
{
|
|
$varItem = addslashes($varItem);
|
|
$varParent = addslashes($varParent);
|
|
}
|
|
|
|
// Check to see that the user has not already rated this item
|
|
if (Rating::CheckRatingsByIp($varParent, $varItem) == 0)
|
|
{
|
|
$ipAddress = $_SERVER['REMOTE_ADDR'];
|
|
$tempTime = time();
|
|
|
|
Database::ExecuteQuery("INSERT INTO `gforum_RunshopsRating` (`runshop_id_fk`, `category_id_fk`, `rating_vote`, `rating_ip`, `rating_date`) VALUES ('{$varParent}', '{$varItem}', {$varRating}, '{$ipAddress}', '{$tempTime}')", "InsertRating");
|
|
Database::FetchResults("InsertRating");
|
|
Database::FreeResults("InsertRating");
|
|
Database::RemoveSavedResults("InsertRating");
|
|
|
|
// Information for the Output
|
|
$averageStars = Rating::CalculateAverageRating($varParent, $varItem);
|
|
$newClassNames = "rated " . Rating::ShowStars($averageStars);
|
|
$averageStars = Rating::CalculateAverageParentRating($varParent);
|
|
$newClassParent = "rated " . Rating::ShowStars($averageStars);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// This is a major issue. NOT enough information was sent to log the item
|
|
Error::LogError("Variable(s) Missing", "You must provide all of the information to log the rating of this item.");
|
|
}
|
|
|
|
// Build Name/Value Pair to return
|
|
$nameValue = "classes={$newClassNames}&item={$varItem}&parent={$varParent}&parentClass={$newClassParent}";
|
|
return $nameValue;
|
|
}
|
|
## END PUBLIC METHODS
|
|
|
|
## PRIVATE METHODS
|
|
// Fetch Category Names & Min/Max Values
|
|
private static function FetchCategoryInfo($varItem)
|
|
{
|
|
// Query Category Info for a specific Category ID
|
|
Database::ExecuteQuery("SELECT * FROM `gforum_RunshopsRatingCategory` WHERE `rating_cat_id`='{$varItem}'", "CategoryInfo");
|
|
$results = Database::FetchResults("CategoryInfo");
|
|
Database::FreeResults("CategoryInfo");
|
|
Database::RemoveSavedResults("CategoryInfo");
|
|
|
|
return $results[0];
|
|
|
|
}
|
|
|
|
// Calculate Average Rating
|
|
// Returns the number of stars to show
|
|
private static function CalculateAverageRating($varParent, $varItem)
|
|
{
|
|
$averageStars = 0;
|
|
|
|
// Query Average Rating for a specific Item
|
|
Database::ExecuteQuery("SELECT AVG(`rating_vote`) AS `averageRating` FROM `gforum_RunshopsRating` WHERE `category_id_fk`='{$varItem}' AND `runshop_id_fk`='{$varParent}'", "AverageRating");
|
|
$results = Database::FetchResults("AverageRating");
|
|
Database::FreeResults("AverageRating");
|
|
Database::RemoveSavedResults("AverageRating");
|
|
|
|
// Round the Average into a Whole Number
|
|
if (sizeof($results) == 1)
|
|
{
|
|
if ($results[0]['averageRating'] != null)
|
|
{
|
|
$averageStars = round($results[0]["averageRating"], 0);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// This is simply a warning, as it isn't vital if no results were found, as the item may be new.
|
|
Error::LogWarning("Rating Data Missing", "No entries were found for '{$varName}', this might be the first entry.");
|
|
}
|
|
|
|
return $averageStars;
|
|
}
|
|
|
|
// Calculate Average Rating
|
|
// Returns the number of stars to show
|
|
private static function CalculateAverageParentRating($varParent)
|
|
{
|
|
$averageStars = 0;
|
|
|
|
// Query Average Rating for a specific Item
|
|
Database::ExecuteQuery("SELECT AVG(`rating_vote`) AS `averageRating` FROM `gforum_RunshopsRating` WHERE `runshop_id_fk`='{$varParent}'", "AverageRating");
|
|
$results = Database::FetchResults("AverageRating");
|
|
Database::FreeResults("AverageRating");
|
|
Database::RemoveSavedResults("AverageRating");
|
|
|
|
// Round the Average into a Whole Number
|
|
if (sizeof($results) == 1)
|
|
{
|
|
if ($results[0]['averageRating'] != null)
|
|
{
|
|
$averageStars = round($results[0]["averageRating"], 0);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// This is simply a warning, as it isn't vital if no results were found, as the item may be new.
|
|
Error::LogWarning("Rating Data Missing", "No entries were found for '{$varName}', this might be the first entry.");
|
|
}
|
|
|
|
return $averageStars;
|
|
}
|
|
|
|
// Show Stars
|
|
// Returns the class information for the number of stars to show
|
|
private static function ShowStars($varStars)
|
|
{
|
|
// Select the Number of Stars Class
|
|
switch ($varStars)
|
|
{
|
|
case 1:
|
|
$classes .= "onestar";
|
|
break;
|
|
case 2:
|
|
$classes .= "twostar";
|
|
break;
|
|
case 3:
|
|
$classes .= "threestar";
|
|
break;
|
|
case 4:
|
|
$classes .= "fourstar";
|
|
break;
|
|
case 5:
|
|
$classes .= "fivestar";
|
|
break;
|
|
default:
|
|
$classes .= "nostar";
|
|
break;
|
|
}
|
|
|
|
return $classes;
|
|
}
|
|
|
|
// Check Ratings By IP Address
|
|
// Returns the number of ratings for an item by an ip address
|
|
private static function CheckRatingsByIp($varParent, $varItem)
|
|
{
|
|
$ipAddress = $_SERVER['REMOTE_ADDR'];
|
|
|
|
Database::ExecuteQuery("SELECT COUNT(*) AS `totalRatings` FROM `gforum_RunshopsRating` WHERE `category_id_fk`='{$varItem}' AND `runshop_id_fk`='{$varParent}' AND `rating_ip`='{$ipAddress}'", "AlreadyRated");
|
|
$results = Database::FetchResults("AlreadyRated");
|
|
Database::FreeResults("AlreadyRated");
|
|
Database::RemoveSavedResults("AlreadyRated");
|
|
|
|
// Check to see that the user has not already rated this item
|
|
if ($results != null && $results[0]['totalRatings'] != null)
|
|
{
|
|
return $results[0]['totalRatings'];
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
## END PRIVATE METHODS
|
|
}
|
|
?>
|