MDL-33434 mssql: Improve handling of returned ids on insert.
[moodle.git] / admin / blocks.php
blob15c30e64ee7ac4bca73826a2f56b0ce3eca51c7c
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 // Inform block it's about to be deleted
102 if (file_exists("$CFG->dirroot/blocks/$block->name/block_$block->name.php")) {
103 $blockobject = block_instance($block->name);
104 if ($blockobject) {
105 $blockobject->before_delete(); //only if we can create instance, block might have been already removed
109 // First delete instances and then block
110 $instances = $DB->get_records('block_instances', array('blockname' => $block->name));
111 if(!empty($instances)) {
112 foreach($instances as $instance) {
113 blocks_delete_instance($instance);
117 // Delete block
118 $DB->delete_records('block', array('id'=>$block->id));
120 drop_plugin_tables($block->name, "$CFG->dirroot/blocks/$block->name/db/install.xml", false); // old obsoleted table names
121 drop_plugin_tables('block_'.$block->name, "$CFG->dirroot/blocks/$block->name/db/install.xml", false);
123 // Delete the capabilities that were defined by this block
124 capabilities_cleanup('block/'.$block->name);
126 // Remove event handlers and dequeue pending events
127 events_uninstall('block/'.$block->name);
129 $a = new stdClass();
130 $a->block = $strblockname;
131 $a->directory = $CFG->dirroot.'/blocks/'.$block->name;
132 notice(get_string('blockdeletefiles', '', $a), 'blocks.php');
136 echo $OUTPUT->header();
137 echo $OUTPUT->heading($strmanageblocks);
139 /// Main display starts here
141 /// Get and sort the existing blocks
143 if (!$blocks = $DB->get_records('block', array(), 'name ASC')) {
144 print_error('noblocks', 'error'); // Should never happen
147 $incompatible = array();
149 /// Print the table of all blocks
151 $table = new flexible_table('admin-blocks-compatible');
153 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'undeletable', 'delete', 'settings'));
154 $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strprotecthdr, $strdelete, $strsettings));
155 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
156 $table->set_attribute('class', 'compatibleblockstable blockstable generaltable');
157 $table->setup();
158 $tablerows = array();
160 foreach ($blocks as $blockid=>$block) {
161 $blockname = $block->name;
163 if (!file_exists("$CFG->dirroot/blocks/$blockname/block_$blockname.php")) {
164 $blockobject = false;
165 $strblockname = '<span class="notifyproblem">'.$blockname.' ('.get_string('missingfromdisk').')</span>';
166 $plugin = new stdClass();
167 $plugin->version = $block->version;
169 } else {
170 $plugin = new stdClass();
171 $plugin->version = '???';
172 if (file_exists("$CFG->dirroot/blocks/$blockname/version.php")) {
173 include("$CFG->dirroot/blocks/$blockname/version.php");
176 if (!$blockobject = block_instance($block->name)) {
177 $incompatible[] = $block;
178 continue;
180 $strblockname = get_string('pluginname', 'block_'.$blockname);
183 $delete = '<a href="blocks.php?delete='.$blockid.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>';
185 $settings = ''; // By default, no configuration
186 if ($blockobject and $blockobject->has_config()) {
187 $blocksettings = admin_get_root()->locate('blocksetting' . $block->name);
189 if ($blocksettings instanceof admin_externalpage) {
190 $settings = '<a href="' . $blocksettings->url . '">' . get_string('settings') . '</a>';
191 } else if ($blocksettings instanceof admin_settingpage) {
192 $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
193 } else {
194 $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
198 // MDL-11167, blocks can be placed on mymoodle, or the blogs page
199 // and it should not show up on course search page
201 $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
202 $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
204 if ($count>0) {
205 $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey=".sesskey()."\" ";
206 $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
208 else {
209 $blocklist = "$totalcount";
211 $class = ''; // Nothing fancy, by default
213 if (!$blockobject) {
214 // ignore
215 $visible = '';
216 } else if ($blocks[$blockid]->visible) {
217 $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strhide.'">'.
218 '<img src="'.$OUTPUT->pix_url('i/hide') . '" class="icon" alt="'.$strhide.'" /></a>';
219 } else {
220 $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strshow.'">'.
221 '<img src="'.$OUTPUT->pix_url('i/show') . '" class="icon" alt="'.$strshow.'" /></a>';
222 $class = ' class="dimmed_text"'; // Leading space required!
225 if ($block->version == $plugin->version) {
226 $version = $block->version;
227 } else {
228 $version = "$block->version ($plugin->version)";
231 if (!$blockobject) {
232 // ignore
233 $undeletable = '';
234 } else if (in_array($blockname, $undeletableblocktypes)) {
235 $undeletable = '<a href="blocks.php?unprotect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strunprotect.'">'.
236 '<img src="'.$OUTPUT->pix_url('t/unlock') . '" class="icon" alt="'.$strunprotect.'" /></a>';
237 } else {
238 $undeletable = '<a href="blocks.php?protect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strprotect.'">'.
239 '<img src="'.$OUTPUT->pix_url('t/unlock_gray') . '" class="icon" alt="'.$strprotect.'" /></a>';
242 $row = array(
243 '<span'.$class.'>'.$strblockname.'</span>',
244 $blocklist,
245 '<span'.$class.'>'.$version.'</span>',
246 $visible,
247 $undeletable,
248 $delete,
249 $settings
251 $tablerows[] = array(strip_tags($strblockname), $row); // first element will be used for sorting
254 textlib_get_instance()->asort($tablerows);
255 foreach ($tablerows as $row) {
256 $table->add_data($row[1]);
259 $table->print_html();
261 if (!empty($incompatible)) {
262 echo $OUTPUT->heading(get_string('incompatibleblocks', 'blockstable', 'admin'));
264 $table = new flexible_table('admin-blocks-incompatible');
266 $table->define_columns(array('block', 'delete'));
267 $table->define_headers(array($strname, $strdelete));
268 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
270 $table->set_attribute('class', 'incompatibleblockstable generaltable');
272 $table->setup();
274 foreach ($incompatible as $block) {
275 $table->add_data(array(
276 $block->name,
277 '<a href="blocks.php?delete='.$block->id.'&amp;sesskey='.sesskey().'">'.$strdelete.'</a>',
280 $table->print_html();
283 echo $OUTPUT->footer();