MDL-22054 improving blocks management UI
[moodle.git] / admin / blocks.php
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');
33         }
34         $DB->set_field('block', 'visible', '0', array('id'=>$block->id));      // Hide block
35         admin_get_root(true, false);  // settings not required - only pages
36     }
38     if (!empty($show) && confirm_sesskey() ) {
39         if (!$block = $DB->get_record('block', array('id'=>$show))) {
40             print_error('blockdoesnotexist', 'error');
41         }
42         $DB->set_field('block', 'visible', '1', array('id'=>$block->id));      // Show block
43         admin_get_root(true, false);  // settings not required - only pages
44     }
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');
52         }
54         if (file_exists("$CFG->dirroot/blocks/$block->name/lang/en/block_$block->name.php")) {
55             $strblockname = get_string('pluginname', "block_$block->name");
56         } else {
57             $strblockname = $block->name;
58         }
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
71                 }
72             }
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);
79                 }
80             }
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 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');
97         }
98     }
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
109     }
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         } else {
130             if (!$blockobject  = block_instance($block->name)) {
131                 $incompatible[] = $block;
132                 continue;
133             }
134             $strblockname = get_string('pluginname', 'block_'.$blockname);
135         }
137         $delete = '<a href="blocks.php?delete='.$blockid.'&amp;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>';
143             } else {
144                 $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
145             }
146         }
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-*'));
154         if ($count>0) {
155             $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
156             $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
157         }
158         else {
159             $blocklist = "$totalcount";
160         }
161         $class = ''; // Nothing fancy, by default
163         if (!$blockobject) {
164             // ignore
165             $visible = '';
166         } else if ($blocks[$blockid]->visible) {
167             $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
168                        '<img src="'.$OUTPUT->pix_url('i/hide') . '" class="icon" alt="'.$strhide.'" /></a>';
169         } else {
170             $visible = '<a href="blocks.php?show='.$blockid.'&amp;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!
173         }
176         if ($blockobject) {
177             $blockobject->get_version();
178         }
179         $table->add_data(array(
180             '<span'.$class.'>'.$strblockname.'</span>',
181             $blocklist,
182             '<span'.$class.'>'.$block->version.'</span>',
183             $visible,
184             $delete,
185             $settings
186         ));
187     }
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');
202         $table->setup();
204         foreach ($incompatible as $block) {
205             $table->add_data(array(
206                 $block->name,
207                 '<a href="blocks.php?delete='.$block->id.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>',
208             ));
209         }
210         $table->print_html();
211     }
213     echo $OUTPUT->footer();