Merge branch 'MDL-57614-311-4' of git://github.com/mihailges/moodle into MOODLE_311_STABLE

This commit is contained in:
Andrew Nicols
2020-12-02 15:12:28 +08:00
8 changed files with 248 additions and 52 deletions
+118
View File
@@ -0,0 +1,118 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This is the external method used for fetching the addable blocks in a given page.
*
* @package core_block
* @since Moodle 3.11
* @copyright 2020 Mihail Geshoski <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_block\external;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/externallib.php');
use external_api;
use external_function_parameters;
use external_multiple_structure;
use external_single_structure;
use external_value;
/**
* This is the external method used for fetching the addable blocks in a given page.
*
* @copyright 2020 Mihail Geshoski <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class fetch_addable_blocks extends external_api {
/**
* Describes the parameters for execute.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters(
[
'pagecontextid' => new external_value(PARAM_INT, 'The context ID of the page.'),
'pagetype' => new external_value(PARAM_ALPHAEXT, 'The type of the page.'),
'pagelayout' => new external_value(PARAM_ALPHA, 'The layout of the page.')
]
);
}
/**
* Fetch the addable blocks in a given page.
*
* @param int $pagecontextid The context ID of the page
* @param string $pagetype The type of the page
* @param string $pagelayout The layout of the page
* @return array The blocks list
*/
public static function execute(int $pagecontextid, string $pagetype, string $pagelayout): array {
global $PAGE;
$params = self::validate_parameters(self::execute_parameters(),
[
'pagecontextid' => $pagecontextid,
'pagetype' => $pagetype,
'pagelayout' => $pagelayout
]
);
$context = \context::instance_by_id($params['pagecontextid']);
// Validate the context. This will also set the context in $PAGE.
self::validate_context($context);
// We need to manually set the page layout and page type.
$PAGE->set_pagelayout($params['pagelayout']);
$PAGE->set_pagetype($params['pagetype']);
// Firstly, we need to load all currently existing page blocks to later determine which blocks are addable.
$PAGE->blocks->load_blocks(false);
$PAGE->blocks->create_all_block_instances();
$addableblocks = $PAGE->blocks->get_addable_blocks();
return array_map(function($block) {
return [
'name' => $block->name,
'title' => get_string('pluginname', "block_{$block->name}")
];
}, $addableblocks);
}
/**
* Describes the execute return value.
*
* @return external_multiple_structure
*/
public static function execute_returns(): external_multiple_structure {
return new external_multiple_structure(
new external_single_structure(
[
'name' => new external_value(PARAM_PLUGIN, 'The name of the block.'),
'title' => new external_value(PARAM_RAW, 'The title of the block.'),
]
),
'List of addable blocks in a given page.'
);
}
}
+1 -1
View File
@@ -1,2 +1,2 @@
define ("core/addblockmodal",["jquery","core/modal_factory","core/templates","core/str","core/notification"],function(a,b,c,d,e){return{init:function init(f){var g=a("[data-key=addblock]"),h=f.blocks.map(function(a){return{key:"pluginname",component:"block_"+a}}),i=d.get_strings(h).then(function(a){return a.map(function(a,b){return{name:f.blocks[b],title:a}})}).then(function(a){f.blocks=a;return c.render("core/add_block_body",f)}).fail(e.exception),j=d.get_string("addblock").fail(e.exception);b.create({title:j,body:i,type:"CANCEL"},g)}}});
define ("core/addblockmodal",["exports","core/modal_factory","core/templates","core/str","core/ajax"],function(a,b,c,d,e){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.init=void 0;b=f(b);c=f(c);e=f(e);function f(a){return a&&a.__esModule?a:{default:a}}function g(a,b,c,d,e,f,g){try{var h=a[f](g),i=h.value}catch(a){c(a);return}if(h.done){b(i)}else{Promise.resolve(i).then(d,e)}}function h(a){return function(){var b=this,c=arguments;return new Promise(function(d,e){var i=a.apply(b,c);function f(a){g(i,d,e,f,h,"next",a)}function h(a){g(i,d,e,f,h,"throw",a)}f(void 0)})}}var i={ADD_BLOCK:"[data-key=\"addblock\"]"},j=null,k=function(a,b,c){document.addEventListener("click",function(d){if(d.target.closest(i.ADD_BLOCK)){d.preventDefault();if(j){j.show()}else{l().then(function(d){j=d;var e=m(c,a,b);d.setBody(e);d.show();return e}).catch(function(){j.destroy();j=null})}}})},l=function(){return b.default.create({type:b.default.types.CANCEL,title:(0,d.get_string)("addblock")})},m=function(){var a=h(regeneratorRuntime.mark(function a(b,d,e){var f;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:a.next=2;return n(d,e);case 2:f=a.sent;return a.abrupt("return",c.default.render("core/add_block_body",{blocks:f,url:b}));case 4:case"end":return a.stop();}}},a)}));return function(){return a.apply(this,arguments)}}(),n=function(){var a=h(regeneratorRuntime.mark(function a(b,c){var d;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:d={methodname:"core_block_fetch_addable_blocks",args:{pagecontextid:M.cfg.contextid,pagetype:b,pagelayout:c}};return a.abrupt("return",e.default.call([d])[0]);case 2:case"end":return a.stop();}}},a)}));return function(){return a.apply(this,arguments)}}();a.init=function init(a,b,c){k(a,b,c)}});
//# sourceMappingURL=addblockmodal.min.js.map
File diff suppressed because one or more lines are too long
+106 -41
View File
@@ -22,51 +22,116 @@
* @copyright 2016 Damyon Wiese <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['jquery', 'core/modal_factory', 'core/templates', 'core/str', 'core/notification'],
function($, ModalFactory, Templates, Str, Notification) {
import ModalFactory from 'core/modal_factory';
import Templates from 'core/templates';
import {get_string as getString} from 'core/str';
import Ajax from 'core/ajax';
return /** @alias module:core/addblockmodal */ {
/**
* Global init function for this module.
*
* @method init
* @param {Object} context The template context for rendering this modal body.
*/
init: function(context) {
var addblocklink = $('[data-key=addblock]');
const SELECTORS = {
ADD_BLOCK: '[data-key="addblock"]'
};
// We need the fetch the names of the blocks. It was too much to send in the page.
var titlerequests = context.blocks.map(function(blockName) {
return {
key: 'pluginname',
component: 'block_' + blockName,
};
});
let addBlockModal = null;
var bodyPromise = Str.get_strings(titlerequests)
.then(function(titles) {
return titles.map(function(title, index) {
return {
name: context.blocks[index],
title: title,
};
/**
* Register related event listeners.
*
* @method registerListenerEvents
* @param {String} pageType The type of the page
* @param {String} pageLayout The layout of the page
* @param {String} addBlockUrl The add block URL
*/
const registerListenerEvents = (pageType, pageLayout, addBlockUrl) => {
document.addEventListener('click', e => {
if (e.target.closest(SELECTORS.ADD_BLOCK)) {
e.preventDefault();
if (addBlockModal) { // The 'add block' modal has been already created.
// Display the 'add block' modal.
addBlockModal.show();
} else {
buildAddBlockModal()
.then(modal => {
addBlockModal = modal;
const modalBody = renderBlocks(addBlockUrl, pageType, pageLayout);
modal.setBody(modalBody);
modal.show();
return modalBody;
})
.catch(() => {
addBlockModal.destroy();
// Unset the addBlockModal in case this is a transient error and it goes away on a relaunch.
addBlockModal = null;
});
})
.then(function(blocks) {
context.blocks = blocks;
return Templates.render('core/add_block_body', context);
})
.fail(Notification.exception);
var titlePromise = Str.get_string('addblock')
.fail(Notification.exception);
ModalFactory.create({
title: titlePromise,
body: bodyPromise,
type: 'CANCEL',
}, addblocklink);
}
}
});
};
/**
* Method that creates the 'add block' modal.
*
* @method buildAddBlockModal
* @return {Promise} The modal promise (modal's body will be rendered later).
*/
const buildAddBlockModal = () => {
return ModalFactory.create({
type: ModalFactory.types.CANCEL,
title: getString('addblock')
});
};
/**
* Method that renders the list of available blocks.
*
* @method renderBlocks
* @param {String} addBlockUrl The add block URL
* @param {String} pageType The type of the page
* @param {String} pageLayout The layout of the page
* @return {Promise}
*/
const renderBlocks = async(addBlockUrl, pageType, pageLayout) => {
// Fetch all addable blocks in the given page.
const blocks = await getAddableBlocks(pageType, pageLayout);
return Templates.render('core/add_block_body', {
blocks: blocks,
url: addBlockUrl
});
};
/**
* Method that fetches all addable blocks in a given page.
*
* @method getAddableBlocks
* @param {String} pageType The type of the page
* @param {String} pageLayout The layout of the page
* @return {Promise}
*/
const getAddableBlocks = async(pageType, pageLayout) => {
const request = {
methodname: 'core_block_fetch_addable_blocks',
args: {
pagecontextid: M.cfg.contextid,
pagetype: pageType,
pagelayout: pageLayout
},
};
});
return Ajax.call([request])[0];
};
/**
* Set up the actions.
*
* @method init
* @param {String} pageType The type of the page
* @param {String} pageLayout The layout of the page
* @param {String} addBlockUrl The add block URL
*/
export const init = (pageType, pageLayout, addBlockUrl) => {
registerListenerEvents(pageType, pageLayout, addBlockUrl);
};
+10
View File
@@ -2609,6 +2609,16 @@ $functions = array(
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_block_fetch_addable_blocks' => array(
'classname' => 'core_block\external\fetch_addable_blocks',
'methodname' => 'execute',
'description' => 'Returns all addable blocks in a given page.',
'type' => 'read',
'capabilities' => 'moodle/site:manageblocks',
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
// Filters functions.
'core_filters_get_available_in_context' => array(
'classname' => 'core_filters\external',
+6 -8
View File
@@ -4065,8 +4065,7 @@ class flat_navigation extends navigation_node_collection {
// Add-a-block in editing mode.
if (isset($this->page->theme->addblockposition) &&
$this->page->theme->addblockposition == BLOCK_ADDBLOCK_POSITION_FLATNAV &&
$PAGE->user_is_editing() && $PAGE->user_can_edit_blocks() &&
($addable = $PAGE->blocks->get_addable_blocks())) {
$PAGE->user_is_editing() && $PAGE->user_can_edit_blocks()) {
$url = new moodle_url($PAGE->url, ['bui_addblock' => '', 'sesskey' => sesskey()]);
$addablock = navigation_node::create(get_string('addblock'), $url);
$flat = new flat_navigation_node($addablock, 0);
@@ -4074,12 +4073,11 @@ class flat_navigation extends navigation_node_collection {
$flat->key = 'addblock';
$flat->icon = new pix_icon('i/addblock', '');
$this->add($flat);
$blocks = [];
foreach ($addable as $block) {
$blocks[] = $block->name;
}
$params = array('blocks' => $blocks, 'url' => '?' . $url->get_query_string(false));
$PAGE->requires->js_call_amd('core/addblockmodal', 'init', array($params));
$addblockurl = "?{$url->get_query_string(false)}";
$PAGE->requires->js_call_amd('core/addblockmodal', 'init',
[$PAGE->pagetype, $PAGE->pagelayout, $addblockurl]);
}
}
+5
View File
@@ -34,4 +34,9 @@
{{#blocks}}
<a href="{{url}}&amp;bui_addblock={{name}}" class="list-group-item list-group-item-action">{{title}}</a>
{{/blocks}}
{{^blocks}}
<div class="alert alert-primary" role="alert">
{{#str}} noblockstoaddhere {{/str}}
</div>
{{/blocks}}
</div>
+1 -1
View File
@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2020112700.00; // 20201109 = branching date YYYYMMDD - do not modify!
$version = 2020112700.01; // 20201109 = branching date YYYYMMDD - do not modify!
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '3.11dev (Build: 20201127)';// Human-friendly version name