# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Apply Diff Patch action on respective folder.
# It uses platform neutral UTF-8 encoding.
# Above lines and this line are ignored by the patching process.
Index: moodle/course/moodleform_mod.php
--- moodle/course/moodleform_mod.php Base (1.75)
+++ moodle/course/moodleform_mod.php Locally Modified (Based On 1.75)
@@ -356,17 +356,8 @@
             $permission=CAP_ALLOW;
             $context = get_context_instance(CONTEXT_MODULE, $this->_cm->id);
 
-            $roles1 = get_roles_with_capability('moodle/rating:rate', $permission, $context);
-            $roles2 = get_roles_with_capability('mod/'.$this->_cm->modname.':rate', $permission, $context);
-
-            $rolesthatcanrate = array();
-            foreach($roles1 as $k1=>$v1) {
-                if (array_key_exists($k1, $roles2)) {
-                    $rolesthatcanrate[] = $v1->name;
-                }
-            }
-
-            $mform->addElement('static', 'rolewarning', get_string('rolewarning','rating'), implode(', ', $rolesthatcanrate));
+            $rolenames = get_role_names_with_caps_in_context($context, array('moodle/rating:rate', 'mod/'.$this->_cm->modname.':rate'));
+            $mform->addElement('static', 'rolewarning', get_string('rolewarning','rating'), implode(', ', $rolenames));
             $mform->addHelpButton('rolewarning', 'rolewarning', 'rating');
 
             $mform->addElement('select', 'assessed', get_string('aggregatetype', 'rating') , $rm->get_aggregate_types());
Index: moodle/lib/accesslib.php
--- moodle/lib/accesslib.php Base (1.658)
+++ moodle/lib/accesslib.php Locally Modified (Based On 1.658)
@@ -2448,7 +2448,7 @@
  * Get the roles that have a given capability assigned to it
  * Get the roles that have a given capability assigned to it. This function
  * does not resolve the actual permission of the capability. It just checks
- * for assignment only.
+ * for assignment only. Use get_roles_with_cap_in_context() if resolution is required.
  *
  * @global object
  * @global object
@@ -5891,7 +5891,8 @@
 /**
  * Returns two lists, this can be used to find out if user has capability.
  * Having any needed role and no forbidden role in this context means
- * user has this capability in this context,
+ * user has this capability in this context.
+ * Use get_role_names_with_cap_in_context() if you need role names to display in the UI
  *
  * @param object $context
  * @param string $capability
@@ -5949,6 +5950,68 @@
 }
 
 /**
+ * Returns an array of role IDs that have ALL of the the supplied capabilities
+ * Uses get_roles_with_cap_in_context(). Returns $allowed minus $forbidden
+ *
+ * @param object $context
+ * @param array $capabilities An array of capabilities
+ * @return array of roles with all of the required capabilities
+ */
+function get_roles_with_caps_in_context($context, $capabilities) {
+    $neededarr = array();
+    $forbiddenarr = array();
+    foreach($capabilities as $caprequired) {
+        list($neededarr[], $forbiddenarr[]) = get_roles_with_cap_in_context($context, $caprequired);
+    }
+
+    $rolesthatcanrate = array();
+    if(!empty($neededarr)) {
+        foreach ($neededarr as $needed) {
+            if (empty($rolesthatcanrate)) {
+                $rolesthatcanrate = $needed;
+            } else {
+                //only want roles that have all caps
+                $rolesthatcanrate = array_intersect_key($rolesthatcanrate,$needed);
+            }
+        }
+    }
+    if(!empty($forbiddenarr) && !empty($rolesthatcanrate)) {
+        foreach ($forbiddenarr as $forbidden) {
+           //remove any roles that are forbidden any of the caps
+           $rolesthatcanrate = array_diff($rolesthatcanrate, $forbidden);
+        }
+    }
+    return $rolesthatcanrate;
+}
+
+/**
+ * Returns an array of role names that have ALL of the the supplied capabilities
+ * Uses get_roles_with_caps_in_context(). Returns $allowed minus $forbidden
+ *
+ * @param object $context
+ * @param array $capabilities An array of capabilities
+ * @return array of roles with all of the required capabilities
+ */
+function get_role_names_with_caps_in_context($context, $capabilities) {
+    global $DB;
+
+    $rolesthatcanrate = get_roles_with_caps_in_context($context, $capabilities);
+
+    $allroles = array();
+    $roles = $DB->get_records('role', null, 'sortorder DESC');
+    foreach ($roles as $roleid=>$role) {
+        $allroles[$roleid] = $role->name;
+    }
+
+    $rolenames = array();
+    foreach($rolesthatcanrate as $r) {
+        $rolenames[$r] = $allroles[$r];
+    }
+    $rolenames = role_fix_names($rolenames, $context);
+    return $rolenames;
+}
+
+/**
  * This function verifies the prohibit comes from this context
  * and there are no more prohibits in parent contexts.
  * @param object $context
