Skip to content

Commit

Permalink
MDL-37939 Course: Correct broken sequence data
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Robert Nicols committed Feb 15, 2013
1 parent 09de5eb commit 41d397e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
49 changes: 49 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 41d397e

Please sign in to comment.