forked from bdecentgmbh/moodle-block_dash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
340 lines (291 loc) · 9.17 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
<?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/>.
/**
* Common functions.
*
* @package block_dash
* @copyright 2019 bdecent gmbh <https://bdecent.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use block_dash\local\data_source\form\preferences_form;
use block_dash\local\layout\grid_layout;
use block_dash\local\layout\accordion_layout;
use block_dash\local\layout\one_stat_layout;
use block_dash\local\data_source\users_data_source;
use block_dash\local\widget\mylearning\mylearning_widget;
use block_dash\local\widget\groups\groups_widget;
use block_dash\local\widget\contacts\contacts_widget;
use block_dash\local\widget\skillprogress\skillprogress_widget;
define("BLOCK_DASH_FILTER_TABS_COUNT", 4);
/**
* Register field definitions.
*
* @return array
*/
function block_dash_register_field_definitions() {
global $CFG;
if (PHPUNIT_TEST) {
return require("$CFG->dirroot/blocks/dash/field_definitions_phpunit.php");
}
return require("$CFG->dirroot/blocks/dash/field_definitions.php");
}
/**
* Register data sources.
*
* @return array
* @throws coding_exception
*/
function block_dash_register_data_sources() {
return [
[
'name' => get_string('users'),
'help' => ['name' => 'users', 'component' => 'block_dash'],
'identifier' => users_data_source::class,
],
];
}
/**
* Register layouts.
*
* @return array
* @throws coding_exception
*/
function block_dash_register_layouts() {
return [
[
'name' => get_string('layoutgrid', 'block_dash'),
'identifier' => grid_layout::class,
],
];
}
/**
* Register widgets.
*
* @return array
* @throws coding_exception
*/
function block_dash_register_widget() {
return [
[
'name' => get_string('widget:mylearning', 'block_dash'),
'identifier' => mylearning_widget::class,
'help' => 'widget:mylearning',
],
[
'name' => get_string('widget:mycontacts', 'block_dash'),
'identifier' => contacts_widget::class,
'help' => 'widget:mycontacts',
],
[
'name' => get_string('widget:mygroups', 'block_dash'),
'identifier' => groups_widget::class,
'help' => 'widget:mygroups',
],
];
}
/**
* Serve the new group form as a fragment.
*
* @param array $args List of named arguments for the fragment loader.
* @return string
*/
function block_dash_output_fragment_block_preferences_form($args) {
global $DB, $OUTPUT;
$args = (object) $args;
$context = $args->context;
$blockinstance = $DB->get_record('block_instances', ['id' => $context->instanceid]);
$block = block_instance($blockinstance->blockname, $blockinstance);
if (!$args->tab) {
$args->tab = preferences_form::TAB_GENERAL;
}
$form = new preferences_form(null, ['block' => $block, 'tab' => $args->tab], 'post', '', [
'class' => 'dash-preferences-form',
'data-double-submit-protection' => 'off',
]);
require_capability('block/dash:addinstance', $context);
if (isset($block->config->preferences)) {
$data = block_dash_flatten_array($block->config->preferences, 'config_preferences');
$form->set_data($data);
}
ob_start();
$form->display();
$formhtml = ob_get_contents();
ob_end_clean();
$tabs = [];
foreach (preferences_form::TABS as $tab) {
$tabs[] = [
'label' => get_string($tab, 'block_dash'),
'active' => $tab == $args->tab,
'tabid' => $tab,
];
}
return $OUTPUT->render_from_template('block_dash/preferences_form', [
'formhtml' => $formhtml,
'tabs' => $tabs,
'istotara' => block_dash_is_totara(),
]);
}
/**
* File serving callback
*
* @param stdClass $course course object
* @param stdClass $cm course module object
* @param stdClass $context context object
* @param string $filearea file area
* @param array $args extra arguments
* @param bool $forcedownload whether or not force download
* @param array $options additional options affecting the file serving
* @return bool false if the file was not found, just send the file otherwise and do not return anything
*/
function block_dash_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=[]) {
if ($context->contextlevel != CONTEXT_BLOCK) {
return false;
}
require_login();
if ($filearea == 'images') {
$relativepath = implode('/', $args);
$fullpath = "/$context->id/block_dash/$filearea/$relativepath";
$fs = get_file_storage();
$file = $fs->get_file_by_hash(sha1($fullpath));
if (!$file || $file->is_directory()) {
return false;
}
send_stored_file($file, null, 0, $forcedownload, $options);
}
}
/**
* Flatten array to form field names.
*
* @param array $array
* @param string $prefix
* @return array
*/
function block_dash_flatten_array($array, $prefix = '') {
$result = [];
foreach ($array as $key => $value) {
if (is_integer($key)) {
// Don't flatten arrays with numeric indexes. Otherwise it won't be set on the Moodle form.
$result[$prefix] = $array;
} else if (is_array($value)) {
$result = $result + block_dash_flatten_array($value, $prefix . '[' . $key . ']');
} else {
$result[$prefix . '[' . $key . ']'] = $value;
}
}
return $result;
}
/**
* Check if system is Totara.
*
* @return bool
*/
function block_dash_is_totara() {
global $CFG;
return file_exists("$CFG->dirroot/totara");
}
/**
* Check if pro plugin is installed.
*
* @return bool
*/
function block_dash_has_pro() {
return array_key_exists('dash', core_component::get_plugin_list('local'));
}
/**
* Check if dash output should be disabled.
*
* @return bool
* @throws dml_exception
*/
function block_dash_is_disabled() {
global $CFG;
if (get_config('block_dash', 'disableall')) {
return true;
}
if (isset($CFG->block_dash_disableall) && $CFG->block_dash_disableall) {
return true;
}
return false;
}
/**
* Fragment to load the widget methods.
*
* @param stdclass $args
* @return string Returns the widget content.
*/
function block_dash_output_fragment_loadwidget($args) {
global $DB;
$args = (object) $args;
$context = $args->context;
$blockinstance = $DB->get_record('block_instances', ['id' => $context->instanceid]);
$block = block_instance($blockinstance->blockname, $blockinstance);
$datasource = block_dash\local\block_builder::create($block)->get_configuration()->get_data_source();
if (isset($datasource->iswidget)) {
$method = $args->method;
$params = json_decode($args->args);
if (isset($params->page)) {
$datasource->get_paginator()->set_current_page($params->page);
}
return (method_exists($datasource, $method)) ? $datasource->$method($context, $params) : '';
}
return null;
}
/**
* Load the table pagination via ajax. withou page refresh.
*
* @param stdclass $args
* @return string
*/
function block_dash_output_fragment_loadtable($args) {
global $DB;
$args = (object) $args;
$context = $args->context;
$classstr = 'block_dash\table\\'.$args->handler;
$table = new $classstr($args->uniqueid);
$table->set_filterset(json_decode($args->filter));
$table->set_sort_column($args->sort);
$table->currentpage = isset($args->page) ? $args->page : 0;
ob_start();
echo html_writer::start_div('dash-widget-table');
$table->out(10, true);
echo html_writer::end_div();
$tablehtml = ob_get_contents();
ob_end_clean();
return $tablehtml;
}
/**
* Get list of all suggest users for contact list.
*/
function block_dash_get_suggest_users() {
global $DB, $CFG;
$users = $DB->get_records_sql("SELECT *
FROM {user}
WHERE confirmed = 1 AND deleted = 0 AND id <> ?", [$CFG->siteguest]);
foreach ($users as $user) {
$list[$user->id] = fullname($user);
}
return isset($list) ? $list : [];
}
/**
* Get the current php version related data collection. Data collection implements PHP arrayAccess class.
* PHP arrayaccess parameters are changed from 8.1.
*
* @return mixed
*/
function block_dash_get_data_collection() {
return version_compare(phpversion(), '8.1', '<')
? new block_dash\local\data_grid\data\data_collection() : new \block_dash\local\data_grid\data\data_collection_new();
}