-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
372 lines (344 loc) · 13.6 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
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
367
368
369
370
371
372
<?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/>.
/**
* MM Quicklink lib.
*
* @package block_mmquicklink
* @copyright 2019 Mediamaisteri Oy
* @author Mikko Haikonen <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Check if user is using a 'switched role' functionality.
*
* @param object $user USER object.
* @param object $page $PAGE object.
* @param array $options Options array.
* @return object $returnobject.
*/
function mmquicklink_get_switched_role($user, $page, $options = array()) {
global $DB, $COURSE;
$returnobject = new stdClass();
$returnobject->metadata = array();
$context = context_course::instance($COURSE->id);
if (is_role_switched($COURSE->id)) {
if ($role = $DB->get_record('role', array('id' => $user->access['rsw'][$context->path]))) {
$returnobject->metadata['asotherrole'] = true;
$returnobject->metadata['roleid'] = $role->id;
$returnobject->metadata['rolename'] = role_get_name($role, $context);
}
}
return $returnobject;
}
function mmquicklink_get_default_button_order() {
// Add new buttons here! All buttons shown on button sorting needs to be here.
$buttons = mmquicklink_default_buttons();
// Make button data ready for template.
$defaultorder = [];
foreach ($buttons as $button) {
$button->parent = 'main-list';
$defaultorder[$button->button] = $button;
if (!isset($button->children)) {
continue;
}
foreach ($button->children as $child) {
$child->parent = $button->button;
$defaultorder[$child->button] = $child;
}
$defaultorder[$button->button] = $button;
}
return $defaultorder;
}
function mmquicklink_set_default_order() {
global $DB;
$buttons = mmquicklink_default_buttons();
$defaultbuttons = [];
$parentorder = 1;
foreach ($buttons as $parent) {
$parentbutton = (object) [
'button' => $parent->button,
'parent' => 'main-list',
'sortorder' => $parentorder
];
$defaultbuttons[] = $parentbutton;
$childorder = 1;
if (!empty($parent->children)) {
foreach ($parent->children as $child) {
$childbutton = (object) [
'button' => $child->button,
'parent' => $parent->button,
'sortorder' => $childorder
];
$defaultbuttons[] = $childbutton;
$childorder++;
}
}
$parentorder++;
}
if (!$DB->get_records('block_mmquicklink_sorting', [])) {
$DB->insert_records('block_mmquicklink_sorting', $defaultbuttons);
}
}
function mmquicklink_default_buttons() {
global $CFG;
require_once($CFG->dirroot . "/blocks/mmquicklink/classes/block_mmquicklink.php");
// Add new buttons here! All buttons shown on button sorting needs to be here.
$buttons = [
'courseparent' => (object) [
'button' => 'courseparent',
'string' => get_string('setting_courseparent', 'block_mmquicklink'),
'children' => [
'editcoursesettings' => (object) [
'button' => 'editcoursesettings',
'string' => get_string('editcoursesettings', 'core'),
],
'turneditingon list-turneditingon' => (object) [
'button' => 'turneditingon list-turneditingon',
'string' => get_string('turneditingon'),
],
'addnewcourse' => (object) [
'button' => 'addnewcourse',
'string' => get_string('addcourse', 'block_mmquicklink'),
],
'coursemgmt' => (object) [
'button' => 'coursemgmt',
'string' => get_string('coursemgmt', 'block_mmquicklink'),
],
'restorecourse' => (object) [
'button' => 'restorecourse',
'string' => get_string('restorecourse', 'block_mmquicklink'),
],
'deletecourse' => (object) [
'button' => 'deletecourse',
'string' => get_string('setting_delcourse', 'block_mmquicklink'),
],
'archivecourse' => (object) [
'button' => 'archivecourse',
'string' => get_string('setting_archive', 'block_mmquicklink'),
],
'hidecourse' => (object) [
'button' => 'hidecourse',
'string' => get_string('setting_hidecourse', 'block_mmquicklink'),
],
'enrolmentkey' => (object) [
'button' => 'enrolmentkey',
'string' => get_string('enrolmentkey', 'block_mmquicklink'),
],
'coursebgimagechanger' => (object) [
'button' => 'coursebgimagechanger',
'string' => get_string('coursebgimagechanger', 'block_mmquicklink'),
],
'questionbank' => (object) [
'button' => 'questionbank',
'string' => get_string('questionbank', 'block_mmquicklink'),
],
'questioncategory' => (object) [
'button' => 'questioncategory',
'string' => get_string('questioncategory', 'block_mmquicklink'),
],
]
],
'participantsparent' => (object) [
'button' => 'participantsparent',
'string' => get_string('setting_participantsparent', 'block_mmquicklink'),
'children' => [
'participants' => (object) [
'button' => 'participants',
'string' => get_string('setting_participants', 'block_mmquicklink'),
],
'easylink' => (object) [
'button' => 'easylink',
'string' => get_string('setting_easylink', 'block_mmquicklink'),
],
'grades' => (object) [
'button' => 'grades',
'string' => get_string('setting_course_grades', 'block_mmquicklink'),
],
'otherrole' => (object) [
'button' => 'otherrole',
'string' => get_string('setting_otherrole_select', 'block_mmquicklink'),
]
]
],
'completionparent' => (object) [
'button' => 'completionparent',
'string' => get_string('setting_completionparent', 'block_mmquicklink'),
'children' => [
'localreportssummary' => (object) [
'button' => 'localreportssummary',
'string' => get_string('local_reports_summary', 'block_mmquicklink'),
],
'reports' => (object) [
'button' => 'reports',
'string' => get_string('setting_reports', 'block_mmquicklink'),
],
'coursecompletionsettings' => (object) [
'button' => 'coursecompletionsettings',
'string' => get_string('setting_coursecompletionsettings', 'block_mmquicklink'),
],
'completionprogress' => (object) [
'button' => 'completionprogress',
'string' => get_string('pluginname', 'block_completion_progress'),
],
'activityprogress' => (object) [
'button' => 'activityprogress',
'string' => get_string('pluginname', 'report_progress'),
]
]
],
'pageparent' => (object) [
'button' => 'pageparent',
'string' => get_string('setting_pageparent', 'block_mmquicklink'),
'children' => [
'customlang' => (object) [
'button' => 'customlang',
'string' => get_string('setting_langcust', 'block_mmquicklink'),
],
'themesettings' => (object) [
'button' => 'themesettings',
'string' => get_string('setting_themesettings', 'block_mmquicklink'),
],
'backup' => (object) [
'button' => 'backup',
'string' => get_string('backup', 'block_mmquicklink'),
],
'frontpagesettings' => (object) [
'button' => 'frontpagesettings',
'string' => get_string('frontpagesettings'),
],
]
]
];
$mmquicklink = new \block_mmquicklink\mmquicklink();
foreach ($mmquicklink->get_custombuttons('other') as $button) {
$buttons["custom_$button->id"] = (object) [
'button' => "custom_$button->id",
'string' => $button->description
];
}
return $buttons;
}
// Create an 'admin setting' sub class for sorting.
require_once($CFG->libdir . "/adminlib.php");
class admin_setting_configquicklinksort extends admin_setting {
private function get_sort() {
global $DB;
// Existing sort order.
$order = $DB->get_records('block_mmquicklink_sorting', null, 'sortorder ASC');
$defaultorder = mmquicklink_get_default_button_order();
// If there are no customizations to sort order, use default order.
if (empty($order)) {
mmquicklink_set_default_order();
$order = $DB->get_records('block_mmquicklink_sorting', null, 'sortorder ASC');
}
$parents = [];
foreach ($order as $key => $value) {
if ($value->parent == "main-list") {
unset($order[$key]);
$parent = $value;
$parent->string = $defaultorder[$value->button]->string;
$parent->expandbutton = false;
$children = [];
foreach ($order as $key => $child) {
if ($child->parent == $value->button) {
$child->string = $defaultorder[$child->button]->string;
$children[] = $child;
unset($order[$key]);
unset($defaultorder[$child->button]);
}
}
if ($value->button == 'completionparent' || $value->button == 'courseparent' ||
$value->button == 'pageparent' || $value->button == 'participantsparent') {
$parent->expandbutton = true;
}
$parent->children = $children;
$parent->haschildren = (!empty($children));
$parents[] = $parent;
unset($defaultorder[$value->button]);
}
}
// If there are buttons left that arent in sorting_order db, add them under their default parent.
foreach ($defaultorder as $button) {
if ($button->parent == 'main-list') {
$parents[] = $button;
continue;
}
foreach ($parents as $parent) {
if ($button->parent == $parent->button) {
$parent->children[] = $button;
continue;
}
}
}
return $parents;
}
/**
* Construct.
*
* @param string $name Element name.
* @param string $heading Visible name.
* @param string $information Description.
*/
public function __construct($name, $heading, $information) {
$this->nosave = true;
parent::__construct($name, $heading, $information, '');
}
/**
* Usually retrieves the current setting using the objects name.
* Here return true is enough.
*
* @return true
*/
public function get_setting() {
return true;
}
/**
* Usually retrieves the default settings using the objects name.
* Here return true is enough.
*
* @return true
*/
public function get_defaultsetting() {
return true;
}
/**
* Usually sets the value for the setting.
* Here not needed as we use another database table for sorting.
*
* @param mixed $data
* @return string empty string
*/
public function write_setting($data) {
return '';
}
/**
* Returns the sorting field.
*
* @param string $data Inputted data.
* @param string $query
* @return string HTML to output.
*/
public function output_html($data, $query = '') {
global $OUTPUT;
$context = new stdClass();
$context->title = $this->visiblename;
$context->description = $this->description;
$context->descriptionformatted = highlight($query, markdown_to_html($this->description));
$context->parents = $this->get_sort();
return $OUTPUT->render_from_template('block_mmquicklink/setting_quicklink', $context);
}
}