MDL-10870 A few more fixes to the file.php page's navigation
[moodle-pu.git] / admin / blocks.php
blob3b296beeb9f47cf8e9662d00aae4a4fe7d1a0d1d
1 <?PHP // $Id$
3 // Allows the admin to configure blocks (hide/show, delete 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');
9 require_once($CFG->libdir.'/ddllib.php');
11 admin_externalpage_setup('manageblocks');
13 $confirm = optional_param('confirm', 0, PARAM_BOOL);
14 $hide = optional_param('hide', 0, PARAM_INT);
15 $show = optional_param('show', 0, PARAM_INT);
16 $delete = optional_param('delete', 0, PARAM_INT);
17 $multiple = optional_param('multiple', 0, PARAM_INT);
19 /// Print headings
21 $strmanageblocks = get_string('manageblocks');
22 $strdelete = get_string('delete');
23 $strversion = get_string('version');
24 $strhide = get_string('hide');
25 $strshow = get_string('show');
26 $strsettings = get_string('settings');
27 $strcourses = get_string('blockinstances', 'admin');
28 $strname = get_string('name');
29 $strmultiple = get_string('blockmultiple', 'admin');
30 $strshowblockcourse = get_string('showblockcourse');
32 admin_externalpage_print_header();
34 print_heading($strmanageblocks);
36 /// If data submitted, then process and store.
38 if (!empty($hide) && confirm_sesskey()) {
39 if (!$block = get_record('block', 'id', $hide)) {
40 error("Block doesn't exist!");
42 set_field('block', 'visible', '0', 'id', $block->id); // Hide block
45 if (!empty($show) && confirm_sesskey() ) {
46 if (!$block = get_record('block', 'id', $show)) {
47 error("Block doesn't exist!");
49 set_field('block', 'visible', '1', 'id', $block->id); // Show block
52 if (!empty($multiple) && confirm_sesskey()) {
53 if (!$block = blocks_get_record($multiple)) {
54 error("Block doesn't exist!");
56 $block->multiple = !$block->multiple;
57 update_record('block', $block);
60 if (!empty($delete) && confirm_sesskey()) {
62 if (!$block = blocks_get_record($delete)) {
63 error("Block doesn't exist!");
66 if (!block_is_compatible($block->name)) {
67 $strblockname = $block->name;
69 else {
70 $blockobject = block_instance($block->name);
71 $strblockname = $blockobject->get_title();
74 if (!$confirm) {
75 notice_yesno(get_string('blockdeleteconfirm', '', $strblockname),
76 'blocks.php?delete='.$block->id.'&amp;confirm=1&amp;sesskey='.$USER->sesskey,
77 'blocks.php');
78 admin_externalpage_print_footer();
79 exit;
81 } else {
82 // Inform block it's about to be deleted
83 $blockobject = block_instance($block->name);
84 if ($blockobject) {
85 $blockobject->before_delete(); //only if we can create instance, block might have been already removed
88 // First delete instances and then block
89 $instances = get_records('block_instance', 'blockid', $block->id);
90 if(!empty($instances)) {
91 foreach($instances as $instance) {
92 blocks_delete_instance($instance);
93 blocks_delete_instance($instance, true);
97 // Delete block
98 if (!delete_records('block', 'id', $block->id)) {
99 notify("Error occurred while deleting the $strblockname record from blocks table");
102 // Then the tables themselves
103 if ($tables = $db->Metatables()) {
104 $prefix = $CFG->prefix.$block->name;
105 $prefix2 = $CFG->prefix.'block_'.$block->name;
106 foreach ($tables as $table) {
107 if (strpos($table, $prefix) === 0 || strpos($table, $prefix2) === 0) {
108 /// If the match has been due to the 1st condition, debug to developers
109 if (strpos($table, $prefix) === 0) {
110 debugging('This block has some wrongly named tables. See Moodle Docs coding guidelines (and MDL-6786)', DEBUG_DEVELOPER);
112 /// Strip prefix from $table
113 $table = preg_replace("/^{$CFG->prefix}/", '', $table);
114 $xmldb_table = new XMLDBTable($table);
115 if (!drop_table($xmldb_table, true, false)) {
116 notify("ERROR: while trying to drop table $table");
121 // Delete the capabilities that were defined by this block
122 capabilities_cleanup('block/'.$block->name);
124 // remove entent handlers and dequeue pending events
125 events_uninstall('block/'.$block->name);
127 $a->block = $strblockname;
128 $a->directory = $CFG->dirroot.'/blocks/'.$block->name;
129 notice(get_string('blockdeletefiles', '', $a), 'blocks.php');
133 /// Main display starts here
135 /// Get and sort the existing blocks
137 if (false === ($blocks = get_records('block'))) {
138 error('No blocks found!'); // Should never happen
141 $incompatible = array();
143 foreach ($blocks as $block) {
144 if(!block_is_compatible($block->name)) {
145 notify('Block '. $block->name .' is not compatible with the current version of Moodle and needs to be updated by a programmer.');
146 $incompatible[] = $block;
147 continue;
149 if(($blockobject = block_instance($block->name)) === false) {
150 // Failed to load
151 continue;
153 $blockbyname[$blockobject->get_title()] = $block->id;
154 $blockobjects[$block->id] = $blockobject;
157 if(empty($blockbyname)) {
158 error('One or more blocks are registered in the database, but they all failed to load!');
161 ksort($blockbyname);
163 /// Print the table of all blocks
165 $table = new flexible_table('admin-blocks-compatible');
167 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'multiple', 'delete', 'settings'));
168 $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strmultiple, $strdelete, $strsettings));
169 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
170 $table->set_attribute('id', 'blocks');
171 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
172 $table->setup();
174 foreach ($blockbyname as $blockname => $blockid) {
176 $blockobject = $blockobjects[$blockid];
178 $delete = '<a href="blocks.php?delete='.$blockid.'&amp;sesskey='.$USER->sesskey.'">'.$strdelete.'</a>';
180 $settings = ''; // By default, no configuration
181 if($blockobject->has_config()) {
182 $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
185 $count = count_records('block_instance', 'blockid', $blockid);
186 if ($count>0) {
187 $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey={$USER->sesskey}\" ";
188 $blocklist .= "title=\"$strshowblockcourse\" >$count</a>";
190 else {
191 $blocklist = "$count";
193 $class = ''; // Nothing fancy, by default
195 if ($blocks[$blockid]->visible) {
196 $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.$USER->sesskey.'" title="'.$strhide.'">'.
197 '<img src="'.$CFG->pixpath.'/i/hide.gif" class="icon" alt="'.$strhide.'" /></a>';
198 } else {
199 $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.$USER->sesskey.'" title="'.$strshow.'">'.
200 '<img src="'.$CFG->pixpath.'/i/show.gif" class="icon" alt="'.$strshow.'" /></a>';
201 $class = ' class="dimmed_text"'; // Leading space required!
203 if ($blockobject->instance_allow_multiple()) {
204 if($blocks[$blockid]->multiple) {
205 $multiple = '<span style="white-space: nowrap;">'.get_string('yes').' (<a href="blocks.php?multiple='.$blockid.'&amp;sesskey='.$USER->sesskey.'">'.get_string('change', 'admin').'</a>)</span>';
207 else {
208 $multiple = '<span style="white-space: nowrap;">'.get_string('no').' (<a href="blocks.php?multiple='.$blockid.'&amp;sesskey='.$USER->sesskey.'">'.get_string('change', 'admin').'</a>)</span>';
211 else {
212 $multiple = '';
215 $table->add_data(array(
216 '<span'.$class.'>'.$blockobject->get_title().'</span>',
217 $blocklist,
218 '<span'.$class.'>'.$blockobject->get_version().'</span>',
219 $visible,
220 $multiple,
221 $delete,
222 $settings
226 $table->print_html();
228 if(!empty($incompatible)) {
229 print_heading(get_string('incompatibleblocks', 'admin'));
231 $table = new flexible_table('admin-blocks-incompatible');
233 $table->define_columns(array('block', 'delete'));
234 $table->define_headers(array($strname, $strdelete));
235 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
237 $table->set_attribute('id', 'incompatible');
238 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
240 $table->setup();
242 foreach ($incompatible as $block) {
243 $table->add_data(array(
244 $block->name,
245 '<a href="blocks.php?delete='.$block->id.'&amp;sesskey='.$USER->sesskey.'">'.$strdelete.'</a>',
248 $table->print_html();
251 admin_externalpage_print_footer();