rating MDL-23908 improved rating role tips

This commit is contained in:
Andrew Davis
2010-08-31 07:59:32 +00:00
parent 2d2746753b
commit f9cd798c7e
3 changed files with 75 additions and 14 deletions
+8 -11
View File
@@ -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());
+2 -1
View File
@@ -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.';
+65 -2
View File
@@ -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.