2020-12-11 16:08:35 +01:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
|
import { IonRefresher } from '@ionic/angular';
|
|
|
|
import { CoreTimeUtils } from '@services/utils/time';
|
|
|
|
import { CoreDomUtils } from '@services/utils/dom';
|
|
|
|
import { CoreSites } from '@services/sites';
|
|
|
|
import { CoreUser, CoreUserProfile } from '@features/user/services/user';
|
|
|
|
import { AddonBadges, AddonBadgesUserBadge } from '../../services/badges';
|
|
|
|
import { CoreUtils } from '@services/utils/utils';
|
|
|
|
import { CoreCourses, CoreEnrolledCourseData } from '@features/courses/services/courses';
|
2021-01-25 14:31:11 +01:00
|
|
|
import { CoreNavigator } from '@services/navigator';
|
2021-01-28 17:23:57 +01:00
|
|
|
import { ActivatedRoute } from '@angular/router';
|
2020-12-11 16:08:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Page that displays the list of calendar events.
|
|
|
|
*/
|
|
|
|
@Component({
|
|
|
|
selector: 'page-addon-badges-issued-badge',
|
|
|
|
templateUrl: 'issued-badge.html',
|
|
|
|
})
|
|
|
|
export class AddonBadgesIssuedBadgePage implements OnInit {
|
|
|
|
|
|
|
|
protected badgeHash = '';
|
|
|
|
protected userId!: number;
|
|
|
|
|
|
|
|
courseId = 0;
|
|
|
|
user?: CoreUserProfile;
|
|
|
|
course?: CoreEnrolledCourseData;
|
|
|
|
badge?: AddonBadgesUserBadge;
|
|
|
|
badgeLoaded = false;
|
|
|
|
currentTime = 0;
|
|
|
|
|
2021-01-28 17:23:57 +01:00
|
|
|
constructor(
|
|
|
|
protected route: ActivatedRoute,
|
|
|
|
) { }
|
|
|
|
|
2020-12-11 16:08:35 +01:00
|
|
|
/**
|
|
|
|
* View loaded.
|
|
|
|
*/
|
|
|
|
ngOnInit(): void {
|
2021-04-27 15:44:41 +02:00
|
|
|
this.courseId = CoreNavigator.getRouteNumberParam('courseId') || this.courseId; // Use 0 for site badges.
|
|
|
|
this.userId = CoreNavigator.getRouteNumberParam('userId') || CoreSites.getCurrentSite()!.getUserId();
|
|
|
|
this.badgeHash = CoreNavigator.getRouteParam('badgeHash') || '';
|
2021-01-28 17:23:57 +01:00
|
|
|
|
2021-04-27 15:44:41 +02:00
|
|
|
this.fetchIssuedBadge().finally(() => {
|
|
|
|
this.badgeLoaded = true;
|
2020-12-11 16:08:35 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the issued badge required for the view.
|
|
|
|
*
|
|
|
|
* @return Promise resolved when done.
|
|
|
|
*/
|
|
|
|
async fetchIssuedBadge(): Promise<void> {
|
2021-03-02 11:41:04 +01:00
|
|
|
this.currentTime = CoreTimeUtils.timestamp();
|
2020-12-11 16:08:35 +01:00
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
this.user = await CoreUser.getProfile(this.userId, this.courseId, true);
|
2020-12-11 16:08:35 +01:00
|
|
|
|
|
|
|
try {
|
2021-03-02 11:41:04 +01:00
|
|
|
const badges = await AddonBadges.getUserBadges(this.courseId, this.userId);
|
2020-12-11 16:08:35 +01:00
|
|
|
const badge = badges.find((badge) => this.badgeHash == badge.uniquehash);
|
|
|
|
|
|
|
|
if (!badge) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.badge = badge;
|
|
|
|
if (badge.courseid) {
|
|
|
|
try {
|
2021-03-02 11:41:04 +01:00
|
|
|
this.course = await CoreCourses.getUserCourse(badge.courseid, true);
|
2020-12-11 16:08:35 +01:00
|
|
|
} catch {
|
|
|
|
// Maybe an old deleted course.
|
|
|
|
this.course = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (message) {
|
2021-03-02 11:41:04 +01:00
|
|
|
CoreDomUtils.showErrorModalDefault(message, 'Error getting badge data.');
|
2020-12-11 16:08:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refresh the badges.
|
|
|
|
*
|
|
|
|
* @param refresher Refresher.
|
|
|
|
*/
|
2021-03-12 12:22:55 +01:00
|
|
|
async refreshBadges(refresher?: IonRefresher): Promise<void> {
|
2021-03-02 11:41:04 +01:00
|
|
|
await CoreUtils.ignoreErrors(Promise.all([
|
|
|
|
AddonBadges.invalidateUserBadges(this.courseId, this.userId),
|
2020-12-11 16:08:35 +01:00
|
|
|
]));
|
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
await CoreUtils.ignoreErrors(Promise.all([
|
2020-12-11 16:08:35 +01:00
|
|
|
this.fetchIssuedBadge(),
|
|
|
|
]));
|
|
|
|
|
2021-03-12 12:22:55 +01:00
|
|
|
refresher?.complete();
|
2020-12-11 16:08:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|