Browse files

Merge branch 'MDL-38173-23' of git://github.com/damyon/moodle into MO…

…ODLE_23_STABLE
  • Loading branch information...
2 parents c5c1f07 + a954624 commit 5379574a7d65a22cfdf9bb51d5f55a376d07434a @stronk7 stronk7 committed Mar 6, 2013
Showing with 70 additions and 4 deletions.
  1. +2 −2 course/lib.php
  2. +67 −1 lib/db/upgrade.php
  3. +1 −1 version.php
View
4 course/lib.php
@@ -2848,7 +2848,7 @@ function add_mod_to_section($mod, $beforemod=NULL) {
}
$DB->set_field("course_sections", "sequence", $newsequence, array("id"=>$section->id));
- $DB->set_field("course_modules", "section", $section->id, array("id" => $mod->id));
+ $DB->set_field("course_modules", "section", $section->id, array("id" => $mod->coursemodule));
return $section->id; // Return course_sections ID that was used.
} else { // Insert a new record
@@ -2859,7 +2859,7 @@ function add_mod_to_section($mod, $beforemod=NULL) {
$section->summaryformat = FORMAT_HTML;
$section->sequence = $mod->coursemodule;
$section->id = $DB->insert_record("course_sections", $section);
- $DB->set_field("course_modules", "section", $section->id, array("id" => $mod->id));
+ $DB->set_field("course_modules", "section", $section->id, array("id" => $mod->coursemodule));
return $section->id;
}
}
View
68 lib/db/upgrade.php
@@ -1011,7 +1011,9 @@ function xmldb_main_upgrade($oldversion) {
}
if ($oldversion < 2012062504.08) {
- // Retrieve the list of course_sections as a recordset to save memory
+ // This upgrade step is fixing broken sequence data in the course_sections table (MDL-37939).
+ // It may take a long time to run on large sites.
+ // 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
@@ -1059,5 +1061,69 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2012062504.08);
}
+ // This is checking to see if the site has been running a specific version with a bug in it
+ // because this upgrade step is slow and is only needed if the site has been running with the previous patch installed.
+ if ($oldversion >= 2012062504.08 && $oldversion < 2012062504.11) {
+ // Retrieve the list of course_sections as a recordset to save memory.
+ // This is to fix a regression caused by MDL-37939.
+ // In this case the upgrade step is fixing records where:
+ // The data in course_sections.sequence contains the correct module id
+ // The section field for on the course modules table may have been updated to point to the incorrect id.
+
+ // This query is looking for sections where the sequence is not in sync with the course_modules table.
+ // The syntax for the like query is looking for a value in a comma separated list.
+ // It adds a comma to either site of the list and then searches for LIKE '%,id,%'.
+ $sequenceconcat = $DB->sql_concat("','", 's.sequence', "','");
+ $moduleconcat = $DB->sql_concat("'%,'", 'cm.id', "',%'");
+ $sql = 'SELECT s2.id, s2.course, s2.sequence
+ FROM mdl_course_sections s2
+ JOIN(
+ SELECT DISTINCT s.id
+ FROM
+ {course_modules} cm
+ JOIN {course_sections} s
+ ON
+ cm.course = s.course
+ WHERE cm.section != s.id AND ' . $sequenceconcat . ' LIKE ' . $moduleconcat . '
+ ) d
+ ON s2.id = d.id';
+ $coursesections = $DB->get_recordset_sql($sql);
+
+ 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);
+ $orphanlist = array();
+
+ // Check each of the modules in the current sequence.
+ foreach ($currentsequence as $cmid) {
+ if (!empty($cmid) && !isset($actualsectionmodules[$cmid])) {
+ $orphanlist[] = $cmid;
+ }
+ }
+
+ if (!empty($orphanlist)) {
+ list($sql, $params) = $DB->get_in_or_equal($orphanlist, SQL_PARAMS_NAMED);
+ $sql = "id $sql";
+
+ $DB->set_field_select('course_modules', 'section', $coursesection->id, $sql, $params);
+
+ // 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.11);
+ }
+
return true;
}
View
2 version.php
@@ -30,7 +30,7 @@
defined('MOODLE_INTERNAL') || die();
-$version = 2012062504.10; // YYYYMMDD = weekly release date of this DEV branch
+$version = 2012062504.11; // YYYYMMDD = weekly release date of this DEV branch
// RR = release increments - 00 in DEV branches
// .XX = incremental changes

0 comments on commit 5379574

Please sign in to comment.