Merge branch 'w23_MDL-39882_m25_behatconfig' of git://github.com/skodak/moodle into...
[moodle.git] / admin / blocks.php
blobf2017b912a190bcd3db6223556fffdb0e8fad49d
1 <?php
3 // Allows the admin to configure blocks (hide/show, delete and configure)
5 require_once('../config.php');
6 require_once($CFG->libdir.'/adminlib.php');
7 require_once($CFG->libdir.'/tablelib.php');
9 admin_externalpage_setup('manageblocks');
11 $confirm = optional_param('confirm', 0, PARAM_BOOL);
12 $hide = optional_param('hide', 0, PARAM_INT);
13 $show = optional_param('show', 0, PARAM_INT);
14 $delete = optional_param('delete', 0, PARAM_INT);
15 $unprotect = optional_param('unprotect', 0, PARAM_INT);
16 $protect = optional_param('protect', 0, PARAM_INT);
18 /// Print headings
20 $strmanageblocks = get_string('manageblocks');
21 $strdelete = get_string('delete');
22 $strversion = get_string('version');
23 $strhide = get_string('hide');
24 $strshow = get_string('show');
25 $strsettings = get_string('settings');
26 $strcourses = get_string('blockinstances', 'admin');
27 $strname = get_string('name');
28 $strshowblockcourse = get_string('showblockcourse');
29 $strprotecthdr = get_string('blockprotect', 'admin'). $OUTPUT->help_icon('blockprotect','admin');
30 $strprotect = get_string('blockprotect', 'admin');
31 $strunprotect = get_string('blockunprotect', 'admin');
33 // Purge all caches related to blocks administration.
34 cache::make('core', 'plugininfo_block')->purge();
36 /// If data submitted, then process and store.
38 if (!empty($hide) && confirm_sesskey()) {
39 if (!$block = $DB->get_record('block', array('id'=>$hide))) {
40 print_error('blockdoesnotexist', 'error');
42 $DB->set_field('block', 'visible', '0', array('id'=>$block->id)); // Hide block
43 admin_get_root(true, false); // settings not required - only pages
46 if (!empty($show) && confirm_sesskey() ) {
47 if (!$block = $DB->get_record('block', array('id'=>$show))) {
48 print_error('blockdoesnotexist', 'error');
50 $DB->set_field('block', 'visible', '1', array('id'=>$block->id)); // Show block
51 admin_get_root(true, false); // settings not required - only pages
54 if (!isset($CFG->undeletableblocktypes) || (!is_array($CFG->undeletableblocktypes) && !is_string($CFG->undeletableblocktypes))) {
55 $undeletableblocktypes = array('navigation', 'settings');
56 } else if (is_string($CFG->undeletableblocktypes)) {
57 $undeletableblocktypes = explode(',', $CFG->undeletableblocktypes);
58 } else {
59 $undeletableblocktypes = $CFG->undeletableblocktypes;
62 if (!empty($protect) && confirm_sesskey()) {
63 if (!$block = $DB->get_record('block', array('id'=>$protect))) {
64 print_error('blockdoesnotexist', 'error');
66 if (!in_array($block->name, $undeletableblocktypes)) {
67 $undeletableblocktypes[] = $block->name;
68 set_config('undeletableblocktypes', implode(',', $undeletableblocktypes));
70 admin_get_root(true, false); // settings not required - only pages
73 if (!empty($unprotect) && confirm_sesskey()) {
74 if (!$block = $DB->get_record('block', array('id'=>$unprotect))) {
75 print_error('blockdoesnotexist', 'error');
77 if (in_array($block->name, $undeletableblocktypes)) {
78 $undeletableblocktypes = array_diff($undeletableblocktypes, array($block->name));
79 set_config('undeletableblocktypes', implode(',', $undeletableblocktypes));
81 admin_get_root(true, false); // settings not required - only pages
84 if (!empty($delete) && confirm_sesskey()) {
85 echo $OUTPUT->header();
86 echo $OUTPUT->heading($strmanageblocks);
88 if (!$block = blocks_get_record($delete)) {
89 print_error('blockdoesnotexist', 'error');
92 if (get_string_manager()->string_exists('pluginname', "block_$block->name")) {
93 $strblockname = get_string('pluginname', "block_$block->name");
94 } else {
95 $strblockname = $block->name;
98 if (!$confirm) {
99 echo $OUTPUT->confirm(get_string('blockdeleteconfirm', '', $strblockname), 'blocks.php?delete='.$block->id.'&confirm=1', 'blocks.php');
100 echo $OUTPUT->footer();
101 exit;
103 } else {
104 uninstall_plugin('block', $block->name);
106 $a = new stdClass();
107 $a->block = $strblockname;
108 $a->directory = $CFG->dirroot.'/blocks/'.$block->name;
109 notice(get_string('blockdeletefiles', '', $a), 'blocks.php');
113 echo $OUTPUT->header();
114 echo $OUTPUT->heading($strmanageblocks);
116 /// Main display starts here
118 /// Get and sort the existing blocks
120 if (!$blocks = $DB->get_records('block', array(), 'name ASC')) {
121 print_error('noblocks', 'error'); // Should never happen
124 $incompatible = array();
126 /// Print the table of all blocks
128 $table = new flexible_table('admin-blocks-compatible');
130 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'undeletable', 'delete', 'settings'));
131 $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strprotecthdr, $strdelete, $strsettings));
132 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
133 $table->set_attribute('class', 'admintable blockstable generaltable');
134 $table->set_attribute('id', 'compatibleblockstable');
135 $table->setup();
136 $tablerows = array();
138 // Sort blocks using current locale.
139 $blocknames = array();
140 foreach ($blocks as $blockid=>$block) {
141 $blockname = $block->name;
142 if (file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
143 $blocknames[$blockid] = get_string('pluginname', 'block_'.$blockname);
144 } else {
145 $blocknames[$blockid] = $blockname;
148 collatorlib::asort($blocknames);
150 foreach ($blocknames as $blockid=>$strblockname) {
151 $block = $blocks[$blockid];
152 $blockname = $block->name;
154 if (!file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
155 $blockobject = false;
156 $strblockname = '<span class="notifyproblem">'.$strblockname.' ('.get_string('missingfromdisk').')</span>';
157 $plugin = new stdClass();
158 $plugin->version = $block->version;
160 } else {
161 $plugin = new stdClass();
162 $plugin->version = '???';
163 if (file_exists("$CFG->dirroot/blocks/$blockname/version.php")) {
164 include("$CFG->dirroot/blocks/$blockname/version.php");
167 if (!$blockobject = block_instance($block->name)) {
168 $incompatible[] = $block;
169 continue;
173 $delete = '<a href="blocks.php?delete='.$blockid.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>';
175 $settings = ''; // By default, no configuration
176 if ($blockobject and $blockobject->has_config()) {
177 $blocksettings = admin_get_root()->locate('blocksetting' . $block->name);
179 if ($blocksettings instanceof admin_externalpage) {
180 $settings = '<a href="' . $blocksettings->url . '">' . get_string('settings') . '</a>';
181 } else if ($blocksettings instanceof admin_settingpage) {
182 $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
183 } else {
184 $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
188 // MDL-11167, blocks can be placed on mymoodle, or the blogs page
189 // and it should not show up on course search page
191 $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
192 $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
194 if ($count>0) {
195 $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
196 $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
198 else {
199 $blocklist = "$totalcount";
201 $class = ''; // Nothing fancy, by default
203 if (!$blockobject) {
204 // ignore
205 $visible = '';
206 } else if ($blocks[$blockid]->visible) {
207 $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
208 '<img src="'.$OUTPUT->pix_url('t/hide') . '" class="iconsmall" alt="'.$strhide.'" /></a>';
209 } else {
210 $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strshow.'">'.
211 '<img src="'.$OUTPUT->pix_url('t/show') . '" class="iconsmall" alt="'.$strshow.'" /></a>';
212 $class = ' class="dimmed_text"'; // Leading space required!
215 if ($block->version == $plugin->version) {
216 $version = $block->version;
217 } else {
218 $version = "$block->version ($plugin->version)";
221 if (!$blockobject) {
222 // ignore
223 $undeletable = '';
224 } else if (in_array($blockname, $undeletableblocktypes)) {
225 $undeletable = '<a href="blocks.php?unprotect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strunprotect.'">'.
226 '<img src="'.$OUTPUT->pix_url('t/unlock') . '" class="iconsmall" alt="'.$strunprotect.'" /></a>';
227 } else {
228 $undeletable = '<a href="blocks.php?protect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strprotect.'">'.
229 '<img src="'.$OUTPUT->pix_url('t/lock') . '" class="iconsmall" alt="'.$strprotect.'" /></a>';
232 $row = array(
233 '<span'.$class.'>'.$strblockname.'</span>',
234 $blocklist,
235 '<span'.$class.'>'.$version.'</span>',
236 $visible,
237 $undeletable,
238 $delete,
239 $settings
241 $table->add_data($row);
244 $table->print_html();
246 if (!empty($incompatible)) {
247 echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
249 $table = new flexible_table('admin-blocks-incompatible');
251 $table->define_columns(array('block', 'delete'));
252 $table->define_headers(array($strname, $strdelete));
253 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
255 $table->set_attribute('class', 'incompatibleblockstable generaltable');
257 $table->setup();
259 foreach ($incompatible as $block) {
260 $table->add_data(array(
261 $block->name,
262 '<a href="blocks.php?delete='.$block->id.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>',
265 $table->print_html();
268 echo $OUTPUT->footer();