MDL-55166 admin: Fix display of misleading debug warning on manage block
[moodle.git] / admin / blocks.php
CommitLineData
20207b82 1<?php
0f3fe4b6 2
dd24dd37 3 // Allows the admin to configure blocks (hide/show, uninstall and configure)
0f3fe4b6 4
5 require_once('../config.php');
6e4dc10f 6 require_once($CFG->libdir.'/adminlib.php');
061d8339 7 require_once($CFG->libdir.'/tablelib.php');
cc0bf616 8
1ae083e4 9 admin_externalpage_setup('manageblocks');
0f3fe4b6 10
1c47adc5 11 $confirm = optional_param('confirm', 0, PARAM_BOOL);
9b4b78fd 12 $hide = optional_param('hide', 0, PARAM_INT);
13 $show = optional_param('show', 0, PARAM_INT);
28e63a9d
MG
14 $unprotect = optional_param('unprotect', 0, PARAM_INT);
15 $protect = optional_param('protect', 0, PARAM_INT);
0f3fe4b6 16
0f3fe4b6 17/// Print headings
18
0f3fe4b6 19 $strmanageblocks = get_string('manageblocks');
dd24dd37 20 $struninstall = get_string('uninstallplugin', 'core_admin');
0f3fe4b6 21 $strversion = get_string('version');
22 $strhide = get_string('hide');
23 $strshow = get_string('show');
24 $strsettings = get_string('settings');
9b4b78fd 25 $strcourses = get_string('blockinstances', 'admin');
0f3fe4b6 26 $strname = get_string('name');
616ad119 27 $strshowblockcourse = get_string('showblockcourse');
28e63a9d
MG
28 $strprotecthdr = get_string('blockprotect', 'admin'). $OUTPUT->help_icon('blockprotect','admin');
29 $strprotect = get_string('blockprotect', 'admin');
30 $strunprotect = get_string('blockunprotect', 'admin');
0f3fe4b6 31
0f3fe4b6 32/// If data submitted, then process and store.
33
9b4b78fd 34 if (!empty($hide) && confirm_sesskey()) {
953d72db 35 if (!$block = $DB->get_record('block', array('id'=>$hide))) {
78664df0 36 print_error('blockdoesnotexist', 'error');
0f3fe4b6 37 }
953d72db 38 $DB->set_field('block', 'visible', '0', array('id'=>$block->id)); // Hide block
e87214bd 39 core_plugin_manager::reset_caches();
220a90c5 40 admin_get_root(true, false); // settings not required - only pages
0f3fe4b6 41 }
42
9b4b78fd 43 if (!empty($show) && confirm_sesskey() ) {
953d72db 44 if (!$block = $DB->get_record('block', array('id'=>$show))) {
78664df0 45 print_error('blockdoesnotexist', 'error');
0f3fe4b6 46 }
953d72db 47 $DB->set_field('block', 'visible', '1', array('id'=>$block->id)); // Show block
e87214bd 48 core_plugin_manager::reset_caches();
220a90c5 49 admin_get_root(true, false); // settings not required - only pages
0f3fe4b6 50 }
51
28e63a9d
MG
52 if (!isset($CFG->undeletableblocktypes) || (!is_array($CFG->undeletableblocktypes) && !is_string($CFG->undeletableblocktypes))) {
53 $undeletableblocktypes = array('navigation', 'settings');
54 } else if (is_string($CFG->undeletableblocktypes)) {
55 $undeletableblocktypes = explode(',', $CFG->undeletableblocktypes);
56 } else {
57 $undeletableblocktypes = $CFG->undeletableblocktypes;
58 }
59
60 if (!empty($protect) && confirm_sesskey()) {
61 if (!$block = $DB->get_record('block', array('id'=>$protect))) {
62 print_error('blockdoesnotexist', 'error');
63 }
64 if (!in_array($block->name, $undeletableblocktypes)) {
65 $undeletableblocktypes[] = $block->name;
66 set_config('undeletableblocktypes', implode(',', $undeletableblocktypes));
67 }
68 admin_get_root(true, false); // settings not required - only pages
69 }
70
71 if (!empty($unprotect) && confirm_sesskey()) {
72 if (!$block = $DB->get_record('block', array('id'=>$unprotect))) {
73 print_error('blockdoesnotexist', 'error');
74 }
75 if (in_array($block->name, $undeletableblocktypes)) {
76 $undeletableblocktypes = array_diff($undeletableblocktypes, array($block->name));
77 set_config('undeletableblocktypes', implode(',', $undeletableblocktypes));
78 }
79 admin_get_root(true, false); // settings not required - only pages
80 }
81
61ef8f9f 82 echo $OUTPUT->header();
2fff8846 83 echo $OUTPUT->heading($strmanageblocks);
220a90c5 84
0f3fe4b6 85/// Main display starts here
86
87/// Get and sort the existing blocks
88
15822fe2 89 if (!$blocks = $DB->get_records('block', array(), 'name ASC')) {
9c78aeca 90 print_error('noblocks', 'error'); // Should never happen
0f3fe4b6 91 }
92
061d8339 93 $incompatible = array();
94
0f3fe4b6 95/// Print the table of all blocks
96
061d8339 97 $table = new flexible_table('admin-blocks-compatible');
98
e87214bd
PS
99 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'undeletable', 'settings', 'uninstall'));
100 $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strprotecthdr, $strsettings, $struninstall));
ad0f9c82 101 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
8be244b2
RW
102 $table->set_attribute('class', 'admintable blockstable generaltable');
103 $table->set_attribute('id', 'compatibleblockstable');
061d8339 104 $table->setup();
50c71e0a 105 $tablerows = array();
0f3fe4b6 106
c82d309d
107 // Sort blocks using current locale.
108 $blocknames = array();
15822fe2
PS
109 foreach ($blocks as $blockid=>$block) {
110 $blockname = $block->name;
c82d309d
111 if (file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
112 $blocknames[$blockid] = get_string('pluginname', 'block_'.$blockname);
113 } else {
114 $blocknames[$blockid] = $blockname;
115 }
116 }
2f1e464a 117 core_collator::asort($blocknames);
c82d309d
118
119 foreach ($blocknames as $blockid=>$strblockname) {
120 $block = $blocks[$blockid];
121 $blockname = $block->name;
bde002b8 122 $dbversion = get_config('block_'.$block->name, 'version');
0f3fe4b6 123
15822fe2
PS
124 if (!file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
125 $blockobject = false;
c82d309d 126 $strblockname = '<span class="notifyproblem">'.$strblockname.' ('.get_string('missingfromdisk').')</span>';
a226a972 127 $plugin = new stdClass();
bde002b8 128 $plugin->version = $dbversion;
8571833f 129
15822fe2 130 } else {
a226a972 131 $plugin = new stdClass();
8571833f
PS
132 $plugin->version = '???';
133 if (file_exists("$CFG->dirroot/blocks/$blockname/version.php")) {
134 include("$CFG->dirroot/blocks/$blockname/version.php");
135 }
136
15822fe2
PS
137 if (!$blockobject = block_instance($block->name)) {
138 $incompatible[] = $block;
139 continue;
140 }
15822fe2 141 }
bd103fde 142
e87214bd 143 if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('block_'.$blockname, 'manage')) {
dd24dd37 144 $uninstall = html_writer::link($uninstallurl, $struninstall);
6584d8a8 145 } else {
dd24dd37 146 $uninstall = '';
6584d8a8 147 }
0f3fe4b6 148
149 $settings = ''; // By default, no configuration
15822fe2 150 if ($blockobject and $blockobject->has_config()) {
f723bd3d
DP
151 $blocksettings = admin_get_root()->locate('blocksetting' . $block->name);
152
153 if ($blocksettings instanceof admin_externalpage) {
154 $settings = '<a href="' . $blocksettings->url . '">' . get_string('settings') . '</a>';
155 } else if ($blocksettings instanceof admin_settingpage) {
220a90c5 156 $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
0d7c4171
DM
157 } else if (!file_exists($CFG->dirroot.'/blocks/'.$block->name.'/settings.php')) {
158 // If the block's settings node was not found, we check that the block really provides the settings.php file.
159 // Note that blocks can inject their settings to other nodes in the admin tree without using the default locations.
160 // This can be done by assigning null to $setting in settings.php and it is a valid case.
8159f23e
DM
161 debugging('Warning: block_'.$block->name.' returns true in has_config() but does not provide a settings.php file',
162 DEBUG_DEVELOPER);
220a90c5 163 }
0f3fe4b6 164 }
165
deb2988f 166 // MDL-11167, blocks can be placed on mymoodle, or the blogs page
167 // and it should not show up on course search page
220a90c5 168
f474a4e5 169 $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
170 $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
ab951955 171
616ad119 172 if ($count>0) {
34a2faed 173 $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
ab951955 174 $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
616ad119 175 }
176 else {
ab951955 177 $blocklist = "$totalcount";
616ad119 178 }
0f3fe4b6 179 $class = ''; // Nothing fancy, by default
180
15822fe2
PS
181 if (!$blockobject) {
182 // ignore
183 $visible = '';
184 } else if ($blocks[$blockid]->visible) {
d4a1fcaf 185 $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
fa9c0aab 186 '<img src="'.$OUTPUT->pix_url('t/hide') . '" class="iconsmall" alt="'.$strhide.'" /></a>';
0f3fe4b6 187 } else {
d4a1fcaf 188 $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strshow.'">'.
fa9c0aab 189 '<img src="'.$OUTPUT->pix_url('t/show') . '" class="iconsmall" alt="'.$strshow.'" /></a>';
e87214bd 190 $class = 'dimmed_text';
0f3fe4b6 191 }
192
bde002b8
PS
193 if ($dbversion == $plugin->version) {
194 $version = $dbversion;
8571833f 195 } else {
bde002b8 196 $version = "$dbversion ($plugin->version)";
15822fe2 197 }
8571833f 198
28e63a9d
MG
199 if (!$blockobject) {
200 // ignore
201 $undeletable = '';
202 } else if (in_array($blockname, $undeletableblocktypes)) {
203 $undeletable = '<a href="blocks.php?unprotect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strunprotect.'">'.
fa9c0aab 204 '<img src="'.$OUTPUT->pix_url('t/unlock') . '" class="iconsmall" alt="'.$strunprotect.'" /></a>';
28e63a9d
MG
205 } else {
206 $undeletable = '<a href="blocks.php?protect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strprotect.'">'.
fa9c0aab 207 '<img src="'.$OUTPUT->pix_url('t/lock') . '" class="iconsmall" alt="'.$strprotect.'" /></a>';
28e63a9d 208 }
8571833f 209
50c71e0a 210 $row = array(
e87214bd 211 $strblockname,
616ad119 212 $blocklist,
e87214bd 213 $version,
9b4b78fd 214 $visible,
28e63a9d 215 $undeletable,
e87214bd 216 $settings,
dd24dd37 217 $uninstall,
50c71e0a 218 );
e87214bd 219 $table->add_data($row, $class);
0f3fe4b6 220 }
3c520659 221
061d8339 222 $table->print_html();
223
15822fe2 224 if (!empty($incompatible)) {
90723839 225 echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
061d8339 226
227 $table = new flexible_table('admin-blocks-incompatible');
eef868d1 228
dd24dd37
PS
229 $table->define_columns(array('block', 'uninstall'));
230 $table->define_headers(array($strname, $struninstall));
ad0f9c82 231 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
eef868d1 232
90723839 233 $table->set_attribute('class', 'incompatibleblockstable generaltable');
eef868d1 234
061d8339 235 $table->setup();
236
237 foreach ($incompatible as $block) {
e87214bd 238 if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('block_'.$block->name, 'manage')) {
dd24dd37 239 $uninstall = html_writer::link($uninstallurl, $struninstall);
6584d8a8 240 } else {
dd24dd37 241 $uninstall = '';
6584d8a8 242 }
061d8339 243 $table->add_data(array(
244 $block->name,
dd24dd37 245 $uninstall,
061d8339 246 ));
247 }
248 $table->print_html();
249 }
3c520659 250
73d6f52f 251 echo $OUTPUT->footer();
0f3fe4b6 252
20207b82 253