Automatic installer.php lang files by installer_builder (20080610)
[moodle.git] / admin / blocks.php
blob368299a8298e6f744b69cd2b5bd955887cccce88
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);
21 /// Print headings
23 $strmanageblocks = get_string('manageblocks');
24 $strdelete = get_string('delete');
25 $strversion = get_string('version');
26 $strhide = get_string('hide');
27 $strshow = get_string('show');
28 $strsettings = get_string('settings');
29 $strcourses = get_string('blockinstances', 'admin');
30 $strname = get_string('name');
31 $strmultiple = get_string('blockmultiple', 'admin');
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 = !$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');
169 $table->set_attribute('cellspacing', '0');
170 $table->set_attribute('id', 'blocks');
171 $table->set_attribute('class', 'generaltable generalbox');
173 $table->setup();
175 foreach ($blockbyname as $blockname => $blockid) {
177 $blockobject = $blockobjects[$blockid];
179 $delete = '<a href="blocks.php?delete='.$blockid.'&amp;sesskey='.$USER->sesskey.'">'.$strdelete.'</a>';
181 $settings = ''; // By default, no configuration
182 if($blockobject->has_config()) {
183 $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
186 $count = count_records('block_instance', 'blockid', $blockid);
187 $class = ''; // Nothing fancy, by default
189 if ($blocks[$blockid]->visible) {
190 $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.$USER->sesskey.'" title="'.$strhide.'">'.
191 '<img src="'.$CFG->pixpath.'/i/hide.gif" height="16" width="16" alt="" /></a>';
192 } else {
193 $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.$USER->sesskey.'" title="'.$strshow.'">'.
194 '<img src="'.$CFG->pixpath.'/i/show.gif" height="16" width="16" alt="" /></a>';
195 $class = ' class="dimmed_text"'; // Leading space required!
197 if ($blockobject->instance_allow_multiple()) {
198 if($blocks[$blockid]->multiple) {
199 $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>';
201 else {
202 $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>';
205 else {
206 $multiple = '';
209 $table->add_data(array(
210 '<span'.$class.'>'.$blockobject->get_title().'</span>',
211 $count,
212 $blockobject->get_version(),
213 $visible,
214 $multiple,
215 $delete,
216 $settings
220 $table->print_html();
222 if(!empty($incompatible)) {
223 print_heading(get_string('incompatibleblocks', 'admin'));
225 $table = new flexible_table('admin-blocks-incompatible');
227 $table->define_columns(array('block', 'delete'));
228 $table->define_headers(array($strname, $strdelete));
229 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
231 $table->set_attribute('cellspacing', '0');
232 $table->set_attribute('id', 'incompatible');
233 $table->set_attribute('class', 'generaltable generalbox');
235 $table->setup();
237 foreach ($incompatible as $block) {
238 $table->add_data(array(
239 $block->name,
240 '<a href="blocks.php?delete='.$block->id.'&amp;sesskey='.$USER->sesskey.'">'.$strdelete.'</a>',
243 $table->print_html();
246 admin_externalpage_print_footer($adminroot);