MDL-39189 badges: Allow users with multiple roles choose their role when awarding a badge

This commit is contained in:
Yuliya Bozhko
2013-05-10 08:33:45 +12:00
parent cf5a3296c4
commit 71fd443b34
2 changed files with 32 additions and 13 deletions
+31 -12
View File
@@ -30,6 +30,7 @@ require_once($CFG->dirroot . '/badges/lib/awardlib.php');
$badgeid = required_param('id', PARAM_INT);
$role = optional_param('role', 0, PARAM_INT);
$award = optional_param('award', false, PARAM_BOOL);
require_login();
@@ -44,6 +45,9 @@ $isadmin = is_siteadmin($USER);
$navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
if ($badge->type == BADGE_TYPE_COURSE) {
if (empty($CFG->badges_allowcoursebadges)) {
print_error('coursebadgesdisabled', 'badges');
}
require_login($badge->courseid);
$navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
}
@@ -72,37 +76,52 @@ if (!$badge->is_active()) {
$output = $PAGE->get_renderer('core', 'badges');
// Roles that can award this badge.
$accepted_roles = array_keys($badge->criteria[BADGE_CRITERIA_TYPE_MANUAL]->params);
$acceptedroles = array_keys($badge->criteria[BADGE_CRITERIA_TYPE_MANUAL]->params);
// If site admin, select a role to award a badge.
if ($isadmin) {
list($usertest, $userparams) = $DB->get_in_or_equal($accepted_roles, SQL_PARAMS_NAMED, 'existing', true);
if (count($acceptedroles) > 1) {
// If there is more than one role that can award a badge, prompt user to make a selection.
// If it is an admin, include all accepted roles, otherwise only the ones that current user has in this context.
if ($isadmin) {
$selection = $acceptedroles;
} else {
// Get all the roles that user has and use the ones required by this badge.
$roles = get_user_roles($context, $USER->id);
$roleids = array_map(create_function('$o', 'return $o->roleid;'), $roles);
$selection = array_intersect($acceptedroles, $roleids);
}
list($usertest, $userparams) = $DB->get_in_or_equal($selection, SQL_PARAMS_NAMED, 'existing', true);
$options = $DB->get_records_sql('SELECT * FROM {role} WHERE id ' . $usertest, $userparams);
foreach ($options as $p) {
$select[$p->id] = role_get_name($p);
}
if (!$role) {
$pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
echo $OUTPUT->header();
echo $OUTPUT->box(get_string('adminaward', 'badges') . $OUTPUT->single_select(new moodle_url($PAGE->url), 'role', $select));
echo $OUTPUT->box(get_string('selectaward', 'badges') . $OUTPUT->single_select(new moodle_url($pageurl), 'role', $select));
echo $OUTPUT->footer();
die();
} else {
$pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
$issuerrole = new stdClass();
$issuerrole->roleid = $role;
$roleselect = get_string('adminaward', 'badges') . $OUTPUT->single_select(new moodle_url($PAGE->url), 'role', $select, $role);
$roleselect = get_string('selectaward', 'badges') . $OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, $role);
}
} else {
// Current user's role.
$roles = get_user_roles($context, $USER->id);
$issuerrole = array_shift($roles);
if (!isset($issuerrole->roleid) || !in_array($issuerrole->roleid, $accepted_roles)) {
// User has to be an admin or the one with the required role.
$users = get_role_users($acceptedroles[0], $context, false, 'u.id', 'u.id ASC');
$usersids = array_keys($users);
if (!$isadmin && !in_array($USER->id, $usersids)) {
echo $OUTPUT->header();
$rlink = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), get_string('recipients', 'badges'));
echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $rlink));
echo $OUTPUT->footer();
die();
} else {
$issuerrole = new stdClass();
$issuerrole->roleid = $acceptedroles[0];
}
}
$options = array(
'badgeid' => $badge->id,
'context' => $context,
@@ -113,7 +132,7 @@ $existingselector = new badge_existing_users_selector('existingrecipients', $opt
$recipientselector = new badge_potential_users_selector('potentialrecipients', $options);
$recipientselector->set_existing_recipients($existingselector->find_users(''));
if (optional_param('award', false, PARAM_BOOL) && data_submitted() && has_capability('moodle/badges:awardbadge', $context)) {
if ($award && data_submitted() && has_capability('moodle/badges:awardbadge', $context)) {
require_sesskey();
$users = $recipientselector->get_selected_users();
foreach ($users as $user) {
@@ -136,7 +155,7 @@ if (optional_param('award', false, PARAM_BOOL) && data_submitted() && has_capabi
echo $OUTPUT->header();
echo $OUTPUT->heading($strrecipients);
if ($isadmin) {
if (count($acceptedroles) > 1) {
echo $OUTPUT->box($roleselect);
}
+1 -1
View File
@@ -34,7 +34,6 @@ $string['addcriteriatext'] = 'To start adding criteria, please select one of the
$string['addcourse'] = 'Add courses';
$string['addcourse_help'] = 'Select all courses that should be added to this badge requirement. Hold CTRL key to select multiple items.';
$string['addtobackpack'] = 'Add to backpack';
$string['adminaward'] = 'As a site administrator, you can select a role to award this badge.';
$string['adminonly'] = 'This page is restricted to Site Administrators only';
$string['after'] = 'after the date of issue.';
$string['aggregationmethod'] = 'Aggregation method';
@@ -312,6 +311,7 @@ $string['reviewconfirm'] = '<p>This action will perform a check if any of the us
<p>Would you like to proceed?</p>';
$string['save'] = 'Save';
$string['searchname'] = 'Search by name';
$string['selectaward'] = 'Please select the role you would like to use to award this badge: ';
$string['selectgroup_end'] = 'Only public collections are shown, <a href="http://backpack.openbadges.org">visit your backpack</a> to create more public collections.';
$string['selectgroup_start'] = 'Select collections from your backpack to display on this site:';
$string['selecting'] = 'With selected badges...';