Permalink
Browse files

MDL-37939 Course: Correct broken sequence data

  • Loading branch information...
andrewnicols committed Feb 12, 2013
1 parent 09de5eb commit 41d397e68becf53e2a5586c7748f9f6fd07ebad8
Showing with 50 additions and 1 deletion.
  1. +49 −0 lib/db/upgrade.php
  2. +1 −1 version.php
View
@@ -1524,5 +1524,54 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2012120300.04);
}
if ($oldversion < 2012120301.09) {
// Retrieve the list of course_sections as a recordset to save memory
$coursesections = $DB->get_recordset('course_sections', null, 'course, id', 'id, course, sequence');
foreach ($coursesections as $coursesection) {
// Retrieve all of the actual modules in this course and section combination to reduce DB calls
$actualsectionmodules = $DB->get_records('course_modules',
array('course' => $coursesection->course, 'section' => $coursesection->id), '', 'id, section');
// Break out the current sequence so that we can compare it
$currentsequence = explode(',', $coursesection->sequence);
$newsequence = array();
// Check each of the modules in the current sequence
foreach ($currentsequence as $module) {
if (isset($actualsectionmodules[$module])) {
$newsequence[] = $module;
// We unset the actualsectionmodules so that we don't get duplicates and that we can add orphaned
// modules later
unset($actualsectionmodules[$module]);
}
}
// Append any modules which have somehow been orphaned
foreach ($actualsectionmodules as $module) {
$newsequence[] = $module->id;
}
// Piece it all back together
$sequence = implode(',', $newsequence);
// Only update if there have been changes
if ($sequence !== $coursesection->sequence) {
$coursesection->sequence = $sequence;
$DB->update_record('course_sections', $coursesection);
// And clear the sectioncache and modinfo cache - they'll be regenerated on next use
$course = new stdClass();
$course->id = $coursesection->course;
$course->sectioncache = null;
$course->modinfo = null;
$DB->update_record('course', $course);
}
}
$coursesections->close();
// Main savepoint reached.
upgrade_main_savepoint(true, 2012120301.09);
}
return true;
}
View
@@ -30,7 +30,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2012120301.08; // 20121203 = branching date YYYYMMDD - do not modify!
$version = 2012120301.09; // 20121203 = branching date YYYYMMDD - do not modify!
// RR = release increments - 00 in DEV branches
// .XX = incremental changes

0 comments on commit 41d397e

Please sign in to comment.