b31da9ea00448191b68ca8f73730fb750c45c6bf
[moodle.git] / admin / blocks.php
1 <?php
3     // Allows the admin to configure blocks (hide/show, uninstall and configure)
5     require_once('../config.php');
6     require_once($CFG->libdir.'/adminlib.php');
7     require_once($CFG->libdir.'/blocklib.php');
8     require_once($CFG->libdir.'/tablelib.php');
10     admin_externalpage_setup('manageblocks');
12     $confirm  = optional_param('confirm', 0, PARAM_BOOL);
13     $hide     = optional_param('hide', 0, PARAM_INT);
14     $show     = optional_param('show', 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     $struninstall = get_string('uninstallplugin', 'core_admin');
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');
38         }
39         $DB->set_field('block', 'visible', '0', array('id'=>$block->id));      // Hide block
40         core_plugin_manager::reset_caches();
41         admin_get_root(true, false);  // settings not required - only pages
42     }
44     if (!empty($show) && confirm_sesskey() ) {
45         if (!$block = $DB->get_record('block', array('id'=>$show))) {
46             print_error('blockdoesnotexist', 'error');
47         }
48         $DB->set_field('block', 'visible', '1', array('id'=>$block->id));      // Show block
49         core_plugin_manager::reset_caches();
50         admin_get_root(true, false);  // settings not required - only pages
51     }
53     if (!empty($protect) && confirm_sesskey()) {
54         block_manager::protect_block((int)$protect);
55         admin_get_root(true, false);  // settings not required - only pages
56     }
58     if (!empty($unprotect) && confirm_sesskey()) {
59         block_manager::unprotect_block((int)$unprotect);
60         admin_get_root(true, false);  // settings not required - only pages
61     }
63     $undeletableblocktypes = block_manager::get_undeletable_block_types();
65     echo $OUTPUT->header();
66     echo $OUTPUT->heading($strmanageblocks);
68 /// Main display starts here
70 /// Get and sort the existing blocks
72     if (!$blocks = $DB->get_records('block', array(), 'name ASC')) {
73         print_error('noblocks', 'error');  // Should never happen
74     }
76     $incompatible = array();
78 /// Print the table of all blocks
80     $table = new flexible_table('admin-blocks-compatible');
82     $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'undeletable', 'settings', 'uninstall'));
83     $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strprotecthdr, $strsettings, $struninstall));
84     $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
85     $table->set_attribute('class', 'admintable blockstable generaltable');
86     $table->set_attribute('id', 'compatibleblockstable');
87     $table->setup();
88     $tablerows = array();
90     // Sort blocks using current locale.
91     $blocknames = array();
92     foreach ($blocks as $blockid=>$block) {
93         $blockname = $block->name;
94         if (file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
95             $blocknames[$blockid] = get_string('pluginname', 'block_'.$blockname);
96         } else {
97             $blocknames[$blockid] = $blockname;
98         }
99     }
100     core_collator::asort($blocknames);
102     foreach ($blocknames as $blockid=>$strblockname) {
103         $block = $blocks[$blockid];
104         $blockname = $block->name;
105         $dbversion = get_config('block_'.$block->name, 'version');
107         if (!file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
108             $blockobject  = false;
109             $strblockname = '<span class="notifyproblem">'.$strblockname.' ('.get_string('missingfromdisk').')</span>';
110             $plugin = new stdClass();
111             $plugin->version = $dbversion;
113         } else {
114             $plugin = new stdClass();
115             $plugin->version = '???';
116             if (file_exists("$CFG->dirroot/blocks/$blockname/version.php")) {
117                 include("$CFG->dirroot/blocks/$blockname/version.php");
118             }
120             if (!$blockobject  = block_instance($block->name)) {
121                 $incompatible[] = $block;
122                 continue;
123             }
124         }
126         if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('block_'.$blockname, 'manage')) {
127             $uninstall = html_writer::link($uninstallurl, $struninstall);
128         } else {
129             $uninstall = '';
130         }
132         $settings = ''; // By default, no configuration
133         if ($blockobject and $blockobject->has_config()) {
134             $blocksettings = admin_get_root()->locate('blocksetting' . $block->name);
136             if ($blocksettings instanceof admin_externalpage) {
137                 $settings = '<a href="' . $blocksettings->url .  '">' . get_string('settings') . '</a>';
138             } else if ($blocksettings instanceof admin_settingpage) {
139                 $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
140             } else if (!file_exists($CFG->dirroot.'/blocks/'.$block->name.'/settings.php')) {
141                 // If the block's settings node was not found, we check that the block really provides the settings.php file.
142                 // Note that blocks can inject their settings to other nodes in the admin tree without using the default locations.
143                 // This can be done by assigning null to $setting in settings.php and it is a valid case.
144                 debugging('Warning: block_'.$block->name.' returns true in has_config() but does not provide a settings.php file',
145                     DEBUG_DEVELOPER);
146             }
147         }
149         // MDL-11167, blocks can be placed on mymoodle, or the blogs page
150         // and it should not show up on course search page
152         $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
153         $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
155         if ($count>0) {
156             $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
157             $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
158         }
159         else {
160             $blocklist = "$totalcount";
161         }
162         $class = ''; // Nothing fancy, by default
164         if (!$blockobject) {
165             // ignore
166             $visible = '';
167         } else if ($blocks[$blockid]->visible) {
168             $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
169                        '<img src="'.$OUTPUT->pix_url('t/hide') . '" class="iconsmall" alt="'.$strhide.'" /></a>';
170         } else {
171             $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strshow.'">'.
172                        '<img src="'.$OUTPUT->pix_url('t/show') . '" class="iconsmall" alt="'.$strshow.'" /></a>';
173             $class = 'dimmed_text';
174         }
176         if ($dbversion == $plugin->version) {
177             $version = $dbversion;
178         } else {
179             $version = "$dbversion ($plugin->version)";
180         }
182         if (!$blockobject) {
183             // ignore
184             $undeletable = '';
185         } else if (in_array($blockname, $undeletableblocktypes)) {
186             $undeletable = '<a href="blocks.php?unprotect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strunprotect.'">'.
187                        '<img src="'.$OUTPUT->pix_url('t/unlock') . '" class="iconsmall" alt="'.$strunprotect.'" /></a>';
188         } else {
189             $undeletable = '<a href="blocks.php?protect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strprotect.'">'.
190                        '<img src="'.$OUTPUT->pix_url('t/lock') . '" class="iconsmall" alt="'.$strprotect.'" /></a>';
191         }
193         $row = array(
194             $strblockname,
195             $blocklist,
196             $version,
197             $visible,
198             $undeletable,
199             $settings,
200             $uninstall,
201         );
202         $table->add_data($row, $class);
203     }
205     $table->print_html();
207     if (!empty($incompatible)) {
208         echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
210         $table = new flexible_table('admin-blocks-incompatible');
212         $table->define_columns(array('block', 'uninstall'));
213         $table->define_headers(array($strname, $struninstall));
214         $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
216         $table->set_attribute('class', 'incompatibleblockstable generaltable');
218         $table->setup();
220         foreach ($incompatible as $block) {
221             if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('block_'.$block->name, 'manage')) {
222                 $uninstall = html_writer::link($uninstallurl, $struninstall);
223             } else {
224                 $uninstall = '';
225             }
226             $table->add_data(array(
227                 $block->name,
228                 $uninstall,
229             ));
230         }
231         $table->print_html();
232     }
234     echo $OUTPUT->footer();