/**
  * @throws coding_exception
  */
 public function test_after_restore()
 {
     global $DB;
     $this->resetAfterTest(true);
     // To be able to have global $USER.
     $this->setAdminUser();
     $dg = $this->getDataGenerator();
     // Create test course data.
     $course = $dg->create_course(array('format' => 'topics', 'numsections' => 1));
     $section = $dg->create_course_section(array('course' => $course->id, 'section' => 1));
     // Setup the course section for the Side Bar block-managed activities.
     $sectioninfo = block_side_bar_create_section($course);
     $page = $dg->create_module('page', array('course' => $course->id), array('section' => $sectioninfo->section));
     // Setup the course section for the Side Bar block-managed activities.
     $block = new stdClass();
     $ctx = context_course::instance($course->id);
     $block->blockname = 'side_bar';
     $block->parentcontextid = $ctx->id;
     $block->patypepattern = 'course-view-*';
     $block->defaultregion = 'side-post';
     $block->defaultweight = 2;
     $block->showinsubcontexts = 0;
     $cfg = new stdClass();
     $cfg->title = "Test";
     $cfg->section_id = $sectioninfo->id;
     $block->configdata = base64_encode(serialize($cfg));
     $blockins = $DB->insert_record('block_instances', $block);
     $newcourseid = $this->backup_and_restore($course);
     $reseturl = new moodle_url('/blocks/side_bar/reset.php?cid=' . $newcourseid);
     $newsection = new stdClass();
     $newsection->name = get_string('sidebar', 'block_side_bar');
     $newsection->summary = get_string('sectionsummary', 'block_side_bar', (string) html_writer::link($reseturl, $reseturl));
     $newsection->summaryformat = FORMAT_HTML;
     $newsection->visible = true;
     $section = $DB->get_records('course_sections', array('name' => $newsection->name));
     $section = array_pop($section);
     $this->assertEquals($newsection->summary, $section->summary);
     $blockinstance = $DB->get_records('block_instances', array('blockname' => 'side_bar'));
     $blockinstance = array_pop($blockinstance);
     $newconfig = $blockinstance->configdata;
     $newconfig = unserialize(base64_decode($newconfig));
     $this->assertEquals($section->id, $newconfig->section_id);
 }
 /**
  * Parent class version of this function simply returns NULL This should be implemented by the derived class to return the content object.
  *
  * @return object The content object
  */
 public function get_content()
 {
     global $USER, $CFG, $DB, $OUTPUT;
     if ($this->content !== null) {
         return $this->content;
     }
     $this->content = new stdClass();
     $this->content->items = array();
     $this->content->icons = array();
     $this->content->footer = '';
     if (empty($this->instance)) {
         if (!isset($this->content)) {
             $this->content = new stdClass();
         }
         return $this->content;
     }
     if (!isset($this->config->title)) {
         if (!isset($this->config)) {
             $this->config = new stdClass();
         }
         $this->config->title = '';
     }
     $course = $this->page->course;
     require_once $CFG->dirroot . '/course/lib.php';
     $context = context_course::instance($course->id);
     $isediting = $this->page->user_is_editing() && has_capability('moodle/course:manageactivities', $context);
     // Create a new section for this block (if necessary).
     if (empty($this->config->section)) {
         require_once $CFG->dirroot . '/blocks/side_bar/locallib.php';
         if (null == ($section = block_side_bar_create_section($course))) {
             return $this->content;
         }
         $this->config->section = $section->section;
         $this->config->section_id = $section->id;
         parent::instance_config_commit();
     } else {
         if (empty($this->config->section_id)) {
             $params = array('course' => $course->id, 'section' => $this->config->section);
             $section = $DB->get_record('course_sections', $params);
             $this->config->section_id = $section->id;
             parent::instance_config_commit();
         } else {
             $section = $DB->get_record('course_sections', array('id' => $this->config->section_id));
             if (empty($section)) {
                 require_once $CFG->dirroot . '/blocks/side_bar/locallib.php';
                 if (null == ($section = block_side_bar_create_section($course))) {
                     return $this->content;
                 }
                 $this->config->section = $section->section;
                 $this->config->section_id = $section->id;
                 parent::instance_config_commit();
             }
         }
         // Double check that the section number hasn't been modified by something else.
         // Fixes problem found by Charlotte Owen when moving 'center column' course sections.
         if ($section->section != $this->config->section) {
             $section->section = $this->config->section;
             $DB->update_record('course_sections', $section);
         }
     }
     // extra fast view mode
     $modinfo = get_fast_modinfo($course);
     if (!$isediting) {
         if (!empty($modinfo->sections[$this->config->section])) {
             $options = array('overflowdiv' => true);
             foreach ($modinfo->sections[$this->config->section] as $cmid) {
                 $cm = $modinfo->cms[$cmid];
                 if (!$cm->uservisible) {
                     continue;
                 }
                 $content = $cm->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
                 $instancename = $cm->get_formatted_name();
                 if (!($url = $cm->url)) {
                     $this->content->items[] = $content;
                     $this->content->icons[] = '';
                 } else {
                     $linkcss = $cm->visible ? '' : ' class="dimmed" ';
                     // Accessibility: incidental image - should be empty Alt text
                     $icon = '<img src="' . $cm->get_icon_url() . '" class="icon" alt="" />&nbsp;';
                     $this->content->items[] = '<a title="' . $cm->modplural . '" ' . $linkcss . ' ' . $cm->extra . ' href="' . $url . '">' . $icon . $instancename . '</a>';
                 }
             }
         }
         return $this->content;
     }
     // slow & hacky editing mode
     $courserenderer = $this->page->get_renderer('core', 'course');
     $ismoving = ismoving($course->id);
     if (!($cs = $DB->get_record('course_sections', array('section' => $this->config->section, 'course' => $course->id)))) {
         debugging('Could not get course section record for section ' . $this->config->section, DEBUG_DEVELOPER);
         return $this->content;
     }
     $modinfo = get_fast_modinfo($course);
     $section = $modinfo->get_section_info($this->config->section);
     $modnames = get_module_types_names();
     $groupbuttons = $course->groupmode;
     $groupbuttonslink = !$course->groupmodeforce;
     if ($ismoving) {
         $strmovehere = get_string('movehere');
         $strmovefull = strip_tags(get_string('movefull', '', "'{$USER->activitycopyname}'"));
         $strcancel = get_string('cancel');
         $stractivityclipboard = $USER->activitycopyname;
     } else {
         $strmove = get_string('move');
     }
     // Casting $course->modinfo to string prevents one notice when the field is null
     $editbuttons = '';
     if ($ismoving) {
         $this->content->icons[] = '<img src="' . $OUTPUT->pix_url('t/move') . '" class="iconsmall" alt="" />';
         $this->content->items[] = $USER->activitycopyname . '&nbsp;(<a href="' . $CFG->wwwroot . '/course/mod.php?cancelcopy=true&amp;sesskey=' . sesskey() . '">' . $strcancel . '</a>)';
     }
     if (!empty($modinfo->sections[$this->config->section])) {
         $options = array('overflowdiv' => true);
         foreach ($modinfo->sections[$this->config->section] as $modnumber) {
             $mod = $modinfo->cms[$modnumber];
             if (!$mod->uservisible) {
                 continue;
             }
             if (!$ismoving) {
                 $actions = course_get_cm_edit_actions($mod, -1);
                 // Prepend list of actions with the 'move' action.
                 $actions = array('move' => new action_menu_link_primary(new moodle_url('/course/mod.php', array('sesskey' => sesskey(), 'copy' => $mod->id)), new pix_icon('t/move', $strmove, 'moodle', array('class' => 'iconsmall', 'title' => '')), $strmove)) + $actions;
                 $editactions = $courserenderer->course_section_cm_edit_actions($actions, $mod, array('donotenhance' => true));
                 $editbuttons = html_writer::tag('div', $editactions, array('class' => 'buttons'));
             } else {
                 $editbuttons = '';
             }
             if ($mod->visible || has_capability('moodle/course:viewhiddenactivities', $context)) {
                 if ($ismoving) {
                     if ($mod->id == $USER->activitycopy) {
                         continue;
                     }
                     $this->content->items[] = '<a title="' . $strmovefull . '" href="' . $CFG->wwwroot . '/course/mod.php' . '?moveto=' . $mod->id . '&amp;sesskey=' . sesskey() . '"><img style="height:16px; width:80px; border:0px"' . ' src="' . $OUTPUT->pix_url('movehere') . '" alt="' . $strmovehere . '" /></a>';
                     $this->content->icons[] = '';
                 }
                 $content = $mod->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
                 $instancename = $mod->get_formatted_name();
                 $linkcss = $mod->visible ? '' : ' class="dimmed" ';
                 if (!($url = $mod->url)) {
                     $this->content->items[] = $content . $editbuttons;
                     $this->content->icons[] = '';
                 } else {
                     // Accessibility: incidental image - should be empty Alt text
                     $icon = '<img src="' . $mod->get_icon_url() . '" class="icon" alt="" />&nbsp;';
                     $this->content->items[] = '<a title="' . $mod->modfullname . '" ' . $linkcss . ' ' . $mod->extra . ' href="' . $url . '">' . $icon . $instancename . '</a>' . $editbuttons;
                 }
             }
         }
     }
     if ($ismoving) {
         $this->content->items[] = '<a title="' . $strmovefull . '" href="' . $CFG->wwwroot . '/course/mod.php?' . 'movetosection=' . $section->id . '&amp;sesskey=' . sesskey() . '"><img style="height' . ':16px; width:80px; border:0px" src="' . $OUTPUT->pix_url('movehere') . '" alt="' . $strmovehere . '" /></a>';
         $this->content->icons[] = '';
     }
     $this->content->footer = $courserenderer->course_section_add_cm_control($course, $this->config->section, null, array('inblock' => true));
     // Replace modchooser with dropdown
     $this->content->footer = str_replace('hiddenifjs addresourcedropdown', 'visibleifjs addresourcedropdown', $this->content->footer);
     $this->content->footer = str_replace('visibleifjs addresourcemodchooser', 'hiddenifjs addresourcemodchooser', $this->content->footer);
     return $this->content;
 }
 /**
  * Test that the Side Bar block activity section is appropriately added to a course when that course contains multiple orphaned
  * sections but only the "lowest" orphaned section contains an activity module (orphaned being sections that exist beyond the
  * number of sections configured for the course).
  */
 public function test_create_section_works_with_multiple_orphaned_sections_low_not_empty()
 {
     global $DB;
     $this->resetAfterTest();
     $dg = $this->getDataGenerator();
     // Create test course data
     $course = $dg->create_course(array('format' => 'topics', 'numsections' => 1));
     $section = $dg->create_course_section(array('course' => $course->id, 'section' => 1));
     $section = $dg->create_course_section(array('course' => $course->id, 'section' => 2));
     $section = $dg->create_course_section(array('course' => $course->id, 'section' => 3));
     $section = $dg->create_course_section(array('course' => $course->id, 'section' => 4));
     $page = $dg->create_module('page', array('course' => $course->id), array('section' => 4));
     // Setup the course section for the Side Bar block-managed activities
     $sectioninfo = block_side_bar_create_section($course);
     // Ensure returned data is what we expect
     $this->assertTrue(is_object($sectioninfo));
     $this->assertObjectHasAttribute('id', $sectioninfo);
     $this->assertObjectHasAttribute('section', $sectioninfo);
     $this->assertEquals(5, $sectioninfo->section);
     $this->assertEquals(6, $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, 5, $course->id);
 }