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 |
* Manage multiblock instances.
|
|
|
19 |
*
|
|
|
20 |
* @package block_multiblock
|
|
|
21 |
* @copyright 2019 Peter Spicer <peter.spicer@catalyst-eu.net>
|
|
|
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\navigation;
|
|
|
28 |
|
|
|
29 |
require(__DIR__ . '/../../config.php');
|
|
|
30 |
|
|
|
31 |
require_once($CFG->libdir.'/tablelib.php');
|
|
|
32 |
|
|
|
33 |
$blockid = required_param('id', PARAM_INT);
|
|
|
34 |
$actionableinstance = optional_param('instance', 0, PARAM_INT);
|
|
|
35 |
$performaction = optional_param('action', '', PARAM_TEXT);
|
|
|
36 |
|
|
|
37 |
require_login();
|
|
|
38 |
list($block, $blockinstance, $blockmanager) = helper::bootstrap_page($blockid);
|
|
|
39 |
|
|
|
40 |
// Now we've done permissions checks, reset the URL to be the real one.
|
|
|
41 |
$pageurl = new moodle_url('/blocks/multiblock/manage.php', ['id' => $blockid]);
|
|
|
42 |
helper::set_page_real_url($pageurl);
|
|
|
43 |
|
|
|
44 |
$blockmanager->show_only_fake_blocks(true);
|
|
|
45 |
|
|
|
46 |
$blockctx = context_block::instance($blockid);
|
|
|
47 |
$multiblockblocks = $blockinstance->load_multiblocks($blockctx->id);
|
|
|
48 |
|
|
|
49 |
// Set up the add block routine.
|
|
|
50 |
$forcereload = false;
|
|
|
51 |
$addblock = new \block_multiblock\form\addblock($pageurl, ['id' => $blockid]);
|
|
|
52 |
if ($newblockdata = $addblock->get_data()) {
|
|
|
53 |
if (!empty($newblockdata->addsubmit) && $newblockdata->addblock) {
|
|
|
54 |
$position = 1;
|
|
|
55 |
foreach ($multiblockblocks as $instance) {
|
|
|
56 |
if ((int) $instance->defaultweight > $position) {
|
|
|
57 |
$position = (int) $instance->defaultweight;
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
// Add the block to the parent context, then move it in.
|
|
|
62 |
$blockmanager->add_block($newblockdata->addblock, $blockmanager->get_default_region(), $position + 1,
|
|
|
63 |
$block->showinsubcontexts);
|
|
|
64 |
// Helpfully, $blockmanager won't give us back the id it just added, so we have to go find it.
|
|
|
65 |
$conditions = [
|
|
|
66 |
'blockname' => $newblockdata->addblock,
|
|
|
67 |
'parentcontextid' => $PAGE->context->id,
|
|
|
68 |
];
|
|
|
69 |
$lastinserted = $DB->get_records('block_instances', $conditions, 'id DESC', 'id', 0, 1);
|
|
|
70 |
if ($lastinserted) {
|
|
|
71 |
helper::move_block(current($lastinserted)->id, $blockid);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
// Now we need to re-prep the table exist.
|
|
|
75 |
$forcereload = true;
|
|
|
76 |
} else if (!empty($newblockdata->movesubmit) && !empty($newblockdata->moveblock)) {
|
|
|
77 |
// Merge it in and then reprep the table and form.
|
|
|
78 |
helper::move_block($newblockdata->moveblock, $blockid);
|
|
|
79 |
$forcereload = true;
|
|
|
80 |
}
|
|
|
81 |
} else if ($performaction) {
|
|
|
82 |
switch ($performaction) {
|
|
|
83 |
case 'moveup':
|
|
|
84 |
$positions = array_keys($multiblockblocks);
|
|
|
85 |
if (in_array($actionableinstance, $positions) && $positions[0] != $actionableinstance) {
|
|
|
86 |
$current = array_search($actionableinstance, $positions);
|
|
|
87 |
$temp = $positions[$current - 1];
|
|
|
88 |
$positions[$current - 1] = $positions[$current];
|
|
|
89 |
$positions[$current] = $temp;
|
|
|
90 |
}
|
|
|
91 |
foreach ($positions as $position => $actionableinstance) {
|
|
|
92 |
$new = (object) [
|
|
|
93 |
'id' => $actionableinstance,
|
|
|
94 |
'defaultweight' => $position + 1,
|
|
|
95 |
];
|
|
|
96 |
$DB->update_record('block_instances', $new);
|
|
|
97 |
}
|
|
|
98 |
$forcereload = true;
|
|
|
99 |
break;
|
|
|
100 |
case 'movedown':
|
|
|
101 |
$positions = array_keys($multiblockblocks);
|
|
|
102 |
if (in_array($actionableinstance, $positions) && $positions[count($positions) - 1] != $actionableinstance) {
|
|
|
103 |
$current = array_search($actionableinstance, $positions);
|
|
|
104 |
$temp = $positions[$current + 1];
|
|
|
105 |
$positions[$current + 1] = $positions[$current];
|
|
|
106 |
$positions[$current] = $temp;
|
|
|
107 |
}
|
|
|
108 |
foreach ($positions as $position => $actionableinstance) {
|
|
|
109 |
$new = (object) [
|
|
|
110 |
'id' => $actionableinstance,
|
|
|
111 |
'defaultweight' => $position + 1,
|
|
|
112 |
];
|
|
|
113 |
$DB->update_record('block_instances', $new);
|
|
|
114 |
}
|
|
|
115 |
$forcereload = true;
|
|
|
116 |
break;
|
|
|
117 |
case 'split':
|
|
|
118 |
helper::split_block($blockinstance->instance, $actionableinstance);
|
|
|
119 |
$forcereload = true;
|
|
|
120 |
break;
|
|
|
121 |
case 'delete':
|
|
|
122 |
blocks_delete_instance($multiblockblocks[$actionableinstance]);
|
|
|
123 |
$forcereload = true;
|
|
|
124 |
break;
|
|
|
125 |
case 'splitdelete':
|
|
|
126 |
$parenturl = navigation::get_page_url($blockid);
|
|
|
127 |
foreach (array_keys($multiblockblocks) as $childid) {
|
|
|
128 |
helper::split_block($blockinstance->instance, $childid);
|
|
|
129 |
}
|
|
|
130 |
blocks_delete_instance($blockinstance->instance);
|
|
|
131 |
redirect($parenturl);
|
|
|
132 |
break;
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
// And begin our output.
|
|
|
137 |
echo $OUTPUT->header();
|
|
|
138 |
|
|
|
139 |
if ($forcereload) {
|
|
|
140 |
$multiblockblocks = $blockinstance->load_multiblocks($blockctx->id);
|
|
|
141 |
unset($_POST['addblock'], $_POST['moveblock']); // Reset the form element so it doesn't attempt to reuse values it had before.
|
|
|
142 |
$addblock = new \block_multiblock\form\addblock($pageurl, ['id' => $blockid]);
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
if (empty($multiblockblocks)) {
|
|
|
146 |
echo html_writer::tag('p', get_string('multiblockhasnosubblocks', 'block_multiblock'));
|
|
|
147 |
} else {
|
|
|
148 |
$table = new flexible_table('block_multiblock_admin');
|
|
|
149 |
|
|
|
150 |
$headers = [
|
|
|
151 |
'title' => get_string('table:blocktitle', 'block_multiblock'),
|
|
|
152 |
'type' => get_string('table:blocktype', 'block_multiblock'),
|
|
|
153 |
'actions' => get_string('table:actions', 'block_multiblock'),
|
|
|
154 |
];
|
|
|
155 |
if (!helper::is_totara()) {
|
|
|
156 |
$headers['updated'] = get_string('table:lastupdated', 'block_multiblock');
|
|
|
157 |
}
|
|
|
158 |
$table->define_columns(array_keys($headers));
|
|
|
159 |
$table->define_headers(array_values($headers));
|
|
|
160 |
$table->define_baseurl(new moodle_url('/blocks/multiblock/manage.php', ['id' => $blockid]));
|
|
|
161 |
$table->set_attribute('class', 'admintable blockstable generaltable');
|
|
|
162 |
$table->set_attribute('id', 'multiblocktable');
|
|
|
163 |
$table->sortable(false);
|
|
|
164 |
$table->setup();
|
|
|
165 |
|
|
|
166 |
$first = 0;
|
|
|
167 |
$last = 0;
|
|
|
168 |
foreach ($multiblockblocks as $instance) {
|
|
|
169 |
if (!$first) {
|
|
|
170 |
$first = $instance->id;
|
|
|
171 |
}
|
|
|
172 |
$last = $instance->id;
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
foreach ($multiblockblocks as $instance) {
|
|
|
176 |
$actions = '';
|
|
|
177 |
$baseactionurl = new moodle_url('/blocks/multiblock/manage.php', [
|
|
|
178 |
'id' => $blockid,
|
|
|
179 |
'instance' => $instance->id,
|
|
|
180 |
'sesskey' => sesskey()
|
|
|
181 |
]);
|
|
|
182 |
|
|
|
183 |
// Molve the sub-block up, if it's not the first one.
|
|
|
184 |
if ($instance->id != $first) {
|
|
|
185 |
$url = $baseactionurl;
|
|
|
186 |
$url->params(['action' => 'moveup']);
|
|
|
187 |
$actions .= $OUTPUT->action_icon($url, icon_helper::arrow_up(get_string('moveup')));
|
|
|
188 |
} else {
|
|
|
189 |
$actions .= icon_helper::space();
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
// Move sub-block down, if it's not the last one.
|
|
|
193 |
if ($instance->id != $last) {
|
|
|
194 |
$url = $baseactionurl;
|
|
|
195 |
$url->params(['action' => 'movedown']);
|
|
|
196 |
$actions .= $OUTPUT->action_icon($url, icon_helper::arrow_down(get_string('movedown')));
|
|
|
197 |
} else {
|
|
|
198 |
$actions .= icon_helper::space();
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
// Edit settings button.
|
|
|
202 |
if (file_exists($CFG->dirroot . '/blocks/' . $instance->blockinstance->name() . '/edit_form.php')) {
|
|
|
203 |
$url = new moodle_url('/blocks/multiblock/configinstance.php', [
|
|
|
204 |
'id' => $blockid,
|
|
|
205 |
'instance' => $instance->id,
|
|
|
206 |
'sesskey' => sesskey(),
|
|
|
207 |
]);
|
|
|
208 |
$actions .= $OUTPUT->action_icon($url, icon_helper::settings(get_string('settings')));
|
|
|
209 |
} else {
|
|
|
210 |
$actions .= icon_helper::space();
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
// Split out to parent context.
|
|
|
214 |
$url = $baseactionurl;
|
|
|
215 |
$url->params(['action' => 'split']);
|
|
|
216 |
$actions .= $OUTPUT->action_icon($url, icon_helper::level_up(get_string('movetoparentpage', 'block_multiblock')));
|
|
|
217 |
|
|
|
218 |
// Delete button.
|
|
|
219 |
$url = $baseactionurl;
|
|
|
220 |
$url->params(['action' => 'delete']);
|
|
|
221 |
$actions .= $OUTPUT->action_icon($url, icon_helper::delete(get_string('delete')));
|
|
|
222 |
|
|
|
223 |
$notitle = html_writer::tag('em', get_string('notitle', 'block_multiblock'), ['class' => 'text-muted']);
|
|
|
224 |
|
|
|
225 |
$row = [
|
|
|
226 |
!empty($instance->blockinstance->get_title()) ? $instance->blockinstance->get_title() : $notitle,
|
|
|
227 |
get_string('pluginname', 'block_' . $instance->blockinstance->name()),
|
|
|
228 |
$actions,
|
|
|
229 |
];
|
|
|
230 |
if (!helper::is_totara()) {
|
|
|
231 |
$row[] = userdate($instance->timemodified, get_string('strftimedatetime', 'core_langconfig'));
|
|
|
232 |
}
|
|
|
233 |
$table->add_data($row);
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
$table->print_html();
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
echo html_writer::empty_tag('hr');
|
|
|
240 |
$addblock->display();
|
|
|
241 |
|
|
|
242 |
echo $OUTPUT->footer();
|