forked from bdecentgmbh/moodle-block_dash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_dash.php
413 lines (356 loc) · 13.3 KB
/
block_dash.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?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/>.
/**
* Dash block class.
*
* @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\block_builder;
use block_dash\local\data_source\data_source_factory;
defined('MOODLE_INTERNAL') || die();
require_once("$CFG->dirroot/blocks/dash/lib.php");
require_once("$CFG->libdir/filelib.php");
/**
* Dash block class.
*/
class block_dash extends block_base {
/**
* Initialize block instance.
*
* @throws coding_exception
*/
public function init() {
$this->title = get_string('pluginname', 'block_dash');
}
/**
* This block supports configuration fields.
*
* @return bool
*/
public function has_config() {
return true;
}
/**
* This function is called on your subclass right after an instance is loaded
* Use this function to act on instance data just after it's loaded and before anything else is done
* For instance: if your block will have different title's depending on location (site, course, blog, etc)
*
* @throws coding_exception
*/
public function specialization() {
global $OUTPUT;
if (isset($this->config->title)) {
$this->title = $this->title = format_string($this->config->title, true, ['context' => $this->context]);
} else {
$this->title = get_string('newblock', 'block_dash');
}
try {
$bb = block_builder::create($this);
if ($bb->is_collapsible_content_addon()) {
$addclass = "collapsible-block dash-block-collapse-icon";
if (!$bb->is_section_expand_content_addon()) {
$addclass .= " collapsed";
}
$attr = [
'data-toggle' => 'collapse',
'class' => $addclass,
'href' => "#dash-{$this->instance->id}",
"aria-expanded" => "false",
"aria-controls" => "dash-{$this->instance->id}",
];
$this->title = html_writer::tag('span', $this->title, $attr);
}
} catch (\Exception $e) {
// Configured datasource is missing.
$this->title = get_string('newblock', 'block_dash');
}
}
/**
* Multiple dashes can be added to a single page.
*
* @return bool
*/
public function instance_allow_multiple() {
return true;
}
/**
* Serialize and store config data
*
* @param string $data
* @param bool $nolongerused
* @return void
*/
public function instance_config_save($data, $nolongerused = false) {
if (isset($data->backgroundimage)) {
file_save_draft_area_files($data->backgroundimage, $this->context->id, 'block_dash', 'images',
0, ['subdirs' => 0, 'maxfiles' => 1]);
}
parent::instance_config_save($data, $nolongerused);
}
/**
* Copy any block-specific data when copying to a new block instance.
*
* @param int $frominstanceid the id number of the block instance to copy from
* @return boolean
*/
public function instance_copy($frominstanceid) {
// Copy the block instance background image.
$fromcontext = \context_block::instance($frominstanceid);
$fs = get_file_storage();
// Do not use draft files hacks outside of forms.
$files = $fs->get_area_files($fromcontext->id, 'block_dash', 'images', 0, 'id ASC', false);
foreach ($files as $file) {
$filerecord = ['contextid' => $this->context->id];
$fs->create_file_from_storedfile($filerecord, $file);
}
// Copy the datasource images and files.
$bb = block_builder::create($this);
$datasource = $bb->get_configuration()->get_data_source();
if (!empty($datasource) && method_exists($datasource, 'instance_copy')) {
$datasource->instance_copy($frominstanceid, $this->context->id);
}
return true;
}
/**
* Dashes are suitable on all page types.
*
* @return array
*/
public function applicable_formats() {
return ['all' => true];
}
/**
* Return block content. Build dash.
*
* @return \stdClass
*/
public function get_content() {
global $OUTPUT;
// Prevent the jqueryui conflict with bootstrap tooltip.
if (class_exists('\core\navigation\views\secondary')) {
$this->page->requires->js_init_code(
'require(["jquery", "jqueryui"], function($, ui) {
$.widget.bridge("uibutton", $.ui.button);
$.widget.bridge("uitooltip", $.ui.tooltip);
});'
);
}
if ($this->content !== null) {
return $this->content;
}
if (empty($this->instance)) {
return '';
}
$this->content = new \stdClass();
if (block_dash_is_disabled()) {
$this->content->text = is_siteadmin() ? get_string('disableallmessage', 'block_dash') : '';
return $this->content;
}
try {
$bb = block_builder::create($this);
$datasource = $bb->get_configuration()->get_data_source();
// Conditionally hide the block when empty.
if ($datasource && isset($this->config->hide_when_empty) && $this->config->hide_when_empty
&& (($datasource->is_widget() && $datasource->is_empty())
|| (!$datasource->is_widget() && $datasource->get_data()->is_empty()))
&& !$this->page->user_is_editing()) {
return $this->content;
}
$this->content = $bb->get_block_content();
if ($css = $this->get_extra_css()) {
$this->content->text .= $css;
}
} catch (\Exception $e) {
$this->content->text = $OUTPUT->notification($e->getMessage() . $e->getTraceAsString(), 'error');
}
$this->page->requires->css(new \moodle_url('/blocks/dash/styles/select2.min.css'));
$this->page->requires->css(new \moodle_url('/blocks/dash/styles/datepicker.css'));
$this->page->requires->css(new \moodle_url('/blocks/dash/styles/slick.css'));
return $this->content;
}
/**
* Add block width CSS classes.
*
* @return array
*/
public function html_attributes() {
$attributes = parent::html_attributes();
if (isset($this->config->css_class)) {
$attributes['class'] .= ' ' . $this->config->css_class;
}
if (isset($this->config->width)) {
$attributes['class'] .= ' dash-block-width-' . $this->config->width;
} else {
$attributes['class'] .= ' dash-block-width-100';
}
if (isset($this->config->preferences['layout'])) {
$attributes['class'] .= ' ' . str_replace('\\', '-', $this->config->preferences['layout']);
}
try {
$bb = block_builder::create($this);
if ($bb->is_collapsible_content_addon()) {
$attributes['class'] .= ' block-collapse-block';
}
} catch (\Exception $e) {
$attributes['class'] .= ' missing-datasource';
}
return $attributes;
}
/**
* Get extra CSS styling for this specific block.
*
* @return string
*/
public function get_extra_css() {
global $OUTPUT;
$blockcss = [];
$data = [
'block' => $this,
'headerfootercolor' => isset($this->config->headerfootercolor) ? $this->config->headerfootercolor : null,
];
$backgroundgradient = isset($this->config->backgroundgradient)
? str_replace(';', '', $this->config->backgroundgradient) : null;
if ($this->get_background_image_url()) {
if ($backgroundgradient) {
$blockcss[] = sprintf('background-image: %s, url(%s);',
$backgroundgradient, $this->get_background_image_url()->out()
);
} else {
$blockcss[] = sprintf('background-image: url(%s);', $this->get_background_image_url());
}
} else if ($backgroundgradient) {
$blockcss[] = sprintf('background: %s', $this->config->backgroundgradient);
}
if (isset($this->config->css) && is_array($this->config->css)) {
foreach ($this->config->css as $property => $value) {
if (!empty($value)) {
$blockcss[] = sprintf('%s: %s;', $property, $value);
}
}
}
$data['blockcss'] = implode(PHP_EOL, $blockcss);
return $OUTPUT->render_from_template('block_dash/extra_css', $data);
}
/**
* Get background image.
*
* @return stored_file|null
* @throws coding_exception
*/
public function get_background_image() {
$fs = get_file_storage();
$backgroundimage = null;
foreach ($fs->get_area_files($this->context->id, 'block_dash', 'images', 0) as $file) {
if ($file->is_valid_image()) {
return $file;
}
}
return null;
}
/**
* Get background image URL.
*
* @return moodle_url|null
*/
public function get_background_image_url() {
if ($backgroundimage = $this->get_background_image()) {
return moodle_url::make_pluginfile_url(
$backgroundimage->get_contextid(),
$backgroundimage->get_component(),
$backgroundimage->get_filearea(),
$backgroundimage->get_itemid(),
$backgroundimage->get_filepath(),
$backgroundimage->get_filename()
);
}
return null;
}
/**
* Set dash sorting.
*
* @param string $fieldname
* @param string|null $direction
* @throws coding_exception
*/
public function set_sort($fieldname, $direction = null) {
global $USER;
$key = $USER->id . '_' . $this->instance->id;
$cache = \cache::make_from_params(\cache_store::MODE_SESSION, 'block_dash', 'sort');
if (!$cache->has($key)) {
$sorting = [];
} else {
$sorting = $cache->get($key);
}
if (isset($sorting[$fieldname]) && !$direction) {
if ($sorting[$fieldname] == 'asc') {
$sorting[$fieldname] = 'desc';
} else {
$sorting[$fieldname] = 'asc';
}
} else {
if ($direction) {
$sorting[$fieldname] = $direction;
} else {
$sorting[$fieldname] = 'asc';
}
}
$cache->set($key, [$fieldname => $sorting[$fieldname]]);
}
/**
* Include the preference option to the blocks controls before genreate the output.
*
* @param \core_renderer $output
* @return \block_contents
*/
public function get_content_for_output($output) {
$bc = parent::get_content_for_output($output);
$datasource = $this->config->data_source_idnumber ?? '';
if ($datasource) {
$info = \block_dash\local\data_source\data_source_factory::get_data_source_info($datasource);
$type = $info['type'] ?? 'datasource';
switch($type) {
case 'datasource':
$hascapability = has_capability('block/dash:managedatasource', $this->context);
break;
case 'widget':
$hascapability = has_capability('block/dash:managewidget', $this->context);
break;
case 'custom':
$hascapability = $datasource::has_capbility($this->context);
break;
}
} else {
$hascapability = true;
}
if (!isset($bc->controls) || !$hascapability) {
return $bc;
}
// Move icon.
$str = new lang_string('preferences', 'core');
$icon = $output->render(new pix_icon('i/dashboard', $str, 'moodle', ['class' => 'iconsmall', 'title' => '']));
$newcontrols = [];
foreach ($bc->controls as $controls) {
$newcontrols[] = $controls;
if ($controls->text instanceof lang_string && $controls->text->get_identifier() == 'configureblock') {
$newcontrols[] = html_writer::link('javascript:void(0);', $icon . $str, ['class' => 'dash-edit-preferences']);
}
}
$bc->controls = $newcontrols;
return $bc;
}
}