diff --git a/admin/blocks.php b/admin/blocks.php index 17b035090ec1f..b4fa84147a974 100644 --- a/admin/blocks.php +++ b/admin/blocks.php @@ -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 @@ -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. @@ -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); @@ -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(); @@ -191,12 +226,23 @@ $version = "$block->version ($plugin->version)"; } + if (!$blockobject) { + // ignore + $undeletable = ''; + } else if (in_array($blockname, $undeletableblocktypes)) { + $undeletable = ''. + ''.$strunprotect.''; + } else { + $undeletable = ''. + ''.$strprotect.''; + } $table->add_data(array( ''.$strblockname.'', $blocklist, ''.$version.'', $visible, + $undeletable, $delete, $settings )); diff --git a/config-dist.php b/config-dist.php index 61c4e20bc004b..4ca2b728e029f 100644 --- a/config-dist.php +++ b/config-dist.php @@ -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 diff --git a/lang/en/admin.php b/lang/en/admin.php index cec064ff2453f..0b0617757306c 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -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'; diff --git a/lib/blocklib.php b/lib/blocklib.php index 4c816c2306868..d8a198732cb6d 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -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(); @@ -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'));