Skip to content

Commit

Permalink
MDL-27845 rating: split the get_ratings() query into two to reduce qu…
Browse files Browse the repository at this point in the history
…ery complexity
  • Loading branch information
andyjdavis committed Jul 25, 2011
1 parent a924c53 commit 675c2f5
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions rating/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 675c2f5

Please sign in to comment.