From 7c600d46aa41bf8440c1351188e6cdf98638fa4a Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Sat, 3 Dec 2011 14:07:44 +0100 Subject: [PATCH 1/6] MDL-30340 Trivial change in PHP doc block - the function does not accept any params --- lib/blocklib.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/blocklib.php b/lib/blocklib.php index df617cb4e04..b39badb51a2 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() { From e3cdf40ff5d49a60465b4857d97245bec6905519 Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Sat, 3 Dec 2011 14:08:52 +0100 Subject: [PATCH 2/6] MDL-30340 Always generate pagetype patterns in the block settings form This is an alternative solution to MDL-27812. If the current block instance has page type set to site-index, we need to provide a way how that page type pattern can be reset back to other value. --- blocks/edit_form.php | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/blocks/edit_form.php b/blocks/edit_form.php index 80b361ea3bf..1e1cf8ca3e8 100644 --- a/blocks/edit_form.php +++ b/blocks/edit_form.php @@ -126,23 +126,19 @@ class block_edit_form extends moodleform { $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions); } + // Generate pagetype patterns by callbacks $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 - $pagetypelist = generate_page_type_patterns($this->page->pagetype, $parentcontext, $this->page->context); - 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); - if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) { - $pagetypelist[$this->block->instance->pagetypepattern] = get_string($pagetypestringname, 'pagetype'); - } else { - //as a last resort we could put the page type pattern in the select box - //however this causes mod-data-view to be added if the only option available is mod-data-* - // so we are just showing a warning to users about their prev setting being reset - $displaypagetypewarning = true; - } + $pagetypelist = generate_page_type_patterns($this->page->pagetype, $parentcontext, $this->page->context); + 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); + if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) { + $pagetypelist[$this->block->instance->pagetypepattern] = get_string($pagetypestringname, 'pagetype'); + } else { + //as a last resort we could put the page type pattern in the select box + //however this causes mod-data-view to be added if the only option available is mod-data-* + // so we are just showing a warning to users about their prev setting being reset + $displaypagetypewarning = true; } } From 9d2c424d832404155fb03bd244813ecfdfddb9e0 Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Sat, 3 Dec 2011 14:11:26 +0100 Subject: [PATCH 3/6] MDL-30340 Block stickiness computation improvements This is an attempt to clean up and fix the computation of the block stickiness. At first, the page pattern can't be ignored because the user may want to currently try to limit the page pattern. Second, the site-index pattern can be forced only if the user selected 'Front page only' as the page context. --- lib/blocklib.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/blocklib.php b/lib/blocklib.php index b39badb51a2..a25b5d4b515 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -1235,20 +1235,26 @@ class block_manager { // 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)); + // Should the block be sticky + if ($data->bui_contexts == BUI_CONTEXTS_ENTIRE_SITE or $data->bui_contexts == BUI_CONTEXTS_FRONTPAGE_SUBS) { + $bi->showinsubcontexts = true; + } else { + $bi->showinsubcontexts = false; + } // 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 = '*'; } 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'; + if ($data->bui_contexts == BUI_CONTEXTS_FRONTPAGE_ONLY) { + // If the front page only is specified, the page type setting is ignored + // as explicitely set to site-index + $bi->pagetypepattern = 'site-index'; + } } } } From d4e71a4eed3cd72c0954ba5b117a7ae2725639ef Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Mon, 5 Dec 2011 00:48:08 +0100 Subject: [PATCH 4/6] MDL-30340 blocks - hide some redundant pagetypepattern options at front page The dual front-page/system-wide form to edit blocks can be reduced asuming that, always: A) system sets the context to system, recursively and with page-type set to "*" B) frontpage only sets the context to site-course, non-recursively and with page-type set to "site-index" C) frontpage all added sets the context to site-course, recursively and with paget-type set to "*" And that is the change that this patch provides, by: 1) detecting properly if we are editing blocks @ protpage 2) passing that information to the form data processor 3) setting parentcontextid, showinsubcontexts and pagetypepattern following the A, B, C immutables above. Finally, and affecting some other system-wide pages, there are cases (my, user templates...) having only one possible pagetypepattern, and it looks badly if the page has subpages, so for those system-wide cases we are showing exceptionaly the pagetypepattern statically. This will be revisited once MDL-30574 is decided and implemented, although perhaps it's ok to leave it as default to places with only one pagetypepattern available. --- blocks/edit_form.php | 97 +++++++++++++++++++++++++++++++------------- lib/blocklib.php | 58 ++++++++++++++++---------- 2 files changed, 105 insertions(+), 50 deletions(-) diff --git a/blocks/edit_form.php b/blocks/edit_form.php index 1e1cf8ca3e8..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,19 +146,21 @@ class block_edit_form extends moodleform { $mform->addElement('select', 'bui_contexts', get_string('contexts', 'block'), $contextoptions); } - // Generate pagetype patterns by callbacks - $displaypagetypewarning = false; - $pagetypelist = generate_page_type_patterns($this->page->pagetype, $parentcontext, $this->page->context); - 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); - if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) { - $pagetypelist[$this->block->instance->pagetypepattern] = get_string($pagetypestringname, 'pagetype'); - } else { - //as a last resort we could put the page type pattern in the select box - //however this causes mod-data-view to be added if the only option available is mod-data-* - // so we are just showing a warning to users about their prev setting being reset - $displaypagetypewarning = true; + // 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); + if (get_string_manager()->string_exists($pagetypestringname, 'pagetype')) { + $pagetypelist[$this->block->instance->pagetypepattern] = get_string($pagetypestringname, 'pagetype'); + } else { + //as a last resort we could put the page type pattern in the select box + //however this causes mod-data-view to be added if the only option available is mod-data-* + // so we are just showing a warning to users about their prev setting being reset + $displaypagetypewarning = true; + } } } @@ -152,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 a25b5d4b515..814dc36676c 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -1227,35 +1227,49 @@ 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; - // Should the block be sticky - if ($data->bui_contexts == BUI_CONTEXTS_ENTIRE_SITE or $data->bui_contexts == BUI_CONTEXTS_FRONTPAGE_SUBS) { - $bi->showinsubcontexts = true; - } else { - $bi->showinsubcontexts = false; - } - - // 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; - - } 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; - if ($data->bui_contexts == BUI_CONTEXTS_FRONTPAGE_ONLY) { - // If the front page only is specified, the page type setting is ignored - // as explicitely set to site-index - $bi->pagetypepattern = 'site-index'; + // Perform some exceptions for system/frontpage data. MDL-30340 + switch ($data->bui_contexts) { + case BUI_CONTEXTS_ENTIRE_SITE: + // it's a system-wide block. 100% guaranteed, set parentcontextid and showinsubcontexts + $bi->parentcontextid = $systemcontext->id; + $bi->showinsubcontexts = true; + // and also, if it's one edition @ frontpage, set its pagetypepattern to '*' + // it already arrives that way from the form, but just re-enforce it here + if ($data->bui_editingatfrontpage) { + $bi->pagetypepattern = '*'; } - } + break; + case BUI_CONTEXTS_FRONTPAGE_SUBS: + // it's a frontpage-wide (with subcontexts) block. 100% guaranteed, set parentcontextid and showinsubcontexts + $bi->parentcontextid = $frontpagecontext->id; + $bi->showinsubcontexts = true; + // and also, if it's one edition @ frontpage, set its pagetypepattern to '*' + // it already arrives that way from the form, but just re-enforce it here + if ($data->bui_editingatfrontpage) { + $bi->pagetypepattern = '*'; + } + break; + case BUI_CONTEXTS_FRONTPAGE_ONLY: + // it's a frontpage-only (no subcontexts) block. 100% guaranteed, set parentcontextid and showinsubcontexts + $bi->parentcontextid = $frontpagecontext->id; + $bi->showinsubcontexts = false; + // and also, if it's one edition @ frontpage, set its pagetypepattern to 'site-index' + // it originally comes as '*' from the form, here we change that in proviosion of + // future 'site-index' pages + if ($data->bui_editingatfrontpage) { + $bi->pagetypepattern = 'site-index'; + } + break; } } From 49ae1fdceaa49964aeec0a31f898c0e30a3e6eff Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Mon, 5 Dec 2011 01:14:19 +0100 Subject: [PATCH 5/6] MDL-30340 blocks - incorrect option in user profile template and missing lang string in my template --- lib/blocklib.php | 2 +- user/lib.php | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/blocklib.php b/lib/blocklib.php index 814dc36676c..7662ccea360 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -1717,7 +1717,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')); } From 192a3380f15ef52dd58a1bc848b6f5e93a369b74 Mon Sep 17 00:00:00 2001 From: Martin Dougiamas Date: Mon, 5 Dec 2011 10:28:54 +0100 Subject: [PATCH 6/6] MDL-30340 blocks - fix/restrict frontpage dark magic application It seems that there are some overlapping constants in the blocks subsystem that were causing some code, initially planned only to frontpage, to be executed in other contexts. This commit moves the bui_editingatfrontpage condition as root condition. --- lib/blocklib.php | 54 +++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/lib/blocklib.php b/lib/blocklib.php index 7662ccea360..971a4e65fc7 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -1237,39 +1237,33 @@ class block_manager { // Explicitly set the default context $bi->parentcontextid = $parentcontext->id; - // Perform some exceptions for system/frontpage data. MDL-30340 - switch ($data->bui_contexts) { - case BUI_CONTEXTS_ENTIRE_SITE: - // it's a system-wide block. 100% guaranteed, set parentcontextid and showinsubcontexts - $bi->parentcontextid = $systemcontext->id; - $bi->showinsubcontexts = true; - // and also, if it's one edition @ frontpage, set its pagetypepattern to '*' - // it already arrives that way from the form, but just re-enforce it here - if ($data->bui_editingatfrontpage) { + if ($data->bui_editingatfrontpage) { // The block is being edited on the front page + + // The interface here is a special case because the pagetype pattern is + // totally derived from the context menu. Here are the excpetions. MDL-30340 + + 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: - // it's a frontpage-wide (with subcontexts) block. 100% guaranteed, set parentcontextid and showinsubcontexts - $bi->parentcontextid = $frontpagecontext->id; - $bi->showinsubcontexts = true; - // and also, if it's one edition @ frontpage, set its pagetypepattern to '*' - // it already arrives that way from the form, but just re-enforce it here - if ($data->bui_editingatfrontpage) { + 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: - // it's a frontpage-only (no subcontexts) block. 100% guaranteed, set parentcontextid and showinsubcontexts - $bi->parentcontextid = $frontpagecontext->id; - $bi->showinsubcontexts = false; - // and also, if it's one edition @ frontpage, set its pagetypepattern to 'site-index' - // it originally comes as '*' from the form, here we change that in proviosion of - // future 'site-index' pages - if ($data->bui_editingatfrontpage) { + 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'; - } - break; + // 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; + } } }