Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Common functions.
19
 *
20
 * @package    block_dash
21
 * @copyright  2019 bdecent gmbh <https://bdecent.de>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
use block_dash\local\data_source\categories_data_source;
26
use block_dash\local\data_source\form\preferences_form;
27
use block_dash\local\layout\grid_layout;
28
use block_dash\local\layout\accordion_layout;
29
use block_dash\local\layout\one_stat_layout;
30
use block_dash\local\data_source\users_data_source;
31
 
32
use block_dash\local\widget\mylearning\mylearning_widget;
33
use block_dash\local\widget\groups\groups_widget;
34
use block_dash\local\widget\contacts\contacts_widget;
35
use block_dash\local\widget\skillprogress\skillprogress_widget;
36
 
37
define("BLOCK_DASH_FILTER_TABS_COUNT", 4);
38
 
39
/**
40
 * Register field definitions.
41
 *
42
 * @return array
43
 */
44
function block_dash_register_field_definitions() {
45
    global $CFG;
46
 
47
    if (PHPUNIT_TEST) {
48
        return require("$CFG->dirroot/blocks/dash/field_definitions_phpunit.php");
49
    }
50
 
51
    return require("$CFG->dirroot/blocks/dash/field_definitions.php");
52
}
53
 
54
/**
55
 * Register data sources.
56
 *
57
 * @return array
58
 * @throws coding_exception
59
 */
60
function block_dash_register_data_sources() {
61
    return [
62
        [
63
            'name' => get_string('users'),
64
            'help' => ['name' => 'users', 'component' => 'block_dash'],
65
            'identifier' => users_data_source::class,
66
        ],
67
    ];
68
}
69
 
70
/**
71
 * Register layouts.
72
 *
73
 * @return array
74
 * @throws coding_exception
75
 */
76
function block_dash_register_layouts() {
77
    return [
78
        [
79
            'name' => get_string('layoutgrid', 'block_dash'),
80
            'identifier' => grid_layout::class,
81
        ],
82
    ];
83
}
84
 
85
/**
86
 * Register widgets.
87
 *
88
 * @return array
89
 * @throws coding_exception
90
 */
91
function block_dash_register_widget() {
92
 
93
    return [
94
        [
95
            'name' => get_string('widget:mylearning', 'block_dash'),
96
            'identifier' => mylearning_widget::class,
97
            'help' => 'widget:mylearning',
98
        ],
99
        [
100
            'name' => get_string('widget:mycontacts', 'block_dash'),
101
            'identifier' => contacts_widget::class,
102
            'help' => 'widget:mycontacts',
103
        ],
104
        [
105
            'name' => get_string('widget:mygroups', 'block_dash'),
106
            'identifier' => groups_widget::class,
107
            'help' => 'widget:mygroups',
108
        ],
109
 
110
    ];
111
}
112
 
113
/**
114
 * Serve the new group form as a fragment.
115
 *
116
 * @param array $args List of named arguments for the fragment loader.
117
 * @return string
118
 */
119
function block_dash_output_fragment_block_preferences_form($args) {
120
    global $DB, $OUTPUT;
121
 
122
    $args = (object) $args;
123
    $context = $args->context;
124
 
125
    $blockinstance = $DB->get_record('block_instances', ['id' => $context->instanceid]);
126
    $block = block_instance($blockinstance->blockname, $blockinstance);
127
 
128
    if (!$args->tab) {
129
        $args->tab = preferences_form::TAB_GENERAL;
130
    }
131
 
132
    $form = new preferences_form(null, ['block' => $block, 'tab' => $args->tab], 'post', '', [
133
        'class' => 'dash-preferences-form',
134
        'data-double-submit-protection' => 'off',
135
    ]);
136
 
137
    require_capability('block/dash:addinstance', $context);
138
 
139
    if (isset($block->config->preferences)) {
140
        $data = block_dash_flatten_array($block->config->preferences, 'config_preferences');
141
        $form->set_data($data);
142
    }
143
 
144
    ob_start();
145
    $form->display();
146
    $formhtml = ob_get_contents();
147
    ob_end_clean();
148
 
149
    $tabs = [];
150
    foreach (preferences_form::TABS as $tab) {
151
        $tabs[] = [
152
            'label' => get_string($tab, 'block_dash'),
153
            'active' => $tab == $args->tab,
154
            'tabid' => $tab,
155
        ];
156
    }
157
 
158
    return $OUTPUT->render_from_template('block_dash/preferences_form', [
159
        'formhtml' => $formhtml,
160
        'tabs' => $tabs,
161
        'istotara' => block_dash_is_totara(),
162
    ]);
163
}
164
 
165
/**
166
 * File serving callback
167
 *
168
 * @param stdClass $course course object
169
 * @param stdClass $cm course module object
170
 * @param stdClass $context context object
171
 * @param string $filearea file area
172
 * @param array $args extra arguments
173
 * @param bool $forcedownload whether or not force download
174
 * @param array $options additional options affecting the file serving
175
 * @return bool false if the file was not found, just send the file otherwise and do not return anything
176
 */
177
function block_dash_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=[]) {
178
 
179
    if ($context->contextlevel != CONTEXT_BLOCK && $context->contextlevel != CONTEXT_SYSTEM) {
180
        return false;
181
    }
182
 
183
    require_login();
184
 
185
    if ($filearea == 'images' || $filearea == 'categoryimg') {
186
 
187
        $relativepath = implode('/', $args);
188
 
189
        $fullpath = "/$context->id/block_dash/$filearea/$relativepath";
190
 
191
        $fs = get_file_storage();
192
        $file = $fs->get_file_by_hash(sha1($fullpath));
193
        if (!$file || $file->is_directory()) {
194
            return false;
195
        }
196
 
197
        send_stored_file($file, null, 0, $forcedownload, $options);
198
    }
199
}
200
 
201
/**
202
 * Flatten array to form field names.
203
 *
204
 * @param array $array
205
 * @param string $prefix
206
 * @return array
207
 */
208
function block_dash_flatten_array($array, $prefix = '') {
209
    $result = [];
210
    foreach ($array as $key => $value) {
211
        if (is_integer($key)) {
212
            // Don't flatten arrays with numeric indexes. Otherwise it won't be set on the Moodle form.
213
            $result[$prefix] = $array;
214
        } else if (is_array($value)) {
215
            $result = $result + block_dash_flatten_array($value, $prefix . '[' . $key . ']');
216
        } else {
217
            $result[$prefix . '[' . $key . ']'] = $value;
218
        }
219
    }
220
    return $result;
221
}
222
 
223
/**
224
 * Check if system is Totara.
225
 *
226
 * @return bool
227
 */
228
function block_dash_is_totara() {
229
    global $CFG;
230
    return file_exists("$CFG->dirroot/totara");
231
}
232
 
233
/**
234
 * Check if pro plugin is installed.
235
 *
236
 * @return bool
237
 */
238
function block_dash_has_pro() {
239
    return array_key_exists('dash', core_component::get_plugin_list('local'));
240
}
241
 
242
/**
243
 * Check if dash output should be disabled.
244
 *
245
 * @return bool
246
 * @throws dml_exception
247
 */
248
function block_dash_is_disabled() {
249
    global $CFG;
250
 
251
    if (get_config('block_dash', 'disableall')) {
252
        return true;
253
    }
254
 
255
    if (isset($CFG->block_dash_disableall) && $CFG->block_dash_disableall) {
256
        return true;
257
    }
258
 
259
    return false;
260
}
261
 
262
/**
263
 * Fragment to load the widget methods.
264
 *
265
 * @param stdclass $args
266
 * @return string Returns the widget content.
267
 */
268
function block_dash_output_fragment_loadwidget($args) {
269
    global $DB;
270
    $args = (object) $args;
271
    $context = $args->context;
272
 
273
    $blockinstance = $DB->get_record('block_instances', ['id' => $context->instanceid]);
274
    $block = block_instance($blockinstance->blockname, $blockinstance);
275
    $datasource = block_dash\local\block_builder::create($block)->get_configuration()->get_data_source();
276
 
277
    if (isset($datasource->iswidget)) {
278
        $method = $args->method;
279
        $params = json_decode($args->args);
280
        if (isset($params->page)) {
281
            $datasource->get_paginator()->set_current_page($params->page);
282
        }
283
        return (method_exists($datasource, $method)) ? $datasource->$method($context, $params) : '';
284
    }
285
    return null;
286
}
287
 
288
/**
289
 * Load the table pagination via ajax. withou page refresh.
290
 *
291
 * @param stdclass $args
292
 * @return string
293
 */
294
function block_dash_output_fragment_loadtable($args) {
295
    global $DB;
296
 
297
    $args = (object) $args;
298
    $context = $args->context;
299
 
300
    $classstr = 'block_dash\table\\'.$args->handler;
301
    $table = new $classstr($args->uniqueid);
302
    $table->set_filterset(json_decode($args->filter));
303
    $table->set_sort_column($args->sort);
304
    $table->currentpage = isset($args->page) ? $args->page : 0;
305
 
306
    ob_start();
307
    echo html_writer::start_div('dash-widget-table');
308
    $table->out(10, true);
309
    echo html_writer::end_div();
310
    $tablehtml = ob_get_contents();
311
    ob_end_clean();
312
 
313
    return $tablehtml;
314
 
315
}
316
 
317
/**
318
 * Get list of all suggest users for contact list.
319
 */
320
function block_dash_get_suggest_users() {
321
    global $DB, $CFG;
322
 
323
    $users = $DB->get_records_sql("SELECT *
324
                                   FROM {user}
325
                                  WHERE confirmed = 1 AND deleted = 0 AND id <> ?", [$CFG->siteguest]);
326
    foreach ($users as $user) {
327
        $list[$user->id] = fullname($user);
328
    }
329
    return isset($list) ? $list : [];
330
}
331
 
332
/**
333
 * Get the current php version related data collection. Data collection implements PHP arrayAccess class.
334
 * PHP arrayaccess parameters are changed from 8.1.
335
 *
336
 * @return mixed
337
 */
338
function block_dash_get_data_collection() {
339
    return version_compare(phpversion(), '8.1', '<')
340
        ? new block_dash\local\data_grid\data\data_collection() : new \block_dash\local\data_grid\data\data_collection_new();
341
}
342
 
343
/**
344
 * Return the dash addon visible staus.
345
 *
346
 * @param int $id ID
347
 * @return bool
348
 */
349
function block_dash_visible_addons($id) {
350
    global $CFG;
351
    preg_match('/(dashaddon_[a-zA-Z_]+)/', $id, $matches);
352
    if ($matches) {
353
        $value = $matches[1];
354
        $parts = explode('\\', $value);
355
        if ($parts) {
356
            $addon = $parts[0];
357
            $addondependencies = $addon . "_extend_added_dependencies";
358
            if (get_config($addon, 'enabled')) {
359
                $addonplugin = explode("dashaddon_", $addon)[1];
360
                if (file_exists($CFG->dirroot. "/local/dash/addon/$addonplugin/lib.php")) {
361
                    require_once($CFG->dirroot. "/local/dash/addon/$addonplugin/lib.php");
362
                    if (function_exists($addondependencies) && !empty($addondependencies())) {
363
                        return false;
364
                    }
365
                }
366
            } else {
367
                return false;
368
            }
369
        }
370
    }
371
    return true;
372
}