rating.class.php
Class for ratings and likes. You can apply it on tables: articles, articlescategories, comments, files, filescategories, images, imagescategories, marketproducts, market_categories and users.
Usage:
{{lang:php}}
// INITIALIZATION
$rat = new rating($DB); // $DB is the object from sql class
$rat->set_tablename('articles');
$rat->set_row_id(17);
$rat->set_user_id($_SESSION['user_id']); // optional
$rat->set_extra(date('Y-m-d H:i:s')); // optional, set anything you want into extra field (varchar 255)
// USAGE
$rat->vote(3); // sets vote 3 to the article ID 17
$rat->like(); //sets like to the article ID 17
$rat->dislike(); //sets dislike to the article ID 17
// DELETING RATINGS
$rat->reset_votes(); // clears all votes of article ID 17
$rat->reset_likes(); // clears all likes of article ID 17
$rat->reset(); // clears all votes and likes of article ID 17
// GETTING RESULTS
$rat->set_tablename('market_products');
$rat->set_row_id(9);
$rat->get_rating(); // gets rating of market product ID 9
$rat->get_likes(); // gets likes of market product ID 9 as array('up' => 7, 'down' => 4)
$rat->get_users_vote(4); // gets vote of market product ID 9 by user ID 4
// GETTING GROUP RESULTS
$array = $DB->get_col("SELECT id FROM market_products WHERE cid = 1 AND lang = '$GET[lang]' AND published = 1");
$rat->get_ratings($array); // returns array of ratings for every id
/*
array(
1 => array ('rating' => 2.81, 'votes' => 6),
2 => array ('rating' => 3.21, 'votes' => 5),
3 => array ('rating' => 4.11, 'votes' => 25),
7 => array ('rating' => 2.22, 'votes' => 11),
9 => array ('rating' => 1.93, 'votes' => 7)
)
*/