MDL-26243 calendar: display correct number of repeated events
[moodle.git] / admin / blocks.php
blobb1c53df11f48401e30f865b9c26cf126f3f0a263
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 foreach ($blocks as $blockid=>$block) {
135 $blockname = $block->name;
137 if (!file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
138 $blockobject = false;
139 $strblockname = '<span class="notifyproblem">'.$blockname.' ('.get_string('missingfromdisk').')</span>';
140 $plugin = new stdClass();
141 $plugin->version = $block->version;
143 } else {
144 $plugin = new stdClass();
145 $plugin->version = '???';
146 if (file_exists("$CFG->dirroot/blocks/$blockname/version.php")) {
147 include("$CFG->dirroot/blocks/$blockname/version.php");
150 if (!$blockobject = block_instance($block->name)) {
151 $incompatible[] = $block;
152 continue;
154 $strblockname = get_string('pluginname', 'block_'.$blockname);
157 $delete = '<a href="blocks.php?delete='.$blockid.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>';
159 $settings = ''; // By default, no configuration
160 if ($blockobject and $blockobject->has_config()) {
161 $blocksettings = admin_get_root()->locate('blocksetting' . $block->name);
163 if ($blocksettings instanceof admin_externalpage) {
164 $settings = '<a href="' . $blocksettings->url . '">' . get_string('settings') . '</a>';
165 } else if ($blocksettings instanceof admin_settingpage) {
166 $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
167 } else {
168 $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
172 // MDL-11167, blocks can be placed on mymoodle, or the blogs page
173 // and it should not show up on course search page
175 $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
176 $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
178 if ($count>0) {
179 $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
180 $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
182 else {
183 $blocklist = "$totalcount";
185 $class = ''; // Nothing fancy, by default
187 if (!$blockobject) {
188 // ignore
189 $visible = '';
190 } else if ($blocks[$blockid]->visible) {
191 $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
192 '<img src="'.$OUTPUT->pix_url('i/hide') . '" class="icon" alt="'.$strhide.'" /></a>';
193 } else {
194 $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strshow.'">'.
195 '<img src="'.$OUTPUT->pix_url('i/show') . '" class="icon" alt="'.$strshow.'" /></a>';
196 $class = ' class="dimmed_text"'; // Leading space required!
199 if ($block->version == $plugin->version) {
200 $version = $block->version;
201 } else {
202 $version = "$block->version ($plugin->version)";
205 if (!$blockobject) {
206 // ignore
207 $undeletable = '';
208 } else if (in_array($blockname, $undeletableblocktypes)) {
209 $undeletable = '<a href="blocks.php?unprotect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strunprotect.'">'.
210 '<img src="'.$OUTPUT->pix_url('t/unlock') . '" class="icon" alt="'.$strunprotect.'" /></a>';
211 } else {
212 $undeletable = '<a href="blocks.php?protect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strprotect.'">'.
213 '<img src="'.$OUTPUT->pix_url('t/unlock_gray') . '" class="icon" alt="'.$strprotect.'" /></a>';
216 $row = array(
217 '<span'.$class.'>'.$strblockname.'</span>',
218 $blocklist,
219 '<span'.$class.'>'.$version.'</span>',
220 $visible,
221 $undeletable,
222 $delete,
223 $settings
225 $tablerows[] = array(strip_tags($strblockname), $row); // first element will be used for sorting
228 collatorlib::asort($tablerows);
229 foreach ($tablerows as $row) {
230 $table->add_data($row[1]);
233 $table->print_html();
235 if (!empty($incompatible)) {
236 echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
238 $table = new flexible_table('admin-blocks-incompatible');
240 $table->define_columns(array('block', 'delete'));
241 $table->define_headers(array($strname, $strdelete));
242 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
244 $table->set_attribute('class', 'incompatibleblockstable generaltable');
246 $table->setup();
248 foreach ($incompatible as $block) {
249 $table->add_data(array(
250 $block->name,
251 '<a href="blocks.php?delete='.$block->id.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>',
254 $table->print_html();
257 echo $OUTPUT->footer();