function xmldb_block_side_bar_upgrade($oldversion = 0) { global $CFG, $DB; $result = true; if ($oldversion < 2012062500) { require_once $CFG->dirroot . '/blocks/side_bar/locallib.php'; // Fetch all block instances which have saved configuration data. $select = "blockname = 'side_bar' AND " . $DB->sql_isnotempty('block_instances', 'configdata', true, true); if ($bis = $DB->get_recordset_select('block_instances', $select, array(), 'id, configdata')) { // Perform a semi-cache of course records so we're not constantly fetching course records from the DB when multiple // block instances are found within a single course. $courses = array(); foreach ($bis as $bi) { if (!$result) { continue; } $blockcfg = unserialize(base64_decode($bi->configdata)); if (!is_object($blockcfg) && !isset($blockcfg->section) && !isset($blockcfg->section_id)) { continue; } if (!($section = $DB->get_record('course_sections', array('id' => $blockcfg->section_id)))) { continue; } if (!isset($courses[$section->course])) { if (!($course = $DB->get_record('course', array('id' => $section->course)))) { continue; } $courses[$course->id] = $course; } else { $course = $courses[$section->course]; } /* * We've changed some of the values for text within a section and. the migration code * depends on this so we need to update now. */ $reseturl = new moodle_url('/blocks/side_bar/reset.php?cid=' . $course->id); $supdate = new stdClass(); $supdate->id = $blockcfg->section_id; $supdate->name = get_string('sidebar', 'block_side_bar'); $supdate->summary = get_string('sectionsummary', 'block_side_bar', (string) html_writer::link($reseturl, $reseturl)); $DB->update_record('course_sections', $supdate); $sectioninfo = block_side_bar_migrate_old_section($course, (int) $section->section); if ($sectioninfo == null) { $result = false; } else { // Store the new section number and update the block configuration data. $blockcfg->section = $sectioninfo->section; $DB->set_field('block_instances', 'configdata', base64_encode(serialize($blockcfg)), array('id' => $bi->id)); } } } upgrade_plugin_savepoint($result, 2012062500, 'block', 'side_bar'); } return $result; }
/** * Validate that running the migration function when there is no migration necessary does not modify any data when the * the sidebar section contains an activity. */ public function test_migrate_old_section_multiple_instances_nonsequential() { global $DB; $this->resetAfterTest(); $dg = $this->getDataGenerator(); // Create test course data $course = $dg->create_course(array('format' => 'topics', 'numsections' => 10)); // Create the main course sections for this course (section 1 is already created above). for ($i = 2; $i <= 10; $i++) { $dg->create_course_section(array('course' => $course->id, 'section' => $i)); } // Create a sidebar course section containing an activity module $this->create_sidebar_course_section($course->id, 1000); $page1 = $dg->create_module('page', array('course' => $course->id), array('section' => 1000)); // Create an additional sidebar course section containing an activity module $this->create_sidebar_course_section($course->id, 1001); $page2 = $dg->create_module('page', array('course' => $course->id), array('section' => 1001)); // Run the migration method $sectioninfo = block_side_bar_migrate_old_section($course, 1001); // Ensure returned data is what we expect $this->assertTrue(is_object($sectioninfo)); $this->assertObjectHasAttribute('id', $sectioninfo); $this->assertObjectHasAttribute('section', $sectioninfo); $this->assertEquals(11, $sectioninfo->section); $this->assertEquals(12, $DB->count_records('course_sections', array('course' => $course->id))); // Load the new section record from the DB to make sure the stored values are setup correctly $sbsection = $DB->get_record('course_sections', array('id' => $sectioninfo->id), 'section, name, summary, visible'); $this->validate_sidebar_course_section($sbsection, 11, $course->id); // Validate that the activity module was moved as well $this->assertNotEquals(false, get_coursemodule_from_instance('page', $page1->id, $course->id, 11)); // Run the migration method $sectioninfo = block_side_bar_migrate_old_section($course, 1000); // Ensure returned data is what we expect $this->assertTrue(is_object($sectioninfo)); $this->assertObjectHasAttribute('id', $sectioninfo); $this->assertObjectHasAttribute('section', $sectioninfo); $this->assertEquals(12, $sectioninfo->section); $this->assertEquals(12, $DB->count_records('course_sections', array('course' => $course->id))); // Load the new section record from the DB to make sure the stored values are setup correctly $sbsection = $DB->get_record('course_sections', array('id' => $sectioninfo->id), 'section, name, summary, visible'); $this->validate_sidebar_course_section($sbsection, 12, $course->id); // Validate that the activity module was moved as well $this->assertNotEquals(false, get_coursemodule_from_instance('page', $page2->id, $course->id, 12)); }