MDL-77891 core_calendar: support display of mod iconurl
[moodle.git] / admin / blocks.php
blob19f5829a704e51bb09c089239d2e5d0e922130ee
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');
34 // If data submitted, then process and store.
35 if (!empty($hide) && confirm_sesskey()) {
36 if (!$block = $DB->get_record('block', ['id' => $hide])) {
37 throw new \moodle_exception('blockdoesnotexist', 'error');
40 $class = \core_plugin_manager::resolve_plugininfo_class('block');
41 $class::enable_plugin($block->name, false);
42 // Settings not required - only pages.
43 admin_get_root(true, false);
46 if (!empty($show) && confirm_sesskey() ) {
47 if (!$block = $DB->get_record('block', ['id' => $show])) {
48 throw new \moodle_exception('blockdoesnotexist', 'error');
51 $class = \core_plugin_manager::resolve_plugininfo_class('block');
52 $class::enable_plugin($block->name, true);
53 // Settings not required - only pages.
54 admin_get_root(true, false);
57 if (!empty($protect) && confirm_sesskey()) {
58 block_manager::protect_block((int)$protect);
59 admin_get_root(true, false); // settings not required - only pages
62 if (!empty($unprotect) && confirm_sesskey()) {
63 block_manager::unprotect_block((int)$unprotect);
64 admin_get_root(true, false); // settings not required - only pages
67 $undeletableblocktypes = block_manager::get_undeletable_block_types();
69 echo $OUTPUT->header();
70 echo $OUTPUT->heading($strmanageblocks);
72 echo $OUTPUT->notification(get_string('noteunneededblocks', 'admin'), 'info');
73 /// Main display starts here
75 /// Get and sort the existing blocks
77 if (!$blocks = $DB->get_records('block', array(), 'name ASC')) {
78 throw new \moodle_exception('noblocks', 'error'); // Should never happen.
81 $incompatible = array();
83 /// Print the table of all blocks
85 $table = new flexible_table('admin-blocks-compatible');
87 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'undeletable', 'settings', 'uninstall'));
88 $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strprotecthdr, $strsettings, $struninstall));
89 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
90 $table->set_attribute('class', 'admintable blockstable generaltable');
91 $table->set_attribute('id', 'compatibleblockstable');
92 $table->setup();
93 $tablerows = array();
95 // Sort blocks using current locale.
96 $blocknames = array();
97 foreach ($blocks as $blockid=>$block) {
98 $blockname = $block->name;
99 if (file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
100 $blocknames[$blockid] = get_string('pluginname', 'block_'.$blockname);
101 } else {
102 $blocknames[$blockid] = $blockname;
105 core_collator::asort($blocknames);
107 foreach ($blocknames as $blockid=>$strblockname) {
108 $block = $blocks[$blockid];
109 $blockname = $block->name;
110 $dbversion = get_config('block_'.$block->name, 'version');
112 if (!file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
113 $blockobject = false;
114 $strblockname = '<span class="notifyproblem">'.$strblockname.' ('.get_string('missingfromdisk').')</span>';
115 $plugin = new stdClass();
116 $plugin->version = $dbversion;
118 } else {
119 $plugin = new stdClass();
120 $plugin->version = '???';
121 if (file_exists("$CFG->dirroot/blocks/$blockname/version.php")) {
122 include("$CFG->dirroot/blocks/$blockname/version.php");
125 if (!$blockobject = block_instance($block->name)) {
126 $incompatible[] = $block;
127 continue;
131 if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('block_'.$blockname, 'manage')) {
132 $uninstall = html_writer::link($uninstallurl, $struninstall);
133 } else {
134 $uninstall = '';
137 $settings = ''; // By default, no configuration
138 if ($blockobject and $blockobject->has_config()) {
139 $blocksettings = admin_get_root()->locate('blocksetting' . $block->name);
141 if ($blocksettings instanceof admin_externalpage) {
142 $settings = '<a href="' . $blocksettings->url . '">' . get_string('settings') . '</a>';
143 } else if ($blocksettings instanceof admin_settingpage) {
144 $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
145 } else if (!file_exists($CFG->dirroot.'/blocks/'.$block->name.'/settings.php')) {
146 // If the block's settings node was not found, we check that the block really provides the settings.php file.
147 // Note that blocks can inject their settings to other nodes in the admin tree without using the default locations.
148 // This can be done by assigning null to $setting in settings.php and it is a valid case.
149 debugging('Warning: block_'.$block->name.' returns true in has_config() but does not provide a settings.php file',
150 DEBUG_DEVELOPER);
154 // MDL-11167, blocks can be placed on mymoodle, or the blogs page
155 // and it should not show up on course search page
157 $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
158 $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
160 if ($count>0) {
161 $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
162 $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
164 else {
165 $blocklist = "$totalcount";
167 $class = ''; // Nothing fancy, by default
169 if (!$blockobject) {
170 // ignore
171 $visible = '';
172 } else if ($blocks[$blockid]->visible) {
173 $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
174 $OUTPUT->pix_icon('t/hide', $strhide) . '</a>';
175 } else {
176 $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strshow.'">'.
177 $OUTPUT->pix_icon('t/show', $strshow) . '</a>';
178 $class = 'dimmed_text';
181 if ($dbversion == $plugin->version) {
182 $version = $dbversion;
183 } else {
184 $version = "$dbversion ($plugin->version)";
187 if (!$blockobject) {
188 // ignore
189 $undeletable = '';
190 } else if (in_array($blockname, $undeletableblocktypes)) {
191 $undeletable = '<a href="blocks.php?unprotect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strunprotect.'">'.
192 $OUTPUT->pix_icon('t/unlock', $strunprotect) . '</a>';
193 } else {
194 $undeletable = '<a href="blocks.php?protect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strprotect.'">'.
195 $OUTPUT->pix_icon('t/lock', $strprotect) . '</a>';
198 $row = array(
199 $strblockname,
200 $blocklist,
201 $version,
202 $visible,
203 $undeletable,
204 $settings,
205 $uninstall,
207 $table->add_data($row, $class);
210 $table->print_html();
212 if (!empty($incompatible)) {
213 echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
215 $table = new flexible_table('admin-blocks-incompatible');
217 $table->define_columns(array('block', 'uninstall'));
218 $table->define_headers(array($strname, $struninstall));
219 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
221 $table->set_attribute('class', 'incompatibleblockstable generaltable');
223 $table->setup();
225 foreach ($incompatible as $block) {
226 if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('block_'.$block->name, 'manage')) {
227 $uninstall = html_writer::link($uninstallurl, $struninstall);
228 } else {
229 $uninstall = '';
231 $table->add_data(array(
232 $block->name,
233 $uninstall,
236 $table->print_html();
239 echo $OUTPUT->footer();