Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-38173 Course: Fix DB from regression when adding a (new) course m…

…odule to a section.

Updated to remove performance fix from first upgrade step.
  • Loading branch information...
commit 2b0b5a203d7bb43ccb84609974807a6b1c1bb3cb 1 parent 14c2bad
Damyon Wiese damyon authored damyon committed

Showing 1 changed file with 52 additions and 1 deletion. Show diff stats Hide diff stats

  1. +52 1 lib/db/upgrade.php
53 lib/db/upgrade.php
@@ -1011,7 +1011,7 @@ function xmldb_main_upgrade($oldversion) {
1011 1011 }
1012 1012
1013 1013 if ($oldversion < 2012062504.08) {
1014   - // Retrieve the list of course_sections as a recordset to save memory
  1014 + // Retrieve the list of course_sections as a recordset to save memory.
1015 1015 $coursesections = $DB->get_recordset('course_sections', null, 'course, id', 'id, course, sequence');
1016 1016 foreach ($coursesections as $coursesection) {
1017 1017 // Retrieve all of the actual modules in this course and section combination to reduce DB calls
@@ -1058,6 +1058,57 @@ function xmldb_main_upgrade($oldversion) {
1058 1058 // Main savepoint reached.
1059 1059 upgrade_main_savepoint(true, 2012062504.08);
1060 1060 }
  1061 + if ($oldversion < 2012062504.10) {
  1062 + // Retrieve the list of course_sections as a recordset to save memory.
  1063 + $sequenceconcat = $DB->sql_concat("','", 's.sequence', "','");
  1064 + $moduleconcat = $DB->sql_concat("'%,'", 'm.id', "',%'");
  1065 + $sql = 'SELECT
  1066 + s.id,
  1067 + s.course,
  1068 + s.sequence
  1069 + FROM
  1070 + {course_modules} m
  1071 + JOIN {course_sections} s
  1072 + ON
  1073 + m.section != s.id
  1074 + WHERE ' . $sequenceconcat . ' LIKE ' . $moduleconcat;
  1075 + $coursesections = $DB->get_recordset_sql($sql);
  1076 +
  1077 + foreach ($coursesections as $coursesection) {
  1078 + // Retrieve all of the actual modules in this course and section combination to reduce DB calls
  1079 + $actualsectionmodules = $DB->get_records('course_modules',
  1080 + array('course' => $coursesection->course, 'section' => $coursesection->id), '', 'id, section');
  1081 +
  1082 + // Break out the current sequence so that we can compare it
  1083 + $currentsequence = explode(',', $coursesection->sequence);
  1084 + $orphanlist = array();
  1085 +
  1086 + // Check each of the modules in the current sequence
  1087 + foreach ($currentsequence as $module) {
  1088 + if (!empty($module) && !isset($actualsectionmodules[$module])) {
  1089 + $orphanlist[] = $module;
  1090 + }
  1091 + }
  1092 +
  1093 + if (!empty($orphanlist)) {
  1094 + list($sql, $params) = $DB->get_in_or_equal($orphanlist, SQL_PARAMS_NAMED);
  1095 + $sql = "id $sql";
  1096 +
  1097 + $DB->set_field_select('course_modules', 'section', $coursesection->id, $sql, $params);
  1098 +
  1099 + // And clear the sectioncache and modinfo cache - they'll be regenerated on next use
  1100 + $course = new stdClass();
  1101 + $course->id = $coursesection->course;
  1102 + $course->sectioncache = null;
  1103 + $course->modinfo = null;
  1104 + $DB->update_record('course', $course);
  1105 + }
  1106 + }
  1107 + $coursesections->close();
  1108 +
  1109 + // Main savepoint reached.
  1110 + upgrade_main_savepoint(true, 2012062504.10);
  1111 + }
1061 1112
1062 1113 return true;
1063 1114 }

0 comments on commit 2b0b5a2

Please sign in to comment.
Something went wrong with that request. Please try again.