Primary navigation now should show view and management links if the user has the capabilities in any category

This commit is contained in:
PMKuipers 2023-08-17 07:51:04 +02:00
parent afe43d5678
commit 14fbb4880a
2 changed files with 43 additions and 2 deletions

View File

@ -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 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. * @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 * @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){ public static function has_capabilities($capability,$context){
@ -21,6 +22,7 @@ class webservicehelper {
} }
if(is_array($capability)){ if(is_array($capability)){
//TODO: replace this by accesslib function \has_any_capability()
foreach($capability as $cap){ foreach($capability as $cap){
if(has_capability($cap,$context)){ if(has_capability($cap,$context)){
return true; 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 * 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) * @param array|string $capability One or more capabilities to be tested OR wise (if one capability is given, the function passes)

View File

@ -1,6 +1,7 @@
<?php <?php
require_once($CFG->dirroot.'/course/modlib.php'); require_once($CFG->dirroot.'/course/modlib.php');
use local_treestudyplan\local\helpers\webservicehelper;
use \local_treestudyplan\studyplan; use \local_treestudyplan\studyplan;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
@ -67,7 +68,8 @@ function local_treestudyplan_extend_navigation(global_navigation $navigation) {
else { else {
$hide_primary_hrefs[] = "/local/treestudyplan/myreport.php"; $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( $node = navigation_node::create(
get_string("link_viewplan","local_treestudyplan"), get_string("link_viewplan","local_treestudyplan"),
@ -84,7 +86,9 @@ function local_treestudyplan_extend_navigation(global_navigation $navigation) {
else { else {
$hide_primary_hrefs[] = "/local/treestudyplan/view-plan.php"; $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( $node = navigation_node::create(
get_string("cfg_plans","local_treestudyplan"), get_string("cfg_plans","local_treestudyplan"),