Commit 844f4a7f authored by Paul Holden's avatar Paul Holden
Browse files

MDL-72716 blocks: fix external fetching of available blocks.

Pass `$PAGE->subpage` value in AJAX request, which is required on
some pages (e.g. user dashboard) for loading existing blocks.
parent f8eb3637
...@@ -54,7 +54,8 @@ class fetch_addable_blocks extends external_api { ...@@ -54,7 +54,8 @@ class fetch_addable_blocks extends external_api {
[ [
'pagecontextid' => new external_value(PARAM_INT, 'The context ID of the page.'), 'pagecontextid' => new external_value(PARAM_INT, 'The context ID of the page.'),
'pagetype' => new external_value(PARAM_ALPHANUMEXT, 'The type of the page.'), 'pagetype' => new external_value(PARAM_ALPHANUMEXT, 'The type of the page.'),
'pagelayout' => new external_value(PARAM_ALPHA, 'The layout of the page.') 'pagelayout' => new external_value(PARAM_ALPHA, 'The layout of the page.'),
'subpage' => new external_value(PARAM_TEXT, 'The subpage identifier', VALUE_DEFAULT, ''),
] ]
); );
} }
...@@ -65,16 +66,18 @@ class fetch_addable_blocks extends external_api { ...@@ -65,16 +66,18 @@ class fetch_addable_blocks extends external_api {
* @param int $pagecontextid The context ID of the page * @param int $pagecontextid The context ID of the page
* @param string $pagetype The type of the page * @param string $pagetype The type of the page
* @param string $pagelayout The layout of the page * @param string $pagelayout The layout of the page
* @param string $subpage The subpage identifier
* @return array The blocks list * @return array The blocks list
*/ */
public static function execute(int $pagecontextid, string $pagetype, string $pagelayout): array { public static function execute(int $pagecontextid, string $pagetype, string $pagelayout, string $subpage = ''): array {
global $PAGE; global $PAGE;
$params = self::validate_parameters(self::execute_parameters(), $params = self::validate_parameters(self::execute_parameters(),
[ [
'pagecontextid' => $pagecontextid, 'pagecontextid' => $pagecontextid,
'pagetype' => $pagetype, 'pagetype' => $pagetype,
'pagelayout' => $pagelayout 'pagelayout' => $pagelayout,
'subpage' => $subpage,
] ]
); );
...@@ -85,6 +88,8 @@ class fetch_addable_blocks extends external_api { ...@@ -85,6 +88,8 @@ class fetch_addable_blocks extends external_api {
// We need to manually set the page layout and page type. // We need to manually set the page layout and page type.
$PAGE->set_pagelayout($params['pagelayout']); $PAGE->set_pagelayout($params['pagelayout']);
$PAGE->set_pagetype($params['pagetype']); $PAGE->set_pagetype($params['pagetype']);
$PAGE->set_subpage($params['subpage']);
// Firstly, we need to load all currently existing page blocks to later determine which blocks are addable. // Firstly, we need to load all currently existing page blocks to later determine which blocks are addable.
$PAGE->blocks->load_blocks(false); $PAGE->blocks->load_blocks(false);
$PAGE->blocks->create_all_block_instances(); $PAGE->blocks->create_all_block_instances();
......
...@@ -6,6 +6,8 @@ information provided here is intended especially for developers. ...@@ -6,6 +6,8 @@ information provided here is intended especially for developers.
* Block block_quiz_results has been completely removed from core. * Block block_quiz_results has been completely removed from core.
The Quiz results block is hidden by default since Moodle 2.9. It is recommended to use the Activity results block instead, which works with any type of activity (not just quizzes). The Quiz results block is hidden by default since Moodle 2.9. It is recommended to use the Activity results block instead, which works with any type of activity (not just quizzes).
* External function core_block::get_dashboard_blocks has a new parameter to indicate if you want to receive the block on the my/courses page. * External function core_block::get_dashboard_blocks has a new parameter to indicate if you want to receive the block on the my/courses page.
* The `core_block_fetch_addable_blocks` external method accepts an optional `subpage` parameter, in order to correctly
calculate available blocks for pages that use this property (e.g. the user dashboard)
=== 3.8 === === 3.8 ===
* Block block_community is no longer a part of core. * Block block_community is no longer a part of core.
......
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
...@@ -40,8 +40,9 @@ let listenerEventsRegistered = false; ...@@ -40,8 +40,9 @@ let listenerEventsRegistered = false;
* @param {String} pageType The type of the page * @param {String} pageType The type of the page
* @param {String} pageLayout The layout of the page * @param {String} pageLayout The layout of the page
* @param {String|null} addBlockUrl The add block URL * @param {String|null} addBlockUrl The add block URL
* @param {String} subPage The subpage identifier
*/ */
const registerListenerEvents = (pageType, pageLayout, addBlockUrl) => { const registerListenerEvents = (pageType, pageLayout, addBlockUrl, subPage) => {
document.addEventListener('click', e => { document.addEventListener('click', e => {
const addBlock = e.target.closest(SELECTORS.ADD_BLOCK); const addBlock = e.target.closest(SELECTORS.ADD_BLOCK);
...@@ -54,7 +55,7 @@ const registerListenerEvents = (pageType, pageLayout, addBlockUrl) => { ...@@ -54,7 +55,7 @@ const registerListenerEvents = (pageType, pageLayout, addBlockUrl) => {
buildAddBlockModal() buildAddBlockModal()
.then(modal => { .then(modal => {
addBlockModal = modal; addBlockModal = modal;
const modalBody = renderBlocks(addBlockModalUrl, pageType, pageLayout); const modalBody = renderBlocks(addBlockModalUrl, pageType, pageLayout, subPage);
modal.setBody(modalBody); modal.setBody(modalBody);
modal.show(); modal.show();
...@@ -87,11 +88,12 @@ const buildAddBlockModal = () => { ...@@ -87,11 +88,12 @@ const buildAddBlockModal = () => {
* @param {String} addBlockUrl The add block URL * @param {String} addBlockUrl The add block URL
* @param {String} pageType The type of the page * @param {String} pageType The type of the page
* @param {String} pageLayout The layout of the page * @param {String} pageLayout The layout of the page
* @param {String} subPage The subpage identifier
* @return {Promise} * @return {Promise}
*/ */
const renderBlocks = async(addBlockUrl, pageType, pageLayout) => { const renderBlocks = async(addBlockUrl, pageType, pageLayout, subPage) => {
// Fetch all addable blocks in the given page. // Fetch all addable blocks in the given page.
const blocks = await getAddableBlocks(pageType, pageLayout); const blocks = await getAddableBlocks(pageType, pageLayout, subPage);
return Templates.render('core/add_block_body', { return Templates.render('core/add_block_body', {
blocks: blocks, blocks: blocks,
...@@ -105,15 +107,17 @@ const renderBlocks = async(addBlockUrl, pageType, pageLayout) => { ...@@ -105,15 +107,17 @@ const renderBlocks = async(addBlockUrl, pageType, pageLayout) => {
* @method getAddableBlocks * @method getAddableBlocks
* @param {String} pageType The type of the page * @param {String} pageType The type of the page
* @param {String} pageLayout The layout of the page * @param {String} pageLayout The layout of the page
* @param {String} subPage The subpage identifier
* @return {Promise} * @return {Promise}
*/ */
const getAddableBlocks = async(pageType, pageLayout) => { const getAddableBlocks = async(pageType, pageLayout, subPage) => {
const request = { const request = {
methodname: 'core_block_fetch_addable_blocks', methodname: 'core_block_fetch_addable_blocks',
args: { args: {
pagecontextid: M.cfg.contextid, pagecontextid: M.cfg.contextid,
pagetype: pageType, pagetype: pageType,
pagelayout: pageLayout pagelayout: pageLayout,
subpage: subPage,
}, },
}; };
...@@ -127,10 +131,11 @@ const getAddableBlocks = async(pageType, pageLayout) => { ...@@ -127,10 +131,11 @@ const getAddableBlocks = async(pageType, pageLayout) => {
* @param {String} pageType The type of the page * @param {String} pageType The type of the page
* @param {String} pageLayout The layout of the page * @param {String} pageLayout The layout of the page
* @param {String|null} addBlockUrl The add block URL * @param {String|null} addBlockUrl The add block URL
* @param {String} subPage The subpage identifier
*/ */
export const init = (pageType, pageLayout, addBlockUrl = null) => { export const init = (pageType, pageLayout, addBlockUrl = null, subPage = '') => {
if (!listenerEventsRegistered) { if (!listenerEventsRegistered) {
registerListenerEvents(pageType, pageLayout, addBlockUrl); registerListenerEvents(pageType, pageLayout, addBlockUrl, subPage);
listenerEventsRegistered = true; listenerEventsRegistered = true;
} }
}; };
...@@ -4973,6 +4973,7 @@ EOD; ...@@ -4973,6 +4973,7 @@ EOD;
'escapedlink' => "?{$url->get_query_string(false)}", 'escapedlink' => "?{$url->get_query_string(false)}",
'pageType' => $this->page->pagetype, 'pageType' => $this->page->pagetype,
'pageLayout' => $this->page->pagelayout, 'pageLayout' => $this->page->pagelayout,
'subPage' => $this->page->subpage,
] ]
); );
} }
......
...@@ -24,7 +24,8 @@ ...@@ -24,7 +24,8 @@
"link" : "/my/index.php?bui_addblock&bui_blockregion=content&sesskey=M3mes", "link" : "/my/index.php?bui_addblock&bui_blockregion=content&sesskey=M3mes",
"escapedlink" : "?bui_addblock&bui_blockregion=content&sesskey=M3mes", "escapedlink" : "?bui_addblock&bui_blockregion=content&sesskey=M3mes",
"pageType" : "my-index", "pageType" : "my-index",
"pageLayout" : "mydashboard" "pageLayout" : "mydashboard",
"subPage": "15"
} }
}} }}
...@@ -37,6 +38,6 @@ ...@@ -37,6 +38,6 @@
{{#js}} {{#js}}
// Initialise the JS for the modal window which displays the blocks available to add. // Initialise the JS for the modal window which displays the blocks available to add.
require(['core/addblockmodal'], function(addBlockModal) { require(['core/addblockmodal'], function(addBlockModal) {
addBlockModal.init('{{pageType}}', '{{pageLayout}}'); addBlockModal.init('{{pageType}}', '{{pageLayout}}', null, '{{subPage}}');
}); });
{{/js}} {{/js}}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment