62
* This attempts to get everything we'll need for this page load in a single query, saving
63
* the connection overhead
64
* //FIXME: Improve logic so that misses get cached as average
66
public static function build_cache($type, $ids) {
68
if (!is_array($ids) OR !count($ids)) { return false; }
70
$user_id = Dba::escape($GLOBALS['user']->id);
72
$idlist = '(' . implode(',', $ids) . ')';
73
$sql = "SELECT `rating`, `object_id`,`rating`.`rating` FROM `rating` WHERE `user`='$user_id' AND `object_id` IN $idlist " .
74
"AND `object_type`='$type'";
75
$db_results = Dba::read($sql);
77
while ($row = Dba::fetch_assoc($db_results)) {
78
$user[$row['object_id']] = $row['rating'];
81
$sql = "SELECT `rating`,`object_id` FROM `rating` WHERE `object_id` IN $idlist AND `object_type`='$type'";
82
$db_results = Dba::read($sql);
84
while ($row = Dba::fetch_assoc($db_results)) {
85
$rating[$row['object_id']]['rating'] += $row['rating'];
86
$rating[$row['object_id']]['total']++;
89
foreach ($ids as $id) {
90
parent::add_to_cache('rating_' . $type . '_user',$id,intval($user[$id]));
92
// Do the bit of math required to store this
93
if (!isset($rating[$id])) {
94
$entry = array('average'=>'0','percise'=>'0');
97
$average = round($rating[$id]['rating']/$rating[$id]['total'],1);
98
$entry = array('average'=>floor($average),'percise'=>$average);
101
parent::add_to_cache('rating_' . $type . '_all',$id,$entry);
62
110
* Get the user's rating this is based off the currently logged
63
111
* in user. It returns the value
65
public function get_user($user_id) {
67
$user_id = Dba::escape($user_id);
69
$sql = "SELECT `rating` FROM `rating` WHERE `user`='$user_id' AND `object_id`='$this->id' AND `object_type`='$this->type'";
113
public function get_user($user_id) {
115
$id = intval($this->id);
117
if (parent::is_cached('rating_' . $this->type . '_user',$id)) {
118
return parent::get_from_cache('rating_' . $this->type . '_user',$id);
121
$user_id = Dba::escape($user_id);
123
$sql = "SELECT `rating` FROM `rating` WHERE `user`='$user_id' AND `object_id`='$id' AND `object_type`='$this->type'";
70
124
$db_results = Dba::query($sql);
72
126
$results = Dba::fetch_assoc($db_results);
128
parent::add_to_cache('rating_' . $this->type . '_user',$id,$results['rating']);
74
130
return $results['rating'];
85
141
public function get_average() {
87
$sql = "SELECT `rating` FROM `rating` WHERE `object_id`='$this->id' AND `object_type`='$this->type'";
143
$id = intval($this->id);
145
if (parent::is_cached('rating_' . $this->type . '_all',$id)) {
146
$data = parent::get_from_cache('rating_' . $this->type . '_user',$id);
147
$this->rating = $data['rating'];
148
$this->perciserating = $data['percise'];
152
$sql = "SELECT `rating` FROM `rating` WHERE `object_id`='$id' AND `object_type`='$this->type'";
88
153
$db_results = Dba::query($sql);