MDL-22054 improving blocks management UI and removing debug message because the block...
[moodle.git] / admin / blocks.php
CommitLineData
20207b82 1<?php
0f3fe4b6 2
3 // Allows the admin to configure blocks (hide/show, delete and configure)
4
5 require_once('../config.php');
6e4dc10f 6 require_once($CFG->libdir.'/adminlib.php');
061d8339 7 require_once($CFG->libdir.'/tablelib.php');
cc0bf616 8
1ae083e4 9 admin_externalpage_setup('manageblocks');
0f3fe4b6 10
1c47adc5 11 $confirm = optional_param('confirm', 0, PARAM_BOOL);
9b4b78fd 12 $hide = optional_param('hide', 0, PARAM_INT);
13 $show = optional_param('show', 0, PARAM_INT);
14 $delete = optional_param('delete', 0, PARAM_INT);
0f3fe4b6 15
0f3fe4b6 16/// Print headings
17
0f3fe4b6 18 $strmanageblocks = get_string('manageblocks');
19 $strdelete = get_string('delete');
20 $strversion = get_string('version');
21 $strhide = get_string('hide');
22 $strshow = get_string('show');
23 $strsettings = get_string('settings');
9b4b78fd 24 $strcourses = get_string('blockinstances', 'admin');
0f3fe4b6 25 $strname = get_string('name');
616ad119 26 $strshowblockcourse = get_string('showblockcourse');
0f3fe4b6 27
0f3fe4b6 28/// If data submitted, then process and store.
29
9b4b78fd 30 if (!empty($hide) && confirm_sesskey()) {
953d72db 31 if (!$block = $DB->get_record('block', array('id'=>$hide))) {
78664df0 32 print_error('blockdoesnotexist', 'error');
0f3fe4b6 33 }
953d72db 34 $DB->set_field('block', 'visible', '0', array('id'=>$block->id)); // Hide block
220a90c5 35 admin_get_root(true, false); // settings not required - only pages
0f3fe4b6 36 }
37
9b4b78fd 38 if (!empty($show) && confirm_sesskey() ) {
953d72db 39 if (!$block = $DB->get_record('block', array('id'=>$show))) {
78664df0 40 print_error('blockdoesnotexist', 'error');
0f3fe4b6 41 }
953d72db 42 $DB->set_field('block', 'visible', '1', array('id'=>$block->id)); // Show block
220a90c5 43 admin_get_root(true, false); // settings not required - only pages
0f3fe4b6 44 }
45
9b4b78fd 46 if (!empty($delete) && confirm_sesskey()) {
61ef8f9f 47 echo $OUTPUT->header();
2fff8846 48 echo $OUTPUT->heading($strmanageblocks);
9b4b78fd 49
50 if (!$block = blocks_get_record($delete)) {
78664df0 51 print_error('blockdoesnotexist', 'error');
0f3fe4b6 52 }
53
061d8339 54 else {
55 $blockobject = block_instance($block->name);
56 $strblockname = $blockobject->get_title();
57 }
0f3fe4b6 58
061d8339 59 if (!$confirm) {
414a4a91 60 echo $OUTPUT->confirm(get_string('blockdeleteconfirm', '', $strblockname), 'blocks.php?delete='.$block->id.'&confirm=1', 'blocks.php');
73d6f52f 61 echo $OUTPUT->footer();
0f3fe4b6 62 exit;
63
64 } else {
44c55d46 65 // Inform block it's about to be deleted
592c3037 66 $blockobject = block_instance($block->name);
e9a20759 67 if ($blockobject) {
68 $blockobject->before_delete(); //only if we can create instance, block might have been already removed
0f3fe4b6 69 }
70
e9a20759 71 // First delete instances and then block
f474a4e5 72 $instances = $DB->get_records('block_instances', array('blockname' => $block->name));
9b4b78fd 73 if(!empty($instances)) {
74 foreach($instances as $instance) {
75 blocks_delete_instance($instance);
76 }
77 }
0f3fe4b6 78
e9a20759 79 // Delete block
953d72db 80 if (!$DB->delete_records('block', array('id'=>$block->id))) {
8fbce1c8 81 echo $OUTPUT->notification("Error occurred while deleting the $strblockname record from blocks table");
e9a20759 82 }
83
8293c751 84 drop_plugin_tables($block->name, "$CFG->dirroot/blocks/$block->name/db/install.xml", false); // old obsoleted table names
85 drop_plugin_tables('block_'.$block->name, "$CFG->dirroot/blocks/$block->name/db/install.xml", false);
86
bbbf2d40 87 // Delete the capabilities that were defined by this block
88 capabilities_cleanup('block/'.$block->name);
0f3fe4b6 89
d46306de 90 // remove entent handlers and dequeue pending events
91 events_uninstall('block/'.$block->name);
92
0f3fe4b6 93 $a->block = $strblockname;
94 $a->directory = $CFG->dirroot.'/blocks/'.$block->name;
95 notice(get_string('blockdeletefiles', '', $a), 'blocks.php');
96 }
97 }
98
61ef8f9f 99 echo $OUTPUT->header();
2fff8846 100 echo $OUTPUT->heading($strmanageblocks);
220a90c5 101
0f3fe4b6 102/// Main display starts here
103
104/// Get and sort the existing blocks
105
15822fe2 106 if (!$blocks = $DB->get_records('block', array(), 'name ASC')) {
9c78aeca 107 print_error('noblocks', 'error'); // Should never happen
0f3fe4b6 108 }
109
061d8339 110 $incompatible = array();
111
0f3fe4b6 112/// Print the table of all blocks
113
061d8339 114 $table = new flexible_table('admin-blocks-compatible');
115
95eecbd8 116 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'delete', 'settings'));
117 $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strdelete, $strsettings));
ad0f9c82 118 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
90723839 119 $table->set_attribute('class', 'compatibleblockstable blockstable generaltable');
061d8339 120 $table->setup();
0f3fe4b6 121
15822fe2
PS
122 foreach ($blocks as $blockid=>$block) {
123 $blockname = $block->name;
0f3fe4b6 124
15822fe2
PS
125 if (!file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
126 $blockobject = false;
127 $strblockname = '<span class="notifyproblem">'.$blockname.' ('.get_string('missingfromdisk').')</span>';
128 } else {
129 if (!$blockobject = block_instance($block->name)) {
130 $incompatible[] = $block;
131 continue;
132 }
133 $strblockname = get_string('pluginname', 'block_'.$blockname);
134 }
135
d4a1fcaf 136 $delete = '<a href="blocks.php?delete='.$blockid.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>';
0f3fe4b6 137
138 $settings = ''; // By default, no configuration
15822fe2 139 if ($blockobject and $blockobject->has_config()) {
220a90c5 140 if (file_exists($CFG->dirroot.'/blocks/'.$block->name.'/settings.php')) {
141 $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
142 } else {
143 $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
144 }
0f3fe4b6 145 }
146
deb2988f 147 // MDL-11167, blocks can be placed on mymoodle, or the blogs page
148 // and it should not show up on course search page
220a90c5 149
f474a4e5 150 $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
151 $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
ab951955 152
616ad119 153 if ($count>0) {
34a2faed 154 $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
ab951955 155 $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
616ad119 156 }
157 else {
ab951955 158 $blocklist = "$totalcount";
616ad119 159 }
0f3fe4b6 160 $class = ''; // Nothing fancy, by default
161
15822fe2
PS
162 if (!$blockobject) {
163 // ignore
164 $visible = '';
165 } else if ($blocks[$blockid]->visible) {
d4a1fcaf 166 $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
b5d0cafc 167 '<img src="'.$OUTPUT->pix_url('i/hide') . '" class="icon" alt="'.$strhide.'" /></a>';
0f3fe4b6 168 } else {
d4a1fcaf 169 $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strshow.'">'.
b5d0cafc 170 '<img src="'.$OUTPUT->pix_url('i/show') . '" class="icon" alt="'.$strshow.'" /></a>';
0f3fe4b6 171 $class = ' class="dimmed_text"'; // Leading space required!
172 }
173
15822fe2
PS
174
175 if ($blockobject) {
176 $blockobject->get_version();
177 }
061d8339 178 $table->add_data(array(
15822fe2 179 '<span'.$class.'>'.$strblockname.'</span>',
616ad119 180 $blocklist,
15822fe2 181 '<span'.$class.'>'.$block->version.'</span>',
9b4b78fd 182 $visible,
9b4b78fd 183 $delete,
184 $settings
061d8339 185 ));
0f3fe4b6 186 }
3c520659 187
061d8339 188 $table->print_html();
189
15822fe2 190 if (!empty($incompatible)) {
90723839 191 echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
061d8339 192
193 $table = new flexible_table('admin-blocks-incompatible');
eef868d1 194
061d8339 195 $table->define_columns(array('block', 'delete'));
196 $table->define_headers(array($strname, $strdelete));
ad0f9c82 197 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
eef868d1 198
90723839 199 $table->set_attribute('class', 'incompatibleblockstable generaltable');
eef868d1 200
061d8339 201 $table->setup();
202
203 foreach ($incompatible as $block) {
204 $table->add_data(array(
205 $block->name,
d4a1fcaf 206 '<a href="blocks.php?delete='.$block->id.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>',
061d8339 207 ));
208 }
209 $table->print_html();
210 }
3c520659 211
73d6f52f 212 echo $OUTPUT->footer();
0f3fe4b6 213
20207b82 214