From f9cd798c7eba8a204356b84f2b02d0bf6511e8b1 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Tue, 31 Aug 2010 07:59:32 +0000 Subject: [PATCH] rating MDL-23908 improved rating role tips --- course/moodleform_mod.php | 19 +++++------ lang/en/rating.php | 3 +- lib/accesslib.php | 67 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 75 insertions(+), 14 deletions(-) diff --git a/course/moodleform_mod.php b/course/moodleform_mod.php index 159d57a049a..9875e58ee93 100644 --- a/course/moodleform_mod.php +++ b/course/moodleform_mod.php @@ -354,19 +354,16 @@ abstract class moodleform_mod extends moodleform { $mform->addElement('header', 'modstandardratings', get_string('ratings', 'rating')); $permission=CAP_ALLOW; - $context = get_context_instance(CONTEXT_MODULE, $this->_cm->id); + $rolenamestring = null; + if (!empty($this->_cm)) { + $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; - } + $rolenames = get_role_names_with_caps_in_context($context, array('moodle/rating:rate', 'mod/'.$this->_cm->modname.':rate')); + $rolenamestring = implode(', ', $rolenames); + } else { + $rolenamestring = get_string('capabilitychecknotavailable','rating'); } - - $mform->addElement('static', 'rolewarning', get_string('rolewarning','rating'), implode(', ', $rolesthatcanrate)); + $mform->addElement('static', 'rolewarning', get_string('rolewarning','rating'), $rolenamestring); $mform->addHelpButton('rolewarning', 'rolewarning', 'rating'); $mform->addElement('select', 'assessed', get_string('aggregatetype', 'rating') , $rm->get_aggregate_types()); diff --git a/lang/en/rating.php b/lang/en/rating.php index feb95ef72df..cbbb64bbc75 100644 --- a/lang/en/rating.php +++ b/lang/en/rating.php @@ -34,6 +34,7 @@ $string['aggregatetype_help'] = 'The aggregate type defines how ratings are comb If "No ratings" is selected, then the activity will not appear in the gradebook.'; $string['allowratings'] = 'Allow items to be rated?'; +$string['capabilitychecknotavailable'] = 'Capability check not available until activity is saved'; $string['norate'] = 'Rating of items not allowed!'; $string['noviewanyrate'] = 'You can only look at results for posts that you made'; $string['noviewrate'] = 'You do not have the capability to view post ratings'; @@ -43,4 +44,4 @@ $string['rating'] = 'Rating'; $string['ratingtime'] = 'Restrict ratings to items with dates in this range:'; $string['ratings'] = 'Ratings'; $string['rolewarning'] = 'Roles with permission to rate'; -$string['rolewarning_help'] = 'Users assigned the following roles may rate items. The list of roles may be amended via the permissions link in the settings block.'; +$string['rolewarning_help'] = 'To rate users require the moodle/rating:rate capability and any module specific capabilities. Users assigned the following roles can rate items. The list of roles may be amended via the permissions link in the settings block.'; diff --git a/lib/accesslib.php b/lib/accesslib.php index 40077761b71..9ef676bf362 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -2451,7 +2451,7 @@ function unassign_capability($capability, $roleid, $contextid=NULL) { * 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 @@ -5909,7 +5909,8 @@ function role_cap_duplicate($sourcerole, $targetrole) { /** * 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 @@ -5966,6 +5967,68 @@ function get_roles_with_cap_in_context($context, $capability) { return array($needed, $forbidden); } +/** + * 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.