Permalink
Browse files

MDL-37939 Course: Correct broken sequence data

  • Loading branch information...
1 parent 4bcf7d3 commit 31db9c51a2d690ed80962ba69a7a15e2c9e42cfb @andrewnicols andrewnicols committed Feb 12, 2013
Showing with 49 additions and 0 deletions.
  1. +49 −0 lib/db/upgrade.php
View
@@ -992,5 +992,54 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2012062503.07);
}
+ if ($oldversion < 2012062504.06) {
+ // 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, 2012062504.06);
+ }
+
return true;
}

0 comments on commit 31db9c5

Please sign in to comment.