MDL-22015 fixed more string_manager encapsulation issues
[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
ecd7978c 54 if (get_string_manager()->string_exists('pluginname', "block_$block->name")) {
bd103fde
PS
55 $strblockname = get_string('pluginname', "block_$block->name");
56 } else {
57 $strblockname = $block->name;
061d8339 58 }
0f3fe4b6 59
061d8339 60 if (!$confirm) {
414a4a91 61 echo $OUTPUT->confirm(get_string('blockdeleteconfirm', '', $strblockname), 'blocks.php?delete='.$block->id.'&confirm=1', 'blocks.php');
73d6f52f 62 echo $OUTPUT->footer();
0f3fe4b6 63 exit;
64
65 } else {
44c55d46 66 // Inform block it's about to be deleted
bd103fde
PS
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
71 }
0f3fe4b6 72 }
73
e9a20759 74 // First delete instances and then block
f474a4e5 75 $instances = $DB->get_records('block_instances', array('blockname' => $block->name));
9b4b78fd 76 if(!empty($instances)) {
77 foreach($instances as $instance) {
78 blocks_delete_instance($instance);
79 }
80 }
0f3fe4b6 81
e9a20759 82 // Delete block
bd103fde 83 $DB->delete_records('block', array('id'=>$block->id));
e9a20759 84
8293c751 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);
87
bbbf2d40 88 // Delete the capabilities that were defined by this block
89 capabilities_cleanup('block/'.$block->name);
0f3fe4b6 90
d46306de 91 // remove entent handlers and dequeue pending events
92 events_uninstall('block/'.$block->name);
93
0f3fe4b6 94 $a->block = $strblockname;
95 $a->directory = $CFG->dirroot.'/blocks/'.$block->name;
96 notice(get_string('blockdeletefiles', '', $a), 'blocks.php');
97 }
98 }
99
61ef8f9f 100 echo $OUTPUT->header();
2fff8846 101 echo $OUTPUT->heading($strmanageblocks);
220a90c5 102
0f3fe4b6 103/// Main display starts here
104
105/// Get and sort the existing blocks
106
15822fe2 107 if (!$blocks = $DB->get_records('block', array(), 'name ASC')) {
9c78aeca 108 print_error('noblocks', 'error'); // Should never happen
0f3fe4b6 109 }
110
061d8339 111 $incompatible = array();
112
0f3fe4b6 113/// Print the table of all blocks
114
061d8339 115 $table = new flexible_table('admin-blocks-compatible');
116
95eecbd8 117 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'delete', 'settings'));
118 $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strdelete, $strsettings));
ad0f9c82 119 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
90723839 120 $table->set_attribute('class', 'compatibleblockstable blockstable generaltable');
061d8339 121 $table->setup();
0f3fe4b6 122
15822fe2
PS
123 foreach ($blocks as $blockid=>$block) {
124 $blockname = $block->name;
0f3fe4b6 125
15822fe2
PS
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 } else {
130 if (!$blockobject = block_instance($block->name)) {
131 $incompatible[] = $block;
132 continue;
133 }
134 $strblockname = get_string('pluginname', 'block_'.$blockname);
135 }
bd103fde 136
d4a1fcaf 137 $delete = '<a href="blocks.php?delete='.$blockid.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>';
0f3fe4b6 138
139 $settings = ''; // By default, no configuration
15822fe2 140 if ($blockobject and $blockobject->has_config()) {
220a90c5 141 if (file_exists($CFG->dirroot.'/blocks/'.$block->name.'/settings.php')) {
142 $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
143 } else {
144 $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
145 }
0f3fe4b6 146 }
147
deb2988f 148 // MDL-11167, blocks can be placed on mymoodle, or the blogs page
149 // and it should not show up on course search page
220a90c5 150
f474a4e5 151 $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
152 $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
ab951955 153
616ad119 154 if ($count>0) {
34a2faed 155 $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
ab951955 156 $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
616ad119 157 }
158 else {
ab951955 159 $blocklist = "$totalcount";
616ad119 160 }
0f3fe4b6 161 $class = ''; // Nothing fancy, by default
162
15822fe2
PS
163 if (!$blockobject) {
164 // ignore
165 $visible = '';
166 } else if ($blocks[$blockid]->visible) {
d4a1fcaf 167 $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
b5d0cafc 168 '<img src="'.$OUTPUT->pix_url('i/hide') . '" class="icon" alt="'.$strhide.'" /></a>';
0f3fe4b6 169 } else {
d4a1fcaf 170 $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strshow.'">'.
b5d0cafc 171 '<img src="'.$OUTPUT->pix_url('i/show') . '" class="icon" alt="'.$strshow.'" /></a>';
0f3fe4b6 172 $class = ' class="dimmed_text"'; // Leading space required!
173 }
174
15822fe2
PS
175
176 if ($blockobject) {
177 $blockobject->get_version();
178 }
061d8339 179 $table->add_data(array(
15822fe2 180 '<span'.$class.'>'.$strblockname.'</span>',
616ad119 181 $blocklist,
15822fe2 182 '<span'.$class.'>'.$block->version.'</span>',
9b4b78fd 183 $visible,
9b4b78fd 184 $delete,
185 $settings
061d8339 186 ));
0f3fe4b6 187 }
3c520659 188
061d8339 189 $table->print_html();
190
15822fe2 191 if (!empty($incompatible)) {
90723839 192 echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
061d8339 193
194 $table = new flexible_table('admin-blocks-incompatible');
eef868d1 195
061d8339 196 $table->define_columns(array('block', 'delete'));
197 $table->define_headers(array($strname, $strdelete));
ad0f9c82 198 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
eef868d1 199
90723839 200 $table->set_attribute('class', 'incompatibleblockstable generaltable');
eef868d1 201
061d8339 202 $table->setup();
203
204 foreach ($incompatible as $block) {
205 $table->add_data(array(
206 $block->name,
d4a1fcaf 207 '<a href="blocks.php?delete='.$block->id.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>',
061d8339 208 ));
209 }
210 $table->print_html();
211 }
3c520659 212
73d6f52f 213 echo $OUTPUT->footer();
0f3fe4b6 214
20207b82 215