diff --git a/blocks/moodleblock.class.php b/blocks/moodleblock.class.php
index 3a293cfc69d..3a13521de93 100644
--- a/blocks/moodleblock.class.php
+++ b/blocks/moodleblock.class.php
@@ -597,9 +597,20 @@ class block_base {
* @return boolean
*/
function user_can_addto($page) {
+ global $CFG;
+ require_once($CFG->dirroot . '/user/lib.php');
+
// List of formats this block supports.
$formats = $this->applicable_formats();
+ // Check if user is trying to add blocks to their profile page.
+ $userpagetypes = user_page_type_list($page->pagetype, null, null);
+ if (array_key_exists($page->pagetype, $userpagetypes)) {
+ $capability = 'block/' . $this->name() . ':addinstance';
+ return $this->has_add_block_capability($page, $capability)
+ && has_capability('moodle/user:manageownblocks', $page->context);
+ }
+
// The blocks in My Moodle are a special case and use a different capability.
$mypagetypes = my_page_type_list($page->pagetype); // Get list of possible my page types.
@@ -618,10 +629,12 @@ class block_base {
&& has_capability('moodle/my:manageblocks', $page->context);
}
}
- // Check if this is a block only used on /my.
- unset($formats['my']);
+ // Check if this is a block only used on 'my' or 'user' page types.
+ foreach (array_keys($mypagetypes + $userpagetypes) as $key) {
+ unset($formats[$key]);
+ }
if (empty($formats)) {
- // Block can only be added to /my - return false.
+ // Block can only be added to 'my' or 'user' page types - return false.
return false;
}
diff --git a/editmode.php b/editmode.php
index 36c340d4f98..4d6f452aa47 100644
--- a/editmode.php
+++ b/editmode.php
@@ -34,14 +34,6 @@ require_login();
$context = \context_helper::instance_by_id($contextid);
$PAGE->set_context($context);
-if ($context->id === \context_user::instance($USER->id)->id) {
- $PAGE->set_blocks_editing_capability('moodle/my:manageblocks');
-}
-
-if ($PAGE->user_allowed_editing()) {
- $USER->editing = $setmode;
-} else {
- \core\notification::add(get_string('cannotswitcheditmodeon', 'error'), \core\notification::ERROR);
-}
+$USER->editing = $setmode;
redirect($pageurl);
diff --git a/lib/classes/external/editmode.php b/lib/classes/external/editmode.php
index 06bca19447a..c48ba9d5238 100644
--- a/lib/classes/external/editmode.php
+++ b/lib/classes/external/editmode.php
@@ -14,15 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see .
-/**
- * A web service to load the mapping of moodle pix names to fontawesome icon names.
- *
- * @package core
- * @category external
- * @copyright 2021 Bas Brands
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
namespace core\external;
use external_api;
@@ -54,14 +45,14 @@ class editmode extends external_api {
}
/**
- * Save the image and return any warnings and the new image url
+ * Set the given edit mode
*
- * @param bool $setmode the current edit mode
+ * @param bool $setmode the new edit mode
* @param int $contextid the current page context id
- * @return array the new edit mode.
+ * @return array
*/
public static function change_editmode(bool $setmode, int $contextid): array {
- global $USER, $PAGE;
+ global $USER;
$params = self::validate_parameters(
self::change_editmode_parameters(),
@@ -73,19 +64,10 @@ class editmode extends external_api {
$context = \context_helper::instance_by_id($params['context']);
self::validate_context($context);
- $PAGE->set_context($context);
- if ($context->id === \context_user::instance($USER->id)->id) {
- $PAGE->set_blocks_editing_capability('moodle/my:manageblocks');
- }
+ $USER->editing = $params['setmode'];
- $success = false;
- if ($PAGE->user_allowed_editing()) {
- $USER->editing = $setmode;
- $success = true;
- }
-
- return ['success' => $success];
+ return ['success' => true];
}
/**