Permalink
Browse files

MDL-27845 rating: split the get_ratings() query into two to reduce qu…

…ery complexity
  • Loading branch information...
1 parent fc39162 commit 2f74865eef25c0a46f1146af1f931df6babc4cc5 @andyjdavis andyjdavis committed Jul 25, 2011
Showing with 24 additions and 17 deletions.
  1. +24 −17 rating/lib.php
View
41 rating/lib.php
@@ -567,48 +567,55 @@ public function get_ratings($options) {
$params['component'] = $options->component;
$params['ratingarea'] = $options->ratingarea;
- $sql = "SELECT r.itemid, r.component, r.ratingarea, r.contextid,
- $aggregatestr(r.rating) AS aggrrating, COUNT(r.rating) AS numratings,
- ur.id, ur.userid, ur.scaleid, ur.rating AS usersrating
+ $sql = "SELECT r.itemid, r.id, r.userid, r.scaleid, r.rating AS usersrating
+ FROM {rating} r
+ WHERE r.userid = :userid AND
+ r.contextid = :contextid AND
+ r.itemid {$itemidtest} AND
+ r.component = :component AND
+ r.ratingarea = :ratingarea
+ ORDER BY r.itemid";
+ $userratings = $DB->get_records_sql($sql, $params);
+
+ $sql = "SELECT r.itemid, $aggregatestr(r.rating) AS aggrrating, COUNT(r.rating) AS numratings
FROM {rating} r
- LEFT JOIN {rating} ur ON ur.contextid = r.contextid AND
- ur.itemid = r.itemid AND
- ur.component = r.component AND
- ur.ratingarea = r.ratingarea AND
- ur.userid = :userid
WHERE r.contextid = :contextid AND
r.itemid {$itemidtest} AND
r.component = :component AND
r.ratingarea = :ratingarea
- GROUP BY r.itemid, r.component, r.ratingarea, r.contextid, ur.id, ur.userid, ur.scaleid
+ GROUP BY r.itemid, r.component, r.ratingarea, r.contextid
ORDER BY r.itemid";
- $ratingsrecords = $DB->get_records_sql($sql, $params);
+ $aggregateratings = $DB->get_records_sql($sql, $params);
$ratingoptions = new stdClass;
$ratingoptions->context = $options->context;
$ratingoptions->component = $options->component;
$ratingoptions->ratingarea = $options->ratingarea;
$ratingoptions->settings = $this->generate_rating_settings_object($options);
foreach ($options->items as $item) {
- if (array_key_exists($item->{$itemidcol}, $ratingsrecords)) {
+ if (array_key_exists($item->{$itemidcol}, $userratings)) {
// Note: rec->scaleid = the id of scale at the time the rating was submitted
// may be different from the current scale id
- $rec = $ratingsrecords[$item->{$itemidcol}];
- $ratingoptions->itemid = $item->{$itemidcol};
+ $rec = $userratings[$item->{$itemidcol}];
$ratingoptions->scaleid = $rec->scaleid;
$ratingoptions->userid = $rec->userid;
$ratingoptions->id = $rec->id;
- $ratingoptions->aggregate = min($rec->aggrrating, $ratingoptions->settings->scale->max);
- $ratingoptions->count = $rec->numratings;
$ratingoptions->rating = min($rec->usersrating, $ratingoptions->settings->scale->max);
} else {
- $ratingoptions->itemid = $item->{$itemidcol};
$ratingoptions->scaleid = null;
$ratingoptions->userid = null;
$ratingoptions->id = null;
+ $ratingoptions->rating = null;
+ }
+ if (array_key_exists($item->{$itemidcol}, $aggregateratings)) {
+ $rec = $aggregateratings[$item->{$itemidcol}];
+ $ratingoptions->itemid = $item->{$itemidcol};
+ $ratingoptions->aggregate = min($rec->aggrrating, $ratingoptions->settings->scale->max);
+ $ratingoptions->count = $rec->numratings;
+ } else {
+ $ratingoptions->itemid = $item->{$itemidcol};
$ratingoptions->aggregate = null;
$ratingoptions->count = 0;
- $ratingoptions->rating = null;
}
$rating = new rating($ratingoptions);

0 comments on commit 2f74865

Please sign in to comment.