diff --git a/badges/award.php b/badges/award.php index 50fabb26e8d..bf6855c525f 100644 --- a/badges/award.php +++ b/badges/award.php @@ -217,7 +217,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')); } } diff --git a/badges/lib/awardlib.php b/badges/lib/awardlib.php index f085c56cd01..89d16ad0337 100644 --- a/badges/lib/awardlib.php +++ b/badges/lib/awardlib.php @@ -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( diff --git a/badges/tests/behat/award_badge.feature b/badges/tests/behat/award_badge.feature index f5487fefe95..9ef45afffb6 100644 --- a/badges/tests/behat/award_badge.feature +++ b/badges/tests/behat/award_badge.feature @@ -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)"