MDL-25031 delete blocks for missing courses and activities

This commit is contained in:
Petr Škoda
2013-05-24 18:31:59 +02:00
parent 57af11ee2d
commit 424d02fdd4
+24
View File
@@ -1663,6 +1663,8 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
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 . ",
@@ -1670,6 +1672,17 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
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 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
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