MDL-22054 improving blocks management UI and removing debug message because the block...
[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         else {
55             $blockobject = block_instance($block->name);
56             $strblockname = $blockobject->get_title();
57         }
59         if (!$confirm) {
60             echo $OUTPUT->confirm(get_string('blockdeleteconfirm', '', $strblockname), 'blocks.php?delete='.$block->id.'&confirm=1', 'blocks.php');
61             echo $OUTPUT->footer();
62             exit;
64         } else {
65             // Inform block it's about to be deleted
66             $blockobject = block_instance($block->name);
67             if ($blockobject) {
68                 $blockobject->before_delete();  //only if we can create instance, block might have been already removed
69             }
71             // First delete instances and then block
72             $instances = $DB->get_records('block_instances', array('blockname' => $block->name));
73             if(!empty($instances)) {
74                 foreach($instances as $instance) {
75                     blocks_delete_instance($instance);
76                 }
77             }
79             // Delete block
80             if (!$DB->delete_records('block', array('id'=>$block->id))) {
81                 echo $OUTPUT->notification("Error occurred while deleting the $strblockname record from blocks table");
82             }
84             drop_plugin_tables($block->name, "$CFG->dirroot/blocks/$block->name/db/install.xml", false); // old obsoleted table names
85             drop_plugin_tables('block_'.$block->name, "$CFG->dirroot/blocks/$block->name/db/install.xml", false);
87             // Delete the capabilities that were defined by this block
88             capabilities_cleanup('block/'.$block->name);
90             // remove entent handlers and dequeue pending events
91             events_uninstall('block/'.$block->name);
93             $a->block = $strblockname;
94             $a->directory = $CFG->dirroot.'/blocks/'.$block->name;
95             notice(get_string('blockdeletefiles', '', $a), 'blocks.php');
96         }
97     }
99     echo $OUTPUT->header();
100     echo $OUTPUT->heading($strmanageblocks);
102 /// Main display starts here
104 /// Get and sort the existing blocks
106     if (!$blocks = $DB->get_records('block', array(), 'name ASC')) {
107         print_error('noblocks', 'error');  // Should never happen
108     }
110     $incompatible = array();
112 /// Print the table of all blocks
114     $table = new flexible_table('admin-blocks-compatible');
116     $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'delete', 'settings'));
117     $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strdelete, $strsettings));
118     $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
119     $table->set_attribute('class', 'compatibleblockstable blockstable generaltable');
120     $table->setup();
122     foreach ($blocks as $blockid=>$block) {
123         $blockname = $block->name;
125         if (!file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
126             $blockobject  = false;
127             $strblockname = '<span class="notifyproblem">'.$blockname.' ('.get_string('missingfromdisk').')</span>';
128         } else {
129             if (!$blockobject  = block_instance($block->name)) {
130                 $incompatible[] = $block;
131                 continue;
132             }
133             $strblockname = get_string('pluginname', 'block_'.$blockname);
134         }
135         
136         $delete = '<a href="blocks.php?delete='.$blockid.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>';
138         $settings = ''; // By default, no configuration
139         if ($blockobject and $blockobject->has_config()) {
140             if (file_exists($CFG->dirroot.'/blocks/'.$block->name.'/settings.php')) {
141                 $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
142             } else {
143                 $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
144             }
145         }
147         // MDL-11167, blocks can be placed on mymoodle, or the blogs page
148         // and it should not show up on course search page
150         $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
151         $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
153         if ($count>0) {
154             $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
155             $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
156         }
157         else {
158             $blocklist = "$totalcount";
159         }
160         $class = ''; // Nothing fancy, by default
162         if (!$blockobject) {
163             // ignore
164             $visible = '';
165         } else if ($blocks[$blockid]->visible) {
166             $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
167                        '<img src="'.$OUTPUT->pix_url('i/hide') . '" class="icon" alt="'.$strhide.'" /></a>';
168         } else {
169             $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strshow.'">'.
170                        '<img src="'.$OUTPUT->pix_url('i/show') . '" class="icon" alt="'.$strshow.'" /></a>';
171             $class = ' class="dimmed_text"'; // Leading space required!
172         }
175         if ($blockobject) {
176             $blockobject->get_version();
177         }
178         $table->add_data(array(
179             '<span'.$class.'>'.$strblockname.'</span>',
180             $blocklist,
181             '<span'.$class.'>'.$block->version.'</span>',
182             $visible,
183             $delete,
184             $settings
185         ));
186     }
188     $table->print_html();
190     if (!empty($incompatible)) {
191         echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
193         $table = new flexible_table('admin-blocks-incompatible');
195         $table->define_columns(array('block', 'delete'));
196         $table->define_headers(array($strname, $strdelete));
197         $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
199         $table->set_attribute('class', 'incompatibleblockstable generaltable');
201         $table->setup();
203         foreach ($incompatible as $block) {
204             $table->add_data(array(
205                 $block->name,
206                 '<a href="blocks.php?delete='.$block->id.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>',
207             ));
208         }
209         $table->print_html();
210     }
212     echo $OUTPUT->footer();