MDL-27148 whitespace fix
[moodle.git] / admin / blocks.php
blob17b035090ec1fd1eb916381904d4d1520813c367
1 <?php
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.'/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 $delete = optional_param('delete', 0, PARAM_INT);
16 /// Print headings
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');
24 $strcourses = get_string('blockinstances', 'admin');
25 $strname = get_string('name');
26 $strshowblockcourse = get_string('showblockcourse');
28 /// If data submitted, then process and store.
30 if (!empty($hide) && confirm_sesskey()) {
31 if (!$block = $DB->get_record('block', array('id'=>$hide))) {
32 print_error('blockdoesnotexist', 'error');
34 $DB->set_field('block', 'visible', '0', array('id'=>$block->id)); // Hide block
35 admin_get_root(true, false); // settings not required - only pages
38 if (!empty($show) && confirm_sesskey() ) {
39 if (!$block = $DB->get_record('block', array('id'=>$show))) {
40 print_error('blockdoesnotexist', 'error');
42 $DB->set_field('block', 'visible', '1', array('id'=>$block->id)); // Show block
43 admin_get_root(true, false); // settings not required - only pages
46 if (!empty($delete) && confirm_sesskey()) {
47 echo $OUTPUT->header();
48 echo $OUTPUT->heading($strmanageblocks);
50 if (!$block = blocks_get_record($delete)) {
51 print_error('blockdoesnotexist', 'error');
54 if (get_string_manager()->string_exists('pluginname', "block_$block->name")) {
55 $strblockname = get_string('pluginname', "block_$block->name");
56 } else {
57 $strblockname = $block->name;
60 if (!$confirm) {
61 echo $OUTPUT->confirm(get_string('blockdeleteconfirm', '', $strblockname), 'blocks.php?delete='.$block->id.'&confirm=1', 'blocks.php');
62 echo $OUTPUT->footer();
63 exit;
65 } else {
66 // Inform block it's about to be deleted
67 if (file_exists("$CFG->dirroot/blocks/$block->name/block_$block->name.php")) {
68 $blockobject = block_instance($block->name);
69 if ($blockobject) {
70 $blockobject->before_delete(); //only if we can create instance, block might have been already removed
74 // First delete instances and then block
75 $instances = $DB->get_records('block_instances', array('blockname' => $block->name));
76 if(!empty($instances)) {
77 foreach($instances as $instance) {
78 blocks_delete_instance($instance);
82 // Delete block
83 $DB->delete_records('block', array('id'=>$block->id));
85 drop_plugin_tables($block->name, "$CFG->dirroot/blocks/$block->name/db/install.xml", false); // old obsoleted table names
86 drop_plugin_tables('block_'.$block->name, "$CFG->dirroot/blocks/$block->name/db/install.xml", false);
88 // Delete the capabilities that were defined by this block
89 capabilities_cleanup('block/'.$block->name);
91 // Remove event handlers and dequeue pending events
92 events_uninstall('block/'.$block->name);
94 $a->block = $strblockname;
95 $a->directory = $CFG->dirroot.'/blocks/'.$block->name;
96 notice(get_string('blockdeletefiles', '', $a), 'blocks.php');
100 echo $OUTPUT->header();
101 echo $OUTPUT->heading($strmanageblocks);
103 /// Main display starts here
105 /// Get and sort the existing blocks
107 if (!$blocks = $DB->get_records('block', array(), 'name ASC')) {
108 print_error('noblocks', 'error'); // Should never happen
111 $incompatible = array();
113 /// Print the table of all blocks
115 $table = new flexible_table('admin-blocks-compatible');
117 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'delete', 'settings'));
118 $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strdelete, $strsettings));
119 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
120 $table->set_attribute('class', 'compatibleblockstable blockstable generaltable');
121 $table->setup();
123 foreach ($blocks as $blockid=>$block) {
124 $blockname = $block->name;
126 if (!file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
127 $blockobject = false;
128 $strblockname = '<span class="notifyproblem">'.$blockname.' ('.get_string('missingfromdisk').')</span>';
129 $plugin = new stdClass();
130 $plugin->version = $block->version;
132 } else {
133 $plugin = new stdClass();
134 $plugin->version = '???';
135 if (file_exists("$CFG->dirroot/blocks/$blockname/version.php")) {
136 include("$CFG->dirroot/blocks/$blockname/version.php");
139 if (!$blockobject = block_instance($block->name)) {
140 $incompatible[] = $block;
141 continue;
143 $strblockname = get_string('pluginname', 'block_'.$blockname);
146 $delete = '<a href="blocks.php?delete='.$blockid.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>';
148 $settings = ''; // By default, no configuration
149 if ($blockobject and $blockobject->has_config()) {
150 $blocksettings = admin_get_root()->locate('blocksetting' . $block->name);
152 if ($blocksettings instanceof admin_externalpage) {
153 $settings = '<a href="' . $blocksettings->url . '">' . get_string('settings') . '</a>';
154 } else if ($blocksettings instanceof admin_settingpage) {
155 $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
156 } else {
157 $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
161 // MDL-11167, blocks can be placed on mymoodle, or the blogs page
162 // and it should not show up on course search page
164 $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
165 $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
167 if ($count>0) {
168 $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
169 $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
171 else {
172 $blocklist = "$totalcount";
174 $class = ''; // Nothing fancy, by default
176 if (!$blockobject) {
177 // ignore
178 $visible = '';
179 } else if ($blocks[$blockid]->visible) {
180 $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
181 '<img src="'.$OUTPUT->pix_url('i/hide') . '" class="icon" alt="'.$strhide.'" /></a>';
182 } else {
183 $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strshow.'">'.
184 '<img src="'.$OUTPUT->pix_url('i/show') . '" class="icon" alt="'.$strshow.'" /></a>';
185 $class = ' class="dimmed_text"'; // Leading space required!
188 if ($block->version == $plugin->version) {
189 $version = $block->version;
190 } else {
191 $version = "$block->version ($plugin->version)";
195 $table->add_data(array(
196 '<span'.$class.'>'.$strblockname.'</span>',
197 $blocklist,
198 '<span'.$class.'>'.$version.'</span>',
199 $visible,
200 $delete,
201 $settings
205 $table->print_html();
207 if (!empty($incompatible)) {
208 echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
210 $table = new flexible_table('admin-blocks-incompatible');
212 $table->define_columns(array('block', 'delete'));
213 $table->define_headers(array($strname, $strdelete));
214 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
216 $table->set_attribute('class', 'incompatibleblockstable generaltable');
218 $table->setup();
220 foreach ($incompatible as $block) {
221 $table->add_data(array(
222 $block->name,
223 '<a href="blocks.php?delete='.$block->id.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>',
226 $table->print_html();
229 echo $OUTPUT->footer();