diff options
author | momo-i <momo-i@ampache> | 2008-09-03 22:44:01 +0000 |
---|---|---|
committer | momo-i <momo-i@ampache> | 2008-09-03 22:44:01 +0000 |
commit | 01670c6abda538b5b40893f5015bfc4913382841 (patch) | |
tree | d20886a34636087129877d3284dd1701d42daee1 /lib/class/rating.class.php | |
parent | 64cbf29ff39637f91ab938e8370b67a604c74f9a (diff) | |
download | ampache-01670c6abda538b5b40893f5015bfc4913382841.tar.gz ampache-01670c6abda538b5b40893f5015bfc4913382841.tar.bz2 ampache-01670c6abda538b5b40893f5015bfc4913382841.zip |
simplify code, remove large if else statement
Diffstat (limited to 'lib/class/rating.class.php')
-rw-r--r-- | lib/class/rating.class.php | 64 |
1 files changed, 31 insertions, 33 deletions
diff --git a/lib/class/rating.class.php b/lib/class/rating.class.php index d1b34822..045be750 100644 --- a/lib/class/rating.class.php +++ b/lib/class/rating.class.php @@ -65,45 +65,43 @@ class Rating extends database_object { */ public static function build_cache($type, $ids) { - if ($ids) { - $user_id = Dba::escape($GLOBALS['user']->id); + if (!is_array($ids) OR !count($ids)) { return false; } - $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); + $user_id = Dba::escape($GLOBALS['user']->id); - while ($row = Dba::fetch_assoc($db_results)) { - $user[$row['object_id']] = $row['rating']; - } + $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']; + } - $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); - } - - parent::add_to_cache('rating_' . $type . '_all',$id,$entry); + 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); + } - return true; - } else { - return false; - } + return true; } // build_cache |