Fix a possible race condition in the PaintWeb DML code.
[moodle/mihaisucan.git] / admin / blocks.php
blob277645a49b289469a7f4a48e67d34b3d20a51b70
1 <?PHP // $Id$
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.'/blocklib.php');
8 require_once($CFG->libdir.'/tablelib.php');
9 require_once($CFG->libdir.'/ddllib.php');
11 admin_externalpage_setup('manageblocks');
13 $confirm = optional_param('confirm', 0, PARAM_BOOL);
14 $hide = optional_param('hide', 0, PARAM_INT);
15 $show = optional_param('show', 0, PARAM_INT);
16 $delete = optional_param('delete', 0, PARAM_INT);
17 $multiple = optional_param('multiple', 0, PARAM_INT);
19 /// Print headings
21 $strmanageblocks = get_string('manageblocks');
22 $strdelete = get_string('delete');
23 $strversion = get_string('version');
24 $strhide = get_string('hide');
25 $strshow = get_string('show');
26 $strsettings = get_string('settings');
27 $strcourses = get_string('blockinstances', 'admin');
28 $strname = get_string('name');
29 $strmultiple = get_string('blockmultiple', 'admin');
30 $strshowblockcourse = get_string('showblockcourse');
32 /// If data submitted, then process and store.
34 if (!empty($hide) && confirm_sesskey()) {
35 if (!$block = get_record('block', 'id', $hide)) {
36 error("Block doesn't exist!");
38 set_field('block', 'visible', '0', 'id', $block->id); // Hide block
39 admin_get_root(true, false); // settings not required - only pages
42 if (!empty($show) && confirm_sesskey() ) {
43 if (!$block = get_record('block', 'id', $show)) {
44 error("Block doesn't exist!");
46 set_field('block', 'visible', '1', 'id', $block->id); // Show block
47 admin_get_root(true, false); // settings not required - only pages
50 if (!empty($multiple) && confirm_sesskey()) {
51 if (!$block = blocks_get_record($multiple)) {
52 error("Block doesn't exist!");
54 $block->multiple = !$block->multiple;
55 update_record('block', $block);
56 admin_get_root(true, false); // settings not required - only pages
59 if (!empty($delete) && confirm_sesskey()) {
60 admin_externalpage_print_header();
61 print_heading($strmanageblocks);
63 if (!$block = blocks_get_record($delete)) {
64 error("Block doesn't exist!");
67 if (!block_is_compatible($block->name)) {
68 $strblockname = $block->name;
70 else {
71 $blockobject = block_instance($block->name);
72 $strblockname = $blockobject->get_title();
75 if (!$confirm) {
76 notice_yesno(get_string('blockdeleteconfirm', '', $strblockname),
77 'blocks.php?delete='.$block->id.'&amp;confirm=1&amp;sesskey='.$USER->sesskey,
78 'blocks.php');
79 admin_externalpage_print_footer();
80 exit;
82 } else {
83 // Inform block it's about to be deleted
84 $blockobject = block_instance($block->name);
85 if ($blockobject) {
86 $blockobject->before_delete(); //only if we can create instance, block might have been already removed
89 // First delete instances and then block
90 $instances = get_records('block_instance', 'blockid', $block->id);
91 if(!empty($instances)) {
92 foreach($instances as $instance) {
93 blocks_delete_instance($instance);
94 blocks_delete_instance($instance, true);
98 // Delete block
99 if (!delete_records('block', 'id', $block->id)) {
100 notify("Error occurred while deleting the $strblockname record from blocks table");
103 drop_plugin_tables($block->name, "$CFG->dirroot/blocks/$block->name/db/install.xml", false); // old obsoleted table names
104 drop_plugin_tables('block_'.$block->name, "$CFG->dirroot/blocks/$block->name/db/install.xml", false);
106 // Delete the capabilities that were defined by this block
107 capabilities_cleanup('block/'.$block->name);
109 // remove entent handlers and dequeue pending events
110 events_uninstall('block/'.$block->name);
112 $a->block = $strblockname;
113 $a->directory = $CFG->dirroot.'/blocks/'.$block->name;
114 notice(get_string('blockdeletefiles', '', $a), 'blocks.php');
118 admin_externalpage_print_header();
119 print_heading($strmanageblocks);
121 /// Main display starts here
123 /// Get and sort the existing blocks
125 if (false === ($blocks = get_records('block'))) {
126 error('No blocks found!'); // Should never happen
129 $incompatible = array();
131 foreach ($blocks as $block) {
132 if(!block_is_compatible($block->name)) {
133 notify('Block '. $block->name .' is not compatible with the current version of Moodle and needs to be updated by a programmer.');
134 $incompatible[] = $block;
135 continue;
137 if(($blockobject = block_instance($block->name)) === false) {
138 // Failed to load
139 continue;
141 $blockbyname[$blockobject->get_title()] = $block->id;
142 $blockobjects[$block->id] = $blockobject;
145 if(empty($blockbyname)) {
146 error('One or more blocks are registered in the database, but they all failed to load!');
149 ksort($blockbyname);
151 /// Print the table of all blocks
153 $table = new flexible_table('admin-blocks-compatible');
155 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'multiple', 'delete', 'settings'));
156 $table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strmultiple, $strdelete, $strsettings));
157 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
158 $table->set_attribute('id', 'blocks');
159 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
160 $table->setup();
162 foreach ($blockbyname as $blockname => $blockid) {
164 $blockobject = $blockobjects[$blockid];
165 $block = $blocks[$blockid];
167 $delete = '<a href="blocks.php?delete='.$blockid.'&amp;sesskey='.$USER->sesskey.'">'.$strdelete.'</a>';
169 $settings = ''; // By default, no configuration
170 if ($blockobject->has_config()) {
171 if (file_exists($CFG->dirroot.'/blocks/'.$block->name.'/settings.php')) {
172 $settings = '<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=blocksetting'.$block->name.'">'.$strsettings.'</a>';
173 } else {
174 $settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
178 // MDL-11167, blocks can be placed on mymoodle, or the blogs page
179 // and it should not show up on course search page
181 $totalcount = count_records('block_instance', 'blockid', $blockid);
183 $count = count_records_sql('SELECT COUNT(*)
184 FROM '.$CFG->prefix.'block_instance
185 WHERE blockid = '.$blockid.' AND
186 pagetype = \'course-view\'');
188 if ($count>0) {
189 $blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&amp;sesskey={$USER->sesskey}\" ";
190 $blocklist .= "title=\"$strshowblockcourse\" >$totalcount</a>";
192 else {
193 $blocklist = "$totalcount";
195 $class = ''; // Nothing fancy, by default
197 if ($blocks[$blockid]->visible) {
198 $visible = '<a href="blocks.php?hide='.$blockid.'&amp;sesskey='.$USER->sesskey.'" title="'.$strhide.'">'.
199 '<img src="'.$CFG->pixpath.'/i/hide.gif" class="icon" alt="'.$strhide.'" /></a>';
200 } else {
201 $visible = '<a href="blocks.php?show='.$blockid.'&amp;sesskey='.$USER->sesskey.'" title="'.$strshow.'">'.
202 '<img src="'.$CFG->pixpath.'/i/show.gif" class="icon" alt="'.$strshow.'" /></a>';
203 $class = ' class="dimmed_text"'; // Leading space required!
205 if ($blockobject->instance_allow_multiple()) {
206 if($blocks[$blockid]->multiple) {
207 $multiple = '<span style="white-space: nowrap;">'.get_string('yes').' (<a href="blocks.php?multiple='.$blockid.'&amp;sesskey='.$USER->sesskey.'">'.get_string('change', 'admin').'</a>)</span>';
209 else {
210 $multiple = '<span style="white-space: nowrap;">'.get_string('no').' (<a href="blocks.php?multiple='.$blockid.'&amp;sesskey='.$USER->sesskey.'">'.get_string('change', 'admin').'</a>)</span>';
213 else {
214 $multiple = '';
217 $table->add_data(array(
218 '<span'.$class.'>'.$blockobject->get_title().'</span>',
219 $blocklist,
220 '<span'.$class.'>'.$blockobject->get_version().'</span>',
221 $visible,
222 $multiple,
223 $delete,
224 $settings
228 $table->print_html();
230 if(!empty($incompatible)) {
231 print_heading(get_string('incompatibleblocks', 'admin'));
233 $table = new flexible_table('admin-blocks-incompatible');
235 $table->define_columns(array('block', 'delete'));
236 $table->define_headers(array($strname, $strdelete));
237 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
239 $table->set_attribute('id', 'incompatible');
240 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
242 $table->setup();
244 foreach ($incompatible as $block) {
245 $table->add_data(array(
246 $block->name,
247 '<a href="blocks.php?delete='.$block->id.'&amp;sesskey='.$USER->sesskey.'">'.$strdelete.'</a>',
250 $table->print_html();
253 admin_externalpage_print_footer();