diff --git a/lib/db/upgradelib.php b/lib/db/upgradelib.php index 911eb483f67..4d4926dd691 100644 --- a/lib/db/upgradelib.php +++ b/lib/db/upgradelib.php @@ -395,4 +395,60 @@ function upgrade_cleanup_unwanted_block_contexts($contextidarray) { $outcome4 = $DB->delete_records_select('context', 'id IN ('.$blockcontextidsstring.')'); 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 diff --git a/lib/upgradelib.php b/lib/upgradelib.php index 9f594d5ffd1..ed7899804b4 100644 --- a/lib/upgradelib.php +++ b/lib/upgradelib.php @@ -1508,54 +1508,3 @@ function admin_mnet_method_profile(Zend_Server_Reflection_Function_Abstract $fun } 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; -} diff --git a/mod/data/db/upgrade.php b/mod/data/db/upgrade.php index 3a62f178035..8c160822c41 100644 --- a/mod/data/db/upgrade.php +++ b/mod/data/db/upgrade.php @@ -255,6 +255,8 @@ function xmldb_data_upgrade($oldversion) { 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 diff --git a/mod/forum/db/upgrade.php b/mod/forum/db/upgrade.php index fd332a8022e..8cd0131fc76 100644 --- a/mod/forum/db/upgrade.php +++ b/mod/forum/db/upgrade.php @@ -287,6 +287,8 @@ function xmldb_forum_upgrade($oldversion) { 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 diff --git a/mod/glossary/db/upgrade.php b/mod/glossary/db/upgrade.php index 144a670b249..8c5b4e6736d 100644 --- a/mod/glossary/db/upgrade.php +++ b/mod/glossary/db/upgrade.php @@ -259,6 +259,8 @@ function xmldb_glossary_upgrade($oldversion) { 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 diff --git a/rating/lib.php b/rating/lib.php index bc298568e37..0b76899253a 100644 --- a/rating/lib.php +++ b/rating/lib.php @@ -225,6 +225,8 @@ class rating_manager { 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 @@ class rating_manager { WHERE r.contextid = :contextid AND r.itemid $itemidtest - GROUP BY r.itemid, ur.rating + GROUP BY r.itemid, ur.rating, ur.id, ur.userid, ur.scaleid ORDER BY r.itemid"; $params['userid'] = $options->userid;