MOBILE-2325 badges: Show new page in user profile

main
Juan Leyva 2018-02-14 12:46:40 +01:00 committed by Dani Palou
parent dfd5788271
commit 138f83d979
6 changed files with 237 additions and 3 deletions

View File

@ -0,0 +1,34 @@
// (C) Copyright 2015 Martin Dougiamas
//
// 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 { NgModule } from '@angular/core';
import { AddonBadgesProvider } from './providers/badges';
import { AddonBadgesUserHandler } from './providers/user-handler';
import { CoreUserDelegate } from '../../core/user/providers/user-delegate';
@NgModule({
declarations: [
],
imports: [
],
providers: [
AddonBadgesProvider,
AddonBadgesUserHandler
]
})
export class AddonBadgesModule {
constructor(userDelegate: CoreUserDelegate, userHandler: AddonBadgesUserHandler) {
userDelegate.registerHandler(userHandler);
}
}

View File

@ -0,0 +1,13 @@
{
"badgedetails": "Badge details",
"badges": "Badges",
"contact": "Contact",
"dateawarded": "Date issued",
"expired": "Expired",
"expirydate": "Expiry date",
"issuancedetails": "Badge expiry",
"issuerdetails": "Issuer details",
"issuername": "Issuer name",
"nobadges": "There are no badges available.",
"recipientdetails": "Recipient details"
}

View File

@ -0,0 +1,110 @@
// (C) Copyright 2015 Martin Dougiamas
//
// 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 { Injectable } from '@angular/core';
import { CoreLoggerProvider } from '../../../providers/logger';
import { CoreSitesProvider } from '../../../providers/sites';
/**
* Service to handle badges.
*/
@Injectable()
export class AddonBadgesProvider {
protected logger;
protected ROOT_CACHE_KEY = 'mmaBadges:';
constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider) {
this.logger = logger.getInstance('AddonBadgesProvider');
}
/**
* Returns whether or not the badge plugin is enabled for a certain site.
*
* This method is called quite often and thus should only perform a quick
* check, we should not be calling WS from here.
*
* @param {string} [siteId] Site ID. If not defined, current site.
* @return {Promise<boolean>} Promise resolved with true if enabled, false otherwise.
*/
isPluginEnabled(siteId?: string): Promise<boolean> {
return this.sitesProvider.getSite(siteId).then((site) => {
if (!site.canUseAdvancedFeature('enablebadges')) {
return false;
} else if (!site.wsAvailable('core_course_get_user_navigation_options')) {
return false;
}
return true;
});
}
/**
* Get the cache key for the get badges call.
*
* @param {number} courseId ID of the course to get the badges from.
* @param {number} userId ID of the user to get the badges from.
* @return {string} Cache key.
*/
protected getBadgesCacheKey(courseId: number, userId: number): string {
return this.ROOT_CACHE_KEY + 'badges:' + courseId + ':' + userId;
}
/**
* Get issued badges for a certain user in a course.
*
* @param {number} courseId ID of the course to get the badges from.
* @param {number} userId ID of the user to get the badges from.
* @param {string} [siteId] Site ID. If not defined, current site.
* @return {Promise<any>}Promise to be resolved when the badges are retrieved.
*/
getUserBadges(courseId: number, userId: number, siteId?: string): Promise<any> {
this.logger.debug('Get badges for course ' + courseId);
return this.sitesProvider.getSite(siteId).then((site) => {
const data = {
courseid : courseId,
userid : userId
},
presets = {
cacheKey: this.getBadgesCacheKey(courseId, userId)
};
return site.read('core_badges_get_user_badges', data, presets).then((response) => {
if (response && response.badges) {
return response.badges;
} else {
return Promise.reject(null);
}
});
});
}
/**
* Invalidate get badges WS call.
*
* @param {number} courseId Course ID.
* @param {number} userId ID of the user to get the badges from.
* @param {string} [siteId] Site ID. If not defined, current site.
* @return {Promise<any>} Promise resolved when data is invalidated.
*/
invalidateUserBadges(courseId: number, userId: number, siteId?: string): Promise<any> {
return this.sitesProvider.getSite(siteId).then((site) => {
return site.invalidateWsCacheForKey(this.getBadgesCacheKey(courseId, userId));
});
}
}

View File

@ -0,0 +1,75 @@
// (C) Copyright 2015 Martin Dougiamas
//
// 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 { Injectable } from '@angular/core';
import { CoreUserDelegate, CoreUserProfileHandler, CoreUserProfileHandlerData } from '../../../core/user/providers/user-delegate';
import { AddonBadgesProvider } from './badges';
/**
* Profile badges handler.
*/
@Injectable()
export class AddonBadgesUserHandler implements CoreUserProfileHandler {
name = 'mmaBadges';
priority = 50;
type = CoreUserDelegate.TYPE_NEW_PAGE;
constructor(protected badgesProvider: AddonBadgesProvider) { }
/**
* Check if handler is enabled.
*
* @return {Promise<boolean>} Always enabled.
*/
isEnabled(): Promise<boolean> {
return this.badgesProvider.isPluginEnabled();
}
/**
* Check if handler is enabled for this user in this context.
*
* @param {any} user User to check.
* @param {number} courseId Course ID.
* @param {any} [navOptions] Course navigation options for current user. See CoreCoursesProvider.getUserNavigationOptions.
* @param {any} [admOptions] Course admin options for current user. See CoreCoursesProvider.getUserAdministrationOptions.
* @return {boolean} True if enabled, false otherwise.
*/
isEnabledForUser(user: any, courseId: number, navOptions?: any, admOptions?: any): boolean {
if (navOptions && typeof navOptions.badges != 'undefined') {
return navOptions.badges;
}
// If we reach here, it means we are opening the user site profile.
return true;
}
/**
* Returns the data needed to render the handler.
*
* @return {CoreUserProfileHandlerData} Data needed to render the handler.
*/
getDisplayData(user: any, courseId: number): CoreUserProfileHandlerData {
return {
icon: 'ion-trophy',
title: 'addon.badges.badges',
class: '',
action: (event, navCtrl, user, courseId): void => {
event.preventDefault();
event.stopPropagation();
//navCtrl.push();
}
};
}
}

View File

@ -69,8 +69,9 @@ import { CoreSitePluginsModule } from '@core/siteplugins/siteplugins.module';
import { CoreCompileModule } from '@core/compile/compile.module';
// Addon modules.
import { AddonBadgesModule } from '@addon/badges/badges.module';
import { AddonCalendarModule } from '@addon/calendar/calendar.module';
import { AddonCompetencyModule } from '../addon/competency/competency.module';
import { AddonCompetencyModule } from '@addon/competency/competency.module';
import { AddonUserProfileFieldModule } from '@addon/userprofilefield/userprofilefield.module';
import { AddonFilesModule } from '@addon/files/files.module';
import { AddonModBookModule } from '@addon/mod/book/book.module';
@ -150,6 +151,7 @@ export const CORE_PROVIDERS: any[] = [
CoreSettingsModule,
CoreSitePluginsModule,
CoreCompileModule,
AddonBadgesModule,
AddonCalendarModule,
AddonCompetencyModule,
AddonUserProfileFieldModule,

View File

@ -41,8 +41,8 @@ export class CoreUserProfileMailHandler implements CoreUserProfileHandler {
*
* @param {any} user User to check.
* @param {number} courseId Course ID.
* @param {any} [navOptions] Course navigation options for current user. See $mmCourses#getUserNavigationOptions.
* @param {any} [admOptions] Course admin options for current user. See $mmCourses#getUserAdministrationOptions.
* @param {any} [navOptions] Course navigation options for current user. See CoreCoursesProvider.getUserNavigationOptions.
* @param {any} [admOptions] Course admin options for current user. See CoreCoursesProvider.getUserAdministrationOptions.
* @return {boolean|Promise<boolean>} Promise resolved with true if enabled, resolved with false otherwise.
*/
isEnabledForUser(user: any, courseId: number, navOptions?: any, admOptions?: any): boolean | Promise<boolean> {