MDL-30639 mod_lesson: Used a consistent format for users display of answer
[moodle.git] / admin / blocks.php
blobca9633d58ec970b68f5f04793fd2dc23f1891d12
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->block = $strblockname;
104 $a->directory = $CFG->dirroot.'/blocks/'.$block->name;
105 notice(get_string('blockdeletefiles', '', $a), 'blocks.php');
109 echo $OUTPUT->header();
110 echo $OUTPUT->heading($strmanageblocks);
112 /// Main display starts here
114 /// Get and sort the existing blocks
116 if (!$blocks = $DB->get_records('block', array(), 'name ASC')) {
117 print_error('noblocks', 'error'); // Should never happen
120 $incompatible = array();
122 /// Print the table of all blocks
124 $table = new flexible_table('admin-blocks-compatible');
126 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'undeletable', 'delete', 'settings'));
127 $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strprotecthdr, $strdelete, $strsettings));
128 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
129 $table->set_attribute('class', 'compatibleblockstable blockstable generaltable');
130 $table->setup();
131 $tablerows = array();
133 foreach ($blocks as $blockid=>$block) {
134 $blockname = $block->name;
136 if (!file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
137 $blockobject = false;
138 $strblockname = '<span class="notifyproblem">'.$blockname.' ('.get_string('missingfromdisk').')</span>';
139 $plugin = new stdClass();
140 $plugin->version = $block->version;
142 } else {
143 $plugin = new stdClass();
144 $plugin->version = '???';
145 if (file_exists("$CFG->dirroot/blocks/$blockname/version.php")) {
146 include("$CFG->dirroot/blocks/$blockname/version.php");
149 if (!$blockobject = block_instance($block->name)) {
150 $incompatible[] = $block;
151 continue;
153 $strblockname = get_string('pluginname', 'block_'.$blockname);
156 $delete = '<a href="blocks.php?delete='.$blockid.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>';
158 $settings = ''; // By default, no configuration
159 if ($blockobject and $blockobject->has_config()) {
160 $blocksettings = admin_get_root()->locate('blocksetting' . $block->name);
162 if ($blocksettings instanceof admin_externalpage) {
163 $settings = '<a href="' . $blocksettings->url . '">' . get_string('settings') . '</a>';
164 } else if ($blocksettings instanceof admin_settingpage) {
165 $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
166 } else {
167 $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
171 // MDL-11167, blocks can be placed on mymoodle, or the blogs page
172 // and it should not show up on course search page
174 $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
175 $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
177 if ($count>0) {
178 $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
179 $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
181 else {
182 $blocklist = "$totalcount";
184 $class = ''; // Nothing fancy, by default
186 if (!$blockobject) {
187 // ignore
188 $visible = '';
189 } else if ($blocks[$blockid]->visible) {
190 $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
191 '<img src="'.$OUTPUT->pix_url('i/hide') . '" class="icon" alt="'.$strhide.'" /></a>';
192 } else {
193 $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strshow.'">'.
194 '<img src="'.$OUTPUT->pix_url('i/show') . '" class="icon" alt="'.$strshow.'" /></a>';
195 $class = ' class="dimmed_text"'; // Leading space required!
198 if ($block->version == $plugin->version) {
199 $version = $block->version;
200 } else {
201 $version = "$block->version ($plugin->version)";
204 if (!$blockobject) {
205 // ignore
206 $undeletable = '';
207 } else if (in_array($blockname, $undeletableblocktypes)) {
208 $undeletable = '<a href="blocks.php?unprotect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strunprotect.'">'.
209 '<img src="'.$OUTPUT->pix_url('t/unlock') . '" class="icon" alt="'.$strunprotect.'" /></a>';
210 } else {
211 $undeletable = '<a href="blocks.php?protect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strprotect.'">'.
212 '<img src="'.$OUTPUT->pix_url('t/unlock_gray') . '" class="icon" alt="'.$strprotect.'" /></a>';
215 $row = array(
216 '<span'.$class.'>'.$strblockname.'</span>',
217 $blocklist,
218 '<span'.$class.'>'.$version.'</span>',
219 $visible,
220 $undeletable,
221 $delete,
222 $settings
224 $tablerows[] = array(strip_tags($strblockname), $row); // first element will be used for sorting
227 collatorlib::asort($tablerows);
228 foreach ($tablerows as $row) {
229 $table->add_data($row[1]);
232 $table->print_html();
234 if (!empty($incompatible)) {
235 echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
237 $table = new flexible_table('admin-blocks-incompatible');
239 $table->define_columns(array('block', 'delete'));
240 $table->define_headers(array($strname, $strdelete));
241 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
243 $table->set_attribute('class', 'incompatibleblockstable generaltable');
245 $table->setup();
247 foreach ($incompatible as $block) {
248 $table->add_data(array(
249 $block->name,
250 '<a href="blocks.php?delete='.$block->id.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>',
253 $table->print_html();
256 echo $OUTPUT->footer();