MDL-56830 config_log: Add block plugin visibility changes to config_log.
[moodle.git] / admin / blocks.php
1 <?php
3     // Allows the admin to configure blocks (hide/show, uninstall and configure)
5     require_once('../config.php');
6     require_once($CFG->libdir.'/adminlib.php');
7     require_once($CFG->libdir.'/blocklib.php');
8     require_once($CFG->libdir.'/tablelib.php');
10     admin_externalpage_setup('manageblocks');
12     $confirm  = optional_param('confirm', 0, PARAM_BOOL);
13     $hide     = optional_param('hide', 0, PARAM_INT);
14     $show     = optional_param('show', 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     $struninstall = get_string('uninstallplugin', 'core_admin');
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 /// If data submitted, then process and store.
35     if (!empty($hide) && confirm_sesskey()) {
36         if (!$block = $DB->get_record('block', array('id'=>$hide))) {
37             print_error('blockdoesnotexist', 'error');
38         }
39         $DB->set_field('block', 'visible', '0', array('id'=>$block->id));      // Hide block
40         add_to_config_log('block_visibility', $block->visible, '0', $block->name);
41         core_plugin_manager::reset_caches();
42         admin_get_root(true, false);  // settings not required - only pages
43     }
45     if (!empty($show) && confirm_sesskey() ) {
46         if (!$block = $DB->get_record('block', array('id'=>$show))) {
47             print_error('blockdoesnotexist', 'error');
48         }
49         $DB->set_field('block', 'visible', '1', array('id'=>$block->id));      // Show block
50         add_to_config_log('block_visibility', $block->visible, '1', $block->name);
51         core_plugin_manager::reset_caches();
52         admin_get_root(true, false);  // settings not required - only pages
53     }
55     if (!empty($protect) && confirm_sesskey()) {
56         block_manager::protect_block((int)$protect);
57         admin_get_root(true, false);  // settings not required - only pages
58     }
60     if (!empty($unprotect) && confirm_sesskey()) {
61         block_manager::unprotect_block((int)$unprotect);
62         admin_get_root(true, false);  // settings not required - only pages
63     }
65     $undeletableblocktypes = block_manager::get_undeletable_block_types();
67     echo $OUTPUT->header();
68     echo $OUTPUT->heading($strmanageblocks);
70 /// Main display starts here
72 /// Get and sort the existing blocks
74     if (!$blocks = $DB->get_records('block', array(), 'name ASC')) {
75         print_error('noblocks', 'error');  // Should never happen
76     }
78     $incompatible = array();
80 /// Print the table of all blocks
82     $table = new flexible_table('admin-blocks-compatible');
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));
86     $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
87     $table->set_attribute('class', 'admintable blockstable generaltable');
88     $table->set_attribute('id', 'compatibleblockstable');
89     $table->setup();
90     $tablerows = array();
92     // Sort blocks using current locale.
93     $blocknames = array();
94     foreach ($blocks as $blockid=>$block) {
95         $blockname = $block->name;
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     }
102     core_collator::asort($blocknames);
104     foreach ($blocknames as $blockid=>$strblockname) {
105         $block = $blocks[$blockid];
106         $blockname = $block->name;
107         $dbversion = get_config('block_'.$block->name, 'version');
109         if (!file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
110             $blockobject  = false;
111             $strblockname = '<span class="notifyproblem">'.$strblockname.' ('.get_string('missingfromdisk').')</span>';
112             $plugin = new stdClass();
113             $plugin->version = $dbversion;
115         } else {
116             $plugin = new stdClass();
117             $plugin->version = '???';
118             if (file_exists("$CFG->dirroot/blocks/$blockname/version.php")) {
119                 include("$CFG->dirroot/blocks/$blockname/version.php");
120             }
122             if (!$blockobject  = block_instance($block->name)) {
123                 $incompatible[] = $block;
124                 continue;
125             }
126         }
128         if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('block_'.$blockname, 'manage')) {
129             $uninstall = html_writer::link($uninstallurl, $struninstall);
130         } else {
131             $uninstall = '';
132         }
134         $settings = ''; // By default, no configuration
135         if ($blockobject and $blockobject->has_config()) {
136             $blocksettings = admin_get_root()->locate('blocksetting' . $block->name);
138             if ($blocksettings instanceof admin_externalpage) {
139                 $settings = '<a href="' . $blocksettings->url .  '">' . get_string('settings') . '</a>';
140             } else if ($blocksettings instanceof admin_settingpage) {
141                 $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
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.
146                 debugging('Warning: block_'.$block->name.' returns true in has_config() but does not provide a settings.php file',
147                     DEBUG_DEVELOPER);
148             }
149         }
151         // MDL-11167, blocks can be placed on mymoodle, or the blogs page
152         // and it should not show up on course search page
154         $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
155         $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
157         if ($count>0) {
158             $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
159             $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
160         }
161         else {
162             $blocklist = "$totalcount";
163         }
164         $class = ''; // Nothing fancy, by default
166         if (!$blockobject) {
167             // ignore
168             $visible = '';
169         } else if ($blocks[$blockid]->visible) {
170             $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
171                        '<img src="'.$OUTPUT->pix_url('t/hide') . '" class="iconsmall" alt="'.$strhide.'" /></a>';
172         } else {
173             $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strshow.'">'.
174                        '<img src="'.$OUTPUT->pix_url('t/show') . '" class="iconsmall" alt="'.$strshow.'" /></a>';
175             $class = 'dimmed_text';
176         }
178         if ($dbversion == $plugin->version) {
179             $version = $dbversion;
180         } else {
181             $version = "$dbversion ($plugin->version)";
182         }
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.'">'.
189                        '<img src="'.$OUTPUT->pix_url('t/unlock') . '" class="iconsmall" alt="'.$strunprotect.'" /></a>';
190         } else {
191             $undeletable = '<a href="blocks.php?protect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strprotect.'">'.
192                        '<img src="'.$OUTPUT->pix_url('t/lock') . '" class="iconsmall" alt="'.$strprotect.'" /></a>';
193         }
195         $row = array(
196             $strblockname,
197             $blocklist,
198             $version,
199             $visible,
200             $undeletable,
201             $settings,
202             $uninstall,
203         );
204         $table->add_data($row, $class);
205     }
207     $table->print_html();
209     if (!empty($incompatible)) {
210         echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
212         $table = new flexible_table('admin-blocks-incompatible');
214         $table->define_columns(array('block', 'uninstall'));
215         $table->define_headers(array($strname, $struninstall));
216         $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
218         $table->set_attribute('class', 'incompatibleblockstable generaltable');
220         $table->setup();
222         foreach ($incompatible as $block) {
223             if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('block_'.$block->name, 'manage')) {
224                 $uninstall = html_writer::link($uninstallurl, $struninstall);
225             } else {
226                 $uninstall = '';
227             }
228             $table->add_data(array(
229                 $block->name,
230                 $uninstall,
231             ));
232         }
233         $table->print_html();
234     }
236     echo $OUTPUT->footer();