mirror of
https://github.com/e107inc/e107.git
synced 2025-01-17 20:58:30 +01:00
Rating Class: Option to reduce the number of ratings lookups. eg. {NEWS_RATE: multi=1}
This commit is contained in:
parent
fab33e46fc
commit
024cac1854
@ -13,14 +13,40 @@ if (!defined('e107_INIT')) { exit; }
|
||||
|
||||
e107::includeLan(e_LANGUAGEDIR.e_LANGUAGE."/lan_rate.php");
|
||||
|
||||
class rater {
|
||||
|
||||
|
||||
function render($table,$id,$options=array())
|
||||
class rater
|
||||
{
|
||||
private $ratings = array(); // loaded values.
|
||||
private $multi = false; // multiple lookup .
|
||||
|
||||
function __construct()
|
||||
{
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Render ratings widget
|
||||
*
|
||||
* @param string $table eg. 'news'
|
||||
* @param integer $id
|
||||
* @param array $options
|
||||
* @param bool $options['multi'] - set to true if used in a loop. ie. mutliple table lookups.
|
||||
* @param bool $options['readonly']
|
||||
* @param string $options['label']
|
||||
* @param string $options['template'] - default is 'STATUS |RATE|VOTES'
|
||||
* @param string $options['uniqueId'] (optional) = unique identifer to avoid ID conflicts;
|
||||
* *
|
||||
* @return string
|
||||
*/
|
||||
function render($table, $id, $options=array())
|
||||
{
|
||||
if(!empty($options['multi']))
|
||||
{
|
||||
$this->multi = true;
|
||||
}
|
||||
|
||||
list($votes,$score,$uvoted) = $this->getrating($table, $id);
|
||||
|
||||
|
||||
// return "Table=".$table." itmeId=".$id." Votes=".$votes." score=".$score;
|
||||
|
||||
if(is_string($options))
|
||||
@ -188,9 +214,14 @@ class rater {
|
||||
}
|
||||
}
|
||||
|
||||
function getrating($table, $id, $userid=FALSE) {
|
||||
function getrating($table, $id, $userid=false)
|
||||
{
|
||||
|
||||
//userid : boolean, get rating for a single user, or get general total rating of the item
|
||||
|
||||
|
||||
|
||||
|
||||
$table = preg_replace('/\W/', '', $table);
|
||||
$id = intval($id);
|
||||
|
||||
@ -198,55 +229,109 @@ class rater {
|
||||
{
|
||||
return RATELAN_10;
|
||||
}
|
||||
$sep = chr(1);
|
||||
|
||||
if($this->multi === true)
|
||||
{
|
||||
if(isset($this->ratings[$table]))
|
||||
{
|
||||
if(!empty($this->ratings[$table][$id]))
|
||||
{
|
||||
return $this->ratings[$table][$id];
|
||||
}
|
||||
|
||||
return array(0,0,0);
|
||||
|
||||
}
|
||||
|
||||
$tmp = e107::getDb('rate')->retrieve("rate", "*", "rate_table = '{$table}' ",true);
|
||||
$val = array();
|
||||
foreach($tmp as $row)
|
||||
{
|
||||
$rid = $row['rate_itemid'];
|
||||
$val[$rid] = $this->processRow($row,$userid);
|
||||
}
|
||||
|
||||
$this->ratings[$table] = $val;
|
||||
|
||||
if(!empty($this->ratings[$table][$id]))
|
||||
{
|
||||
return $this->ratings[$table][$id];
|
||||
}
|
||||
|
||||
return array(0,0,0);
|
||||
|
||||
}
|
||||
|
||||
// $sep = chr(1);
|
||||
|
||||
$sql = new db;
|
||||
if (!$sql->select("rate", "*", "rate_table = '{$table}' AND rate_itemid = '{$id}' "))
|
||||
{
|
||||
return FALSE;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
$rowgr = $sql->fetch();
|
||||
|
||||
if($userid==TRUE)
|
||||
|
||||
return $this->processRow($rowgr,$userid);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $rowgr
|
||||
* @param bool $userid
|
||||
* @return array
|
||||
*/
|
||||
private function processRow($rowgr,$userid = false)
|
||||
{
|
||||
$sep = chr(1);
|
||||
|
||||
if($userid == true)
|
||||
{
|
||||
$rating = array();
|
||||
|
||||
$rateusers = explode(".", $rowgr['rate_voters']);
|
||||
for($i = 0; $i < count($rateusers); $i++)
|
||||
{
|
||||
$rating = "";
|
||||
$rateusers = explode(".", $rowgr['rate_voters']);
|
||||
for($i=0;$i<count($rateusers);$i++){
|
||||
if(strpos($rateusers[$i], $sep)){
|
||||
$rateuserinfo[$i] = explode($sep, $rateusers[$i]);
|
||||
if($userid == $rateuserinfo[$i][0]){
|
||||
$rating[0] = 0; //number of votes, not relevant in users rating
|
||||
$rating[1] = $rateuserinfo[$i][1]; //the rating by this user
|
||||
$rating[2] = 0; //no remainder is present, because we have a single users rating
|
||||
break;
|
||||
}
|
||||
}else{
|
||||
$rating[0] = 0; //number of votes, not relevant in users rating
|
||||
$rating[1] = 0; //the rating by this user
|
||||
$rating[2] = 0; //no remainder is present, because we have a single users rating
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$rating[0] = $rowgr['rate_votes']; // $rating[0] == number of votes
|
||||
$tmp = $rowgr['rate_rating'] / $rowgr['rate_votes'];
|
||||
$tmp = (strpos($tmp,",")) ? explode(",", $tmp) : explode(".", $tmp);
|
||||
$rating[1] = $tmp[0];
|
||||
if(isset($tmp[1]))
|
||||
if(strpos($rateusers[$i], $sep))
|
||||
{
|
||||
$rating[2] = substr($tmp[1], 0, 1);
|
||||
$rateuserinfo[$i] = explode($sep, $rateusers[$i]);
|
||||
|
||||
if($userid == $rateuserinfo[$i][0])
|
||||
{
|
||||
$rating[0] = 0; //number of votes, not relevant in users rating
|
||||
$rating[1] = $rateuserinfo[$i][1]; //the rating by this user
|
||||
$rating[2] = 0; //no remainder is present, because we have a single users rating
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$rating[2] = "0";
|
||||
$rating[0] = 0; //number of votes, not relevant in users rating
|
||||
$rating[1] = 0; //the rating by this user
|
||||
$rating[2] = 0; //no remainder is present, because we have a single users rating
|
||||
}
|
||||
}
|
||||
|
||||
return $rating;
|
||||
}
|
||||
else
|
||||
{
|
||||
$rating[0] = $rowgr['rate_votes']; // $rating[0] == number of votes
|
||||
$tmp = $rowgr['rate_rating'] / $rowgr['rate_votes'];
|
||||
$tmp = (strpos($tmp, ",")) ? explode(",", $tmp) : explode(".", $tmp);
|
||||
$rating[1] = $tmp[0];
|
||||
|
||||
if(isset($tmp[1]))
|
||||
{
|
||||
$rating[2] = substr($tmp[1], 0, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
$rating[2] = "0";
|
||||
}
|
||||
}
|
||||
|
||||
return $rating;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user