forked from catalyst/moodle-block_multiblock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configinstance.php
103 lines (81 loc) · 3.92 KB
/
configinstance.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
<?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\navigation;
require(__DIR__ . '/../../config.php');
require_once($CFG->libdir.'/tablelib.php');
$blockid = required_param('id', PARAM_INT);
$actionableinstance = required_param('instance', PARAM_INT);
require_login();
list($block, $blockinstance, $blockmanager) = helper::bootstrap_page($blockid);
$pageurl = new moodle_url('/blocks/multiblock/configinstance.php', ['id' => $blockid, 'instance' => $actionableinstance]);
helper::set_page_real_url($pageurl);
$blockmanager->show_only_fake_blocks(true);
$blockctx = context_block::instance($blockid);
$multiblockblocks = $blockinstance->load_multiblocks($blockctx->id);
if (!isset($multiblockblocks[$actionableinstance])) {
redirect(new moodle_url('/blocks/multiblock/manage.php', ['id' => $blockid, 'sesskey' => sesskey()]));
}
$PAGE->navbar->add($multiblockblocks[$actionableinstance]->blockinstance->get_title());
$formfile = $CFG->dirroot . '/blocks/' . $multiblockblocks[$actionableinstance]->blockinstance->name() . '/edit_form.php';
$classname = '';
if (is_readable($formfile)) {
require_once($CFG->dirroot . '/blocks/edit_form.php');
require_once($formfile);
$classname = 'block_' . $multiblockblocks[$actionableinstance]->blockinstance->name() . '_edit_form';
}
if (!$classname || !class_exists($classname)) {
throw new \Exception('Could not load block configuration for ' . $classname);
}
class_alias($classname, 'block_multiblock_proxy_edit_form');
$editform = helper::get_edit_form($pageurl, $multiblockblocks[$actionableinstance], $PAGE, $blockinstance);
if ($editform->is_cancelled()) {
redirect(new moodle_url('/blocks/multiblock/manage.php', ['id' => $blockid, 'sesskey' => sesskey()]));
} else if ($data = $editform->get_data()) {
$config = new stdClass;
// Totara has some common config that it handles separately to everything else.
if (method_exists($editform->block, 'serialize_common_config')) {
$editform->block->validate_common_config_value($data);
$commonconfig = $editform->block->serialize_common_config($editform->split_common_settings_data($data));
$multiblockblocks[$actionableinstance]->common_config = $commonconfig;
$DB->update_record('block_instances', $multiblockblocks[$actionableinstance]);
}
foreach ($data as $configfield => $value) {
if (strpos($configfield, 'config_') !== 0) {
continue;
}
$field = substr($configfield, 7);
$config->$field = $value;
}
$multiblockblocks[$actionableinstance]->blockinstance->instance_config_save($config);
// If we pressed save and display, go to the page where the block lives.
if (!empty($data->saveanddisplay)) {
redirect(navigation::get_page_url($blockid));
}
// Otherwise return to the management page.
redirect(new moodle_url('/blocks/multiblock/manage.php', ['id' => $blockid, 'sesskey' => sesskey()]));
}
echo $OUTPUT->header();
$editform->set_data($multiblockblocks[$actionableinstance]->blockinstance->instance);
$editform->display();
echo $OUTPUT->footer();