MDL-76614 quiz: move quiz_review_nav_panel to classes
[moodle.git] / admin / blocks.php
blob24bb9d552224a8d82fec6af274b9aa4e7f4f0b45
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 & sort the existing blocks, Select the id, name & visible fields along with count of number of total blocks & course blocks.
77 $sql = "SELECT b.id, b.name, b.visible, COUNT(DISTINCT binst.id) as totalcount, COUNT(DISTINCT bcinst.id) as courseviewcount
78 FROM {block} b
79 LEFT JOIN {block_instances} binst ON binst.blockname = b.name
80 LEFT JOIN {block_instances} bcinst ON bcinst.blockname = b.name AND bcinst.pagetypepattern = 'course-view-*'
81 GROUP BY b.id, b.name, b.visible, binst.blockname, bcinst.blockname
82 ORDER BY b.name ASC";
83 if (!$blocks = $DB->get_records_sql($sql)) {
84 throw new \moodle_exception('noblocks', 'error'); // Should never happen.
87 $incompatible = array();
89 /// Print the table of all blocks
91 $table = new flexible_table('admin-blocks-compatible');
93 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'undeletable', 'settings', 'uninstall'));
94 $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strprotecthdr, $strsettings, $struninstall));
95 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
96 $table->set_attribute('class', 'admintable blockstable generaltable');
97 $table->set_attribute('id', 'compatibleblockstable');
98 $table->setup();
99 $tablerows = array();
101 // Sort blocks using current locale.
102 $blocknames = array();
103 foreach ($blocks as $blockid=>$block) {
104 $blockname = $block->name;
105 if (file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
106 $blocknames[$blockid] = get_string('pluginname', 'block_'.$blockname);
107 } else {
108 $blocknames[$blockid] = $blockname;
111 core_collator::asort($blocknames);
113 foreach ($blocknames as $blockid=>$strblockname) {
114 $block = $blocks[$blockid];
115 $blockname = $block->name;
116 $dbversion = get_config('block_'.$block->name, 'version');
118 if (!file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
119 $blockobject = false;
120 $strblockname = '<span class="notifyproblem">'.$strblockname.' ('.get_string('missingfromdisk').')</span>';
121 $plugin = new stdClass();
122 $plugin->version = $dbversion;
124 } else {
125 $plugin = new stdClass();
126 $plugin->version = '???';
127 if (file_exists("$CFG->dirroot/blocks/$blockname/version.php")) {
128 include("$CFG->dirroot/blocks/$blockname/version.php");
131 if (!$blockobject = block_instance($block->name)) {
132 $incompatible[] = $block;
133 continue;
137 if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('block_'.$blockname, 'manage')) {
138 $uninstall = html_writer::link($uninstallurl, $struninstall);
139 } else {
140 $uninstall = '';
143 $settings = ''; // By default, no configuration
144 if ($blockobject and $blockobject->has_config()) {
145 $blocksettings = admin_get_root()->locate('blocksetting' . $block->name);
147 if ($blocksettings instanceof admin_externalpage) {
148 $settings = '<a href="' . $blocksettings->url . '">' . get_string('settings') . '</a>';
149 } else if ($blocksettings instanceof admin_settingpage) {
150 $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
151 } else if (!file_exists($CFG->dirroot.'/blocks/'.$block->name.'/settings.php')) {
152 // If the block's settings node was not found, we check that the block really provides the settings.php file.
153 // Note that blocks can inject their settings to other nodes in the admin tree without using the default locations.
154 // This can be done by assigning null to $setting in settings.php and it is a valid case.
155 debugging('Warning: block_'.$block->name.' returns true in has_config() but does not provide a settings.php file',
156 DEBUG_DEVELOPER);
160 // MDL-11167, blocks can be placed on mymoodle, or the blogs page
161 // and it should not show up on course search page
163 $totalcount = $blocks[$blockid]->totalcount;
164 $count = $blocks[$blockid]->courseviewcount;
165 if ($count>0) {
166 $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
167 $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
169 else {
170 $blocklist = "$totalcount";
172 $class = ''; // Nothing fancy, by default
174 if (!$blockobject) {
175 // ignore
176 $visible = '';
177 } else if ($blocks[$blockid]->visible) {
178 $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
179 $OUTPUT->pix_icon('t/hide', $strhide) . '</a>';
180 } else {
181 $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strshow.'">'.
182 $OUTPUT->pix_icon('t/show', $strshow) . '</a>';
183 $class = 'dimmed_text';
186 if ($dbversion == $plugin->version) {
187 $version = $dbversion;
188 } else {
189 $version = "$dbversion ($plugin->version)";
192 if (!$blockobject) {
193 // ignore
194 $undeletable = '';
195 } else if (in_array($blockname, $undeletableblocktypes)) {
196 $undeletable = '<a href="blocks.php?unprotect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strunprotect.'">'.
197 $OUTPUT->pix_icon('t/unlock', $strunprotect) . '</a>';
198 } else {
199 $undeletable = '<a href="blocks.php?protect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strprotect.'">'.
200 $OUTPUT->pix_icon('t/lock', $strprotect) . '</a>';
203 $row = array(
204 $strblockname,
205 $blocklist,
206 $version,
207 $visible,
208 $undeletable,
209 $settings,
210 $uninstall,
212 $table->add_data($row, $class);
215 $table->print_html();
217 if (!empty($incompatible)) {
218 echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
220 $table = new flexible_table('admin-blocks-incompatible');
222 $table->define_columns(array('block', 'uninstall'));
223 $table->define_headers(array($strname, $struninstall));
224 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
226 $table->set_attribute('class', 'incompatibleblockstable generaltable');
228 $table->setup();
230 foreach ($incompatible as $block) {
231 if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('block_'.$block->name, 'manage')) {
232 $uninstall = html_writer::link($uninstallurl, $struninstall);
233 } else {
234 $uninstall = '';
236 $table->add_data(array(
237 $block->name,
238 $uninstall,
241 $table->print_html();
244 echo $OUTPUT->footer();