MDL-50666 core: Add function get_viewable_roles to set role visibility
This commit is contained in:
committed by
Jun Pataleta
parent
95b7be7f05
commit
a63cd3e2ca
+86
-4
@@ -1984,7 +1984,7 @@ function get_default_capabilities($archetype) {
|
||||
* Return default roles that can be assigned, overridden or switched
|
||||
* by give role archetype.
|
||||
*
|
||||
* @param string $type assign|override|switch
|
||||
* @param string $type assign|override|switch|view
|
||||
* @param string $archetype
|
||||
* @return array of role ids
|
||||
*/
|
||||
@@ -2034,6 +2034,16 @@ function get_default_role_archetype_allows($type, $archetype) {
|
||||
'user' => array(),
|
||||
'frontpage' => array(),
|
||||
),
|
||||
'view' => array(
|
||||
'manager' => array('manager', 'coursecreator', 'editingteacher', 'teacher', 'student', 'guest', 'user', 'frontpage'),
|
||||
'coursecreator' => array('coursecreator', 'editingteacher', 'teacher', 'student'),
|
||||
'editingteacher' => array('coursecreator', 'editingteacher', 'teacher', 'student'),
|
||||
'teacher' => array('coursecreator', 'editingteacher', 'teacher', 'student'),
|
||||
'student' => array('coursecreator', 'editingteacher', 'teacher', 'student'),
|
||||
'guest' => array(),
|
||||
'user' => array(),
|
||||
'frontpage' => array(),
|
||||
),
|
||||
);
|
||||
|
||||
if (!isset($defaults[$type][$archetype])) {
|
||||
@@ -2602,10 +2612,14 @@ function get_user_roles_in_course($userid, $courseid) {
|
||||
$rolestring = '';
|
||||
|
||||
if ($roles = $DB->get_records_sql($sql, $params)) {
|
||||
$rolenames = role_fix_names($roles, $context, ROLENAME_ALIAS, true); // Substitute aliases
|
||||
$viewableroles = get_viewable_roles($context, $userid);
|
||||
|
||||
foreach ($rolenames as $roleid => $rolename) {
|
||||
$rolenames[$roleid] = '<a href="'.$CFG->wwwroot.'/user/index.php?contextid='.$context->id.'&roleid='.$roleid.'">'.$rolename.'</a>';
|
||||
$rolenames = array();
|
||||
foreach ($roles as $roleid => $unused) {
|
||||
if (isset($viewableroles[$roleid])) {
|
||||
$url = new moodle_url('/user/index.php', ['contextid' => $context->id, 'roleid' => $roleid]);
|
||||
$rolenames[] = '<a href="' . $url . '">' . $viewableroles[$roleid] . '</a>';
|
||||
}
|
||||
}
|
||||
$rolestring = implode(',', $rolenames);
|
||||
}
|
||||
@@ -2883,6 +2897,22 @@ function allow_switch($fromroleid, $targetroleid) {
|
||||
$DB->insert_record('role_allow_switch', $record);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a record in the role_allow_view table
|
||||
*
|
||||
* @param int $fromroleid source roleid
|
||||
* @param int $targetroleid target roleid
|
||||
* @return void
|
||||
*/
|
||||
function core_role_set_view_allowed($fromroleid, $targetroleid) {
|
||||
global $DB;
|
||||
|
||||
$record = new stdClass();
|
||||
$record->roleid = $fromroleid;
|
||||
$record->allowview = $targetroleid;
|
||||
$DB->insert_record('role_allow_view', $record);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of roles that this user can assign in this context
|
||||
*
|
||||
@@ -3023,6 +3053,58 @@ function get_switchable_roles(context $context) {
|
||||
return role_fix_names($roles, $context, ROLENAME_ALIAS, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of roles that this user can view in a context
|
||||
*
|
||||
* @param context $context a context.
|
||||
* @param int $userid id of user.
|
||||
* @return array an array $roleid => $rolename.
|
||||
*/
|
||||
function get_viewable_roles(context $context, $userid = null) {
|
||||
global $USER, $DB;
|
||||
|
||||
if ($userid == null) {
|
||||
$userid = $USER->id;
|
||||
}
|
||||
|
||||
$params = array();
|
||||
$extrajoins = '';
|
||||
$extrawhere = '';
|
||||
if (!is_siteadmin()) {
|
||||
// Admins are allowed to view any role.
|
||||
// Others are subject to the additional constraint that the view role must be allowed by
|
||||
// 'role_allow_view' for some role they have assigned in this context or any parent.
|
||||
$contexts = $context->get_parent_context_ids(true);
|
||||
list($insql, $inparams) = $DB->get_in_or_equal($contexts, SQL_PARAMS_NAMED);
|
||||
|
||||
$extrajoins = "JOIN {role_allow_view} ras ON ras.allowview = r.id
|
||||
JOIN {role_assignments} ra ON ra.roleid = ras.roleid";
|
||||
$extrawhere = "WHERE ra.userid = :userid AND ra.contextid $insql";
|
||||
|
||||
$params += $inparams;
|
||||
$params['userid'] = $userid;
|
||||
}
|
||||
|
||||
if ($coursecontext = $context->get_course_context(false)) {
|
||||
$params['coursecontext'] = $coursecontext->id;
|
||||
} else {
|
||||
$params['coursecontext'] = 0; // No course aliases.
|
||||
$coursecontext = null;
|
||||
}
|
||||
|
||||
$query = "
|
||||
SELECT r.id, r.name, r.shortname, rn.name AS coursealias
|
||||
FROM {role} r
|
||||
$extrajoins
|
||||
LEFT JOIN {role_names} rn ON (rn.contextid = :coursecontext AND rn.roleid = r.id)
|
||||
$extrawhere
|
||||
GROUP BY r.id, r.name, r.shortname, rn.name
|
||||
ORDER BY r.sortorder";
|
||||
$roles = $DB->get_records_sql($query, $params);
|
||||
|
||||
return role_fix_names($roles, $context, ROLENAME_ALIAS, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of roles that this user can override in this context.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user