-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblock_genericotemp.php
288 lines (245 loc) · 8.61 KB
/
block_genericotemp.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
<?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/>.
/**
* Form for editing genericotemp block instances.
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @package block_genericotemp
* @copyright 29/12/2019 Mfreak.nl | LdesignMedia.nl - Luuk Verhoeven
* @author Luuk Verhoeven
*/
/**
* Class block_genericotemp
*/
class block_genericotemp extends block_base {
/**
* @throws coding_exception
*/
public function init() {
$this->title = get_string('pluginname', 'block_genericotemp');
}
/**
* @return bool
*/
public function has_config() {
return true;
}
/**
* @return array
*/
public function applicable_formats() {
return [
'course-view' => true,
'my' => true,
];
}
/**
* @throws coding_exception
*/
public function specialization() {
if (isset($this->config->title)) {
$this->title = $this->title = format_string($this->config->title, true, ['context' => $this->context]);
} else {
$this->title = '';
}
}
/**
* @return bool
*/
public function instance_allow_multiple() {
return true;
}
/**
* @return stdClass|stdObject
* @throws coding_exception
*/
public function get_content() {
global $CFG;
require_once($CFG->libdir . '/filelib.php');
if ($this->content !== null) {
return $this->content;
}
if (!has_capability('block/genericotemp:view', $this->context)) {
return (object)[
];
}
$filteropt = new stdClass;
$filteropt->overflowdiv = true;
if ($this->content_is_trusted()) {
// fancy html allowed only on course, category and system blocks.
$filteropt->noclean = true;
}
$this->content = new stdClass;
$this->content->footer = '';
if (isset($this->config->text)) {
// rewrite url
$this->config->text = file_rewrite_pluginfile_urls($this->config->text, 'pluginfile.php', $this->context->id, 'block_genericotemp', 'content', null);
// Default to FORMAT_HTML which is what will have been used before the
// editor was properly implemented for the block.
$format = FORMAT_HTML;
// Check to see if the format has been properly set on the config
if (isset($this->config->format)) {
$format = $this->config->format;
}
$this->content->text = format_text($this->config->text, $format, $filteropt);
} else {
$this->content->text = '';
}
unset($filteropt); // memory footprint
return $this->content;
}
/**
* @param core_renderer $output
*
* @return stdClass
* @throws coding_exception
*/
public function get_content_for_external($output) {
global $CFG;
require_once($CFG->libdir . '/externallib.php');
$bc = new stdClass;
$bc->title = null;
$bc->content = '';
$bc->contenformat = FORMAT_MOODLE;
$bc->footer = '';
$bc->files = [];
if (!$this->hide_header()) {
$bc->title = $this->title;
}
if (isset($this->config->text)) {
$filteropt = new stdClass;
if ($this->content_is_trusted()) {
// Fancy html allowed only on course, category and system blocks.
$filteropt->noclean = true;
}
$format = FORMAT_HTML;
// Check to see if the format has been properly set on the config.
if (isset($this->config->format)) {
$format = $this->config->format;
}
[$bc->content, $bc->contentformat] =
external_format_text($this->config->text, $format, $this->context, 'block_genericotemp', 'content', null, $filteropt);
$bc->files = external_util::get_area_files($this->context->id, 'block_genericotemp', 'content', false, false);
}
return $bc;
}
/**
* Serialize and store config data
*/
public function instance_config_save($data, $nolongerused = false) {
$config = clone($data);
// Move embedded files into a proper filearea and adjust HTML links to match
$config->text = file_save_draft_area_files($data->text['itemid'], $this->context->id, 'block_genericotemp', 'content', 0, ['subdirs' => true], $data->text['text']);
$config->format = $data->text['format'];
parent::instance_config_save($config, $nolongerused);
}
/**
* @return bool
*/
public function instance_delete() {
$fs = get_file_storage();
$fs->delete_area_files($this->context->id, 'block_genericotemp');
return true;
}
/**
* Copy any block-specific data when copying to a new block instance.
*
* @param int $fromid the id number of the block instance to copy from
*
* @return boolean
*/
public function instance_copy($fromid) {
$fromcontext = context_block::instance($fromid);
$fs = get_file_storage();
// This extra check if file area is empty adds one query if it is not empty but saves several if it is.
if (!$fs->is_area_empty($fromcontext->id, 'block_genericotemp', 'content', 0, false)) {
$draftitemid = 0;
file_prepare_draft_area($draftitemid, $fromcontext->id, 'block_genericotemp', 'content', 0, ['subdirs' => true]);
file_save_draft_area_files($draftitemid, $this->context->id, 'block_genericotemp', 'content', 0, ['subdirs' => true]);
}
return true;
}
/**
* @return bool
* @throws coding_exception
*/
public function content_is_trusted() {
global $SCRIPT;
if (!$context = context::instance_by_id($this->instance->parentcontextid, IGNORE_MISSING)) {
return false;
}
//find out if this block is on the profile page
if ($context->contextlevel == CONTEXT_USER) {
if ($SCRIPT === '/my/index.php') {
// this is exception - page is completely private, nobody else may see content there
// that is why we allow JS here
return true;
} else {
// no JS on public personal pages, it would be a big security issue
return false;
}
}
return true;
}
/**
* The block should only be dockable when the title of the block is not empty
* and when parent allows docking.
*
* @return bool
*/
public function instance_can_be_docked() {
return (!empty($this->config->title) && parent::instance_can_be_docked());
}
/**
* Add custom html attributes to aid with theming and styling
*
* @return array
*/
public function html_attributes() {
global $CFG;
$attributes = parent::html_attributes();
if (!empty($CFG->block_genericotemp_allowcssclasses)) {
if (!empty($this->config->classes)) {
$attributes['class'] .= ' ' . $this->config->classes;
}
}
return $attributes;
}
/**
* Do any additional initialization you may need at the time a new block instance is created
*
* @return boolean
* @throws dml_exception
*/
public function instance_create() {
global $DB, $COURSE;
// Add default content.
// Make visible on all pages.
$config = new stdClass();
$config->text = get_config('block_genericotemp', 'text');
$config->format = FORMAT_HTML;
parent::instance_config_save($config);
if (!empty($COURSE->id) && $COURSE->id > 1) {
// Update default to course-*.
$DB->update_record('block_instances', (object)[
'id' => $this->instance->id,
'pagetypepattern' => 'course-*',
]);
}
return true;
}
}