-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.php
322 lines (294 loc) · 10.1 KB
/
lib.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?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/>.
/**
* Main class and callbacks implementations for Learningmap
*
* Documentation: {@link https://moodledev.io/docs/apis/plugintypes/format}
*
* @package format_learningmap
* @copyright 2024 ISB Bayern
* @author Stefan Hanauska <stefan.hanauska@csg-in.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core\navigation\views\secondary;
use core\navigation\views\view;
/**
* format_learningmap plugin implementation
*
* @package format_learningmap
* @copyright 2024 ISB Bayern
* @author Stefan Hanauska <stefan.hanauska@csg-in.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class format_learningmap extends core_courseformat\base {
/** @var $mainlearningmap Main learningmap if already discovered. */
private cm_info|false|null $mainlearningmap = null;
/**
* Returns whether the first activity in the course is a learningmap.
*
* @return bool
*/
public function main_learningmap_exists(): bool {
if ($this->mainlearningmap !== null) {
return $this->mainlearningmap !== false;
}
$cms = $this->get_modinfo()->cms;
while (!empty($cms)) {
$activity = array_shift($cms);
if ($activity->modname == 'learningmap' && $activity->uservisible) {
$this->mainlearningmap = $activity;
return true;
}
}
$this->mainlearningmap = false;
return false;
}
/**
* Returns the first learningmap activity in the course. Throws an exception if there is no learningmap, so you should
* check with main_learningmap_exists() first.
*
* @return cm_info
* @throws moodle_exception
*/
public function get_main_learningmap() {
if ($this->main_learningmap_exists()) {
return $this->mainlearningmap;
} else {
throw new moodle_exception('nolearningmap', 'format_learningmap');
}
}
/**
* Supports components.
*
* @return bool
*/
public function supports_components() {
return true;
}
/**
* This format uses sections.
*
* @return bool
*/
public function uses_sections() {
return true;
}
/**
* Returns true if the course has a front page.
*
* @return bool
*/
public function has_view_page() {
return $this->show_editor();
}
/**
* This format uses course index only in editing mode.
*
* @return bool
*/
public function uses_course_index() {
return $this->show_editor();
}
/**
* Return the plugin configs for external functions.
*
* @return array the list of configuration settings
*/
public function get_config_for_external() {
return $this->get_format_options();
}
/**
* Used to set the secondary navigation for the singleactivity format.
*
* @param moodle_page $page
* @return void
*/
public function set_singleactivity_navigation($page) {
$coursehomenode = $page->navigation->find('coursehome', view::TYPE_COURSE);
// This is really ugly - but right now, there is no other way to use the secondary navigation
// built for single activity format in other course formats.
$page->course->format = 'singleactivity';
$secondarynav = new secondary($page);
$secondarynav->initialise();
if (!empty($coursehomenode)) {
$secondarynav->add_node($coursehomenode);
}
$page->set_secondarynav($secondarynav);
$page->course->format = 'learningmap';
}
/**
* Used to redirect to the main learningmap activity if not in editing mode.
*
* @param moodle_page $page
* @return void
*/
public function page_set_course(moodle_page $page) {
global $PAGE;
if (
$PAGE == $page &&
$page->has_set_url() &&
$page->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE) &&
!$this->show_editor()
) {
if (!$this->main_learningmap_exists()) {
if (!has_capability('moodle/course:update', context_course::instance($this->courseid))) {
redirect(new moodle_url('/course/format/learningmap/error.php?courseid=' . $this->courseid));
}
} else {
$cm = $this->get_main_learningmap();
if (!$cm->uservisible) {
redirect(new moodle_url('/course/format/learningmap/error.php?courseid=' . $this->courseid));
} else {
redirect($cm->url);
}
}
}
}
/**
* Allows course format to execute code on moodle_page::set_cm()
*
* @param moodle_page $page instance of page calling set_cm
*/
public function page_set_cm(moodle_page $page) {
global $PAGE;
parent::page_set_cm($page);
$options = $this->get_format_options();
if (
!empty($options['hidesecondarynavforstudents']) &&
!(has_capability('moodle/course:manageactivities', context_module::instance($page->cm->id)) ||
has_capability('moodle/course:manageactivities', context_course::instance($this->courseid)))
) {
$page->set_secondary_navigation(false);
}
if ($PAGE == $page && $PAGE->has_set_url()) {
$this->set_singleactivity_navigation($page);
}
}
/**
* Stealth modules are allowed here (but not necessary). This is set just for better compatibility with
* courses that are converted from other formats.
*
* @param stdClass|cm_info $cm course module (may be null if we are displaying a form for adding a module)
* @param stdClass|section_info $section section where this module is located or will be added to
* @return bool
*/
public function allow_stealth_module_visibility($cm, $section) {
return true;
}
/**
* Returns the section name.
*
* @param stdClass $section
* @return string
*/
public function get_section_name($section) {
$section = $this->get_section($section);
if ($section->name !== '' && $section->name !== null) {
return format_string($section->name, true);
} else {
return $this->get_default_section_name($section);
}
}
/**
* Returns the default section name.
*
* @param stdClass $section
* @return string
*/
public function get_default_section_name($section) {
if ($section->sectionnum == 0) {
return get_string('section0name', 'format_learningmap');
}
return get_string('newsection', 'format_learningmap');
}
/**
* Whether this format allows to delete sections.
*
* @param int|stdClass|section_info $section
* @return bool
*/
public function can_delete_section($section) {
return true;
}
/**
* Returns the information about the ajax support in the given source format.
*
* The returned object's property (boolean)capable indicates that
* the course format supports Moodle course ajax features.
*
* @return stdClass
*/
public function supports_ajax() {
$ajaxsupport = new stdClass();
$ajaxsupport->capable = true;
return $ajaxsupport;
}
/**
* Defines the course format options.
*
* @param boolean $foreditform whether the options are for the course edit form
* @return array the list of options
*/
public function course_format_options($foreditform = false) {
$options = parent::course_format_options($foreditform);
$options['hidesecondarynavforstudents'] = [
'label' => new \lang_string('hidesecondarynavforstudents', 'format_learningmap'),
'type' => PARAM_BOOL,
'element_type' => 'advcheckbox',
'default' => 0,
'help' => 'hidesecondarynavforstudents',
'help_component' => 'format_learningmap',
];
return $options;
}
}
/**
* Implements callback inplace_editable() allowing to edit values in-place.
*
* @param string $itemtype
* @param int $itemid
* @param mixed $newvalue
* @return inplace_editable
*/
function format_learningmap_inplace_editable($itemtype, $itemid, $newvalue) {
global $DB, $CFG;
require_once($CFG->dirroot . '/course/lib.php');
if ($itemtype === 'sectionname' || $itemtype === 'sectionnamenl') {
$section = $DB->get_record_sql(
'SELECT s.* FROM {course_sections} s JOIN {course} c ON s.course = c.id WHERE s.id = ?',
[$itemid],
MUST_EXIST
);
return course_get_format($section->course)->inplace_editable_update_section_name($section, $itemtype, $newvalue);
}
}
/**
* Implements callback format_learningmap_coursemodule_definition_after_data() allowing to modify course module definition.
*
* @param moodleform_mod $moodleform
* @param MoodleQuickForm $form
* @return void
*/
function format_learningmap_coursemodule_definition_after_data($moodleform, $form) {
global $COURSE;
$current = $moodleform->get_current();
if ($COURSE->format === 'learningmap' && empty($current->instance)) {
// Override default completion setting for learningmap format.
if ($current->completion == COMPLETION_DISABLED) {
$form->setDefault('completion', COMPLETION_TRACKING_MANUAL);
}
}
}