Bumping version for 1.8.10 release (note new convention for numbering)
[moodle.git] / admin / blocks.php
blobc50d3a12c55642000527e64de854737ac7f318dc
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 $adminroot = admin_get_root();
12 admin_externalpage_setup('manageblocks', $adminroot);
14 $confirm = optional_param('confirm', 0, PARAM_BOOL);
15 $hide = optional_param('hide', 0, PARAM_INT);
16 $show = optional_param('show', 0, PARAM_INT);
17 $delete = optional_param('delete', 0, PARAM_INT);
18 $multiple = optional_param('multiple', 0, PARAM_INT);
20 /// Print headings
22 $strmanageblocks = get_string('manageblocks');
23 $strdelete = get_string('delete');
24 $strversion = get_string('version');
25 $strhide = get_string('hide');
26 $strshow = get_string('show');
27 $strsettings = get_string('settings');
28 $strcourses = get_string('blockinstances', 'admin');
29 $strname = get_string('name');
30 $strmultiple = get_string('blockmultiple', 'admin');
31 $strshowblockcourse = get_string('showblockcourse');
33 admin_externalpage_print_header($adminroot);
35 print_heading($strmanageblocks);
37 /// If data submitted, then process and store.
39 if (!empty($hide) && confirm_sesskey()) {
40 if (!$block = get_record('block', 'id', $hide)) {
41 error("Block doesn't exist!");
43 set_field('block', 'visible', '0', 'id', $block->id); // Hide block
46 if (!empty($show) && confirm_sesskey() ) {
47 if (!$block = get_record('block', 'id', $show)) {
48 error("Block doesn't exist!");
50 set_field('block', 'visible', '1', 'id', $block->id); // Show block
53 if (!empty($multiple) && confirm_sesskey()) {
54 if (!$block = blocks_get_record($multiple)) {
55 error("Block doesn't exist!");
57 $block->multiple = intval(!$block->multiple);
58 update_record('block', $block);
61 if (!empty($delete) && confirm_sesskey()) {
63 if (!$block = blocks_get_record($delete)) {
64 error("Block doesn't exist!");
67 if (!block_is_compatible($block->name)) {
68 $strblockname = $block->name;
70 else {
71 $blockobject = block_instance($block->name);
72 $strblockname = $blockobject->get_title();
75 if (!$confirm) {
76 notice_yesno(get_string('blockdeleteconfirm', '', $strblockname),
77 'blocks.php?delete='.$block->id.'&amp;confirm=1&amp;sesskey='.$USER->sesskey,
78 'blocks.php');
79 admin_externalpage_print_footer($adminroot);
80 exit;
82 } else {
83 // Inform block it's about to be deleted
84 $blockobject = block_instance($block->name);
85 if ($blockobject) {
86 $blockobject->before_delete(); //only if we can create instance, block might have been already removed
89 // First delete instances and then block
90 $instances = get_records('block_instance', 'blockid', $block->id);
91 if(!empty($instances)) {
92 foreach($instances as $instance) {
93 blocks_delete_instance($instance);
94 blocks_delete_instance($instance, true);
98 // Delete block
99 if (!delete_records('block', 'id', $block->id)) {
100 notify("Error occurred while deleting the $strblockname record from blocks table");
103 // Then the tables themselves
104 if ($tables = $db->Metatables()) {
105 $prefix = $CFG->prefix.$block->name;
106 $prefix2 = $CFG->prefix.'block_'.$block->name;
107 foreach ($tables as $table) {
108 if (strpos($table, $prefix) === 0 || strpos($table, $prefix2) === 0) {
109 /// If the match has been due to the 1st condition, debug to developers
110 if (strpos($table, $prefix) === 0) {
111 debugging('This block has some wrongly named tables. See Moodle Docs coding guidelines (and MDL-6786)', DEBUG_DEVELOPER);
113 /// Strip prefix from $table
114 $table = preg_replace("/^{$CFG->prefix}/", '', $table);
115 $xmldb_table = new XMLDBTable($table);
116 if (!drop_table($xmldb_table, true, false)) {
117 notify("ERROR: while trying to drop table $table");
122 // Delete the capabilities that were defined by this block
123 capabilities_cleanup('block/'.$block->name);
125 $a->block = $strblockname;
126 $a->directory = $CFG->dirroot.'/blocks/'.$block->name;
127 notice(get_string('blockdeletefiles', '', $a), 'blocks.php');
131 /// Main display starts here
133 /// Get and sort the existing blocks
135 if (false === ($blocks = get_records('block'))) {
136 error('No blocks found!'); // Should never happen
139 $incompatible = array();
141 foreach ($blocks as $block) {
142 if(!block_is_compatible($block->name)) {
143 notify('Block '. $block->name .' is not compatible with the current version of Moodle and needs to be updated by a programmer.');
144 $incompatible[] = $block;
145 continue;
147 if(($blockobject = block_instance($block->name)) === false) {
148 // Failed to load
149 continue;
151 $blockbyname[$blockobject->get_title()] = $block->id;
152 $blockobjects[$block->id] = $blockobject;
155 if(empty($blockbyname)) {
156 error('One or more blocks are registered in the database, but they all failed to load!');
159 ksort($blockbyname);
161 /// Print the table of all blocks
163 $table = new flexible_table('admin-blocks-compatible');
165 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'multiple', 'delete', 'settings'));
166 $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strmultiple, $strdelete, $strsettings));
167 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
168 $table->set_attribute('id', 'blocks');
169 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
170 $table->setup();
172 foreach ($blockbyname as $blockname => $blockid) {
174 $blockobject = $blockobjects[$blockid];
176 $delete = '<a href="blocks.php?delete='.$blockid.'&amp;sesskey='.$USER->sesskey.'">'.$strdelete.'</a>';
178 $settings = ''; // By default, no configuration
179 if($blockobject->has_config()) {
180 $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
183 // MDL-11167, blocks can be placed on mymoodle, or the blogs page
184 // and it should not show up on course search page
185 $count = count_records_sql('SELECT COUNT(*)
186 FROM '.$CFG->prefix.'block_instance
187 WHERE blockid = '.$blockid.' AND
188 pagetype = \'course-view\'');
189 if ($count>0) {
190 $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey={$USER->sesskey}\" ";
191 $blocklist .= "title=\"$strshowblockcourse\" >$count</a>";
193 else {
194 $blocklist = "$count";
196 $class = ''; // Nothing fancy, by default
198 if ($blocks[$blockid]->visible) {
199 $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.$USER->sesskey.'" title="'.$strhide.'">'.
200 '<img src="'.$CFG->pixpath.'/i/hide.gif" class="icon" alt="'.$strhide.'" /></a>';
201 } else {
202 $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.$USER->sesskey.'" title="'.$strshow.'">'.
203 '<img src="'.$CFG->pixpath.'/i/show.gif" class="icon" alt="'.$strshow.'" /></a>';
204 $class = ' class="dimmed_text"'; // Leading space required!
206 if ($blockobject->instance_allow_multiple()) {
207 if($blocks[$blockid]->multiple) {
208 $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>';
210 else {
211 $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>';
214 else {
215 $multiple = '';
218 $table->add_data(array(
219 '<span'.$class.'>'.$blockobject->get_title().'</span>',
220 $blocklist,
221 '<span'.$class.'>'.$blockobject->get_version().'</span>',
222 $visible,
223 $multiple,
224 $delete,
225 $settings
229 $table->print_html();
231 if(!empty($incompatible)) {
232 print_heading(get_string('incompatibleblocks', 'admin'));
234 $table = new flexible_table('admin-blocks-incompatible');
236 $table->define_columns(array('block', 'delete'));
237 $table->define_headers(array($strname, $strdelete));
238 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
240 $table->set_attribute('id', 'incompatible');
241 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
243 $table->setup();
245 foreach ($incompatible as $block) {
246 $table->add_data(array(
247 $block->name,
248 '<a href="blocks.php?delete='.$block->id.'&amp;sesskey='.$USER->sesskey.'">'.$strdelete.'</a>',
251 $table->print_html();
254 admin_externalpage_print_footer($adminroot);