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);
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");
57 $strblockname = $block->name;
61 echo $OUTPUT->confirm(get_string('blockdeleteconfirm', '', $strblockname), 'blocks.php?delete='.$block->id.'&confirm=1', 'blocks.php');
62 echo $OUTPUT->footer();
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);
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);
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 entent 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');
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>';
130 if (!$blockobject = block_instance($block->name)) {
131 $incompatible[] = $block;
134 $strblockname = get_string('pluginname', 'block_'.$blockname);
137 $delete = '<a href="blocks.php?delete='.$blockid.'&sesskey='.sesskey().'">'.$strdelete.'</a>';
139 $settings = ''; // By default, no configuration
140 if ($blockobject and $blockobject->has_config()) {
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>';
144 $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
148 // MDL-11167, blocks can be placed on mymoodle, or the blogs page
149 // and it should not show up on course search page
151 $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
152 $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
155 $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&sesskey=".sesskey()."\" ";
156 $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
159 $blocklist = "$totalcount";
161 $class = ''; // Nothing fancy, by default
166 } else if ($blocks[$blockid]->visible) {
167 $visible = '<a href="blocks.php?hide='.$blockid.'&sesskey='.sesskey().'" title="'.$strhide.'">'.
168 '<img src="'.$OUTPUT->pix_url('i/hide') . '" class="icon" alt="'.$strhide.'" /></a>';
170 $visible = '<a href="blocks.php?show='.$blockid.'&sesskey='.sesskey().'" title="'.$strshow.'">'.
171 '<img src="'.$OUTPUT->pix_url('i/show') . '" class="icon" alt="'.$strshow.'" /></a>';
172 $class = ' class="dimmed_text"'; // Leading space required!
177 $blockobject->get_version();
179 $table->add_data(array(
180 '<span'.$class.'>'.$strblockname.'</span>',
182 '<span'.$class.'>'.$block->version.'</span>',
189 $table->print_html();
191 if (!empty($incompatible)) {
192 echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
194 $table = new flexible_table('admin-blocks-incompatible');
196 $table->define_columns(array('block', 'delete'));
197 $table->define_headers(array($strname, $strdelete));
198 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
200 $table->set_attribute('class', 'incompatibleblockstable generaltable');
204 foreach ($incompatible as $block) {
205 $table->add_data(array(
207 '<a href="blocks.php?delete='.$block->id.'&sesskey='.sesskey().'">'.$strdelete.'</a>',
210 $table->print_html();
213 echo $OUTPUT->footer();