Skip to content

Commit

Permalink
rating MDL-23389 altered the grade retrieval sql to avoid something p…
Browse files Browse the repository at this point in the history
…ostgres doesnt like
  • Loading branch information
Andrew Davis committed Jul 19, 2010
1 parent db42bc5 commit 0680752
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions rating/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ public function get_ratings($options) {
return $options->items;
}

$userid = null;
if (empty($options->userid)) {
$userid = $USER->id;
} else {
Expand Down Expand Up @@ -436,7 +437,7 @@ private function get_item_time_created($item) {
/**
* Returns an array of grades calculated by aggregating item ratings.
* @param object $options {
* userid => int the id of the user whose items have been rated. NOT the user who submitted the ratings [required]
* userid => int the id of the user whose items have been rated. NOT the user who submitted the ratings. 0 to update all. [required]
* aggregationmethod => int the aggregation method to apply when calculating grades ie RATING_AGGREGATE_AVERAGE [required]
* scaleid => int the scale from which the user can select a rating. Used for bounds checking. [required]
* itemtable => int the table containing the items [required]
Expand Down Expand Up @@ -465,7 +466,7 @@ public function get_user_grades($options) {
}
else if ( !empty($options->modulename) && !empty($options->moduleid) ) {
$modulename = $options->modulename;
$moduleid = $options->moduleid;
$moduleid = intval($options->moduleid);

//going direct to the db for the context id seems wrong
list($ctxselect, $ctxjoin) = context_instance_preload_sql('cm.id', CONTEXT_MODULE, 'ctx');
Expand All @@ -485,23 +486,22 @@ public function get_user_grades($options) {
$scaleid = $options->scaleid;
$aggregationstring = $this->get_aggregation_method($options->aggregationmethod);

//if userid is 0 we want all user grades within this context
//if userid is not 0 we only want the grade for a single user
$singleuserwhere = '';
if ($options->userid!=0) {
$params['userid1'] = $params['userid2'] = $params['userid3'] = $options->userid;
$sql = "SELECT :userid1 AS id, :userid2 AS userid, $aggregationstring(r.rating) AS rawgrade
FROM {rating} r
JOIN {{$itemtable}} i ON r.itemid=i.id
WHERE r.contextid=:contextid
AND i.{$itemtableusercolumn} = :userid3";
} else {
$sql = "SELECT u.id as id, u.id AS userid, $aggregationstring(r.rating) AS rawgrade
FROM {user} u
LEFT JOIN {{$itemtable}} i ON u.id=i.{$itemtableusercolumn}
LEFT JOIN {rating} r ON r.itemid=i.id
WHERE (r.contextid is null or r.contextid=:contextid)
GROUP BY u.id";
$params['userid1'] = intval($options->userid);
$singleuserwhere = "AND i.{$itemtableusercolumn} = :userid1";
}

//note: r.contextid will be null for users who haven't been rated yet
$sql = "SELECT u.id as id, u.id AS userid, $aggregationstring(r.rating) AS rawgrade
FROM {user} u
LEFT JOIN {{$itemtable}} i ON u.id=i.{$itemtableusercolumn}
LEFT JOIN {rating} r ON r.itemid=i.id
WHERE (r.contextid is null or r.contextid=:contextid)
$singleuserwhere
GROUP BY u.id";

$results = $DB->get_records_sql($sql, $params);

if ($results) {
Expand Down

0 comments on commit 0680752

Please sign in to comment.