MDL-73549 Course: My course page menu improvement

- Introduce core_course_category::get_nearest_editable_subcategory()
 - This function will return the first creatable/manageable category
for current user
 - With this new function, we can fix the issue that the users with
course management or creation permision at category level cannot see
the manage menu on My courses page
This commit is contained in:
Huong Nguyen 2022-03-02 13:16:18 +07:00
parent 1d99ba19a2
commit 481cfdc3f0
6 changed files with 251 additions and 9 deletions

View file

@ -3100,6 +3100,36 @@ class core_course_category implements renderable, cacheable_object, IteratorAggr
return course_request::can_request($this->get_context());
}
/**
* Returns true if the user has all the given permissions.
*
* @param array $permissionstocheck The value can be create, manage or any specific capability.
* @return bool
*/
private function has_capabilities(array $permissionstocheck): bool {
if (empty($permissionstocheck)) {
throw new coding_exception('Invalid permissionstocheck parameter');
}
foreach ($permissionstocheck as $permission) {
if ($permission == 'create') {
if (!$this->can_create_course()) {
return false;
}
} else if ($permission == 'manage') {
if (!$this->has_manage_capability()) {
return false;
}
} else {
// Specific capability.
if (!$this->is_uservisible() || !has_capability($permission, $this->get_context())) {
return false;
}
}
}
return true;
}
/**
* Returns true if the user can approve course requests.
* @return bool
@ -3146,4 +3176,32 @@ class core_course_category implements renderable, cacheable_object, IteratorAggr
}
}
}
/**
* Returns the core_course_category object for the first category that the current user have the permission for the course.
*
* Only returns if it exists and is creatable/manageable to the current user
*
* @param core_course_category $parentcat Parent category to check.
* @param array $permissionstocheck The value can be create, manage or any specific capability.
* @return core_course_category|null
*/
public static function get_nearest_editable_subcategory(core_course_category $parentcat,
array $permissionstocheck): ?core_course_category {
// First, check the parent category.
if ($parentcat->has_capabilities($permissionstocheck)) {
return $parentcat;
}
// Check the child categories.
$subcategoryids = $parentcat->get_all_children_ids();
foreach ($subcategoryids as $subcategoryid) {
$subcategory = static::get($subcategoryid);
if ($subcategory->has_capabilities($permissionstocheck)) {
return $subcategory;
}
}
return null;
}
}