forked from catalyst/moodle-block_multiblock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.php
242 lines (214 loc) · 9.33 KB
/
manage.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
<?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/>.
/**
* Manage multiblock instances.
*
* @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\helper;
use block_multiblock\icon_helper;
use block_multiblock\navigation;
require(__DIR__ . '/../../config.php');
require_once($CFG->libdir.'/tablelib.php');
$blockid = required_param('id', PARAM_INT);
$actionableinstance = optional_param('instance', 0, PARAM_INT);
$performaction = optional_param('action', '', PARAM_TEXT);
require_login();
list($block, $blockinstance, $blockmanager) = helper::bootstrap_page($blockid);
// Now we've done permissions checks, reset the URL to be the real one.
$pageurl = new moodle_url('/blocks/multiblock/manage.php', ['id' => $blockid]);
helper::set_page_real_url($pageurl);
$blockmanager->show_only_fake_blocks(true);
$blockctx = context_block::instance($blockid);
$multiblockblocks = $blockinstance->load_multiblocks($blockctx->id);
// Set up the add block routine.
$forcereload = false;
$addblock = new \block_multiblock\form\addblock($pageurl, ['id' => $blockid]);
if ($newblockdata = $addblock->get_data()) {
if (!empty($newblockdata->addsubmit) && $newblockdata->addblock) {
$position = 1;
foreach ($multiblockblocks as $instance) {
if ((int) $instance->defaultweight > $position) {
$position = (int) $instance->defaultweight;
}
}
// Add the block to the parent context, then move it in.
$blockmanager->add_block($newblockdata->addblock, $blockmanager->get_default_region(), $position + 1,
$block->showinsubcontexts);
// Helpfully, $blockmanager won't give us back the id it just added, so we have to go find it.
$conditions = [
'blockname' => $newblockdata->addblock,
'parentcontextid' => $PAGE->context->id,
];
$lastinserted = $DB->get_records('block_instances', $conditions, 'id DESC', 'id', 0, 1);
if ($lastinserted) {
helper::move_block(current($lastinserted)->id, $blockid);
}
// Now we need to re-prep the table exist.
$forcereload = true;
} else if (!empty($newblockdata->movesubmit) && !empty($newblockdata->moveblock)) {
// Merge it in and then reprep the table and form.
helper::move_block($newblockdata->moveblock, $blockid);
$forcereload = true;
}
} else if ($performaction) {
switch ($performaction) {
case 'moveup':
$positions = array_keys($multiblockblocks);
if (in_array($actionableinstance, $positions) && $positions[0] != $actionableinstance) {
$current = array_search($actionableinstance, $positions);
$temp = $positions[$current - 1];
$positions[$current - 1] = $positions[$current];
$positions[$current] = $temp;
}
foreach ($positions as $position => $actionableinstance) {
$new = (object) [
'id' => $actionableinstance,
'defaultweight' => $position + 1,
];
$DB->update_record('block_instances', $new);
}
$forcereload = true;
break;
case 'movedown':
$positions = array_keys($multiblockblocks);
if (in_array($actionableinstance, $positions) && $positions[count($positions) - 1] != $actionableinstance) {
$current = array_search($actionableinstance, $positions);
$temp = $positions[$current + 1];
$positions[$current + 1] = $positions[$current];
$positions[$current] = $temp;
}
foreach ($positions as $position => $actionableinstance) {
$new = (object) [
'id' => $actionableinstance,
'defaultweight' => $position + 1,
];
$DB->update_record('block_instances', $new);
}
$forcereload = true;
break;
case 'split':
helper::split_block($blockinstance->instance, $actionableinstance);
$forcereload = true;
break;
case 'delete':
blocks_delete_instance($multiblockblocks[$actionableinstance]);
$forcereload = true;
break;
case 'splitdelete':
$parenturl = navigation::get_page_url($blockid);
foreach (array_keys($multiblockblocks) as $childid) {
helper::split_block($blockinstance->instance, $childid);
}
blocks_delete_instance($blockinstance->instance);
redirect($parenturl);
break;
}
}
// And begin our output.
echo $OUTPUT->header();
if ($forcereload) {
$multiblockblocks = $blockinstance->load_multiblocks($blockctx->id);
unset($_POST['addblock'], $_POST['moveblock']); // Reset the form element so it doesn't attempt to reuse values it had before.
$addblock = new \block_multiblock\form\addblock($pageurl, ['id' => $blockid]);
}
if (empty($multiblockblocks)) {
echo html_writer::tag('p', get_string('multiblockhasnosubblocks', 'block_multiblock'));
} else {
$table = new flexible_table('block_multiblock_admin');
$headers = [
'title' => get_string('table:blocktitle', 'block_multiblock'),
'type' => get_string('table:blocktype', 'block_multiblock'),
'actions' => get_string('table:actions', 'block_multiblock'),
];
if (!helper::is_totara()) {
$headers['updated'] = get_string('table:lastupdated', 'block_multiblock');
}
$table->define_columns(array_keys($headers));
$table->define_headers(array_values($headers));
$table->define_baseurl(new moodle_url('/blocks/multiblock/manage.php', ['id' => $blockid]));
$table->set_attribute('class', 'admintable blockstable generaltable');
$table->set_attribute('id', 'multiblocktable');
$table->sortable(false);
$table->setup();
$first = 0;
$last = 0;
foreach ($multiblockblocks as $instance) {
if (!$first) {
$first = $instance->id;
}
$last = $instance->id;
}
foreach ($multiblockblocks as $instance) {
$actions = '';
$baseactionurl = new moodle_url('/blocks/multiblock/manage.php', [
'id' => $blockid,
'instance' => $instance->id,
'sesskey' => sesskey()
]);
// Molve the sub-block up, if it's not the first one.
if ($instance->id != $first) {
$url = $baseactionurl;
$url->params(['action' => 'moveup']);
$actions .= $OUTPUT->action_icon($url, icon_helper::arrow_up(get_string('moveup')));
} else {
$actions .= icon_helper::space();
}
// Move sub-block down, if it's not the last one.
if ($instance->id != $last) {
$url = $baseactionurl;
$url->params(['action' => 'movedown']);
$actions .= $OUTPUT->action_icon($url, icon_helper::arrow_down(get_string('movedown')));
} else {
$actions .= icon_helper::space();
}
// Edit settings button.
if (file_exists($CFG->dirroot . '/blocks/' . $instance->blockinstance->name() . '/edit_form.php')) {
$url = new moodle_url('/blocks/multiblock/configinstance.php', [
'id' => $blockid,
'instance' => $instance->id,
'sesskey' => sesskey(),
]);
$actions .= $OUTPUT->action_icon($url, icon_helper::settings(get_string('settings')));
} else {
$actions .= icon_helper::space();
}
// Split out to parent context.
$url = $baseactionurl;
$url->params(['action' => 'split']);
$actions .= $OUTPUT->action_icon($url, icon_helper::level_up(get_string('movetoparentpage', 'block_multiblock')));
// Delete button.
$url = $baseactionurl;
$url->params(['action' => 'delete']);
$actions .= $OUTPUT->action_icon($url, icon_helper::delete(get_string('delete')));
$notitle = html_writer::tag('em', get_string('notitle', 'block_multiblock'), ['class' => 'text-muted']);
$row = [
!empty($instance->blockinstance->get_title()) ? $instance->blockinstance->get_title() : $notitle,
get_string('pluginname', 'block_' . $instance->blockinstance->name()),
$actions,
];
if (!helper::is_totara()) {
$row[] = userdate($instance->timemodified, get_string('strftimedatetime', 'core_langconfig'));
}
$table->add_data($row);
}
$table->print_html();
}
echo html_writer::empty_tag('hr');
$addblock->display();
echo $OUTPUT->footer();