diff options
Diffstat (limited to 'lib/class/rating.class.php')
-rw-r--r-- | lib/class/rating.class.php | 62 |
1 files changed, 33 insertions, 29 deletions
diff --git a/lib/class/rating.class.php b/lib/class/rating.class.php index 5d2f9bf2..d1b34822 100644 --- a/lib/class/rating.class.php +++ b/lib/class/rating.class.php @@ -65,41 +65,45 @@ class Rating extends database_object { */ public static function build_cache($type, $ids) { - $user_id = Dba::escape($GLOBALS['user']->id); + if ($ids) { + $user_id = Dba::escape($GLOBALS['user']->id); - $idlist = '(' . implode(',', $ids) . ')'; - $sql = "SELECT `rating`, `object_id`,`rating`.`rating` FROM `rating` WHERE `user`='$user_id' AND `object_id` IN $idlist " . - "AND `object_type`='$type'"; - $db_results = Dba::read($sql); + $idlist = '(' . implode(',', $ids) . ')'; + $sql = "SELECT `rating`, `object_id`,`rating`.`rating` FROM `rating` WHERE `user`='$user_id' AND `object_id` IN $idlist " . + "AND `object_type`='$type'"; + $db_results = Dba::read($sql); - while ($row = Dba::fetch_assoc($db_results)) { - $user[$row['object_id']] = $row['rating']; - } + while ($row = Dba::fetch_assoc($db_results)) { + $user[$row['object_id']] = $row['rating']; + } - $sql = "SELECT `rating`,`object_id` FROM `rating` WHERE `object_id` IN $idlist AND `object_type`='$type'"; - $db_results = Dba::read($sql); + $sql = "SELECT `rating`,`object_id` FROM `rating` WHERE `object_id` IN $idlist AND `object_type`='$type'"; + $db_results = Dba::read($sql); - while ($row = Dba::fetch_assoc($db_results)) { - $rating[$row['object_id']]['rating'] += $row['rating']; - $rating[$row['object_id']]['total']++; - } - - foreach ($ids as $id) { - parent::add_to_cache('rating_' . $type . '_user',$id,intval($user[$id])); - - // Do the bit of math required to store this - if (!isset($rating[$id])) { - $entry = array('average'=>'0','percise'=>'0'); - } - else { - $average = round($rating[$id]['rating']/$rating[$id]['total'],1); - $entry = array('average'=>floor($average),'percise'=>$average); - } + while ($row = Dba::fetch_assoc($db_results)) { + $rating[$row['object_id']]['rating'] += $row['rating']; + $rating[$row['object_id']]['total']++; + } + + foreach ($ids as $id) { + parent::add_to_cache('rating_' . $type . '_user',$id,intval($user[$id])); + + // Do the bit of math required to store this + if (!isset($rating[$id])) { + $entry = array('average'=>'0','percise'=>'0'); + } + else { + $average = round($rating[$id]['rating']/$rating[$id]['total'],1); + $entry = array('average'=>floor($average),'percise'=>$average); + } - parent::add_to_cache('rating_' . $type . '_all',$id,$entry); - } + parent::add_to_cache('rating_' . $type . '_all',$id,$entry); + } - return true; + return true; + } else { + return false; + } } // build_cache |