Skip to content

Commit 24fddd8

Browse files
committed
MOBILE-4639 badges: Fix structure of alignments
1 parent 404cbf7 commit 24fddd8

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

src/addons/badges/pages/issued-badge/issued-badge.html

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,21 +217,16 @@ <h2>{{ 'addon.badges.relatedbages' | translate}}</h2>
217217
</ion-item-group>
218218

219219
<!-- Competencies alignment -->
220-
<ion-item-group *ngIf="badge.alignment">
220+
<ion-item-group *ngIf="badge.alignment?.length">
221221
<ion-item-divider>
222222
<ion-label>
223223
<h2>{{ 'addon.badges.alignment' | translate}}</h2>
224224
</ion-label>
225225
</ion-item-divider>
226-
<ion-item class="ion-text-wrap" *ngFor="let alignment of badge.alignment" [href]="alignment.targeturl" core-link
226+
<ion-item class="ion-text-wrap" *ngFor="let alignment of badge.alignment" [href]="alignment.targetUrl" core-link
227227
[autoLogin]="false">
228228
<ion-label>
229-
<p class="item-heading">{{ alignment.targetname }}</p>
230-
</ion-label>
231-
</ion-item>
232-
<ion-item class="ion-text-wrap" *ngIf="badge.alignment.length === 0">
233-
<ion-label>
234-
<p class="item-heading">{{ 'addon.badges.noalignment' | translate}}</p>
229+
<p class="item-heading">{{ alignment.targetName }}</p>
235230
</ion-label>
236231
</ion-item>
237232
</ion-item-group>

src/addons/badges/services/badges.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,18 @@ export class AddonBadgesProvider {
7878
throw new CoreError('Invalid badges response');
7979
}
8080

81-
// In 3.7, competencies was renamed to alignment. Rename the property in 3.6 too.
8281
response.badges.forEach((badge) => {
82+
// In 3.7, competencies was renamed to alignment.
83+
if (!badge.alignment && badge.competencies) {
84+
badge.alignment = badge.competencies.map((competency) => ({
85+
targetName: competency.targetname,
86+
targetUrl: competency.targeturl,
87+
}));
88+
}
8389
badge.alignment = badge.alignment || badge.competencies;
8490

85-
// Check that the alignment is valid, they were broken in 3.7.
86-
if (badge.alignment && badge.alignment[0] && badge.alignment[0].targetname === undefined) {
87-
// If any badge lacks targetname it means they are affected by the Moodle bug, don't display them.
88-
delete badge.alignment;
89-
}
91+
// Exclude alignments without targetName, we can't display them.
92+
badge.alignment = badge.alignment?.filter((alignment) => alignment.targetName);
9093
});
9194

9295
return response.badges;
@@ -138,11 +141,15 @@ export class AddonBadgesProvider {
138141
data,
139142
preSets,
140143
);
141-
if (!response || !response.badge?.[0]) {
144+
const badge = response?.badge?.[0];
145+
if (!badge) {
142146
throw new CoreError('Invalid badge response');
143147
}
144148

145-
return response.badge[0];
149+
// Exclude alignments without targetName, we can't display them.
150+
badge.alignment = badge.alignment?.filter((alignment) => alignment.targetName);
151+
152+
return badge;
146153
}
147154

148155
/**
@@ -242,11 +249,11 @@ export type AddonBadgesUserBadge = {
242249
alignment?: { // @since 3.7. Calculated by the app for 3.6 sites. Badge alignments.
243250
id?: number; // Alignment id.
244251
badgeid?: number; // Badge id.
245-
targetname?: string; // Target name.
246-
targeturl?: string; // Target URL.
247-
targetdescription?: string; // Target description.
248-
targetframework?: string; // Target framework.
249-
targetcode?: string; // Target code.
252+
targetName?: string; // Target name.
253+
targetUrl?: string; // Target URL.
254+
targetDescription?: string; // Target description.
255+
targetFramework?: string; // Target framework.
256+
targetCode?: string; // Target code.
250257
}[];
251258
competencies?: { // @deprecatedonmoodle since 3.7. @since 3.6. In 3.7 it was renamed to alignment.
252259
id?: number; // Alignment id.

0 commit comments

Comments
 (0)