1 |
efrain |
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.
|
|
|
13 |
//
|
|
|
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/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* This file contains the main class for the section links block.
|
|
|
19 |
*
|
|
|
20 |
* @package block_section_links
|
|
|
21 |
* @copyright Jason Hardin
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Section links block class.
|
|
|
27 |
*
|
|
|
28 |
* @package block_section_links
|
|
|
29 |
* @copyright Jason Hardin
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
*/
|
|
|
32 |
class block_section_links extends block_base {
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Initialises the block instance.
|
|
|
36 |
*/
|
|
|
37 |
public function init() {
|
|
|
38 |
$this->title = get_string('pluginname', 'block_section_links');
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Returns an array of formats for which this block can be used.
|
|
|
43 |
*
|
|
|
44 |
* @return array
|
|
|
45 |
*/
|
|
|
46 |
public function applicable_formats() {
|
|
|
47 |
return [
|
|
|
48 |
'course-view-weeks' => true,
|
|
|
49 |
'course-view-topics' => true,
|
|
|
50 |
'course-view-section-weeks' => true,
|
|
|
51 |
'course-view-section-topics' => true,
|
|
|
52 |
];
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Generates the content of the block and returns it.
|
|
|
57 |
*
|
|
|
58 |
* If the content has already been generated then the previously generated content is returned.
|
|
|
59 |
*
|
|
|
60 |
* @return stdClass
|
|
|
61 |
*/
|
|
|
62 |
public function get_content() {
|
|
|
63 |
|
|
|
64 |
// The config should be loaded by now.
|
|
|
65 |
// If its empty then we will use the global config for the section links block.
|
|
|
66 |
if (isset($this->config)){
|
|
|
67 |
$config = $this->config;
|
|
|
68 |
} else{
|
|
|
69 |
$config = get_config('block_section_links');
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
if ($this->content !== null) {
|
|
|
73 |
return $this->content;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
$this->content = new stdClass;
|
|
|
77 |
$this->content->footer = '';
|
|
|
78 |
$this->content->text = '';
|
|
|
79 |
|
|
|
80 |
if (empty($this->instance)) {
|
|
|
81 |
return $this->content;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
$course = $this->page->course;
|
|
|
85 |
$courseformat = course_get_format($course);
|
|
|
86 |
$numsections = $courseformat->get_last_section_number();
|
|
|
87 |
$context = context_course::instance($course->id);
|
|
|
88 |
|
|
|
89 |
// Course format options 'numsections' is required to display the block.
|
|
|
90 |
if (empty($numsections)) {
|
|
|
91 |
return $this->content;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
// Prepare the increment value.
|
|
|
95 |
if (!empty($config->numsections1) and ($numsections > $config->numsections1)) {
|
|
|
96 |
$inc = $config->incby1;
|
|
|
97 |
} else if ($numsections > 22) {
|
|
|
98 |
$inc = 2;
|
|
|
99 |
} else {
|
|
|
100 |
$inc = 1;
|
|
|
101 |
}
|
|
|
102 |
if (!empty($config->numsections2) and ($numsections > $config->numsections2)) {
|
|
|
103 |
$inc = $config->incby2;
|
|
|
104 |
} else {
|
|
|
105 |
if ($numsections > 40) {
|
|
|
106 |
$inc = 5;
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
// Whether or not section name should be displayed.
|
|
|
111 |
$showsectionname = !empty($config->showsectionname) ? true : false;
|
|
|
112 |
|
|
|
113 |
// Prepare an array of sections to create links for.
|
|
|
114 |
$sections = array();
|
|
|
115 |
$canviewhidden = has_capability('moodle/course:update', $context);
|
|
|
116 |
$coursesections = $courseformat->get_sections();
|
|
|
117 |
$coursesectionscount = count($coursesections);
|
|
|
118 |
$sectiontojumpto = false;
|
|
|
119 |
for ($i = $inc; $i <= $coursesectionscount; $i += $inc) {
|
|
|
120 |
if ($i > $numsections || !isset($coursesections[$i])) {
|
|
|
121 |
continue;
|
|
|
122 |
}
|
|
|
123 |
$section = $coursesections[$i];
|
|
|
124 |
if ($section->section && ($section->visible || $canviewhidden)) {
|
|
|
125 |
$sections[$i] = (object)array(
|
|
|
126 |
'section' => $section->section,
|
|
|
127 |
'visible' => $section->visible,
|
|
|
128 |
'highlight' => false
|
|
|
129 |
);
|
|
|
130 |
if ($courseformat->is_section_current($section)) {
|
|
|
131 |
$sections[$i]->highlight = true;
|
|
|
132 |
$sectiontojumpto = $section->section;
|
|
|
133 |
}
|
|
|
134 |
if ($showsectionname) {
|
|
|
135 |
$sections[$i]->name = $courseformat->get_section_name($i);
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
if (!empty($sections)) {
|
|
|
141 |
// Render the sections.
|
|
|
142 |
$renderer = $this->page->get_renderer('block_section_links');
|
|
|
143 |
$this->content->text = $renderer->render_section_links($this->page->course, $sections,
|
|
|
144 |
$sectiontojumpto, $showsectionname);
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
return $this->content;
|
|
|
148 |
}
|
|
|
149 |
/**
|
|
|
150 |
* Returns true if this block has instance config.
|
|
|
151 |
*
|
|
|
152 |
* @return bool
|
|
|
153 |
**/
|
|
|
154 |
public function instance_allow_config() {
|
|
|
155 |
return true;
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
/**
|
|
|
159 |
* Returns true if this block has global config.
|
|
|
160 |
*
|
|
|
161 |
* @return bool
|
|
|
162 |
*/
|
|
|
163 |
public function has_config() {
|
|
|
164 |
return true;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
/**
|
|
|
168 |
* Return the plugin config settings for external functions.
|
|
|
169 |
*
|
|
|
170 |
* @return stdClass the configs for both the block instance and plugin
|
|
|
171 |
* @since Moodle 3.8
|
|
|
172 |
*/
|
|
|
173 |
public function get_config_for_external() {
|
|
|
174 |
// Return all settings for all users since it is safe (no private keys, etc..).
|
|
|
175 |
$instanceconfigs = !empty($this->config) ? $this->config : new stdClass();
|
|
|
176 |
$pluginconfigs = get_config('block_section_links');
|
|
|
177 |
|
|
|
178 |
return (object) [
|
|
|
179 |
'instance' => $instanceconfigs,
|
|
|
180 |
'plugin' => $pluginconfigs,
|
|
|
181 |
];
|
|
|
182 |
}
|
|
|
183 |
}
|