From 0aed347fd1ce22c08de27e025ac61871ede0030f Mon Sep 17 00:00:00 2001 From: Martin Dougiamas Date: Wed, 20 Jan 2010 09:08:04 +0000 Subject: [PATCH] blocks config MDL-21375 Usability improvements for editing block stickiness, especially when dealing with frontpage and system blocks. --- blocks/edit_form.php | 35 ++++++++++++++++++----------------- lang/en_utf8/block.php | 10 +++++++++- lib/blocklib.php | 29 +++++++++++++++++++++++------ 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/blocks/edit_form.php b/blocks/edit_form.php index 2c5676142e7..070612e5601 100644 --- a/blocks/edit_form.php +++ b/blocks/edit_form.php @@ -82,26 +82,20 @@ class block_edit_form extends moodleform { $regionoptions = $this->page->theme->get_all_block_regions(); $parentcontext = get_context_instance_by_id($this->block->instance->parentcontextid); + $mform->addElement('hidden', 'bui_parentcontextid', $parentcontext->id); - // Check if the block is on the front page (explictly or as system block): MDL-21375 - if ($parentcontext->contextlevel == CONTEXT_COURSE && $parentcontext->instanceid == SITEID) { - $frontpagecontext = $parentcontext; - $systemcontext = get_context_instance(CONTEXT_SYSTEM); - } else if ($parentcontext->contextlevel == CONTEXT_SYSTEM) { - $systemcontext = $parentcontext; - $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID); - } - - if (!empty($frontpagecontext)) { // This block appears on the front page, offer a choice - $contextoptions = array(); - $contextoptions[$frontpagecontext->id] = print_context_name($frontpagecontext); - $contextoptions[$systemcontext->id] = print_context_name($systemcontext); - $mform->addElement('select', 'bui_parentcontextid', get_string('thisblockbelongsto', 'block'), $contextoptions); + $contextoptions = array(); + if ( ($parentcontext->contextlevel == CONTEXT_COURSE && $parentcontext->instanceid == SITEID) || + ($parentcontext->contextlevel == CONTEXT_SYSTEM)) { + $contextoptions[0] = get_string('showonfrontpageonly', 'block'); + $contextoptions[1] = get_string('showonfrontpageandsubs', 'block'); + $contextoptions[2] = get_string('showonentiresite', 'block'); } else { - $mform->addElement('static', 'contextname', get_string('thisblockbelongsto', 'block'), print_context_name($parentcontext)); + $parentcontextname = print_context_name($parentcontext); + $contextoptions[0] = get_string('showoncontextonly', 'block', $parentcontextname); + $contextoptions[1] = get_string('showoncontextandsubs', 'block', $parentcontextname); } - - $mform->addElement('selectyesno', 'bui_showinsubcontexts', get_string('appearsinsubcontexts', 'block')); + $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions); $pagetypeoptions = matching_page_type_patterns($this->page->pagetype); $pagetypeoptions = array_combine($pagetypeoptions, $pagetypeoptions); @@ -170,6 +164,13 @@ class block_edit_form extends moodleform { $defaults->bui_subpagepattern = '%@NULL@%'; } + $systemcontext = get_context_instance(CONTEXT_SYSTEM); + if ($defaults->parentcontextid == $systemcontext->id) { + $defaults->bui_contexts = 2; // System-wide and sticky + } else { + $defaults->bui_contexts = $defaults->bui_showinsubcontexts; + } + parent::set_data($defaults); } diff --git a/lang/en_utf8/block.php b/lang/en_utf8/block.php index e06c51d3dd0..f0e1fea3861 100644 --- a/lang/en_utf8/block.php +++ b/lang/en_utf8/block.php @@ -7,6 +7,7 @@ $string['anypagematchingtheabove'] = 'Any page matching the above'; $string['blocksettings'] = 'Block settings'; $string['bracketfirst'] = '$a (first)'; $string['bracketlast'] = '$a (last)'; +$string['contexts'] = 'Page contexts'; $string['defaultregion'] = 'Default region'; $string['defaultweight'] = 'Default weight'; $string['moveblockhere'] = 'Move block here'; @@ -14,11 +15,18 @@ $string['movingthisblockcancel'] = 'Moving this block ($a)'; $string['onthispage'] = 'On this page'; $string['pagetypes'] = 'Page types'; $string['region'] = 'Region'; +$string['showoncontextonly'] = 'Display on \'$a\' only'; +$string['showoncontextandsubs'] = 'Display on \'$a\' and any pages within it'; +$string['showonentiresite'] = 'Display throughout the entire site'; +$string['showonfrontpageonly'] = 'Display on the front page only'; +$string['showonfrontpageandsubs'] = 'Display on the front page and any pages added to the front page'; $string['subpages'] = 'Specific sub-page'; -$string['thisblockbelongsto'] = 'This block belongs to'; $string['thisspecificpage'] = 'This specific page (page $a)'; $string['visible'] = 'Visible'; $string['weight'] = 'Weight'; $string['wherethisblockappears'] = 'Where this block appears'; $string['undockall'] = 'Undock all'; $string['undockitem'] = 'Undock this item'; + +$string['*'] = 'Any page'; +$string['site-*'] = 'Any top-level site page'; diff --git a/lib/blocklib.php b/lib/blocklib.php index 17882063c72..9157e6a07d4 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -1092,20 +1092,37 @@ class block_manager { } else if ($data = $mform->get_data()) { $bi = new stdClass; $bi->id = $block->instance->id; - $bi->showinsubcontexts = $data->bui_showinsubcontexts; $bi->pagetypepattern = $data->bui_pagetypepattern; if (empty($data->bui_subpagepattern) || $data->bui_subpagepattern == '%@NULL@%') { $bi->subpagepattern = null; } else { $bi->subpagepattern = $data->bui_subpagepattern; } - if (!empty($data->bui_parentcontextid)) { - // Ignore context changing if the user doesn't have block manage for the system - // to prevent more ordinary users moving blocks illegally - if (has_capability('moodle/site:manageblocks', get_context_instance(CONTEXT_SYSTEM))) { - $bi->parentcontextid = $data->bui_parentcontextid; + + $parentcontext = get_context_instance_by_id($data->bui_parentcontextid); + $systemcontext = get_context_instance(CONTEXT_SYSTEM); + + // Updating stickiness and contexts. See MDL-21375 for details. + if (has_capability('moodle/site:manageblocks', $parentcontext)) { // Check permissions in destination + // Explicitly set the context + $bi->parentcontextid = $parentcontext->id; + + // If the context type is > 0 then we'll explicitly set the block as sticky, otherwise not + $bi->showinsubcontexts = (int)(!empty($data->bui_contexts)); + + // If the block wants to be system-wide, then explicitly set that + if ($data->bui_contexts == 2) { // Only possible on a frontpage or system page + $bi->parentcontextid = $systemcontext->id; + + } else { // The block doesn't want to be system-wide, so let's ensure that + if ($parentcontext->id == $systemcontext->id) { // We need to move it to the front page + $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID); + $bi->parentcontextid = $frontpagecontext->id; + $bi->pagetypepattern = '*'; // Just in case + } } } + $bi->defaultregion = $data->bui_defaultregion; $bi->defaultweight = $data->bui_defaultweight; $DB->update_record('block_instances', $bi);