From e1da77a6857a93f8cd307cd109366cd79d312992 Mon Sep 17 00:00:00 2001 From: David Matamoros Date: Thu, 21 Apr 2022 09:04:36 +0200 Subject: [PATCH] MDL-74317 blocks: Blocks can be added to profile pages - Fixed method user_can_addto to take into account that blocks can also be added to user profile pages --- blocks/moodleblock.class.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) 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; }