# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Apply Diff Patch action on respective folder.
# It uses platform neutral UTF-8 encoding.
# Above lines and this line are ignored by the patching process.
Index: moodle/lib/db/upgradelib.php
--- moodle/lib/db/upgradelib.php Base (1.34)
+++ moodle/lib/db/upgradelib.php Locally Modified (Based On 1.34)
@@ -396,3 +396,59 @@
 
     return ($outcome1 && $outcome2 && $outcome4 && $outcome4);
 }
+
+/**
+ * Shifts ratings from modules into the central rating table
+ *
+ * @param string $ratingssql sql to return the module's rating
+ * @param string $modulename the name of the module
+ * @return boolean success flag
+ */
+function upgrade_module_ratings($ratingssql, $modulename) {
+    global $DB;
+    $contextid = null;
+    $contextarray = array();
+    $result = true;
+    $i=0;
+
+    $ratings = $DB->get_recordset_sql($ratingssql);
+
+    foreach ($ratings as $old_rating) {
+        if($i++%500==0) {
+            upgrade_set_timeout(60);//prevent a timeout
+        }
+
+        //all posts within a given forum, glossary etc will have the same context id so store them in an array
+        if( !array_key_exists($old_rating->mid, $contextarray) ) {
+	    $sql = "SELECT cxt.id
+                      FROM {context} cxt
+                      JOIN {course_modules} cm ON cm.id = cxt.instanceid
+                      JOIN {modules} m ON cm.module = m.id
+                     WHERE m.name = :modulename AND cm.instance = :moduleinstanceid AND cxt.contextlevel = :contextmodule";
+            $params = array();
+	    $params['modulename'] = $modulename;
+            $params['moduleinstanceid'] = $old_rating->mid;
+	    $params['contextmodule'] = CONTEXT_MODULE;
+            $results = $DB->get_record_sql($sql, $params);
+            $contextarray[$old_rating->mid] = $results->id;
+        }
+        $contextid = $contextarray[$old_rating->mid];
+
+        $rating = new stdclass();
+        $rating->contextid = $contextid;
+        $rating->scaleid = $old_rating->scale;
+        $rating->itemid = $old_rating->itemid;
+        $rating->rating = $old_rating->rating;
+        $rating->userid = $old_rating->userid;
+        $rating->timecreated = $old_rating->timecreated;
+        $rating->timemodified = $old_rating->timemodified;
+
+        if( !$DB->insert_record('rating', $rating) ) {
+	    return false;
+	}
+    }
+
+    $ratings->close();
+
+    return true;
+}
\ No newline at end of file
Index: moodle/lib/upgradelib.php
--- moodle/lib/upgradelib.php Base (1.49)
+++ moodle/lib/upgradelib.php Locally Modified (Based On 1.49)
@@ -1508,54 +1508,3 @@
     }
     return $profile;
 }
-
-/**
- * Shifts ratings from modules into the central rating table
- *
- * @param string $ratingssql sql to return the module's rating
- * @param string $modulename the name of the module
- * @return boolean success flag
- */
-function upgrade_module_ratings($ratingssql, $modulename) {
-    global $DB;
-    $contextid = null;
-    $contextarray = array();
-    $result = true;
-    $i=0;
-
-    $ratings = $DB->get_records_sql($ratingssql);
-
-    foreach ($ratings as $old_rating) {
-        if($i++%500==0) {
-            upgrade_set_timeout(60);//prevent a timeout
-        }
-
-        //all posts within a given forum, glossary etc will have the same context id so store them in an array
-        if( !array_key_exists($old_rating->mid, $contextarray) ) {
-            $sql = 'select cxt.id from {course_modules} cm inner join {modules} m on cm.module=m.id
-inner join {context} cxt on cxt.instanceid=cm.id
-where m.name=:modulename and cm.instance=:moduleinstanceid and cxt.contextlevel='.CONTEXT_MODULE;
-            $params = array();
-            $params['moduleinstanceid'] = $old_rating->mid;
-            $params['modulename'] = $modulename;
-            $results = $DB->get_record_sql($sql, $params);
-            $contextarray[$old_rating->mid] = $results->id;
-        }
-        $contextid = $contextarray[$old_rating->mid];
-
-        $rating = new stdclass;
-        $rating->contextid = $contextid;
-        $rating->scaleid = $old_rating->scale;
-        $rating->itemid = $old_rating->itemid;
-        $rating->rating = $old_rating->rating;
-        $rating->userid = $old_rating->userid;
-        $rating->timecreated = $old_rating->timecreated;
-        $rating->timemodified = $old_rating->timemodified;
-
-        $result = $result && $DB->insert_record('rating', $rating);
-    }
-
-    //$ratings->close();
-
-    return $result;
-}
Index: moodle/mod/data/db/upgrade.php
--- moodle/mod/data/db/upgrade.php Base (1.37)
+++ moodle/mod/data/db/upgrade.php Locally Modified (Based On 1.37)
@@ -255,6 +255,8 @@
 
     if($result && $oldversion < 2010031800) {
         //migrate data_ratings to the central rating table
+	require_once('lib/db/upgradelib.php');
+	
         //data ratings didnt store time created and modified so Im using the times from the record the rating was attached to
         $ratingssql = 'SELECT r.id AS rid, r.recordid AS itemid, r.rating, r.userid, re.timecreated, re.timemodified, d.scale, d.id AS mid
 FROM {data_ratings} r
Index: moodle/mod/forum/db/upgrade.php
--- moodle/mod/forum/db/upgrade.php Base (1.41)
+++ moodle/mod/forum/db/upgrade.php Locally Modified (Based On 1.41)
@@ -287,6 +287,8 @@
 
     if($result && $oldversion < 2010031800) {
         //migrate forumratings to the central rating table
+	require_once('lib/db/upgradelib.php');
+
         //forum ratings only have a single time column so use it for both time created and modified
         $ratingssql = 'SELECT r.id AS rid, r.post AS itemid, r.rating, r.userid, r.time AS timecreated, r.time AS timemodified, f.scale, f.id AS mid
 FROM {forum_ratings} r
Index: moodle/mod/glossary/db/upgrade.php
--- moodle/mod/glossary/db/upgrade.php Base (1.29)
+++ moodle/mod/glossary/db/upgrade.php Locally Modified (Based On 1.29)
@@ -259,6 +259,8 @@
 
     if($result && $oldversion < 2010031800) {
         //migrate glossary_ratings to the central rating table
+	require_once('lib/db/upgradelib.php');
+
         //glossary ratings only have a single time column so use it for both time created and modified
         $ratingssql = 'SELECT r.id AS rid, r.entryid AS itemid, r.rating, r.userid, r.time AS timecreated, r.time AS timemodified, g.scale, g.id AS mid
 FROM {glossary_ratings} r
Index: moodle/rating/lib.php
--- moodle/rating/lib.php Base (1.11)
+++ moodle/rating/lib.php Locally Modified (Based On 1.11)
@@ -225,6 +225,8 @@
         list($itemidtest, $params) = $DB->get_in_or_equal(
                 $itemids, SQL_PARAMS_NAMED, 'itemid0000');
 
+	//note: all the group bys arent really necessary but PostgreSQL complains
+	//about selecting a mixture of grouped and non-grouped columns
         $sql = "SELECT r.itemid, ur.id, ur.userid, ur.scaleid,
         $aggregatestr(r.rating) AS aggrrating,
         COUNT(r.rating) AS numratings,
@@ -236,7 +238,7 @@
     WHERE
         r.contextid = :contextid AND
         r.itemid $itemidtest
-    GROUP BY r.itemid, ur.rating
\ No newline at end of file
+    GROUP BY r.itemid, ur.rating, ur.id, ur.userid, ur.scaleid
\ No newline at end of file
     ORDER BY r.itemid";
 
         $params['userid'] = $options->userid;
