weekly release 2.4.6+
[moodle.git] / admin / blocks.php
blobe703ee810cc356c306558579bdc078a96fb9dc5f
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 uninstall_plugin('block', $block->name);
103 $a = new stdClass();
104 $a->block = $strblockname;
105 $a->directory = $CFG->dirroot.'/blocks/'.$block->name;
106 notice(get_string('blockdeletefiles', '', $a), 'blocks.php');
110 echo $OUTPUT->header();
111 echo $OUTPUT->heading($strmanageblocks);
113 /// Main display starts here
115 /// Get and sort the existing blocks
117 if (!$blocks = $DB->get_records('block', array(), 'name ASC')) {
118 print_error('noblocks', 'error'); // Should never happen
121 $incompatible = array();
123 /// Print the table of all blocks
125 $table = new flexible_table('admin-blocks-compatible');
127 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'undeletable', 'delete', 'settings'));
128 $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strprotecthdr, $strdelete, $strsettings));
129 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
130 $table->set_attribute('class', 'compatibleblockstable blockstable generaltable');
131 $table->setup();
132 $tablerows = array();
134 // Sort blocks using current locale.
135 $blocknames = array();
136 foreach ($blocks as $blockid=>$block) {
137 $blockname = $block->name;
138 if (file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
139 $blocknames[$blockid] = get_string('pluginname', 'block_'.$blockname);
140 } else {
141 $blocknames[$blockid] = $blockname;
144 collatorlib::asort($blocknames);
146 foreach ($blocknames as $blockid=>$strblockname) {
147 $block = $blocks[$blockid];
148 $blockname = $block->name;
150 if (!file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
151 $blockobject = false;
152 $strblockname = '<span class="notifyproblem">'.$strblockname.' ('.get_string('missingfromdisk').')</span>';
153 $plugin = new stdClass();
154 $plugin->version = $block->version;
156 } else {
157 $plugin = new stdClass();
158 $plugin->version = '???';
159 if (file_exists("$CFG->dirroot/blocks/$blockname/version.php")) {
160 include("$CFG->dirroot/blocks/$blockname/version.php");
163 if (!$blockobject = block_instance($block->name)) {
164 $incompatible[] = $block;
165 continue;
169 $delete = '<a href="blocks.php?delete='.$blockid.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>';
171 $settings = ''; // By default, no configuration
172 if ($blockobject and $blockobject->has_config()) {
173 $blocksettings = admin_get_root()->locate('blocksetting' . $block->name);
175 if ($blocksettings instanceof admin_externalpage) {
176 $settings = '<a href="' . $blocksettings->url . '">' . get_string('settings') . '</a>';
177 } else if ($blocksettings instanceof admin_settingpage) {
178 $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
179 } else {
180 $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
184 // MDL-11167, blocks can be placed on mymoodle, or the blogs page
185 // and it should not show up on course search page
187 $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
188 $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
190 if ($count>0) {
191 $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
192 $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
194 else {
195 $blocklist = "$totalcount";
197 $class = ''; // Nothing fancy, by default
199 if (!$blockobject) {
200 // ignore
201 $visible = '';
202 } else if ($blocks[$blockid]->visible) {
203 $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
204 '<img src="'.$OUTPUT->pix_url('t/hide') . '" class="iconsmall" alt="'.$strhide.'" /></a>';
205 } else {
206 $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strshow.'">'.
207 '<img src="'.$OUTPUT->pix_url('t/show') . '" class="iconsmall" alt="'.$strshow.'" /></a>';
208 $class = ' class="dimmed_text"'; // Leading space required!
211 if ($block->version == $plugin->version) {
212 $version = $block->version;
213 } else {
214 $version = "$block->version ($plugin->version)";
217 if (!$blockobject) {
218 // ignore
219 $undeletable = '';
220 } else if (in_array($blockname, $undeletableblocktypes)) {
221 $undeletable = '<a href="blocks.php?unprotect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strunprotect.'">'.
222 '<img src="'.$OUTPUT->pix_url('t/unlock') . '" class="iconsmall" alt="'.$strunprotect.'" /></a>';
223 } else {
224 $undeletable = '<a href="blocks.php?protect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strprotect.'">'.
225 '<img src="'.$OUTPUT->pix_url('t/lock') . '" class="iconsmall" alt="'.$strprotect.'" /></a>';
228 $row = array(
229 '<span'.$class.'>'.$strblockname.'</span>',
230 $blocklist,
231 '<span'.$class.'>'.$version.'</span>',
232 $visible,
233 $undeletable,
234 $delete,
235 $settings
237 $table->add_data($row);
240 $table->print_html();
242 if (!empty($incompatible)) {
243 echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
245 $table = new flexible_table('admin-blocks-incompatible');
247 $table->define_columns(array('block', 'delete'));
248 $table->define_headers(array($strname, $strdelete));
249 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
251 $table->set_attribute('class', 'incompatibleblockstable generaltable');
253 $table->setup();
255 foreach ($incompatible as $block) {
256 $table->add_data(array(
257 $block->name,
258 '<a href="blocks.php?delete='.$block->id.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>',
261 $table->print_html();
264 echo $OUTPUT->footer();