diff --git a/blocks/edit_form.php b/blocks/edit_form.php index 80b361ea3bf..9b7198c0c50 100644 --- a/blocks/edit_form.php +++ b/blocks/edit_form.php @@ -93,24 +93,44 @@ class block_edit_form extends moodleform { $mform->addElement('static', 'bui_homecontext', get_string('createdat', 'block'), print_context_name($parentcontext)); $mform->addHelpButton('bui_homecontext', 'createdat', 'block'); + // For pre-calculated (fixed) pagetype lists + $pagetypelist = array(); + // parse pagetype patterns $bits = explode('-', $this->page->pagetype); - $contextoptions = array(); - if ( ($parentcontext->contextlevel == CONTEXT_COURSE && $parentcontext->instanceid == SITEID) || - ($parentcontext->contextlevel == CONTEXT_SYSTEM)) { // Home page - if ($bits[0] == 'tag' || $bits[0] == 'admin') { - // tag and admin pages always use system context - // the contexts options don't make differences, so we use - // page type patterns only - $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_ENTIRE_SITE); - } else { - $contextoptions[BUI_CONTEXTS_FRONTPAGE_ONLY] = get_string('showonfrontpageonly', 'block'); - $contextoptions[BUI_CONTEXTS_FRONTPAGE_SUBS] = get_string('showonfrontpageandsubs', 'block'); - $contextoptions[BUI_CONTEXTS_ENTIRE_SITE] = get_string('showonentiresite', 'block'); - $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions); - $mform->addHelpButton('bui_contexts', 'contexts', 'block'); - } + // First of all, check if we are editing blocks @ front-page or no and + // make some dark magic if so (MDL-30340) because each page context + // implies one (and only one) harcoded page-type that will be set later + // when processing the form data at {@link block_manager::process_url_edit()} + + // There are some conditions to check related to contexts + $ctxconditions = $this->page->context->contextlevel == CONTEXT_COURSE && + $this->page->context->instanceid == get_site()->id; + // And also some pagetype conditions + $pageconditions = isset($bits[0]) && isset($bits[1]) && $bits[0] == 'site' && $bits[1] == 'index'; + // So now we can be 100% sure if edition is happening at frontpage + $editingatfrontpage = $ctxconditions && $pageconditions; + + // Let the form to know about that, can be useful later + $mform->addElement('hidden', 'bui_editingatfrontpage', (int)$editingatfrontpage); + + // Front page, show the page-contexts element and set $pagetypelist to 'any page' (*) + // as unique option. Processign the form will do any change if needed + if ($editingatfrontpage) { + $contextoptions = array(); + $contextoptions[BUI_CONTEXTS_FRONTPAGE_ONLY] = get_string('showonfrontpageonly', 'block'); + $contextoptions[BUI_CONTEXTS_FRONTPAGE_SUBS] = get_string('showonfrontpageandsubs', 'block'); + $contextoptions[BUI_CONTEXTS_ENTIRE_SITE] = get_string('showonentiresite', 'block'); + $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions); + $mform->addHelpButton('bui_contexts', 'contexts', 'block'); + $pagetypelist['*'] = '*'; // This is not going to be shown ever, it's an unique option + + // Any other system context block, hide the page-contexts element, + // it's always system-wide BUI_CONTEXTS_ENTIRE_SITE + } else if ($parentcontext->contextlevel == CONTEXT_SYSTEM) { + $mform->addElement('hidden', 'bui_contexts', BUI_CONTEXTS_ENTIRE_SITE); + } else if ($parentcontext->contextlevel == CONTEXT_COURSE) { // 0 means display on current context only, not child contexts // but if course managers select mod-* as pagetype patterns, block system will overwrite this option @@ -126,12 +146,10 @@ class block_edit_form extends moodleform { $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions); } - $displaypagetypewarning = false; - if ($this->page->pagetype == 'site-index') { // No need for pagetype list on home page - $pagetypelist = array('*'=>get_string('page-x', 'pagetype')); - } else { - // Generate pagetype patterns by callbacks + // Generate pagetype patterns by callbacks if necessary (has not been set specifically) + if (empty($pagetypelist)) { $pagetypelist = generate_page_type_patterns($this->page->pagetype, $parentcontext, $this->page->context); + $displaypagetypewarning = false; if (!array_key_exists($this->block->instance->pagetypepattern, $pagetypelist)) { // Pushing block's existing page type pattern $pagetypestringname = 'page-'.str_replace('*', 'x', $this->block->instance->pagetypepattern); @@ -156,6 +174,25 @@ class block_edit_form extends moodleform { } else { $value = array_pop(array_keys($pagetypelist)); $mform->addElement('hidden', 'bui_pagetypepattern', $value); + // Now we are really hiding a lot (both page-contexts and page-type-patterns), + // specially in some systemcontext pages having only one option (my/user...) + // so, until it's decided if we are going to add the 'bring-back' pattern to + // all those pages or no (see MDL-30574), we are going to show the unique + // element statically + // TODO: Revisit this once MDL-30574 has been decided and implemented, although + // perhaps it's not bad to always show this statically when only one pattern is + // available. + if (!$editingatfrontpage) { + // Try to beautify it + $strvalue = $value; + $strkey = 'page-'.str_replace('*', 'x', $strvalue); + if (get_string_manager()->string_exists($strkey, 'pagetype')) { + $strvalue = get_string($strkey, 'pagetype'); + } + // Show as static (hidden has been set already) + $mform->addElement('static', 'bui_staticpagetypepattern', + get_string('restrictpagetypes','block'), $strvalue); + } } if ($this->page->subpage) { diff --git a/lib/blocklib.php b/lib/blocklib.php index ba81ec748cb..d5d1c3b1372 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -1061,9 +1061,6 @@ class block_manager { /** * Process any block actions that were specified in the URL. * - * This can only be done given a valid $page object. - * - * @param moodle_page $page the page to add blocks to. * @return boolean true if anything was done. False if not. */ public function process_url_actions() { @@ -1230,28 +1227,42 @@ class block_manager { $bi->subpagepattern = $data->bui_subpagepattern; } - $parentcontext = get_context_instance_by_id($data->bui_parentcontextid); $systemcontext = get_context_instance(CONTEXT_SYSTEM); + $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID); + $parentcontext = get_context_instance_by_id($data->bui_parentcontextid); // Updating stickiness and contexts. See MDL-21375 for details. if (has_capability('moodle/site:manageblocks', $parentcontext)) { // Check permissions in destination - // Explicitly set the context + + // Explicitly set the default 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 ($data->bui_editingatfrontpage) { // The block is being edited on the front page - // If the block wants to be system-wide, then explicitly set that - if ($data->bui_contexts == BUI_CONTEXTS_ENTIRE_SITE) { // Only possible on a frontpage or system page - $bi->parentcontextid = $systemcontext->id; - $bi->showinsubcontexts = BUI_CONTEXTS_CURRENT_SUBS; //show in current and sub contexts - $bi->pagetypepattern = '*'; + // The interface here is a special case because the pagetype pattern is + // totally derived from the context menu. Here are the excpetions. MDL-30340 - } 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 = 'site-index'; + switch ($data->bui_contexts) { + case BUI_CONTEXTS_ENTIRE_SITE: + // The user wants to show the block across the entire site + $bi->parentcontextid = $systemcontext->id; + $bi->showinsubcontexts = true; + $bi->pagetypepattern = '*'; + break; + case BUI_CONTEXTS_FRONTPAGE_SUBS: + // The user wants the block shown on the front page and all subcontexts + $bi->parentcontextid = $frontpagecontext->id; + $bi->showinsubcontexts = true; + $bi->pagetypepattern = '*'; + break; + case BUI_CONTEXTS_FRONTPAGE_ONLY: + // The user want to show the front page on the frontpage only + $bi->parentcontextid = $frontpagecontext->id; + $bi->showinsubcontexts = false; + $bi->pagetypepattern = 'site-index'; + // This is the only relevant page type anyway but we'll set it explicitly just + // in case the front page grows site-index-* subpages of its own later + break; } } } @@ -1707,7 +1718,7 @@ function default_page_type_list($pagetype, $parentcontext = null, $currentcontex * @return array */ function my_page_type_list($pagetype, $parentcontext = null, $currentcontext = null) { - return array('my-index' => 'my-index'); + return array('my-index' => get_string('page-my-index', 'pagetype')); } /** diff --git a/user/lib.php b/user/lib.php index 7c2267fe327..66f84047a47 100644 --- a/user/lib.php +++ b/user/lib.php @@ -392,8 +392,5 @@ function user_get_user_details($user, $course = null, array $userfields = array( * @param stdClass $currentcontext Current context of block */ function user_page_type_list($pagetype, $parentcontext, $currentcontext) { - return array( - 'user-profile'=>get_string('page-user-profile', 'pagetype'), - 'my-index'=>get_string('page-my-index', 'pagetype') - ); + return array('user-profile'=>get_string('page-user-profile', 'pagetype')); }