forked from ponlawat-w/moodle-local_accessibility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
335 lines (305 loc) · 9.66 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
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
/**
* Plugin function library
*
* @package local_accessibility
* @category string
* @copyright 2023 Ponlawat Weerapanpisit <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/classes/widgetbase.php');
require_once(__DIR__ . '/classes/rangewidget.php');
require_once(__DIR__ . '/classes/colourwidget.php');
/**
* Return names of installed widgets
*
* @return string[]
*/
function local_accessibility_getinstalledwidgetnames() {
$pluginmanager = core_plugin_manager::instance();
$plugins = $pluginmanager->get_plugins_of_type('accessibility');
$results = [];
foreach ($plugins as $plugin) {
if ($plugin->is_installed_and_upgraded()) {
$results[] = $plugin->name;
}
}
return $results;
}
/**
* Add installed widget into database table
*
* @param bool $enablenewplugin
*
* @return void
*/
function local_accessibility_addwidgetstodb($enablenewplugin = true) {
global $DB;
/** @var \moodle_database $DB */ $DB;
$installedwidgets = local_accessibility_getinstalledwidgetnames();
if (!count($installedwidgets)) {
return;
}
$widgets = $DB->get_records('local_accessibility_widgets', [], 'sequence ASC', 'id,name,sequence');
$widgetnames = array_map(function($record) {
return $record->name;
}, $widgets);
$i = count($widgets) ? array_values($widgets)[count($widgets) - 1]->sequence : 0;
$newrecords = [];
foreach ($installedwidgets as $installedwidget) {
if (in_array($installedwidget, $widgetnames)) {
continue;
}
$newrecord = new stdClass();
$newrecord->name = $installedwidget;
$newrecord->enabled = $enablenewplugin ? 1 : 0;
$newrecord->sequence = $enablenewplugin ? ++$i : -1;
$newrecords[] = $newrecord;
}
if (!count($newrecords)) {
return;
}
$DB->insert_records('local_accessibility_widgets', $newrecords);
local_accessibility_resequence();
}
/**
* Check if database structure of plugin is before version 1.0.0
*
* @return bool
*/
function local_accessiblity_before_1_0_0() {
return core_plugin_manager::instance()->get_plugin_info('local_accessibility')->versiondb < 2023110101;
}
/**
* Return database records of enabled widgets
*
* @return stdClass[]
*/
function local_accessibility_getenabledwidgets() {
global $DB;
/** @var moodle_database $DB */ $DB;
// Table names have been changed after 1.0.0,
// to prevent database exception and bring to upgrade page, return an empty array.
if (local_accessiblity_before_1_0_0()) {
return [];
}
local_accessibility_addwidgetstodb();
return $DB->get_records('local_accessibility_widgets', ['enabled' => 1], 'sequence ASC');
}
/**
* Return names of enabled widgets
*
* @return string[]
*/
function local_accessibility_getenabledwidgetnames() {
return array_map(function($record) {
return $record->name;
}, local_accessibility_getenabledwidgets());
}
/**
* Clean widget sequence from 1
*
* @return void
*/
function local_accessibility_resequence() {
global $DB;
/** @var moodle_database $DB */ $DB;
$widgets = local_accessibility_getenabledwidgets();
$i = 1;
foreach ($widgets as $widget) {
$widget->sequence = $i++;
$DB->update_record('local_accessibility_widgets', $widget);
}
}
/**
* Enable a widget
*
* @param string $widgetname
* @return bool
*/
function local_accessibility_enablewidget($widgetname) {
global $DB;
/** @var moodle_database $DB */ $DB;
local_accessibility_addwidgetstodb();
$widget = $DB->get_record('local_accessibility_widgets', ['name' => $widgetname]);
if (!$widget) {
throw new moodle_exception("widget name {$widgetname} is not installed");
}
if ($widget->enabled) {
throw new moodle_exception("widget {$widgetname} is already enabled");
}
$widget->enabled = 1;
$widget->sequence = count(local_accessibility_getenabledwidgets()) + 1;
return $DB->update_record('local_accessibility_widgets', $widget);
}
/**
* Disable a widget
*
* @param string $widgetname
* @return void
*/
function local_accessibility_disablewidget($widgetname) {
global $DB;
/** @var moodle_database $DB */ $DB;
local_accessibility_addwidgetstodb();
$widget = $DB->get_record('local_accessibility_widgets', ['name' => $widgetname]);
if (!$widget) {
throw new moodle_exception("widget name {$widgetname} is not installed");
}
if (!$widget->enabled) {
throw new moodle_exception("widget {$widgetname} is already disabled");
}
$widget->enabled = 0;
$widget->sequence = -1;
if (!$DB->update_record('local_accessibility_widgets', $widget)) {
return false;
}
local_accessibility_resequence();
return true;
}
/**
* Swap display sequence of two widgets
*
* @param stdClass $widget1 widget record
* @param stdClass $widget2 widget record
* @return bool
*/
function local_accessibility_swapsequence($widget1, $widget2) {
global $DB;
/** @var moodle_database $DB */ $DB;
$temp = $widget1->sequence;
$widget1->sequence = $widget2->sequence;
$widget2->sequence = $temp;
return $DB->update_record('local_accessibility_widgets', $widget1)
&& $DB->update_record('local_accessibility_widgets', $widget2);
}
/**
* Move widget sequence up
*
* @param stdClass $widget
* @return void
*/
function local_accessibility_moveup($widget) {
global $DB;
/** @var moodle_database $DB */ $DB;
$previouswidget = $DB->get_record_sql(
'SELECT * FROM {local_accessibility_widgets} WHERE enabled = 1 AND sequence < ? ORDER BY sequence DESC LIMIT 1',
[$widget->sequence]
);
if (!$previouswidget) {
return;
}
local_accessibility_swapsequence($widget, $previouswidget);
}
/**
* Move widget sequence down
*
* @param stdClass $widget
* @return void
*/
function local_accessibility_movedown($widget) {
global $DB;
/** @var moodle_database $DB */ $DB;
$nextwidget = $DB->get_record_sql(
'SELECT * FROM {local_accessibility_widgets} WHERE enabled = 1 AND sequence > ? ORDER BY sequence ASC LIMIT 1',
[$widget->sequence]
);
if (!$nextwidget) {
return;
}
local_accessibility_swapsequence($widget, $nextwidget);
}
/**
* Get enabled widget instances
*
* @return local_accessibility\widgets\widgetbase[]
*/
function local_accessibility_getwidgetinstances() {
$enabledwidgetnames = local_accessibility_getenabledwidgetnames();
return array_map(function($name) {
return local_accessibility_getwidgetinstancebyname($name);
}, $enabledwidgetnames);
}
/**
* Get widget instance by name
*
* @param string $widgetname
* @return local_accessibility\widgets\widgetbase
*/
function local_accessibility_getwidgetinstancebyname($widgetname) {
global $CFG;
$filepath = "{$CFG->dirroot}/local/accessibility/widgets/{$widgetname}/accessibility_{$widgetname}.php";
if (!file_exists($filepath)) {
throw new moodle_exception("File {$filepath} does not exist");
}
require_once($filepath);
$classname = 'local_accessibility\widgets\\' . $widgetname;
if (!class_exists($classname)) {
throw new moodle_exception("Class {$classname} does not exist in {$filepath}");
}
return new $classname();
}
/**
* Injector of widget initialisation before rendering page
*
* @return void
*/
function local_accessibility_before_http_headers() {
global $PAGE;
$widgetinstances = local_accessibility_getwidgetinstances();
if (!count($widgetinstances)) {
return;
}
/** @var \moodle_page $PAGE */
$PAGE->requires->css('/local/accessibility/styles.css');
foreach ($widgetinstances as $widgetinstance) {
$widgetinstance->init();
}
}
/**
* Injector of widgets and panel initialisation before finish rendering page
*
* @return string
*/
function local_accessibility_before_footer() {
global $OUTPUT, $PAGE;
/** @var \core_renderer $OUTPUT */ $OUTPUT; /** @var \moodle_page $PAGE */ $PAGE;
$widgetinstances = local_accessibility_getwidgetinstances();
if (!count($widgetinstances)) {
return '';
}
$PAGE->requires->js_call_amd('local_accessibility/panel', 'init');
$mainbutton = $OUTPUT->render_from_template('local_accessibility/mainbutton', null);
$widgets = [];
foreach ($widgetinstances as $widgetinstance) {
$widgets[] = [
'name' => $widgetinstance->getname(),
'title' => $widgetinstance->gettitle(),
'class' => $widgetinstance->getclass(),
'content' => $widgetinstance->getcontent(),
];
}
$panel = $OUTPUT->render_from_template('local_accessibility/panel', [
'widgets' => $widgets,
'resetallurl' => (new moodle_url('/local/accessibility/resetall.php', [
'returnurl' => $PAGE->url,
'sesskey' => sesskey(),
]))->out(false),
]);
return $mainbutton . $panel;
}