MDL-61635 tool_dataprivacy: Filter out protected contexts in delete
[moodle.git] / blocks / section_links / block_section_links.php
blob7f1268330696b68fb42419a6f00e2a9b2f50566e
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This file contains the main class for the section links block.
20 * @package block_section_links
21 * @copyright Jason Hardin
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 /**
26 * Section links block class.
28 * @package block_section_links
29 * @copyright Jason Hardin
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 class block_section_links extends block_base {
34 /**
35 * Initialises the block instance.
37 public function init() {
38 $this->title = get_string('pluginname', 'block_section_links');
41 /**
42 * Returns an array of formats for which this block can be used.
44 * @return array
46 public function applicable_formats() {
47 return array(
48 'course-view-weeks' => true,
49 'course-view-topics' => true
53 /**
54 * Generates the content of the block and returns it.
56 * If the content has already been generated then the previously generated content is returned.
58 * @return stdClass
60 public function get_content() {
62 // The config should be loaded by now.
63 // If its empty then we will use the global config for the section links block.
64 if (isset($this->config)){
65 $config = $this->config;
66 } else{
67 $config = get_config('block_section_links');
70 if ($this->content !== null) {
71 return $this->content;
74 $this->content = new stdClass;
75 $this->content->footer = '';
76 $this->content->text = '';
78 if (empty($this->instance)) {
79 return $this->content;
82 $course = $this->page->course;
83 $courseformat = course_get_format($course);
84 $numsections = $courseformat->get_last_section_number();
85 $context = context_course::instance($course->id);
87 // Course format options 'numsections' is required to display the block.
88 if (empty($numsections)) {
89 return $this->content;
92 // Prepare the increment value.
93 if (!empty($config->numsections1) and ($numsections > $config->numsections1)) {
94 $inc = $config->incby1;
95 } else if ($numsections > 22) {
96 $inc = 2;
97 } else {
98 $inc = 1;
100 if (!empty($config->numsections2) and ($numsections > $config->numsections2)) {
101 $inc = $config->incby2;
102 } else {
103 if ($numsections > 40) {
104 $inc = 5;
108 // Prepare an array of sections to create links for.
109 $sections = array();
110 $canviewhidden = has_capability('moodle/course:update', $context);
111 $coursesections = $courseformat->get_sections();
112 $coursesectionscount = count($coursesections);
113 $sectiontojumpto = false;
114 for ($i = $inc; $i <= $coursesectionscount; $i += $inc) {
115 if ($i > $numsections || !isset($coursesections[$i])) {
116 continue;
118 $section = $coursesections[$i];
119 if ($section->section && ($section->visible || $canviewhidden)) {
120 $sections[$i] = (object)array(
121 'section' => $section->section,
122 'visible' => $section->visible,
123 'highlight' => false
125 if ($courseformat->is_section_current($section)) {
126 $sections[$i]->highlight = true;
127 $sectiontojumpto = $section->section;
132 if (!empty($sections)) {
133 // Render the sections.
134 $renderer = $this->page->get_renderer('block_section_links');
135 $this->content->text = $renderer->render_section_links($this->page->course, $sections, $sectiontojumpto);
138 return $this->content;
141 * Returns true if this block has instance config.
143 * @return bool
145 public function instance_allow_config() {
146 return true;
150 * Returns true if this block has global config.
152 * @return bool
154 public function has_config() {
155 return true;