This commit is contained in:
Sara Arjona 2024-08-21 16:43:44 +02:00
commit b628db7abe
No known key found for this signature in database
5 changed files with 39 additions and 3 deletions

View File

@ -0,0 +1,9 @@
issueNumber: MDL-82742
notes:
core_badges:
- message: >-
Added fields `recipientid` and `recipientfullname` to
`user_badge_exporter`, which is used in the return structure of
external functions `core_badges_get_user_badge_by_hash` and
`core_badges_get_user_badges`.
type: changed

View File

@ -181,6 +181,14 @@ class user_badge_exporter extends exporter {
'optional' => true,
'default' => 0,
],
'recipientid' => [
'type' => PARAM_INT,
'description' => 'Id of the awarded user',
],
'recipientfullname' => [
'type' => PARAM_NOTAGS,
'description' => 'Full name of the awarded user',
],
'email' => [
'type' => PARAM_TEXT,
'description' => 'User email',

View File

@ -209,6 +209,10 @@ class external_test extends externallib_advanced_testcase {
}
}
// Add recipient.
$badge->recipientid = $this->student->id;
$badge->recipientfullname = fullname($this->student);
$expectedbadges[] = (array) $badge;
if (isset($badge->courseid)) {
// Save the course badge to be able to compare it in our tests.

View File

@ -102,6 +102,10 @@ class get_user_badge_by_hash_test extends externallib_advanced_testcase {
'f3')->out(false);
$badge->status = BADGE_STATUS_ACTIVE_LOCKED;
// Add recipient.
$badge->recipientid = $student1->id;
$badge->recipientfullname = fullname($student1);
// Add an endorsement for the badge.
$endorsement = new \stdClass();
$endorsement->badgeid = $badgeid;
@ -184,13 +188,13 @@ class get_user_badge_by_hash_test extends externallib_advanced_testcase {
// Site badge.
$result = get_user_badge_by_hash::execute($data['sitebadge'][0]['uniquehash']);
$result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
$this->assertEquals($data['sitebadge'][0]['uniquehash'], $result['badge'][0]['uniquehash']);
$this->assertEquals($data['sitebadge'][0], $result['badge'][0]);
$this->assertEmpty($result['warnings']);
// Course badge.
$result = get_user_badge_by_hash::execute($data['coursebadge'][0]['uniquehash']);
$result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
$this->assertEquals($data['coursebadge'][0]['uniquehash'], $result['badge'][0]['uniquehash']);
$this->assertEquals($data['coursebadge'][0], $result['badge'][0]);
$this->assertEmpty($result['warnings']);
// Wrong hash.

View File

@ -421,7 +421,7 @@ function badges_get_badge_by_hash(string $hash): object|bool {
* @return object
*/
function badges_prepare_badge_for_external(stdClass $badge, stdClass $user): object {
global $PAGE, $USER;
global $PAGE, $SITE, $USER;
if ($badge->type == BADGE_TYPE_SITE) {
$context = context_system::instance();
} else {
@ -450,6 +450,17 @@ function badges_prepare_badge_for_external(stdClass $badge, stdClass $user): obj
];
}
// Recipient (the badge was awarded to this person).
$badge->recipientid = $user->id;
if ($user->deleted) {
$strdata = new stdClass();
$strdata->user = fullname($user);
$strdata->site = format_string($SITE->fullname, true, ['context' => context_system::instance()]);
$badge->recipientfullname = get_string('error:userdeleted', 'badges', $strdata);
} else {
$badge->recipientfullname = fullname($user);
}
// Create a badge instance to be able to get the endorsement and other info.
$badgeinstance = new badge($badge->id);
$endorsement = $badgeinstance->get_endorsement();