Skip to content
Browse files

MDL-25031 delete blocks for missing courses and activities

  • Loading branch information...
1 parent 57af11e commit 424d02fdd4c150ba28abc9fe51a41826a26cd8bc @skodak skodak committed May 24, 2013
Showing with 24 additions and 0 deletions.
  1. +24 −0 lib/db/upgrade.php
View
24 lib/db/upgrade.php
@@ -1663,13 +1663,26 @@ function xmldb_main_upgrade($oldversion) {
if ($oldversion < 2009050613) {
/// fill in contextid and subpage, and update pagetypepattern from pagetype and pageid
+ upgrade_set_timeout(60*20);
+
/// site-index
$frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
$DB->execute("UPDATE {block_instances} SET contextid = " . $frontpagecontext->id . ",
pagetypepattern = 'site-index',
subpagepattern = NULL
WHERE pagetypepattern = 'site-index'");
+ /// delete all blocks from incorrectly deleted courses
+ $rs = $DB->get_recordset_sql(
+ "SELECT bi.id
+ FROM {block_instances} bi
+ LEFT JOIN {course} c ON (c.id = bi.pageid)
+ WHERE bi.pagetypepattern = 'course-view' AND c.id IS NULL");
+ foreach ($rs as $bi) {
+ $DB->delete_records('block_instances', array('id'=>$bi->id));
+ }
+ $rs->close();
+
/// course-view
$DB->execute("UPDATE {block_instances} SET
contextid = (
@@ -1727,6 +1740,17 @@ function xmldb_main_upgrade($oldversion) {
if (!$dbman->table_exists($modname)) {
continue;
}
+ /// delete all blocks that belonged to incorrectly deleted activities
+ $rs = $DB->get_recordset_sql(
+ "SELECT bi.id
+ FROM {block_instances} bi
+ LEFT JOIN {{$modname}} m ON (m.id = bi.pageid)
+ WHERE bi.pagetypepattern = 'mod-$modname-view' AND m.id IS NULL");
+ foreach ($rs as $bi) {
+ $DB->delete_records('block_instances', array('id'=>$bi->id));
+ }
+ $rs->close();
+
$DB->execute("UPDATE {block_instances} SET
contextid = (
SELECT {context}.id

0 comments on commit 424d02f

Please sign in to comment.
Something went wrong with that request. Please try again.