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