Skip to content

Commit

Permalink
MDL-27122 Allow admin to change CFG->undeletableblocktypes on 'Manage…
Browse files Browse the repository at this point in the history
… blocks' settings page. Instances of protected block types will be undeletable only from site context
  • Loading branch information
marinaglancy committed Jul 29, 2011
1 parent ca3e8e9 commit 28e63a9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
50 changes: 48 additions & 2 deletions admin/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
$hide = optional_param('hide', 0, PARAM_INT);
$show = optional_param('show', 0, PARAM_INT);
$delete = optional_param('delete', 0, PARAM_INT);
$unprotect = optional_param('unprotect', 0, PARAM_INT);
$protect = optional_param('protect', 0, PARAM_INT);

/// Print headings

Expand All @@ -24,6 +26,9 @@
$strcourses = get_string('blockinstances', 'admin');
$strname = get_string('name');
$strshowblockcourse = get_string('showblockcourse');
$strprotecthdr = get_string('blockprotect', 'admin'). $OUTPUT->help_icon('blockprotect','admin');
$strprotect = get_string('blockprotect', 'admin');
$strunprotect = get_string('blockunprotect', 'admin');

/// If data submitted, then process and store.

Expand All @@ -43,6 +48,36 @@
admin_get_root(true, false); // settings not required - only pages
}

if (!isset($CFG->undeletableblocktypes) || (!is_array($CFG->undeletableblocktypes) && !is_string($CFG->undeletableblocktypes))) {
$undeletableblocktypes = array('navigation', 'settings');
} else if (is_string($CFG->undeletableblocktypes)) {
$undeletableblocktypes = explode(',', $CFG->undeletableblocktypes);
} else {
$undeletableblocktypes = $CFG->undeletableblocktypes;
}

if (!empty($protect) && confirm_sesskey()) {
if (!$block = $DB->get_record('block', array('id'=>$protect))) {
print_error('blockdoesnotexist', 'error');
}
if (!in_array($block->name, $undeletableblocktypes)) {
$undeletableblocktypes[] = $block->name;
set_config('undeletableblocktypes', implode(',', $undeletableblocktypes));
}
admin_get_root(true, false); // settings not required - only pages
}

if (!empty($unprotect) && confirm_sesskey()) {
if (!$block = $DB->get_record('block', array('id'=>$unprotect))) {
print_error('blockdoesnotexist', 'error');
}
if (in_array($block->name, $undeletableblocktypes)) {
$undeletableblocktypes = array_diff($undeletableblocktypes, array($block->name));
set_config('undeletableblocktypes', implode(',', $undeletableblocktypes));
}
admin_get_root(true, false); // settings not required - only pages
}

if (!empty($delete) && confirm_sesskey()) {
echo $OUTPUT->header();
echo $OUTPUT->heading($strmanageblocks);
Expand Down Expand Up @@ -114,8 +149,8 @@

$table = new flexible_table('admin-blocks-compatible');

$table->define_columns(array('name', 'instances', 'version', 'hideshow', 'delete', 'settings'));
$table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strdelete, $strsettings));
$table->define_columns(array('name', 'instances', 'version', 'hideshow', 'undeletable', 'delete', 'settings'));
$table->define_headers(array($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strprotecthdr, $strdelete, $strsettings));
$table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/blocks.php');
$table->set_attribute('class', 'compatibleblockstable blockstable generaltable');
$table->setup();
Expand Down Expand Up @@ -191,12 +226,23 @@
$version = "$block->version ($plugin->version)";
}

if (!$blockobject) {
// ignore
$undeletable = '';
} else if (in_array($blockname, $undeletableblocktypes)) {
$undeletable = '<a href="blocks.php?unprotect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strunprotect.'">'.
'<img src="'.$OUTPUT->pix_url('t/unlock') . '" class="icon" alt="'.$strunprotect.'" /></a>';
} else {
$undeletable = '<a href="blocks.php?protect='.$blockid.'&amp;sesskey='.sesskey().'" title="'.$strprotect.'">'.
'<img src="'.$OUTPUT->pix_url('t/unlock_gray') . '" class="icon" alt="'.$strprotect.'" /></a>';
}

$table->add_data(array(
'<span'.$class.'>'.$strblockname.'</span>',
$blocklist,
'<span'.$class.'>'.$version.'</span>',
$visible,
$undeletable,
$delete,
$settings
));
Expand Down
5 changes: 0 additions & 5 deletions config-dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,6 @@
// These blocks are used when no other default setting is found.
// $CFG->defaultblocks = 'participants,activity_modules,search_forums,admin,course_list:news_items,calendar_upcoming,recent_activity';
//
// The blocks in this list will be protected from deletion, and this is primarily
// used to protect the navigation and settings blocks which can be very hard to
// get back if accidentally delete.
// $CFG->undeletableblocktypes = 'navigation,settings';
//
// You can specify a different class to be created for the $PAGE global, and to
// compute which blocks appear on each page. However, I cannot think of any good
// reason why you would need to change that. It just felt wrong to hard-code the
Expand Down
3 changes: 3 additions & 0 deletions lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
$string['blockediplist'] = 'Blocked IP List';
$string['blockinstances'] = 'Instances';
$string['blockmultiple'] = 'Multiple';
$string['blockprotect'] = 'Protect from delete';
$string['blockprotect_help'] = 'The blocks instances will be protected from deletion from the site-wide context, and this is primarily used to protect the navigation and settings blocks which can be very hard to get back if accidentally delete.';
$string['blockunprotect'] = 'Unprotect';
$string['blocksettings'] = 'Manage blocks';
$string['bloglevel'] = 'Blog visibility';
$string['bloglevelupgrade'] = 'Blog visibility upgrade';
Expand Down
10 changes: 7 additions & 3 deletions lib/blocklib.php
Original file line number Diff line number Diff line change
Expand Up @@ -997,9 +997,11 @@ public function edit_controls($block) {
global $CFG;

if (!isset($CFG->undeletableblocktypes) || (!is_array($CFG->undeletableblocktypes) && !is_string($CFG->undeletableblocktypes))) {
$CFG->undeletableblocktypes = array('navigation','settings');
$undeletableblocktypes = array('navigation','settings');
} else if (is_string($CFG->undeletableblocktypes)) {
$CFG->undeletableblocktypes = explode(',', $CFG->undeletableblocktypes);
$undeletableblocktypes = explode(',', $CFG->undeletableblocktypes);
} else {
$undeletableblocktypes = $CFG->undeletableblocktypes;
}

$controls = array();
Expand Down Expand Up @@ -1037,7 +1039,9 @@ public function edit_controls($block) {
}

if ($this->page->user_can_edit_blocks() && $block->user_can_edit() && $block->user_can_addto($this->page)) {
if (!in_array($block->instance->blockname, $CFG->undeletableblocktypes)) {
if (!in_array($block->instance->blockname, $undeletableblocktypes)
|| !in_array($block->instance->pagetypepattern, array('*', 'site-index'))
|| $block->instance->parentcontextid != SITEID) {
// Delete icon.
$controls[] = array('url' => $actionurl . '&bui_deleteid=' . $block->instance->id,
'icon' => 't/delete', 'caption' => get_string('delete'));
Expand Down

0 comments on commit 28e63a9

Please sign in to comment.