From d423b6dc9af4627e609bf445534d3198b7cf2fed Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Fri, 15 Feb 2008 16:40:45 +0000 Subject: [PATCH] MDL-13459 Didn't use the patch, which puts its finger in too many pies. Instead just implemented a quick-fix, a temporary get_assignable_for_switchrole() function. It only affects the switchroles menu. Merging from MOODLE_19_STABLE --- lib/accesslib.php | 54 ++++++++++++++++++++++++++++++++++++++++++++++- lib/weblib.php | 2 +- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index 2f10620d16..aa74b3f7f9 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -7,7 +7,7 @@ // Moodle - Modular Object-Oriented Dynamic Learning Environment // // http://moodle.org // // // -// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // +// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // // // // This program is free software; you can redistribute it and/or modify // // it under the terms of the GNU General Public License as published by // @@ -4220,6 +4220,58 @@ function get_assignable_roles ($context, $field="name") { return $roles; } +/** + * Gets a list of roles that this user can assign in this context, for the switchrole menu + * + * This is a quick-fix for MDL-13459 until MDL-8312 is sorted out... + * @param object $context + * @param string $field + * @return array + */ +function get_assignable_roles_for_switchrole ($context, $field="name") { + + global $CFG; + + // this users RAs + $ras = get_user_roles($context); + $roleids = array(); + foreach ($ras as $ra) { + $roleids[] = $ra->roleid; + } + unset($ra); + + if (count($roleids)===0) { + return array(); + } + + $roleids = implode(',',$roleids); + + // The subselect scopes the DISTINCT down to + // the role ids - a DISTINCT over the whole of + // the role table is much more expensive on some DBs + $sql = "SELECT r.id, r.$field + FROM {$CFG->prefix}role r + JOIN ( SELECT DISTINCT allowassign as allowedrole + FROM {$CFG->prefix}role_allow_assign raa + WHERE raa.roleid IN ($roleids) ) ar + ON r.id=ar.allowedrole + JOIN mdl_role_capabilities rc ON r.id = rc.roleid + AND rc.capability = 'moodle/course:view' + AND rc.capability != 'moodle/site:doanything' + ORDER BY sortorder ASC"; + + $rs = get_recordset_sql($sql); + $roles = array(); + while ($r = rs_fetch_next_record($rs)) { + $roles[$r->id] = $r->{$field}; + } + rs_close($rs); + foreach ($roles as $roleid => $rolename) { + $roles[$roleid] = strip_tags(format_string($rolename, true)); + } + return $roles; +} + /** * Gets a list of roles that this user can override in this context * @param object $context diff --git a/lib/weblib.php b/lib/weblib.php index 03a11225e5..8660b4bcc0 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -5063,7 +5063,7 @@ function switchroles_form($courseid) { } if (has_capability('moodle/role:switchroles', $context)) { - if (!$roles = get_assignable_roles($context)) { + if (!$roles = get_assignable_roles_for_switchrole($context)) { return ''; // Nothing to show! } // unset default user role - it would not work -- 2.39.5