]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-13459 Didn't use the patch, which puts its finger in too many pies. Instead just...
authornicolasconnault <nicolasconnault>
Fri, 15 Feb 2008 16:40:45 +0000 (16:40 +0000)
committernicolasconnault <nicolasconnault>
Fri, 15 Feb 2008 16:40:45 +0000 (16:40 +0000)
lib/accesslib.php
lib/weblib.php

index 2f10620d165734d287b9421d6094c479fcb41ec9..aa74b3f7f9bafe7ed22fd1118a6c886560b961e3 100755 (executable)
@@ -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
index 03a11225e5336e483b3a2c7fcfc1f5db11d58e34..8660b4bcc03ea9baca0fedf4c6d52e466119937f 100644 (file)
@@ -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