forked from catalyst/moodle-block_multiblock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_multiblock.php
290 lines (252 loc) · 10.1 KB
/
block_multiblock.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
<?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/>.
/**
* Class that does all the magic.
*
* @package block_multiblock
* @copyright 2019 Peter Spicer <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use block_multiblock\icon_helper;
defined('MOODLE_INTERNAL') || die();
/**
* Block multiblock class definition.
*
* This block can be added to a variety of places to display multiple blocks in one space.
*
* @package block_multiblock
* @copyright 2019 Peter Spicer <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_multiblock extends block_base {
/** @var object $output Temporary storage of the injected page renderer so we can pass it to child blocks at render time. */
private $output;
/**
* Core function used to initialize the block.
*/
public function init() {
$this->title = get_string('pluginname', 'block_multiblock');
}
/**
* Core function, specifies where the block can be used.
* @return array
*/
public function applicable_formats() {
return [
'all' => true,
];
}
/**
* Sets the block's title for a specific instance based on its configuration.
*/
public function specialization() {
if (isset($this->config->title)) {
$this->title = format_string($this->config->title, true, ['context' => $this->context]);
} else {
$this->title = get_string('pluginname', 'block_multiblock');
}
}
/**
* Loads the child blocks of the current multiblock.
*
* @param int $contextid The multiblock's context instance id.
* @return array An array of child blocks.
*/
public function load_multiblocks($contextid) {
global $DB;
// Find all the things that relate to this block.
$this->blocks = $DB->get_records('block_instances', ['parentcontextid' => $contextid], 'defaultweight, id');
foreach ($this->blocks as $id => $block) {
if (block_load_class($block->blockname)) {
// Make the proxy class we'll need.
$this->blocks[$id]->blockinstance = block_instance($block->blockname, $block);
$this->blocks[$id]->blockname = $block->blockname;
$this->blocks[$id]->visible = true;
$this->blocks[$id]->blockpositionid = 0;
}
}
return $this->blocks;
}
/**
* Used to generate the content for the block.
*
* @return string
*/
public function get_content() {
global $DB;
if ($this->content !== null) {
return $this->content;
}
$this->content = new stdClass;
$this->content->text = '';
$this->content->footer = '';
if (empty($this->instance)) {
return $this->content;
}
$context = $DB->get_record('context', ['contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id]);
$this->load_multiblocks($context->id);
$multiblock = [];
$isodd = true;
foreach ($this->blocks as $id => $block) {
if (empty($block->blockinstance)) {
continue;
}
$content = $block->blockinstance->get_content_for_output($this->output);
$multiblock[] = [
'id' => $id,
'class' => 'block_' . $block->blockinstance->name(),
'type' => $block->blockinstance->name(),
'is_odd' => $isodd,
'title' => $block->blockinstance->get_title(),
'content' => !empty($content->content) ? $content->content : '',
'footer' => !empty($content->footer) ? $content->footer : '',
];
$isodd = !$isodd;
}
$template = !empty($this->config->presentation) ? $this->config->presentation : 'tabbed-list';
$renderable = new \block_multiblock\output\main((int) $this->instance->id, $multiblock, $template);
$renderer = $this->page->get_renderer('block_multiblock');
$this->content = (object) [
'text' => $renderer->render($renderable),
'footer' => ''
];
return $this->content;
}
/**
* Return a block_contents object representing the full contents of this block.
*
* This internally calls ->get_content(), and then adds the editing controls etc.
*
* @param object $output The output renderer from the parent context (e.g. page renderer)
* @return block_contents a representation of the block, for rendering.
*/
public function get_content_for_output($output) {
$this->output = $output;
$bc = parent::get_content_for_output($output);
if (empty($bc->controls)) {
return $bc;
}
$newcontrols = [];
foreach ($bc->controls as $control) {
$newcontrols[] = $control;
// Append our new item onto the controls if we're on the correct item.
if (strpos($control->attributes['class'], 'editing_edit') !== false) {
$str = get_string('managemultiblock', 'block_multiblock', $this->title);
$newcontrols[] = new action_menu_link_secondary(
new moodle_url('/blocks/multiblock/manage.php', ['id' => $this->instance->id, 'sesskey' => sesskey()]),
icon_helper::preferences($str),
$str,
['class' => 'editing_manage']
);
}
}
// Append a delete+split item on the end.
$newcontrols[] = new action_menu_link_secondary(
new moodle_url('/blocks/multiblock/manage.php', ['id' => $this->instance->id, 'sesskey' => sesskey(),
'action' => 'splitdelete']),
icon_helper::delete($str),
get_string('splitanddelete', 'block_multiblock', $this->title),
['class' => 'editing_manage']
);
$bc->controls = $newcontrols;
return $bc;
}
/**
* Allows the block to be added multiple times to a single page
* @return boolean
*/
public function instance_allow_multiple() {
return true;
}
/**
* Copy all the children when copying to a new block instance.
*
* @param int $fromid The id number of the block instance to copy from
* @return bool
*/
public function instance_copy($fromid) {
global $DB;
$fromcontext = context_block::instance($fromid);
$blockinstances = $DB->get_records('block_instances', ['parentcontextid' => $fromcontext->id], 'defaultweight, id');
// Create all the new block instances.
$newblockinstanceids = [];
foreach ($blockinstances as $instance) {
$originalid = $instance->id;
unset($instance->id);
$instance->parentcontextid = $this->context->id;
$instance->timecreated = time();
$instance->timemodified = $instance->timecreated;
$instance->id = $DB->insert_record('block_instances', $instance);
$newblockinstanceids[$originalid] = $instance->id;
$blockcontext = context_block::instance($instance->id); // Just creates the context record.
$block = block_instance($instance->blockname, $instance);
if (!$block->instance_copy($originalid)) {
debugging("Unable to copy block-specific data for original block instance: $originalid
to new block instance: $instance->id", DEBUG_DEVELOPER);
}
}
return true;
}
/**
* Callback for when this block instance is being deleted, to clean up child blocks.
*
* @return bool
*/
public function instance_delete() {
global $DB;
// Find all the things that relate to this block.
foreach ($DB->get_records('block_instances', ['parentcontextid' => $this->context->id]) as $subblock) {
blocks_delete_instance($subblock);
}
return true;
}
/**
* Lists all the known presentation types that exist in the block.
*
* @return array An array of presentations for block rendering.
*/
public static function get_valid_presentations(): array {
static $presentations = null;
if ($presentations === null) {
foreach (core_component::get_component_classes_in_namespace('block_multiblock', 'layout') as $class => $ns) {
if (strpos($class, $ns[0]) === 0) {
// We only care about non-abstract classes here.
$reflection = new ReflectionClass($class);
if ($reflection->isAbstract()) {
continue;
}
$classname = substr($class, strlen($ns[0]));
$instance = new $class;
$presentations[$instance->get_layout_id()] = $instance;
}
}
}
return $presentations;
}
/**
* Returns the default presentation for the multiblock.
*
* @return string The default presentation's identifier.
*/
public static function get_default_presentation(): string {
$presentations = static::get_valid_presentations();
if (isset($presentations['tabbed-list'])) {
return 'tabbed-list';
}
// Our expected default is not present, make sure we fall back to something.
return array_keys($presentations)[0];
}
}