* * This file will automatically create the MySQL tables it needs to function * should they not exist. * * HOW TO USE: * 1) Put the following code at the top of every page you want to use this * script on: * * 2) Wherever you want to show a ratings div, use the following code: * showStars("anotherGreatArticle"); ?> * REPLACE "anotherGreatArticle" WITH A UNIQUE KEY FOR YOUR ITEM. * * See the included README file for license and more information. * * @author Michelle Steigerwalt * @copyright 2007 Michelle Steigerwalt */ /* The line below handles ratings sent if the $_POST variables are set. */ if ($_POST['vote'] && $_POST['id']) { $r = new RabidRatings(); $r->doVote($_POST['id'], $_POST['vote']); }; class RabidRatings { function RabidRatings() { global $rConfig; list ($dbHost, $dbUser, $dbPassword, $dbDatabase, $prefix) = $rConfig; $this->dbHost = $dbHost; $this->dbUser = $dbUser; $this->dbPassword = $dbPassword; $this->dbDatabase = $dbDatabase; $this->ratables = $prefix."ratables"; $this->ratings = $prefix."ratings"; $this->scale = 5; //It's a five star scale. $this->initializeDatabase(); } /** * RabidRatings::showStars * Outputs the HTML code for a ratings box, including the current score (if * any) of the item being rated. * * @param string ratableKey - The unique idenifier for your rateable item. * ratableKey should be made up in whatever manner works best for the * site using it. */ function showStars($ratableKey) { $rating = $this->loadRating($ratableKey); $ratingId = $rating['keyID']; if ($rating['totalRatings'] == 0) { $unratedClass = " ratingUnrated"; } $stars = $this->percentToStars($rating['rating']); echo "
scale\" " ."class=\"rabidRating$unratedClass\"> $stars out of $this->scale stars " ."($rating[totalRatings] votes)
"; } /** * RabidRatings::doVote * * This is the function in charge of handling a vote and saving it to the * database. * * @param integer id - The id of key to register a rating for. * @param integer percent - The rating in percentages. */ function doVote($ratableId, $percent) { $ip = $_SERVER['REMOTE_ADDR']; if (is_numeric($ratableId)) { $id = $ratableId; } else { die("Id invalid."); } if (is_numeric($percent) && $percent < 101) { $rating = $percent; } else { die("Rating percent invalid."); } $SQL_INSERT_VOTE = "INSERT INTO $this->ratings(ratable_id, ip_address, ". "rating) VALUES ('$id', '$ip', '$rating');"; mysql_query($SQL_INSERT_VOTE); } function initializeDatabase() { error_reporting(1); $this->dbConnection = mysql_connect($this->dbHost, $this->dbUser, $this->dbPassword); if (!mysql_select_db($this->dbDatabase)) { die("RabidRatings couldn't connect to the database. Please make " ."sure your configuration is correct."); } $this->ensureTables(); } function ensureTables() { $SQL_CREATE_RATABLES = "CREATE TABLE $this->ratables ( `id` int(11) NOT NULL auto_increment, `ratableKey` varchar(50) NOT NULL, `created_at` timestamp NOT NULL default CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `ratableKey` (`ratableKey`) ) AUTO_INCREMENT=1 ; "; $SQL_CREATE_RATINGS = "CREATE TABLE $this->ratings ( `id` int(11) NOT NULL auto_increment, `ratable_id` int(11) NOT NULL, `ip_address` varchar(50) NOT NULL, `rating` int(11) NOT NULL, `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE ( `ratable_id` , `ip_address`), CONSTRAINT rabid_ratings_fk FOREIGN KEY (`ratable_id`) REFERENCES $this->ratables(id) ) AUTO_INCREMENT=1 ;"; $result = mysql_query("SHOW TABLES;"); while ($table = mysql_fetch_array($result)) { if ($table[0] == $this->ratables) { $ratablesFound = true; } if ($table[0] == $this->ratings) { $ratingsFound = true; } } if ($ratablesFound != true) { mysql_query($SQL_CREATE_RATABLES); } if ($ratingsFound != true) { mysql_query($SQL_CREATE_RATINGS); } } function loadRating($ratableKey) { $WHERE = "WHERE `ratableKey` = '$ratableKey'"; $SQL_GET_RATINGS = "SELECT $this->ratables.id AS keyID, AVG( $this->ratings.rating ) AS rating, COUNT( $this->ratings.rating ) AS totalRatings FROM $this->ratables JOIN $this->ratings ON ( $this->ratables.id = $this->ratings.ratable_id ) $WHERE GROUP BY keyID;"; $result = mysql_fetch_array(mysql_query($SQL_GET_RATINGS), MYSQL_ASSOC); if ($result == null) { $result = $this->createNewKey($ratableKey); } return $result; } function createNewKey($key) { $SQL_INSERT_KEY = "INSERT INTO $this->ratables(ratableKey) " ."VALUES ('$key');"; $SQL_GET_NEW_KEY = "SELECT id AS keyID FROM $this->ratables WHERE " ."ratableKey = '$key';"; mysql_query($SQL_INSERT_KEY); $newKey = mysql_fetch_array(mysql_query($SQL_GET_NEW_KEY), MYSQL_ASSOC); $newKey['rating'] = 0; $newKey['totalRatings'] = 0; return $newKey; } function percentToStars($percent) { $modifier = 100 / $this->scale; return round($percent / $modifier, 1); } } ?>