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 |
* Class that does all the magic.
|
|
|
19 |
*
|
|
|
20 |
* @package block_multiblock
|
|
|
21 |
* @copyright 2019 Peter Spicer <peter.spicer@catalyst-eu.net> 2021 James Pearce <jmp201@bath.ac.uk>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
use block_multiblock\helper;
|
|
|
26 |
use block_multiblock\icon_helper;
|
|
|
27 |
use block_multiblock\adddefaultblock;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Block multiblock class definition.
|
|
|
31 |
*
|
|
|
32 |
* This block can be added to a variety of places to display multiple blocks in one space.
|
|
|
33 |
*
|
|
|
34 |
* @package block_multiblock
|
|
|
35 |
* @copyright 2019 Peter Spicer <peter.spicer@catalyst-eu.net>
|
|
|
36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
37 |
*/
|
|
|
38 |
class block_multiblock extends block_base {
|
|
|
39 |
/** @var object $output Temporary storage of the injected page renderer so we can pass it to child blocks at render time. */
|
|
|
40 |
private $output;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Core function used to initialize the block.
|
|
|
44 |
*/
|
|
|
45 |
public function init() {
|
|
|
46 |
$this->title = get_string('pluginname', 'block_multiblock');
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* has_config - denotes whether your block wants to present a configuration interface to site admins or not
|
|
|
51 |
*
|
|
|
52 |
* @return boolean
|
|
|
53 |
*/
|
|
|
54 |
public function has_config() {
|
|
|
55 |
return true;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Core function, specifies where the block can be used.
|
|
|
60 |
*
|
|
|
61 |
* @return array
|
|
|
62 |
*/
|
|
|
63 |
public function applicable_formats() {
|
|
|
64 |
return [
|
|
|
65 |
'all' => true,
|
|
|
66 |
];
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Sets the block's title for a specific instance based on its configuration.
|
|
|
71 |
*/
|
|
|
72 |
public function specialization() {
|
|
|
73 |
$defaulttitle = get_config('block_multiblock', 'title');
|
|
|
74 |
if (isset($this->config->title)) {
|
|
|
75 |
$this->title = format_string($this->config->title, true, ['context' => $this->context]);
|
|
|
76 |
} else if ($defaulttitle) {
|
|
|
77 |
$this->title = format_string($defaulttitle, true, ['context' => $this->context]);
|
|
|
78 |
} else {
|
|
|
79 |
$this->title = get_string('pluginname', 'block_multiblock');
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Loads the child blocks of the current multiblock.
|
|
|
85 |
*
|
|
|
86 |
* @param int $contextid The multiblock's context instance id.
|
|
|
87 |
* @return array An array of child blocks.
|
|
|
88 |
*/
|
|
|
89 |
public function load_multiblocks($contextid) {
|
|
|
90 |
global $DB;
|
|
|
91 |
|
|
|
92 |
// Find all the things that relate to this block.
|
|
|
93 |
$this->blocks = $DB->get_records('block_instances', ['parentcontextid' => $contextid], 'defaultweight, id');
|
|
|
94 |
foreach ($this->blocks as $id => $block) {
|
|
|
95 |
if (block_load_class($block->blockname)) {
|
|
|
96 |
// Make the proxy class we'll need.
|
|
|
97 |
$this->blocks[$id]->blockinstance = block_instance($block->blockname, $block);
|
|
|
98 |
$this->blocks[$id]->blockname = $block->blockname;
|
|
|
99 |
$this->blocks[$id]->visible = true;
|
|
|
100 |
$this->blocks[$id]->blockpositionid = 0;
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
return $this->blocks;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Used to add the default blocks to the multiblock.
|
|
|
109 |
*/
|
|
|
110 |
public function add_default_blocks() {
|
|
|
111 |
global $DB, $CFG;
|
|
|
112 |
|
|
|
113 |
if (empty($this->instance)) {
|
|
|
114 |
return $this->content;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
$context = $DB->get_record('context', ['contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id]);
|
|
|
118 |
|
|
|
119 |
$this->load_multiblocks($context->id);
|
|
|
120 |
|
|
|
121 |
$multiblock = [];
|
|
|
122 |
$isodd = true;
|
|
|
123 |
$blockid = $this->instance->id;
|
|
|
124 |
if (empty($this->blocks)) {
|
|
|
125 |
|
|
|
126 |
$defaultblocksarray = explode(',', get_config('block_multiblock')->subblock);
|
|
|
127 |
|
|
|
128 |
$addblock = new adddefaultblock();
|
|
|
129 |
$addblock->init($blockid, $defaultblocksarray, $this->instance);
|
|
|
130 |
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
/**
|
|
|
135 |
* Used to generate the content for the block.
|
|
|
136 |
*
|
|
|
137 |
* @return string
|
|
|
138 |
*/
|
|
|
139 |
public function get_content() {
|
|
|
140 |
global $DB, $CFG;
|
|
|
141 |
if ($this->content !== null) {
|
|
|
142 |
return $this->content;
|
|
|
143 |
}
|
|
|
144 |
$this->content = new stdClass;
|
|
|
145 |
$this->content->text = '';
|
|
|
146 |
$this->content->footer = '';
|
|
|
147 |
if (empty($this->instance)) {
|
|
|
148 |
return $this->content;
|
|
|
149 |
}
|
|
|
150 |
$context = $DB->get_record('context', ['contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id]);
|
|
|
151 |
|
|
|
152 |
$this->load_multiblocks($context->id);
|
|
|
153 |
|
|
|
154 |
$multiblock = [];
|
|
|
155 |
$isodd = true;
|
|
|
156 |
$blockid = $this->instance->id;
|
|
|
157 |
|
|
|
158 |
if (empty($this->blocks)) {
|
|
|
159 |
$this->add_default_blocks();
|
|
|
160 |
$this->load_multiblocks($context->id);
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
foreach ($this->blocks as $id => $block) {
|
|
|
164 |
if (empty($block->blockinstance)) {
|
|
|
165 |
continue;
|
|
|
166 |
}
|
|
|
167 |
$content = $block->blockinstance->get_content_for_output($this->output);
|
|
|
168 |
$multiblock[] = [
|
|
|
169 |
'id' => $id,
|
|
|
170 |
'class' => 'block_' . $block->blockinstance->name(),
|
|
|
171 |
'type' => $block->blockinstance->name(),
|
|
|
172 |
'is_odd' => $isodd,
|
|
|
173 |
'title' => $block->blockinstance->get_title(),
|
|
|
174 |
'content' => !empty($content->content) ? $content->content : '',
|
|
|
175 |
'footer' => !empty($content->footer) ? $content->footer : '',
|
|
|
176 |
];
|
|
|
177 |
$isodd = !$isodd;
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
$template = '';
|
|
|
181 |
$presentations = static::get_valid_presentations();
|
|
|
182 |
$multiblockpresentationoptions = [];
|
|
|
183 |
foreach ($presentations as $presentationid => $presentation) {
|
|
|
184 |
array_push($multiblockpresentationoptions, $presentationid);
|
|
|
185 |
}
|
|
|
186 |
$configuredpresentation = get_config('block_multiblock', 'presentation');
|
|
|
187 |
if (!empty($this->config->presentation)) {
|
|
|
188 |
$template = $this->config->presentation;
|
|
|
189 |
} else if (isset($configuredpresentation)) {
|
|
|
190 |
$template = $multiblockpresentationoptions[$configuredpresentation];
|
|
|
191 |
} else if (isset($presentations['tabbed-list'])) {
|
|
|
192 |
$template = 'tabbed-list';
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
$renderable = new \block_multiblock\output\main((int) $this->instance->id, $multiblock, $template);
|
|
|
196 |
$renderer = $this->page->get_renderer('block_multiblock');
|
|
|
197 |
|
|
|
198 |
$this->content = (object) [
|
|
|
199 |
'text' => $renderer->render($renderable),
|
|
|
200 |
'footer' => ''
|
|
|
201 |
];
|
|
|
202 |
return $this->content;
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
/**
|
|
|
206 |
* Return a block_contents object representing the full contents of this block.
|
|
|
207 |
*
|
|
|
208 |
* This internally calls ->get_content(), and then adds the editing controls etc.
|
|
|
209 |
*
|
|
|
210 |
* @param object $output The output renderer from the parent context (e.g. page renderer)
|
|
|
211 |
* @return block_contents a representation of the block, for rendering.
|
|
|
212 |
*/
|
|
|
213 |
public function get_content_for_output($output) {
|
|
|
214 |
$this->output = $output;
|
|
|
215 |
$bc = parent::get_content_for_output($output);
|
|
|
216 |
|
|
|
217 |
if (empty($bc->controls)) {
|
|
|
218 |
return $bc;
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
$str = get_string('managemultiblock', 'block_multiblock', $this->title);
|
|
|
222 |
|
|
|
223 |
$newcontrols = [];
|
|
|
224 |
foreach ($bc->controls as $control) {
|
|
|
225 |
$newcontrols[] = $control;
|
|
|
226 |
// Append our new item onto the controls if we're on the correct item.
|
|
|
227 |
if (strpos($control->attributes['class'], 'editing_edit') !== false) {
|
|
|
228 |
$newcontrols[] = new action_menu_link_secondary(
|
|
|
229 |
new moodle_url('/blocks/multiblock/manage.php', ['id' => $this->instance->id, 'sesskey' => sesskey()]),
|
|
|
230 |
icon_helper::preferences($str),
|
|
|
231 |
$str,
|
|
|
232 |
['class' => 'editing_manage']
|
|
|
233 |
);
|
|
|
234 |
}
|
|
|
235 |
}
|
|
|
236 |
// Append a delete+split item on the end.
|
|
|
237 |
$newcontrols[] = new action_menu_link_secondary(
|
|
|
238 |
new moodle_url('/blocks/multiblock/manage.php', ['id' => $this->instance->id, 'sesskey' => sesskey(),
|
|
|
239 |
'action' => 'splitdelete']),
|
|
|
240 |
icon_helper::delete($str),
|
|
|
241 |
get_string('splitanddelete', 'block_multiblock', $this->title),
|
|
|
242 |
['class' => 'editing_manage']
|
|
|
243 |
);
|
|
|
244 |
$bc->controls = $newcontrols;
|
|
|
245 |
return $bc;
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
/**
|
|
|
249 |
* Allows the block to be added multiple times to a single page
|
|
|
250 |
* @return boolean
|
|
|
251 |
*/
|
|
|
252 |
public function instance_allow_multiple() {
|
|
|
253 |
return true;
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
/**
|
|
|
257 |
* Copy all the children when copying to a new block instance.
|
|
|
258 |
*
|
|
|
259 |
* @param int $fromid The id number of the block instance to copy from
|
|
|
260 |
* @return bool
|
|
|
261 |
*/
|
|
|
262 |
public function instance_copy($fromid) {
|
|
|
263 |
global $DB;
|
|
|
264 |
|
|
|
265 |
$fromcontext = context_block::instance($fromid);
|
|
|
266 |
|
|
|
267 |
$blockinstances = $DB->get_records('block_instances', ['parentcontextid' => $fromcontext->id], 'defaultweight, id');
|
|
|
268 |
|
|
|
269 |
// Create all the new block instances.
|
|
|
270 |
$newblockinstanceids = [];
|
|
|
271 |
foreach ($blockinstances as $instance) {
|
|
|
272 |
$originalid = $instance->id;
|
|
|
273 |
unset($instance->id);
|
|
|
274 |
$instance->parentcontextid = $this->context->id;
|
|
|
275 |
$instance->timecreated = time();
|
|
|
276 |
$instance->timemodified = $instance->timecreated;
|
|
|
277 |
$instance->id = $DB->insert_record('block_instances', $instance);
|
|
|
278 |
$newblockinstanceids[$originalid] = $instance->id;
|
|
|
279 |
$blockcontext = context_block::instance($instance->id); // Just creates the context record.
|
|
|
280 |
$block = block_instance($instance->blockname, $instance);
|
|
|
281 |
if (!$block->instance_copy($originalid)) {
|
|
|
282 |
debugging("Unable to copy block-specific data for original block instance: $originalid
|
|
|
283 |
to new block instance: $instance->id", DEBUG_DEVELOPER);
|
|
|
284 |
}
|
|
|
285 |
}
|
|
|
286 |
return true;
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
/**
|
|
|
290 |
* Callback for when this block instance is being deleted, to clean up child blocks.
|
|
|
291 |
*
|
|
|
292 |
* @return bool
|
|
|
293 |
*/
|
|
|
294 |
public function instance_delete() {
|
|
|
295 |
global $DB;
|
|
|
296 |
|
|
|
297 |
// Find all the things that relate to this block.
|
|
|
298 |
foreach ($DB->get_records('block_instances', ['parentcontextid' => $this->context->id]) as $subblock) {
|
|
|
299 |
blocks_delete_instance($subblock);
|
|
|
300 |
}
|
|
|
301 |
return true;
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
/**
|
|
|
305 |
* Lists all the known presentation types that exist in the block.
|
|
|
306 |
*
|
|
|
307 |
* @return array An array of presentations for block rendering.
|
|
|
308 |
*/
|
|
|
309 |
public static function get_valid_presentations(): array {
|
|
|
310 |
static $presentations = null;
|
|
|
311 |
|
|
|
312 |
if ($presentations === null) {
|
|
|
313 |
|
|
|
314 |
foreach (core_component::get_component_classes_in_namespace('block_multiblock', 'layout') as $class => $ns) {
|
|
|
315 |
if (strpos($class, $ns[0]) === 0) {
|
|
|
316 |
// We only care about non-abstract classes here.
|
|
|
317 |
$reflection = new ReflectionClass($class);
|
|
|
318 |
if ($reflection->isAbstract()) {
|
|
|
319 |
continue;
|
|
|
320 |
}
|
|
|
321 |
$classname = substr($class, strlen($ns[0]));
|
|
|
322 |
|
|
|
323 |
$instance = new $class;
|
|
|
324 |
$presentations[$instance->get_layout_id()] = $instance;
|
|
|
325 |
}
|
|
|
326 |
}
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
return $presentations;
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
/**
|
|
|
333 |
* Returns the default presentation for the multiblock.
|
|
|
334 |
*
|
|
|
335 |
* @return string The default presentation's identifier.
|
|
|
336 |
*/
|
|
|
337 |
public static function get_default_presentation(): string {
|
|
|
338 |
$presentations = static::get_valid_presentations();
|
|
|
339 |
$multiblockpresentationoptions = [];
|
|
|
340 |
$configuredpresentation = get_config('block_multiblock', 'presentation');
|
|
|
341 |
foreach ($presentations as $presentationid => $presentation) {
|
|
|
342 |
array_push($multiblockpresentationoptions, $presentationid);
|
|
|
343 |
}
|
|
|
344 |
if ($configuredpresentation) {
|
|
|
345 |
return $multiblockpresentationoptions[$configuredpresentation];
|
|
|
346 |
}
|
|
|
347 |
// Our expected default is not present, make sure we fall back to something.
|
|
|
348 |
return array_keys($presentations)[0];
|
|
|
349 |
}
|
|
|
350 |
}
|