/**
  * Validate that running the move_section() function with a single sidebar section as the last visible course section
  * with already existing orphaned sections moves the sidebar section "down" to be the last non-visible course section.
  */
 public function test_move_section_single_one_up_with_filler_non_empty()
 {
     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 <= 9; $i++) {
         $dg->create_course_section(array('course' => $course->id, 'section' => $i));
     }
     // Create an empty sidebar course section
     $this->create_sidebar_course_section($course->id, 10);
     $page1 = $dg->create_module('page', array('course' => $course->id), array('section' => 10));
     $dg->create_course_section(array('course' => $course->id, 'section' => 11));
     $page2 = $dg->create_module('page', array('course' => $course->id), array('section' => 11));
     $dg->create_course_section(array('course' => $course->id, 'section' => 12));
     $page3 = $dg->create_module('page', array('course' => $course->id), array('section' => 12));
     // Attempt to "move" the section to be not visible
     $sectioninfo = block_side_bar_move_section($course, 10);
     // Ensure returned data is what we expect
     $this->assertTrue(is_object($sectioninfo));
     $this->assertObjectHasAttribute('id', $sectioninfo);
     $this->assertObjectHasAttribute('section', $sectioninfo);
     $this->assertEquals(13, $sectioninfo->section);
     $this->assertEquals(13, $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, 13, $course->id);
     // Validate that the activity module was moved as well
     $this->assertNotEquals(false, get_coursemodule_from_instance('page', $page1->id, $course->id, 13));
     // Validate that the activities in the pre-existing orhpaned course sections were not moved
     $this->assertNotEquals(false, get_coursemodule_from_instance('page', $page2->id, $course->id, 11));
     $this->assertNotEquals(false, get_coursemodule_from_instance('page', $page3->id, $course->id, 12));
 }
 * @copyright  2013 onwards Justin Filip
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require_once dirname(__FILE__) . '/../../config.php';
require_once dirname(__FILE__) . '/locallib.php';
$cid = required_param('cid', PARAM_INT);
require_login($cid, false);
$course = $DB->get_record('course', array('id' => $cid), '*', MUST_EXIST);
$context = context_course::instance($course->id);
require_capability('moodle/course:manageactivities', $context);
// Fetch all block instances which have saved configuration data
$select = "parentcontextid = :ctxid AND blockname = 'side_bar' AND " . $DB->sql_isnotempty('block_instances', 'configdata', true, true);
if ($bis = $DB->get_recordset_select('block_instances', $select, array('ctxid' => $context->id), 'id, configdata')) {
    foreach ($bis as $bi) {
        $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;
        }
        $sectioninfo = block_side_bar_move_section($course, (int) $section->section);
        if ($sectioninfo != null) {
            // 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));
        }
    }
}
// We're done, so head back to the course
redirect(new moodle_url('/course/view.php?id=' . $course->id));