MDL-39846 fix forgotten object event property
[moodle.git] / admin / blocks.php
blob2ad0a0eced4d15134c8531b1688d34e182263a7e
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.'/tablelib.php');
9 admin_externalpage_setup('manageblocks');
11 $confirm = optional_param('confirm', 0, PARAM_BOOL);
12 $hide = optional_param('hide', 0, PARAM_INT);
13 $show = optional_param('show', 0, PARAM_INT);
14 $unprotect = optional_param('unprotect', 0, PARAM_INT);
15 $protect = optional_param('protect', 0, PARAM_INT);
17 /// Print headings
19 $strmanageblocks = get_string('manageblocks');
20 $struninstall = get_string('uninstallplugin', 'core_admin');
21 $strversion = get_string('version');
22 $strhide = get_string('hide');
23 $strshow = get_string('show');
24 $strsettings = get_string('settings');
25 $strcourses = get_string('blockinstances', 'admin');
26 $strname = get_string('name');
27 $strshowblockcourse = get_string('showblockcourse');
28 $strprotecthdr = get_string('blockprotect', 'admin'). $OUTPUT->help_icon('blockprotect','admin');
29 $strprotect = get_string('blockprotect', 'admin');
30 $strunprotect = get_string('blockunprotect', 'admin');
32 // Purge all caches related to blocks administration.
33 cache::make('core', 'plugininfo_block')->purge();
35 /// If data submitted, then process and store.
37 if (!empty($hide) && confirm_sesskey()) {
38 if (!$block = $DB->get_record('block', array('id'=>$hide))) {
39 print_error('blockdoesnotexist', 'error');
41 $DB->set_field('block', 'visible', '0', array('id'=>$block->id)); // Hide block
42 admin_get_root(true, false); // settings not required - only pages
45 if (!empty($show) && confirm_sesskey() ) {
46 if (!$block = $DB->get_record('block', array('id'=>$show))) {
47 print_error('blockdoesnotexist', 'error');
49 $DB->set_field('block', 'visible', '1', array('id'=>$block->id)); // Show block
50 admin_get_root(true, false); // settings not required - only pages
53 if (!isset($CFG->undeletableblocktypes) || (!is_array($CFG->undeletableblocktypes) && !is_string($CFG->undeletableblocktypes))) {
54 $undeletableblocktypes = array('navigation', 'settings');
55 } else if (is_string($CFG->undeletableblocktypes)) {
56 $undeletableblocktypes = explode(',', $CFG->undeletableblocktypes);
57 } else {
58 $undeletableblocktypes = $CFG->undeletableblocktypes;
61 if (!empty($protect) && confirm_sesskey()) {
62 if (!$block = $DB->get_record('block', array('id'=>$protect))) {
63 print_error('blockdoesnotexist', 'error');
65 if (!in_array($block->name, $undeletableblocktypes)) {
66 $undeletableblocktypes[] = $block->name;
67 set_config('undeletableblocktypes', implode(',', $undeletableblocktypes));
69 admin_get_root(true, false); // settings not required - only pages
72 if (!empty($unprotect) && confirm_sesskey()) {
73 if (!$block = $DB->get_record('block', array('id'=>$unprotect))) {
74 print_error('blockdoesnotexist', 'error');
76 if (in_array($block->name, $undeletableblocktypes)) {
77 $undeletableblocktypes = array_diff($undeletableblocktypes, array($block->name));
78 set_config('undeletableblocktypes', implode(',', $undeletableblocktypes));
80 admin_get_root(true, false); // settings not required - only pages
83 echo $OUTPUT->header();
84 echo $OUTPUT->heading($strmanageblocks);
86 /// Main display starts here
88 /// Get and sort the existing blocks
90 if (!$blocks = $DB->get_records('block', array(), 'name ASC')) {
91 print_error('noblocks', 'error'); // Should never happen
94 $incompatible = array();
96 /// Print the table of all blocks
98 $table = new flexible_table('admin-blocks-compatible');
100 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'undeletable', 'uninstall', 'settings'));
101 $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strprotecthdr, $struninstall, $strsettings));
102 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
103 $table->set_attribute('class', 'admintable blockstable generaltable');
104 $table->set_attribute('id', 'compatibleblockstable');
105 $table->setup();
106 $tablerows = array();
108 // Sort blocks using current locale.
109 $blocknames = array();
110 foreach ($blocks as $blockid=>$block) {
111 $blockname = $block->name;
112 if (file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
113 $blocknames[$blockid] = get_string('pluginname', 'block_'.$blockname);
114 } else {
115 $blocknames[$blockid] = $blockname;
118 collatorlib::asort($blocknames);
120 foreach ($blocknames as $blockid=>$strblockname) {
121 $block = $blocks[$blockid];
122 $blockname = $block->name;
124 if (!file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
125 $blockobject = false;
126 $strblockname = '<span class="notifyproblem">'.$strblockname.' ('.get_string('missingfromdisk').')</span>';
127 $plugin = new stdClass();
128 $plugin->version = $block->version;
130 } else {
131 $plugin = new stdClass();
132 $plugin->version = '???';
133 if (file_exists("$CFG->dirroot/blocks/$blockname/version.php")) {
134 include("$CFG->dirroot/blocks/$blockname/version.php");
137 if (!$blockobject = block_instance($block->name)) {
138 $incompatible[] = $block;
139 continue;
143 if ($uninstallurl = plugin_manager::instance()->get_uninstall_url('block_'.$blockname)) {
144 $uninstall = html_writer::link($uninstallurl, $struninstall);
145 } else {
146 $uninstall = '';
149 $settings = ''; // By default, no configuration
150 if ($blockobject and $blockobject->has_config()) {
151 $blocksettings = admin_get_root()->locate('blocksetting' . $block->name);
153 if ($blocksettings instanceof admin_externalpage) {
154 $settings = '<a href="' . $blocksettings->url . '">' . get_string('settings') . '</a>';
155 } else if ($blocksettings instanceof admin_settingpage) {
156 $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
157 } else {
158 $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
162 // MDL-11167, blocks can be placed on mymoodle, or the blogs page
163 // and it should not show up on course search page
165 $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
166 $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
168 if ($count>0) {
169 $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
170 $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
172 else {
173 $blocklist = "$totalcount";
175 $class = ''; // Nothing fancy, by default
177 if (!$blockobject) {
178 // ignore
179 $visible = '';
180 } else if ($blocks[$blockid]->visible) {
181 $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
182 '<img src="'.$OUTPUT->pix_url('t/hide') . '" class="iconsmall" alt="'.$strhide.'" /></a>';
183 } else {
184 $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strshow.'">'.
185 '<img src="'.$OUTPUT->pix_url('t/show') . '" class="iconsmall" alt="'.$strshow.'" /></a>';
186 $class = ' class="dimmed_text"'; // Leading space required!
189 if ($block->version == $plugin->version) {
190 $version = $block->version;
191 } else {
192 $version = "$block->version ($plugin->version)";
195 if (!$blockobject) {
196 // ignore
197 $undeletable = '';
198 } else if (in_array($blockname, $undeletableblocktypes)) {
199 $undeletable = '<a href="blocks.php?unprotect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strunprotect.'">'.
200 '<img src="'.$OUTPUT->pix_url('t/unlock') . '" class="iconsmall" alt="'.$strunprotect.'" /></a>';
201 } else {
202 $undeletable = '<a href="blocks.php?protect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strprotect.'">'.
203 '<img src="'.$OUTPUT->pix_url('t/lock') . '" class="iconsmall" alt="'.$strprotect.'" /></a>';
206 $row = array(
207 '<span'.$class.'>'.$strblockname.'</span>',
208 $blocklist,
209 '<span'.$class.'>'.$version.'</span>',
210 $visible,
211 $undeletable,
212 $uninstall,
213 $settings
215 $table->add_data($row);
218 $table->print_html();
220 if (!empty($incompatible)) {
221 echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
223 $table = new flexible_table('admin-blocks-incompatible');
225 $table->define_columns(array('block', 'uninstall'));
226 $table->define_headers(array($strname, $struninstall));
227 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
229 $table->set_attribute('class', 'incompatibleblockstable generaltable');
231 $table->setup();
233 foreach ($incompatible as $block) {
234 if ($uninstallurl = plugin_manager::instance()->get_uninstall_url('block_'.$block->name)) {
235 $uninstall = html_writer::link($uninstallurl, $struninstall);
236 } else {
237 $uninstall = '';
239 $table->add_data(array(
240 $block->name,
241 $uninstall,
244 $table->print_html();
247 echo $OUTPUT->footer();