MDL-67462 badges: Fix badge alignments returned in external functions

This commit is contained in:
Albert Gasset
2025-02-07 09:02:25 +01:00
parent 139a0ad5f0
commit 5f6ebef730
+32 -19
View File
@@ -462,19 +462,26 @@ function badges_prepare_badge_for_external(stdClass $badge, stdClass $user): obj
// Create a badge instance to be able to get the endorsement and other info.
$badgeinstance = new badge($badge->id);
$endorsement = $badgeinstance->get_endorsement();
$alignments = $badgeinstance->get_alignments();
$relatedbadges = $badgeinstance->get_related_badges();
$alignments = [];
foreach ($badgeinstance->get_alignments() as $alignment) {
$alignmentobj = (object) [
'id' => $alignment->id,
'badgeid' => $alignment->badgeid,
'targetName' => $alignment->targetname,
'targetUrl' => $alignment->targeturl,
];
// Include only the properties visible by the user.
if ($canconfiguredetails) {
$alignmentobj->targetDescription = $alignment->targetdescription;
$alignmentobj->targetFramework = $alignment->targetframework;
$alignmentobj->targetCode = $alignment->targetcode;
}
$alignments[] = $alignmentobj;
}
if (!$canconfiguredetails) {
// Return only the properties visible by the user.
if (!empty($alignments)) {
foreach ($alignments as $alignment) {
unset($alignment->targetdescription);
unset($alignment->targetframework);
unset($alignment->targetcode);
}
}
if (!empty($relatedbadges)) {
foreach ($relatedbadges as $relatedbadge) {
unset($relatedbadge->version);
@@ -530,21 +537,27 @@ function badges_prepare_badgeclass_for_external(core_badges\output\badgeclass $b
// Create a badge instance to be able to get the endorsement and other info.
$badgeinstance = new badge($badgeclass->badge->id);
$endorsement = $badgeinstance->get_endorsement();
$alignments = $badgeinstance->get_alignments();
$relatedbadges = $badgeinstance->get_related_badges();
$canconfiguredetails = has_capability('moodle/badges:configuredetails', $context);
$alignments = [];
foreach ($badgeinstance->get_alignments() as $alignment) {
$alignmentobj = (object) [
'id' => $alignment->id,
'badgeid' => $alignment->badgeid,
'targetName' => $alignment->targetname,
'targetUrl' => $alignment->targeturl,
];
// Include only the properties visible by the user.
if ($canconfiguredetails) {
$alignmentobj->targetDescription = $alignment->targetdescription;
$alignmentobj->targetFramework = $alignment->targetframework;
$alignmentobj->targetCode = $alignment->targetcode;
}
$alignments[] = $alignmentobj;
}
if (!$canconfiguredetails) {
// Return only the properties visible by the user.
if (!empty($alignments)) {
foreach ($alignments as $alignment) {
unset($alignment->targetdescription);
unset($alignment->targetframework);
unset($alignment->targetcode);
}
}
if (!empty($relatedbadges)) {
foreach ($relatedbadges as $relatedbadge) {
unset($relatedbadge->version);