MDL-68062 badges: Allow revoke by every user with the correct role

Co-authored-by: Rajneel Totaram <[email protected]>
This commit is contained in:
Stefan Hanauska
2025-11-04 14:12:35 +01:00
co-authored by Rajneel Totaram
parent a1c5c162b3
commit 824fe6ec83
3 changed files with 33 additions and 21 deletions
+1 -1
View File
@@ -207,7 +207,7 @@ if ($award && data_submitted() && has_capability('moodle/badges:awardbadge', $co
$users = $existingselector->get_selected_users();
foreach ($users as $user) {
if (!process_manual_revoke($user->id, $USER->id, $issuerrole->roleid, $badgeid)) {
if (!process_manual_revoke($user->id, 0, $issuerrole->roleid, $badgeid)) {
echo $OUTPUT->error_text(get_string('error:cannotrevokebadge', 'badges'));
}
}
+16 -16
View File
@@ -290,27 +290,27 @@ function process_manual_award($recipientid, $issuerid, $issuerrole, $badgeid) {
/**
* Manually revoke awarded badges.
*
* @param int $recipientid
* @param int $issuerid
* @param int $issuerrole
* @param int $badgeid
* @param int $recipientid User ID of the recipient
* @param int $issuerid User ID of the issuer (if 0, issuer will be ignored)
* @param int $issuerrole Role of the issuer
* @param int $badgeid ID of the badge
* @return bool
*/
function process_manual_revoke($recipientid, $issuerid, $issuerrole, $badgeid) {
global $DB;
$params = array(
'badgeid' => $badgeid,
'issuerid' => $issuerid,
'issuerrole' => $issuerrole,
'recipientid' => $recipientid
);
$params = [
'badgeid' => $badgeid,
'issuerrole' => $issuerrole,
'recipientid' => $recipientid,
];
if (!empty($issuerid)) {
$params['issuerid'] = $issuerid;
}
if ($DB->record_exists('badge_manual_award', $params)) {
if ($DB->delete_records('badge_manual_award', array('badgeid' => $badgeid,
'issuerid' => $issuerid,
'recipientid' => $recipientid))
&& $DB->delete_records('badge_issued', array('badgeid' => $badgeid,
'userid' => $recipientid))) {
if (
$DB->delete_records('badge_manual_award', $params) &&
$DB->delete_records('badge_issued', ['badgeid' => $badgeid, 'userid' => $recipientid])
) {
// Trigger event, badge revoked.
$badge = new \badge($badgeid);
$eventparams = array(
+16 -4
View File
@@ -11,11 +11,13 @@ Feature: Award badges
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| teacher2 | Teacher | 2 | teacher2@example.com |
| student1 | Student | 1 | student1@example.com |
| student2 | Student | 2 | student2@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| teacher2 | C1 | editingteacher |
| student1 | C1 | student |
| student2 | C1 | student |
And the following "activity" exists:
@@ -371,14 +373,24 @@ Feature: Award badges
And I am on "Course 1" course homepage
And I navigate to "Badges" in current page administration
And I follow "Course Badge"
Then I should see "Recipients (2)"
And I select "Recipients (2)" from the "jump" singleselect
And I press "Award badge"
And I set the field "existingrecipients[]" to "Student 2 (student2@example.com)"
And I press "Revoke badge"
And I set the field "existingrecipients[]" to "Student 1 (student1@example.com)"
When I press "Revoke badge"
And I am on "Course 1" course homepage
And I navigate to "Badges" in current page administration
And I follow "Course Badge"
Then I should see "Recipients (0)"
Then I should see "Recipients (1)"
And I log out
# Now attempt to revoke a badge as another teacher.
And I am on the "Course 1" "course" page logged in as "teacher2"
And I navigate to "Badges" in current page administration
And I follow "Course Badge"
And I select "Recipients (1)" from the "jump" singleselect
And I press "Award badge"
And I set the field "existingrecipients[]" to "Student 1 (student1@example.com)"
And I press "Revoke badge"
And I am on "Course 1" course homepage
And I navigate to "Badges" in current page administration
And I follow "Course Badge"
And I should see "Recipients (0)"