MDL-29912 course search - oracle inabilities with concats, empties, nulls and type...
[moodle.git] / admin / blocks.php
blob55170d28dface86a79fae69eb1f644d105ee2c6d
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);
15 $unprotect = optional_param('unprotect', 0, PARAM_INT);
16 $protect = optional_param('protect', 0, PARAM_INT);
18 /// Print headings
20 $strmanageblocks = get_string('manageblocks');
21 $strdelete = get_string('delete');
22 $strversion = get_string('version');
23 $strhide = get_string('hide');
24 $strshow = get_string('show');
25 $strsettings = get_string('settings');
26 $strcourses = get_string('blockinstances', 'admin');
27 $strname = get_string('name');
28 $strshowblockcourse = get_string('showblockcourse');
29 $strprotecthdr = get_string('blockprotect', 'admin'). $OUTPUT->help_icon('blockprotect','admin');
30 $strprotect = get_string('blockprotect', 'admin');
31 $strunprotect = get_string('blockunprotect', 'admin');
33 /// If data submitted, then process and store.
35 if (!empty($hide) && confirm_sesskey()) {
36 if (!$block = $DB->get_record('block', array('id'=>$hide))) {
37 print_error('blockdoesnotexist', 'error');
39 $DB->set_field('block', 'visible', '0', array('id'=>$block->id)); // Hide block
40 admin_get_root(true, false); // settings not required - only pages
43 if (!empty($show) && confirm_sesskey() ) {
44 if (!$block = $DB->get_record('block', array('id'=>$show))) {
45 print_error('blockdoesnotexist', 'error');
47 $DB->set_field('block', 'visible', '1', array('id'=>$block->id)); // Show block
48 admin_get_root(true, false); // settings not required - only pages
51 if (!isset($CFG->undeletableblocktypes) || (!is_array($CFG->undeletableblocktypes) && !is_string($CFG->undeletableblocktypes))) {
52 $undeletableblocktypes = array('navigation', 'settings');
53 } else if (is_string($CFG->undeletableblocktypes)) {
54 $undeletableblocktypes = explode(',', $CFG->undeletableblocktypes);
55 } else {
56 $undeletableblocktypes = $CFG->undeletableblocktypes;
59 if (!empty($protect) && confirm_sesskey()) {
60 if (!$block = $DB->get_record('block', array('id'=>$protect))) {
61 print_error('blockdoesnotexist', 'error');
63 if (!in_array($block->name, $undeletableblocktypes)) {
64 $undeletableblocktypes[] = $block->name;
65 set_config('undeletableblocktypes', implode(',', $undeletableblocktypes));
67 admin_get_root(true, false); // settings not required - only pages
70 if (!empty($unprotect) && confirm_sesskey()) {
71 if (!$block = $DB->get_record('block', array('id'=>$unprotect))) {
72 print_error('blockdoesnotexist', 'error');
74 if (in_array($block->name, $undeletableblocktypes)) {
75 $undeletableblocktypes = array_diff($undeletableblocktypes, array($block->name));
76 set_config('undeletableblocktypes', implode(',', $undeletableblocktypes));
78 admin_get_root(true, false); // settings not required - only pages
81 if (!empty($delete) && confirm_sesskey()) {
82 echo $OUTPUT->header();
83 echo $OUTPUT->heading($strmanageblocks);
85 if (!$block = blocks_get_record($delete)) {
86 print_error('blockdoesnotexist', 'error');
89 if (get_string_manager()->string_exists('pluginname', "block_$block->name")) {
90 $strblockname = get_string('pluginname', "block_$block->name");
91 } else {
92 $strblockname = $block->name;
95 if (!$confirm) {
96 echo $OUTPUT->confirm(get_string('blockdeleteconfirm', '', $strblockname), 'blocks.php?delete='.$block->id.'&confirm=1', 'blocks.php');
97 echo $OUTPUT->footer();
98 exit;
100 } else {
101 // Inform block it's about to be deleted
102 if (file_exists("$CFG->dirroot/blocks/$block->name/block_$block->name.php")) {
103 $blockobject = block_instance($block->name);
104 if ($blockobject) {
105 $blockobject->before_delete(); //only if we can create instance, block might have been already removed
109 // First delete instances and then block
110 $instances = $DB->get_records('block_instances', array('blockname' => $block->name));
111 if(!empty($instances)) {
112 foreach($instances as $instance) {
113 blocks_delete_instance($instance);
117 // Delete block
118 $DB->delete_records('block', array('id'=>$block->id));
120 drop_plugin_tables($block->name, "$CFG->dirroot/blocks/$block->name/db/install.xml", false); // old obsoleted table names
121 drop_plugin_tables('block_'.$block->name, "$CFG->dirroot/blocks/$block->name/db/install.xml", false);
123 // Delete the capabilities that were defined by this block
124 capabilities_cleanup('block/'.$block->name);
126 // Remove event handlers and dequeue pending events
127 events_uninstall('block/'.$block->name);
129 $a->block = $strblockname;
130 $a->directory = $CFG->dirroot.'/blocks/'.$block->name;
131 notice(get_string('blockdeletefiles', '', $a), 'blocks.php');
135 echo $OUTPUT->header();
136 echo $OUTPUT->heading($strmanageblocks);
138 /// Main display starts here
140 /// Get and sort the existing blocks
142 if (!$blocks = $DB->get_records('block', array(), 'name ASC')) {
143 print_error('noblocks', 'error'); // Should never happen
146 $incompatible = array();
148 /// Print the table of all blocks
150 $table = new flexible_table('admin-blocks-compatible');
152 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'undeletable', 'delete', 'settings'));
153 $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strprotecthdr, $strdelete, $strsettings));
154 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
155 $table->set_attribute('class', 'compatibleblockstable blockstable generaltable');
156 $table->setup();
157 $tablerows = array();
159 foreach ($blocks as $blockid=>$block) {
160 $blockname = $block->name;
162 if (!file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
163 $blockobject = false;
164 $strblockname = '<span class="notifyproblem">'.$blockname.' ('.get_string('missingfromdisk').')</span>';
165 $plugin = new stdClass();
166 $plugin->version = $block->version;
168 } else {
169 $plugin = new stdClass();
170 $plugin->version = '???';
171 if (file_exists("$CFG->dirroot/blocks/$blockname/version.php")) {
172 include("$CFG->dirroot/blocks/$blockname/version.php");
175 if (!$blockobject = block_instance($block->name)) {
176 $incompatible[] = $block;
177 continue;
179 $strblockname = get_string('pluginname', 'block_'.$blockname);
182 $delete = '<a href="blocks.php?delete='.$blockid.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>';
184 $settings = ''; // By default, no configuration
185 if ($blockobject and $blockobject->has_config()) {
186 $blocksettings = admin_get_root()->locate('blocksetting' . $block->name);
188 if ($blocksettings instanceof admin_externalpage) {
189 $settings = '<a href="' . $blocksettings->url . '">' . get_string('settings') . '</a>';
190 } else if ($blocksettings instanceof admin_settingpage) {
191 $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
192 } else {
193 $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
197 // MDL-11167, blocks can be placed on mymoodle, or the blogs page
198 // and it should not show up on course search page
200 $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
201 $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
203 if ($count>0) {
204 $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
205 $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
207 else {
208 $blocklist = "$totalcount";
210 $class = ''; // Nothing fancy, by default
212 if (!$blockobject) {
213 // ignore
214 $visible = '';
215 } else if ($blocks[$blockid]->visible) {
216 $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
217 '<img src="'.$OUTPUT->pix_url('i/hide') . '" class="icon" alt="'.$strhide.'" /></a>';
218 } else {
219 $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strshow.'">'.
220 '<img src="'.$OUTPUT->pix_url('i/show') . '" class="icon" alt="'.$strshow.'" /></a>';
221 $class = ' class="dimmed_text"'; // Leading space required!
224 if ($block->version == $plugin->version) {
225 $version = $block->version;
226 } else {
227 $version = "$block->version ($plugin->version)";
230 if (!$blockobject) {
231 // ignore
232 $undeletable = '';
233 } else if (in_array($blockname, $undeletableblocktypes)) {
234 $undeletable = '<a href="blocks.php?unprotect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strunprotect.'">'.
235 '<img src="'.$OUTPUT->pix_url('t/unlock') . '" class="icon" alt="'.$strunprotect.'" /></a>';
236 } else {
237 $undeletable = '<a href="blocks.php?protect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strprotect.'">'.
238 '<img src="'.$OUTPUT->pix_url('t/unlock_gray') . '" class="icon" alt="'.$strprotect.'" /></a>';
241 $row = array(
242 '<span'.$class.'>'.$strblockname.'</span>',
243 $blocklist,
244 '<span'.$class.'>'.$version.'</span>',
245 $visible,
246 $undeletable,
247 $delete,
248 $settings
250 $tablerows[] = array(strip_tags($strblockname), $row); // first element will be used for sorting
253 textlib_get_instance()->asort($tablerows);
254 foreach ($tablerows as $row) {
255 $table->add_data($row[1]);
258 $table->print_html();
260 if (!empty($incompatible)) {
261 echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
263 $table = new flexible_table('admin-blocks-incompatible');
265 $table->define_columns(array('block', 'delete'));
266 $table->define_headers(array($strname, $strdelete));
267 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
269 $table->set_attribute('class', 'incompatibleblockstable generaltable');
271 $table->setup();
273 foreach ($incompatible as $block) {
274 $table->add_data(array(
275 $block->name,
276 '<a href="blocks.php?delete='.$block->id.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>',
279 $table->print_html();
282 echo $OUTPUT->footer();