SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
-
Jul 17, 2009, 11:55 #1
Drop some of your understanding about this class please...
The code below is part of a rating system and I have decided to go step by step and learn the code by some of your explanations and undertanding about this class, so solutions and even modification to this system can be made. I have just realize that I need to understand the whole system first.. Please drop as many opinions about this clase called OutputRating. I will greatly appriciated. I will post other classes but first I am interested in understanding this one first and then the other. one at the time.
PHP Code:class Rating
{
## PRIVATE VARIABLES
## END PRIVATE VARIABLES
## PUBLIC METHODS
// Output the Rating information
// Returns a string of HTML
public static function OutputRating($varItem)
{
// Verify $varItem was provided
if ($varItem != null && strlen(trim($varItem)) != 0)
{
// Check if Magic QUotes is ON
if (!get_magic_quotes_gpc())
{
$varItem = addslashes($varItem);
}
// Information for the Output
$averageStars = Rating::CalculateAverageRating($varItem);
// Check to see that the user has not already rated this item
if (Rating::CheckRatingsByIp($varItem) == 0)
{
$classes = "rating" . Rating::ShowStars($averageStars);
// Write Output HTML for the Rating Data
$output = "\r\n";
$output .= "<ul class=\"{$classes}\" id=\"{$varItem}\">\r\n";
$output .= " <li class=\"one\"><a href=\"javascript:RateItem('{$varItem}', 1);\" title=\"1 Star\">1</a></li>\r\n";
$output .= " <li class=\"two\"><a href=\"javascript:RateItem('{$varItem}', 2);\" title=\"2 Stars\">2</a></li>\r\n";
$output .= " <li class=\"three\"><a href=\"javascript:RateItem('{$varItem}', 3);\" title=\"3 Stars\">3</a></li>\r\n";
$output .= " <li class=\"four\"><a href=\"javascript:RateItem('{$varItem}', 4);\" title=\"4 Stars\">4</a></li>\r\n";
$output .= " <li class=\"five\"><a href=\"javascript:RateItem('{$varItem}', 5);\" title=\"5 Stars\">5</a></li>\r\n";
$output .= "</ul>\r\n";
}
else
{
$classes = "rated " . Rating::ShowStars($averageStars);
// Write Output HTML for the Rating Data
$output = "\r\n";
$output .= "<ul class=\"{$classes}\" id=\"{$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 .= " <li class=\"total\">[78]</li>";
$output .= "</ul>\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;
}
-
Jul 17, 2009, 17:03 #2
Why is the whole class static?
-
Jul 17, 2009, 17:53 #3
- Join Date
- Oct 2008
- Posts
- 372
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Maybe this will help you understand it better
PHP Code:class Rating {
public static function OutputRating($varItem) {//Accepting a parameter
if ($varItem != null && strlen(trim($varItem)) != 0) {//Checking if $varItem is not empty and is not zero. If this is true continue
if (!get_magic_quotes_gpc()) {//You don't need this you can just stripslashes() function
$varItem = addslashes($varItem);
}
$averageStars = Rating::CalculateAverageRating($varItem); //Some type of math equation from another static method
// Check to see that the user has not already rated this item
if (Rating::CheckRatingsByIp($varItem) == 0) { //Execute code if $varItem equals zero A.K.A User has not rated this item
$classes = "rating" . Rating::ShowStars($averageStars);
// Write Output HTML for the Rating Data
$output = "\r\n";
$output .= "<ul class=\"{$classes}\" id=\"{$varItem}\">\r\n";
$output .= " <li class=\"one\"><a href=\"javascript:RateItem('{$varItem}', 1);\" title=\"1 Star\">1</a></li>\r\n";
$output .= " <li class=\"two\"><a href=\"javascript:RateItem('{$varItem}', 2);\" title=\"2 Stars\">2</a></li>\r\n";
$output .= " <li class=\"three\"><a href=\"javascript:RateItem('{$varItem}', 3);\" title=\"3 Stars\">3</a></li>\r\n";
$output .= " <li class=\"four\"><a href=\"javascript:RateItem('{$varItem}', 4);\" title=\"4 Stars\">4</a></li>\r\n";
$output .= " <li class=\"five\"><a href=\"javascript:RateItem('{$varItem}', 5);\" title=\"5 Stars\">5</a></li>\r\n";
$output .= "</ul>\r\n";
}
else { //$varItem does not equal zero A.K.A. User has already rated this item
$classes = "rated " . Rating::ShowStars($averageStars);
// Write Output HTML for the Rating Data
$output = "\r\n";
$output .= "<ul class=\"{$classes}\" id=\"{$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 .= " <li class=\"total\">[78]</li>";
$output .= "</ul>\r\n";
}
}
else { //$varItem is null or is zero so show error message
$output = "";
Error::LogError("Variable Missing", "You must provide the item name for this function to find the average.");
}
return $output;
}
}
-
Jul 19, 2009, 04:45 #4
- Join Date
- Mar 2003
- Location
- England, UK
- Posts
- 2,906
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm not sure what kind of opinions you want on it. A class consisting of only static methods might as well be written procedurally. Also that is not the full class definition.
-
Jul 20, 2009, 04:48 #5
The full class definitino goes as below:
PHP Code:<?php
class Rating
{
## PRIVATE VARIABLES
## END PRIVATE VARIABLES
## PUBLIC METHODS
// Output the Rating information
// Returns a string of HTML
public static function OutputRating($varItem)
{
// Verify $varItem was provided
if ($varItem != null && strlen(trim($varItem)) != 0)
{
// Check if Magic QUotes is ON
if (!get_magic_quotes_gpc())
{
$varItem = addslashes($varItem);
}
// Information for the Output
$averageStars = Rating::CalculateAverageRating($varItem);
// Check to see that the user has not already rated this item
if (Rating::CheckRatingsByIp($varItem) == 0)
{
$classes = "rating" . Rating::ShowStars($averageStars);
// Write Output HTML for the Rating Data
$output = "\r\n";
$output .= "<ul class=\"{$classes}\" id=\"{$varItem}\">\r\n";
$output .= " <li class=\"one\"><a href=\"javascript:RateItem('{$varItem}', 1);\" title=\"1 Star\">1</a></li>\r\n";
$output .= " <li class=\"two\"><a href=\"javascript:RateItem('{$varItem}', 2);\" title=\"2 Stars\">2</a></li>\r\n";
$output .= " <li class=\"three\"><a href=\"javascript:RateItem('{$varItem}', 3);\" title=\"3 Stars\">3</a></li>\r\n";
$output .= " <li class=\"four\"><a href=\"javascript:RateItem('{$varItem}', 4);\" title=\"4 Stars\">4</a></li>\r\n";
$output .= " <li class=\"five\"><a href=\"javascript:RateItem('{$varItem}', 5);\" title=\"5 Stars\">5</a></li>\r\n";
$output .= "</ul>\r\n";
}
else
{
$classes = "rated " . Rating::ShowStars($averageStars);
// Write Output HTML for the Rating Data
$output = "\r\n";
$output .= "<ul class=\"{$classes}\" id=\"{$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 .= " <li class=\"total\">[78]</li>";
$output .= "</ul>\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;
}
// Rate an Item
// Returns the name/value pair of new class names and the item name
public static function RateItem($varItem, $varRating, $varClasses)
{
$newClassNames = $varClasses;
// Verify $varName was provided
if ($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);
}
// Check to see that the user has not already rated this item
if (Rating::CheckRatingsByIp($varItem) == 0)
{
$ipAddress = $_SERVER['REMOTE_ADDR'];
Database::ExecuteQuery("INSERT INTO `rating` (`item_name`, `rating`, `ip_address`, `date_rated`) VALUES ('{$varItem}', {$varRating}, '{$ipAddress}', NOW())", "InsertRating");
Database::FetchResults("InsertRating");
Database::FreeResults("InsertRating");
Database::RemoveSavedResults("InsertRating");
// Information for the Output
$averageStars = Rating::CalculateAverageRating($varItem);
$newClassNames = "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}";
return $nameValue;
}
## END PUBLIC METHODS
## PRIVATE METHODS
// Calculate Average Rating
// Returns the number of stars to show
private static function CalculateAverageRating($varItem)
{
$averageStars = 0;
// Query Average Rating for a specific Item
Database::ExecuteQuery("SELECT AVG(`rating`) AS `averageRating` FROM `rating` WHERE `item_name`='{$varItem}'", "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 ShowSars($varStars)
{
$aStars = array(
1 => 'onestar',
2 => 'twostar',
3 => 'threestar',
4 => 'fourstar',
5 => 'fivestar'
);
return (true === array_key_exists((integer)$varStars, $aStars)) ? $aStars[(integer)$varStars] : 'nostar' ;
}
// Check Ratings By IP Address
// Returns the number of ratings for an item by an ip address
private static function CheckRatingsByIp($varItem)
{
$ipAddress = $_SERVER['REMOTE_ADDR'];
Database::ExecuteQuery("SELECT COUNT(*) AS `totalRatings` FROM `rating` WHERE `item_name`='{$varItem}' AND `ip_address`='{$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
}
?> \PHP Code:<?php
$ratingData = Rating::OutputRating('demo');
if (Error::HasErrors())
{
echo Error::ShowErrorMessages();
Error::ClearErrors();
}
else
{
echo $ratingData;
}
?>
PHP Code:Database::ExecuteQuery("SELECT item_name AS 'item' FROM `rating` WHERE `item_name`='{$varItem}'", "item");
$results = Database::FetchResults("item");
Database::FreeResults("item");
Database::RemoveSavedResults("item);
Bookmarks