-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.php
More file actions
366 lines (331 loc) · 12.8 KB
/
Copy pathlib.php
File metadata and controls
366 lines (331 loc) · 12.8 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?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/>.
/**
* Lib for masonry course format.
*
* @package format_masonry
* @copyright eWallah.net
* @author Renaat Debleu <info@eWallah.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/course/format/lib.php');
use core\output\inplace_editable;
/**
* Main class for the masonry course format
*
* @package format_masonry
* @copyright eWallah.net
* @author Renaat Debleu <info@eWallah.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class format_masonry extends core_courseformat\base {
/**
* Returns instance of page renderer used by this plugin
*
* @param moodle_page $page
* @return renderer_base
*/
public function get_renderer(moodle_page $page) {
return $page->get_renderer('format_masonry');
}
/**
* Returns the default section name for the masonry course format.
*
* @param stdClass $section Section object from database or just field course_sections section
* @return string The default value for the section name.
*/
public function get_default_section_name($section) {
if ($section->section == 0) {
// Return the general section.
return get_string('section0name', 'format_masonry');
}
// Use course_format::get_default_section_name implementation which
// will display the section name in "Topic n" format.
return parent::get_default_section_name($section);
}
/**
* Returns the display name of the given section that the course prefers.
*
* @param int|stdClass $section Section object from database or just field section.section
* @return string Display name that the course format prefers, e.g. "Topic 2"
*/
public function get_section_name($section) {
$section = $this->get_section($section);
if ((string)$section->name !== '') {
// Return the name the user set.
return format_string($section->name, true, ['context' => context_course::instance($this->courseid)]);
}
return $this->get_default_section_name($section);
}
/**
* This course format supports components.
*
* @return boolean
*/
public function supports_components() {
return true;
}
/**
* The URL to use for the specified course (without section)
*
* @param int|stdClass $section Section object from database or just field course_sections.section
* @param array $options options for view URL. ignored
* @return null|moodle_url
*/
public function get_view_url($section, $options = []) {
$course = $this->get_course();
return new \moodle_url('/course/view.php', ['id' => $course->id]);
}
/**
* Definitions of the additional options that this course format uses for section
*
* @param bool $foreditform
* @return array
*/
public function section_format_options($foreditform = false) {
$color = get_config('format_masonry', 'defaultbordercolor');
return [
'backcolor' => [
'type' => PARAM_RAW,
'name' => 'bordercolor',
'label' => new \lang_string('backgroundcolor', 'format_masonry'),
'element_type' => 'text',
'default' => $color,
'cache' => true,
'cachedefault' => $color,
'help' => 'colordisplay',
'help_component' => 'format_masonry',
],
];
}
/**
* Definitions of the additional options that this course format uses for course
*
* @param bool $foreditform
* @return array of options
*/
public function course_format_options($foreditform = false) {
static $courseformatoptions = false;
if ($courseformatoptions === false) {
$courseconfig = get_config('moodlecourse');
$courseformatoptions = [
'hiddensections' => ['type' => PARAM_INT, 'default' => 1],
'coursedisplay' => ['type' => PARAM_INT, 'default' => 1],
'borderwidth' => ['type' => PARAM_INT, 'default' => 1],
'bordercolor' => ['type' => PARAM_TEXT, 'default' => '#F0F0F0'],
'backcolor' => ['type' => PARAM_TEXT, 'default' => '#F0F0F0'], ];
}
if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
$courseconfig = get_config('moodlecourse');
$max = (int)$courseconfig->maxsections;
$sectionmenu = [];
for ($i = 0; $i <= $max; $i++) {
$sectionmenu[$i] = "{$i}";
}
$courseoptionsedit = [
'hiddensections' => [
'label' => 'hidden1',
'element_type' => 'hidden',
'element_attributes' => [[1 => new \lang_string('hiddensectionsinvisible')]],
],
'coursedisplay' => [
'label' => 'hidden2',
'element_type' => 'hidden',
'element_attributes' => [[COURSE_DISPLAY_SINGLEPAGE => new \lang_string('coursedisplay_single')]],
],
'borderwidth' => [
'label' => new \lang_string('borderwidth', 'format_masonry'),
'element_type' => 'select',
'element_attributes' => [[0 => '0', 1 => '1', 2 => '2']],
],
'bordercolor' => [
'label' => new \lang_string('bordercolor', 'format_masonry'),
'element_type' => 'text',
'element_attributes' => [['value' => $courseformatoptions['bordercolor']['default']]],
],
'backcolor' => [
'label' => new \lang_string('colordisplay', 'format_masonry'),
'element_type' => 'text',
'help' => 'colordisplay',
'help_component' => 'format_masonry',
'element_attributes' => [['value' => $courseformatoptions['backcolor']['default']]],
],
];
$courseformatoptions = array_merge_recursive($courseformatoptions, $courseoptionsedit);
}
return $courseformatoptions;
}
/**
* Updates format options for a course
*
* @param stdClass|array $data
* @param stdClass $oldcourse
* @return bool whether there were any changes to the options values
*/
public function update_course_format_options($data, $oldcourse = null) {
if ($oldcourse !== null) {
$data->backcolor = get_config('format_masonry', 'defaultbackgroundcolor');
$data->bordercolor = get_config('format_masonry', 'defaultbordercolor');
return parent::update_course_format_options($data, $oldcourse);
}
return $this->update_format_options($data);
}
/**
* Prepares the templateable object to display section name.
*
* @param \section_info|\stdClass $section
* @param bool $linkifneeded
* @param bool $editable
* @param null|lang_string|string $edithint
* @param null|lang_string|string $editlabel
* @return inplace_editable
*/
public function inplace_editable_render_section_name(
$section,
$linkifneeded = false,
$editable = null,
$edithint = null,
$editlabel = null
) {
if (empty($edithint)) {
$edithint = ($section->section == 0) ? 'section0name' : 'sectionname';
$edithint = new lang_string($edithint, 'format_masonry');
}
if (empty($editlabel)) {
$title = get_section_name($section->course, $section);
$editlabel = new lang_string('newsectionname', 'format_masonry', $title);
}
return parent::inplace_editable_render_section_name($section, $linkifneeded, $editable, $edithint, $editlabel);
}
/**
* Returns whether this course format allows the activity to be hidden on course page but available.
*
* @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 !$section->section || $section->visible;
}
/**
* Returns true if this course format uses sections.
*
* @return bool
*/
public function uses_sections() {
return true;
}
/**
* Whether this format allows to delete sections.
*
* @param int|stdClass|section_info $section
* @return bool
*/
public function can_delete_section($section) {
return ($this->get_section($section)->section != 0);
}
/**
* Whether this format allows course index.
*
* @return bool
*/
public function uses_course_index() {
return false;
}
/**
* Whether this format allows to indentation.
*/
public function uses_indentation(): bool {
return false;
}
/**
* Custom action after section has been moved in AJAX mode.
*
* @return array This will be passed in ajax respose
*/
public function ajax_section_move() {
global $PAGE;
$titles = [];
$course = $this->get_course();
$modinfo = get_fast_modinfo($course);
$renderer = $this->get_renderer($PAGE);
if ($renderer && ($sections = $modinfo->get_section_info_all())) {
foreach ($sections as $number => $section) {
$titles[$number] = $renderer->section_title($section, $course);
}
}
return ['sectiontitles' => $titles, 'action' => 'move'];
}
/**
* Callback used in WS core_course_edit_section when teacher performs an AJAX action on a section (show/hide).
*
* @param section_info|stdClass $section
* @param string $action
* @param int $sr
* @return null|array any data for the Javascript post-processor (must be json-encodeable)
*/
public function section_action($section, $action, $sr) {
global $PAGE;
// For show/hide actions call the parent method and return the new content for .section_availability element.
$rv = parent::section_action($section, $action, $sr);
$renderer = $PAGE->get_renderer('format_masonry');
if (!($section instanceof section_info)) {
$modinfo = course_modinfo::instance($this->courseid);
$section = $modinfo->get_section_info($section->section);
}
$elementclass = $this->get_output_classname('content\\section\\availability');
$availability = new $elementclass($this, $section);
$rv['section_availability'] = $renderer->render($availability);
return $rv;
}
/**
* Returns the information about the ajax support in the given source format.
*
* @return stdClass
*/
public function supports_ajax() {
$ajaxsupport = new stdClass();
$ajaxsupport->capable = true;
return $ajaxsupport;
}
/**
* Return the plugin configs for external functions.
*
* @return array the list of configuration settings
*/
public function get_config_for_external() {
// Return everything (nothing to hide).
return $this->get_format_options();
}
}
/**
* Implements callback inplace_editable() allowing to edit values in-place
*
* @param string $itemtype
* @param int $itemid
* @param mixed $newvalue
* @return \core\output\inplace_editable
*/
function format_masonry_inplace_editable($itemtype, $itemid, $newvalue) {
global $CFG, $DB;
require_once($CFG->dirroot . '/course/lib.php');
if ($itemtype == 'sectionname' || $itemtype == 'sectionnamenl') {
$sql = 'SELECT s.* FROM {course_sections} s JOIN {course} c ON s.course = c.id WHERE s.id = ? AND c.format = ?';
$section = $DB->get_record_sql($sql, [$itemid, 'masonry'], MUST_EXIST);
return course_get_format($section->course)->inplace_editable_update_section_name($section, $itemtype, $newvalue);
}
}