Skip to content

Commit

Permalink
MDL-27845 rating: altered the get_ratings() query to avoid possible f…
Browse files Browse the repository at this point in the history
…uture duplicate key problems
  • Loading branch information
andyjdavis committed Aug 5, 2011
1 parent 675c2f5 commit 02f4051
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions rating/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ public function get_ratings($options) {
$params['component'] = $options->component;
$params['ratingarea'] = $options->ratingarea;

$sql = "SELECT r.itemid, r.id, r.userid, r.scaleid, r.rating AS usersrating
$sql = "SELECT r.id, r.itemid, r.userid, r.scaleid, r.rating AS usersrating
FROM {rating} r
WHERE r.userid = :userid AND
r.contextid = :contextid AND
Expand All @@ -593,20 +593,28 @@ public function get_ratings($options) {
$ratingoptions->ratingarea = $options->ratingarea;
$ratingoptions->settings = $this->generate_rating_settings_object($options);
foreach ($options->items as $item) {
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 = $userratings[$item->{$itemidcol}];
$ratingoptions->scaleid = $rec->scaleid;
$ratingoptions->userid = $rec->userid;
$ratingoptions->id = $rec->id;
$ratingoptions->rating = min($rec->usersrating, $ratingoptions->settings->scale->max);
} else {
$founduserrating = false;
foreach($userratings as $userrating) {
//look for an existing rating from this user of this item
if ($item->{$itemidcol} == $userrating->itemid) {
// Note: rec->scaleid = the id of scale at the time the rating was submitted
// may be different from the current scale id
$ratingoptions->scaleid = $userrating->scaleid;
$ratingoptions->userid = $userrating->userid;
$ratingoptions->id = $userrating->id;
$ratingoptions->rating = min($userrating->usersrating, $ratingoptions->settings->scale->max);

$founduserrating = true;
break;
}
}
if (!$founduserrating) {
$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};
Expand Down

0 comments on commit 02f4051

Please sign in to comment.