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