diff options
Diffstat (limited to 'site/app/models/review.php')
-rw-r--r-- | site/app/models/review.php | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/site/app/models/review.php b/site/app/models/review.php index 8c53304..96ad236 100644 --- a/site/app/models/review.php +++ b/site/app/models/review.php @@ -231,6 +231,78 @@ class Review extends AppModel } return $results; } + + /** + * Update the bayesian rating (cf. bug 477343) for a single (or multiple) add-on(s). + * Also updates average rating and total review count. + * + * Note that similar code exists in the reviews/ratings sections of bin/maintenance.php + * + * @param array addonids add-on IDs whose bayesian ratings to update. + * @return boolean success + */ + function updateBayesianRating($addonids = array()) { + if (empty($addonids)) return false; + + // get average review count and average rating + $rows = $this->query(" + SELECT AVG(a.cnt) AS avg_cnt + FROM ( + SELECT COUNT(*) AS cnt + FROM reviews AS r + INNER JOIN versions AS v ON (r.version_id = v.id) + WHERE reply_to IS NULL + AND rating > 0 + GROUP BY v.addon_id + ) AS a + "); + $avg_num_votes = $rows[0][0]['avg_cnt']; + + $rows = $this->query(" + SELECT AVG(a.addon_rating) AS avg_rating + FROM ( + SELECT AVG(rating) AS addon_rating + FROM reviews AS r + INNER JOIN versions AS v ON (r.version_id = v.id) + WHERE reply_to IS NULL + AND rating > 0 + GROUP BY v.addon_id + ) AS a + "); + $avg_rating = $rows[0][0]['avg_rating']; + + // update total review count and average rating + $this->query(" + UPDATE addons AS a + INNER JOIN ( + SELECT + versions.addon_id as addon_id, + COUNT(*) as count, + AVG(rating) as avg_rating + FROM reviews + INNER JOIN versions ON reviews.version_id = versions.id + WHERE reviews.reply_to IS NULL + AND versions.addon_id IN (".(implode(',',$addonids)).") + AND reviews.rating > 0 + GROUP BY versions.addon_id + ) AS c ON (a.id = c.addon_id) + SET a.totalreviews = c.count, + a.averagerating = ROUND(c.avg_rating, 2) + WHERE a.id IN (".(implode(',',$addonids)).") + "); + + // calculate and store bayesian rating + $this->query(" + UPDATE addons AS a + SET a.bayesianrating = + IF (a.totalreviews > 0, ( + ( ({$avg_num_votes} * {$avg_rating}) + (a.totalreviews * a.averagerating) ) / + ({$avg_num_votes} + a.totalreviews) + ), 0) + WHERE a.id IN (".(implode(',',$addonids)).") + "); + return true; + } } ?> |