From 14fbb4880a878affabd017c88da461dbc2a124ef Mon Sep 17 00:00:00 2001 From: PMKuipers Date: Thu, 17 Aug 2023 07:51:04 +0200 Subject: [PATCH] Primary navigation now should show view and management links if the user has the capabilities in any category --- classes/local/helpers/webservicehelper.php | 37 ++++++++++++++++++++++ lib.php | 8 +++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/classes/local/helpers/webservicehelper.php b/classes/local/helpers/webservicehelper.php index 8e40687..96f114a 100644 --- a/classes/local/helpers/webservicehelper.php +++ b/classes/local/helpers/webservicehelper.php @@ -13,6 +13,7 @@ class webservicehelper { * @param array|string $capability One or more capabilities to be tested OR wise (if one capability is given, the function passes) * @param \context $context The context in which to check for the capability. * @throws \webservice_access_exception If none of the capabilities provided are given to the current user + * @return boolean */ public static function has_capabilities($capability,$context){ @@ -21,6 +22,7 @@ class webservicehelper { } if(is_array($capability)){ + //TODO: replace this by accesslib function \has_any_capability() foreach($capability as $cap){ if(has_capability($cap,$context)){ return true; @@ -32,6 +34,41 @@ class webservicehelper { } } + /** + * Test if the current user has a certain capability in any of the categories they have access to + * @param string $capability The capability to scan for in the categories + * @param \core_course_category $parent The parent category to use as a scanning base. Used in recursing Leave empty if calling this function + * @return boolean + */ + public static function has_capability_in_any_category($capability,\core_course_category $parent=null){ + + // List the categories in which the user has a specific capability + $list = []; + // initialize parent if needed + if($parent == null){ + $parent = \core_course_category::user_top(); + if(has_capability($capability,$parent->get_context())){ + $list[] = $parent; + } + } + + $children = $parent->get_children(); + foreach($children as $child){ + // Check if we should add this category + if(has_capability($capability,$child->get_context())){ + return true; + } else { + if($child->get_children_count() > 0){ + if(self::has_capability_in_any_category($capability,$child)){ + return true; + } + } + } + } + + return false; + } + /** * Test for capability in the given context for the current user and throw a \webservice_access_exception if not * @param array|string $capability One or more capabilities to be tested OR wise (if one capability is given, the function passes) diff --git a/lib.php b/lib.php index 5f29854..e519a8b 100644 --- a/lib.php +++ b/lib.php @@ -1,6 +1,7 @@ dirroot.'/course/modlib.php'); +use local_treestudyplan\local\helpers\webservicehelper; use \local_treestudyplan\studyplan; defined('MOODLE_INTERNAL') || die(); @@ -67,7 +68,8 @@ function local_treestudyplan_extend_navigation(global_navigation $navigation) { else { $hide_primary_hrefs[] = "/local/treestudyplan/myreport.php"; } - if(has_capability('local/treestudyplan:viewuserreports',context_system::instance())) + if( has_capability('local/treestudyplan:viewuserreports',context_system::instance()) + || webservicehelper::has_capability_in_any_category('local/treestudyplan:viewuserreports')) { $node = navigation_node::create( get_string("link_viewplan","local_treestudyplan"), @@ -84,7 +86,9 @@ function local_treestudyplan_extend_navigation(global_navigation $navigation) { else { $hide_primary_hrefs[] = "/local/treestudyplan/view-plan.php"; } - if(has_capability('local/treestudyplan:editstudyplan',context_system::instance())) + if( has_capability('local/treestudyplan:editstudyplan',context_system::instance()) + || webservicehelper::has_capability_in_any_category('local/treestudyplan:editstudyplan') + ) { $node = navigation_node::create( get_string("cfg_plans","local_treestudyplan"),