Permalink
Browse files

Merge branch 'MDL-37939-m23' of https://github.com/andrewnicols/moodle

…into MOODLE_23_STABLE

Conflicts:
	lib/db/upgrade.php
  • Loading branch information...
2 parents 45c8baa + 0c9e550 commit bf78144a45f9eb0971081c3730a67807b9292c44 @damyon damyon committed Feb 18, 2013
Showing with 118 additions and 2 deletions.
  1. +4 −1 course/lib.php
  2. +64 −0 course/tests/courselib_test.php
  3. +49 −0 lib/db/upgrade.php
  4. +1 −1 version.php
View
@@ -2848,6 +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));
return $section->id; // Return course_sections ID that was used.
} else { // Insert a new record
@@ -2857,7 +2858,9 @@ function add_mod_to_section($mod, $beforemod=NULL) {
$section->summary = "";
$section->summaryformat = FORMAT_HTML;
$section->sequence = $mod->coursemodule;
- return $DB->insert_record("course_sections", $section);
+ $section->id = $DB->insert_record("course_sections", $section);
+ $DB->set_field("course_modules", "section", $section->id, array("id" => $mod->id));
+ return $section->id;
}
}
@@ -226,4 +226,68 @@ public function test_create_course_category() {
$this->assertGreaterThanOrEqual($category2->sortorder, $category3->sortorder);
$this->assertGreaterThanOrEqual($category1->sortorder, $category3->sortorder);
}
+
+ public function test_move_module_in_course() {
+ global $DB;
+
+ $this->resetAfterTest(true);
+ // Setup fixture
+ $course = $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections' => true));
+ $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
+
+ $cms = get_fast_modinfo($course)->get_cms();
+ $cm = reset($cms);
+
+ $newsection = get_fast_modinfo($course)->get_section_info(3);
+ $oldsectionid = $cm->section;
+
+ // Perform the move
+ $result = moveto_module($cm, $newsection);
+ $this->assertTrue($result);
+
+ // get_fast_modinfo(reset) is usually called the code calling moveto_module so call it here
+ $reset = 'reset';
+ get_fast_modinfo($reset);
+ $cms = get_fast_modinfo($course)->get_cms();
+ $cm = reset($cms);
+
+ // Check that the old section's sequence no longer contains this ID
+ $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
+ $oldsequences = explode(',', $newsection->sequence);
+ $this->assertFalse(in_array($cm->id, $oldsequences));
+
+ // Check that the new section's sequence now contains this ID
+ $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
+ $newsequences = explode(',', $newsection->sequence);
+ $this->assertTrue(in_array($cm->id, $newsequences));
+
+ // Check that the section number has been changed in the cm
+ $this->assertEquals($newsection->id, $cm->section);
+
+
+ // Perform a second move as some issues were only seen on the second move
+ $newsection = get_fast_modinfo($course)->get_section_info(2);
+ $oldsectionid = $cm->section;
+ $result = moveto_module($cm, $newsection);
+ $this->assertTrue($result);
+
+ // get_fast_modinfo(reset) is usually called the code calling moveto_module so call it here
+ $reset = 'reset';
+ get_fast_modinfo($reset);
+ $cms = get_fast_modinfo($course)->get_cms();
+ $cm = reset($cms);
+
+ // Check that the old section's sequence no longer contains this ID
+ $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
+ $oldsequences = explode(',', $newsection->sequence);
+ $this->assertFalse(in_array($cm->id, $oldsequences));
+
+ // Check that the new section's sequence now contains this ID
+ $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
+ $newsequences = explode(',', $newsection->sequence);
+ $this->assertTrue(in_array($cm->id, $newsequences));
+
+ // Check that the section number has been changed in the cm
+ $this->assertEquals($newsection->id, $cm->section);
+ }
}
View
@@ -1010,5 +1010,54 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2012062504.07);
}
+ if ($oldversion < 2012062504.08) {
+ // 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.08);
+ }
+
return true;
}
View
@@ -30,7 +30,7 @@
defined('MOODLE_INTERNAL') || die();
-$version = 2012062504.07; // YYYYMMDD = weekly release date of this DEV branch
+$version = 2012062504.08; // YYYYMMDD = weekly release date of this DEV branch
// RR = release increments - 00 in DEV branches
// .XX = incremental changes

0 comments on commit bf78144

Please sign in to comment.