diff --git a/my/lib.php b/my/lib.php index 1ed7676ceed..b20b1652a06 100644 --- a/my/lib.php +++ b/my/lib.php @@ -109,9 +109,11 @@ function my_copy_page( $blockinstances = $DB->get_records('block_instances', array('parentcontextid' => $systemcontext->id, 'pagetypepattern' => $pagetype, 'subpagepattern' => $systempage->id)); + $roles = get_all_roles(); $newblockinstanceids = []; foreach ($blockinstances as $instance) { $originalid = $instance->id; + $originalcontext = context_block::instance($originalid); unset($instance->id); $instance->parentcontextid = $usercontext->id; $instance->subpagepattern = $page->id; @@ -126,6 +128,22 @@ function my_copy_page( instance: $originalid to new block instance: $instance->id for block: $instance->blockname", DEBUG_DEVELOPER); } + // Check if there are any overrides on this block instance. + // We check against all roles, not just roles assigned to the user. + // This is so any overrides that are applied to the system default page + // will be applied to the user's page as well, even if their role assignment changes in the future. + foreach ($roles as $role) { + $rolecapabilities = get_capabilities_from_role_on_context($role, $originalcontext); + // If there are overrides, then apply them to the new block instance. + foreach ($rolecapabilities as $rolecapability) { + role_change_permission( + $rolecapability->roleid, + $blockcontext, + $rolecapability->capability, + $rolecapability->permission + ); + } + } } // Clone block position overrides.