MOBILE-2921 competency: Support push clicks and link clicks

This commit is contained in:
Dani Palou 2019-03-20 11:32:19 +01:00
parent a83ebdfec8
commit bb7e1217a9
7 changed files with 416 additions and 2 deletions

View File

@ -18,10 +18,17 @@ import { AddonCompetencyHelperProvider } from './providers/helper';
import { AddonCompetencyCourseOptionHandler } from './providers/course-option-handler';
import { AddonCompetencyMainMenuHandler } from './providers/mainmenu-handler';
import { AddonCompetencyUserHandler } from './providers/user-handler';
import { AddonCompetencyCompetencyLinkHandler } from './providers/competency-link-handler';
import { AddonCompetencyPlanLinkHandler } from './providers/plan-link-handler';
import { AddonCompetencyPlansLinkHandler } from './providers/plans-link-handler';
import { AddonCompetencyUserCompetencyLinkHandler } from './providers/user-competency-link-handler';
import { AddonCompetencyPushClickHandler } from './providers/push-click-handler';
import { AddonCompetencyComponentsModule } from './components/components.module';
import { CoreCourseOptionsDelegate } from '@core/course/providers/options-delegate';
import { CoreMainMenuDelegate } from '@core/mainmenu/providers/delegate';
import { CoreUserDelegate } from '@core/user/providers/user-delegate';
import { CoreContentLinksDelegate } from '@core/contentlinks/providers/delegate';
import { CorePushNotificationsDelegate } from '@core/pushnotifications/providers/delegate';
// List of providers (without handlers).
export const ADDON_COMPETENCY_PROVIDERS: any[] = [
@ -40,16 +47,30 @@ export const ADDON_COMPETENCY_PROVIDERS: any[] = [
AddonCompetencyHelperProvider,
AddonCompetencyCourseOptionHandler,
AddonCompetencyMainMenuHandler,
AddonCompetencyUserHandler
AddonCompetencyUserHandler,
AddonCompetencyCompetencyLinkHandler,
AddonCompetencyPlanLinkHandler,
AddonCompetencyPlansLinkHandler,
AddonCompetencyUserCompetencyLinkHandler,
AddonCompetencyPushClickHandler
]
})
export class AddonCompetencyModule {
constructor(mainMenuDelegate: CoreMainMenuDelegate, mainMenuHandler: AddonCompetencyMainMenuHandler,
courseOptionsDelegate: CoreCourseOptionsDelegate, courseOptionHandler: AddonCompetencyCourseOptionHandler,
userDelegate: CoreUserDelegate, userHandler: AddonCompetencyUserHandler) {
userDelegate: CoreUserDelegate, userHandler: AddonCompetencyUserHandler,
contentLinksDelegate: CoreContentLinksDelegate, competencyLinkHandler: AddonCompetencyCompetencyLinkHandler,
planLinkHandler: AddonCompetencyPlanLinkHandler, plansLinkHandler: AddonCompetencyPlansLinkHandler,
userComptencyLinkHandler: AddonCompetencyUserCompetencyLinkHandler,
pushNotificationsDelegate: CorePushNotificationsDelegate, pushClickHandler: AddonCompetencyPushClickHandler) {
mainMenuDelegate.registerHandler(mainMenuHandler);
courseOptionsDelegate.registerHandler(courseOptionHandler);
userDelegate.registerHandler(userHandler);
contentLinksDelegate.registerHandler(competencyLinkHandler);
contentLinksDelegate.registerHandler(planLinkHandler);
contentLinksDelegate.registerHandler(plansLinkHandler);
contentLinksDelegate.registerHandler(userComptencyLinkHandler);
pushNotificationsDelegate.registerClickHandler(pushClickHandler);
}
}

View File

@ -0,0 +1,74 @@
// (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 { CoreContentLinksHandlerBase } from '@core/contentlinks/classes/base-handler';
import { CoreContentLinksAction } from '@core/contentlinks/providers/delegate';
import { CoreContentLinksHelperProvider } from '@core/contentlinks/providers/helper';
import { AddonCompetencyProvider } from './competency';
/**
* Handler to treat links to a competency in a plan or in a course.
*/
@Injectable()
export class AddonCompetencyCompetencyLinkHandler extends CoreContentLinksHandlerBase {
name = 'AddonCompetencyCompetencyLinkHandler';
pattern = /\/admin\/tool\/lp\/(user_competency_in_course|user_competency_in_plan)\.php/;
constructor(private linkHelper: CoreContentLinksHelperProvider, private competencyProvider: AddonCompetencyProvider) {
super();
}
/**
* Get the list of actions for a link (url).
*
* @param {string[]} siteIds List of sites the URL belongs to.
* @param {string} url The URL to treat.
* @param {any} params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1}
* @param {number} [courseId] Course ID related to the URL. Optional but recommended.
* @return {CoreContentLinksAction[]|Promise<CoreContentLinksAction[]>} List of (or promise resolved with list of) actions.
*/
getActions(siteIds: string[], url: string, params: any, courseId?: number):
CoreContentLinksAction[] | Promise<CoreContentLinksAction[]> {
courseId = courseId || params.courseid || params.cid;
return [{
action: (siteId, navCtrl?): void => {
this.linkHelper.goInSite(navCtrl, 'AddonCompetencyCompetencyPage', {
planId: params.planid,
competencyId: params.competencyid,
courseId: courseId,
userId: params.userid
}, siteId);
}
}];
}
/**
* Check if the handler is enabled for a certain site (site + user) and a URL.
* If not defined, defaults to true.
*
* @param {string} siteId The site ID.
* @param {string} url The URL to treat.
* @param {any} params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1}
* @param {number} [courseId] Course ID related to the URL. Optional but recommended.
* @return {boolean|Promise<boolean>} Whether the handler is enabled for the URL and site.
*/
isEnabled(siteId: string, url: string, params: any, courseId?: number): boolean | Promise<boolean> {
// Handler is disabled if all competency features are disabled.
return this.competencyProvider.allCompetenciesDisabled(siteId).then((disabled) => {
return !disabled;
});
}
}

View File

@ -42,6 +42,20 @@ export class AddonCompetencyProvider {
this.logger = loggerProvider.getInstance('AddonCompetencyProvider');
}
/**
* Check if all competencies features are disabled.
*
* @param {string} [siteId] Site ID. If not defined, current site.
* @return {Promise<boolean>} Promise resolved with boolean: whether all competency features are disabled.
*/
allCompetenciesDisabled(siteId?: string): Promise<boolean> {
return this.sitesProvider.getSite(siteId).then((site) => {
return site.isFeatureDisabled('CoreMainMenuDelegate_AddonCompetency') &&
site.isFeatureDisabled('CoreCourseOptionsDelegate_AddonCompetency') &&
site.isFeatureDisabled('CoreUserDelegate_AddonCompetency');
});
}
/**
* Get cache key for user learning plans data WS calls.
*

View File

@ -0,0 +1,68 @@
// (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 { CoreContentLinksHandlerBase } from '@core/contentlinks/classes/base-handler';
import { CoreContentLinksAction } from '@core/contentlinks/providers/delegate';
import { CoreContentLinksHelperProvider } from '@core/contentlinks/providers/helper';
import { AddonCompetencyProvider } from './competency';
/**
* Handler to treat links to a plan.
*/
@Injectable()
export class AddonCompetencyPlanLinkHandler extends CoreContentLinksHandlerBase {
name = 'AddonCompetencyPlanLinkHandler';
pattern = /\/admin\/tool\/lp\/plan\.php.*([\?\&]id=\d+)/;
constructor(private linkHelper: CoreContentLinksHelperProvider, private competencyProvider: AddonCompetencyProvider) {
super();
}
/**
* Get the list of actions for a link (url).
*
* @param {string[]} siteIds List of sites the URL belongs to.
* @param {string} url The URL to treat.
* @param {any} params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1}
* @param {number} [courseId] Course ID related to the URL. Optional but recommended.
* @return {CoreContentLinksAction[]|Promise<CoreContentLinksAction[]>} List of (or promise resolved with list of) actions.
*/
getActions(siteIds: string[], url: string, params: any, courseId?: number):
CoreContentLinksAction[] | Promise<CoreContentLinksAction[]> {
return [{
action: (siteId, navCtrl?): void => {
this.linkHelper.goInSite(navCtrl, 'AddonCompetencyPlanPage', { planId: params.id }, siteId);
}
}];
}
/**
* Check if the handler is enabled for a certain site (site + user) and a URL.
* If not defined, defaults to true.
*
* @param {string} siteId The site ID.
* @param {string} url The URL to treat.
* @param {any} params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1}
* @param {number} [courseId] Course ID related to the URL. Optional but recommended.
* @return {boolean|Promise<boolean>} Whether the handler is enabled for the URL and site.
*/
isEnabled(siteId: string, url: string, params: any, courseId?: number): boolean | Promise<boolean> {
// Handler is disabled if all competency features are disabled.
return this.competencyProvider.allCompetenciesDisabled(siteId).then((disabled) => {
return !disabled;
});
}
}

View File

@ -0,0 +1,69 @@
// (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 { CoreContentLinksHandlerBase } from '@core/contentlinks/classes/base-handler';
import { CoreContentLinksAction } from '@core/contentlinks/providers/delegate';
import { CoreLoginHelperProvider } from '@core/login/providers/helper';
import { AddonCompetencyProvider } from './competency';
/**
* Handler to treat links to user plans.
*/
@Injectable()
export class AddonCompetencyPlansLinkHandler extends CoreContentLinksHandlerBase {
name = 'AddonCompetencyPlansLinkHandler';
pattern = /\/admin\/tool\/lp\/plans\.php/;
constructor(private loginHelper: CoreLoginHelperProvider, private competencyProvider: AddonCompetencyProvider) {
super();
}
/**
* Get the list of actions for a link (url).
*
* @param {string[]} siteIds List of sites the URL belongs to.
* @param {string} url The URL to treat.
* @param {any} params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1}
* @param {number} [courseId] Course ID related to the URL. Optional but recommended.
* @return {CoreContentLinksAction[]|Promise<CoreContentLinksAction[]>} List of (or promise resolved with list of) actions.
*/
getActions(siteIds: string[], url: string, params: any, courseId?: number):
CoreContentLinksAction[] | Promise<CoreContentLinksAction[]> {
return [{
action: (siteId, navCtrl?): void => {
// Always use redirect to make it the new history root (to avoid "loops" in history).
this.loginHelper.redirect('AddonCompetencyPlanListPage', { userId: params.userid }, siteId);
}
}];
}
/**
* Check if the handler is enabled for a certain site (site + user) and a URL.
* If not defined, defaults to true.
*
* @param {string} siteId The site ID.
* @param {string} url The URL to treat.
* @param {any} params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1}
* @param {number} [courseId] Course ID related to the URL. Optional but recommended.
* @return {boolean|Promise<boolean>} Whether the handler is enabled for the URL and site.
*/
isEnabled(siteId: string, url: string, params: any, courseId?: number): boolean | Promise<boolean> {
// Handler is disabled if all competency features are disabled.
return this.competencyProvider.allCompetenciesDisabled(siteId).then((disabled) => {
return !disabled;
});
}
}

View File

@ -0,0 +1,100 @@
// (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 { CoreUrlUtilsProvider } from '@providers/utils/url';
import { CoreUtilsProvider } from '@providers/utils/utils';
import { CorePushNotificationsClickHandler } from '@core/pushnotifications/providers/delegate';
import { CoreLoginHelperProvider } from '@core/login/providers/helper';
import { AddonCompetencyProvider } from './competency';
/**
* Handler for competencies push notifications clicks.
*/
@Injectable()
export class AddonCompetencyPushClickHandler implements CorePushNotificationsClickHandler {
name = 'AddonCompetencyPushClickHandler';
priority = 200;
constructor(private utils: CoreUtilsProvider, private urlUtils: CoreUrlUtilsProvider,
private competencyProvider: AddonCompetencyProvider, private loginHelper: CoreLoginHelperProvider) {}
/**
* Check if a notification click is handled by this handler.
*
* @param {any} notification The notification to check.
* @return {boolean} Whether the notification click is handled by this handler
*/
handles(notification: any): boolean | Promise<boolean> {
if (this.utils.isTrueOrOne(notification.notif) && notification.moodlecomponent == 'moodle' &&
(notification.name == 'competencyplancomment' || notification.name == 'competencyusercompcomment')) {
// If all competency features are disabled, don't handle the click.
return this.competencyProvider.allCompetenciesDisabled(notification.site).then((disabled) => {
return !disabled;
});
}
return false;
}
/**
* Handle the notification click.
*
* @param {any} notification The notification to check.
* @return {Promise<any>} Promise resolved when done.
*/
handleClick(notification: any): Promise<any> {
const contextUrlParams = this.urlUtils.extractUrlParams(notification.contexturl);
if (notification.name == 'competencyplancomment') {
// Open the learning plan.
const planId = Number(contextUrlParams.id);
return this.competencyProvider.invalidateLearningPlan(planId, notification.site).catch(() => {
// Ignore errors.
}).then(() => {
return this.loginHelper.redirect('AddonCompetencyPlanPage', { planId: planId }, notification.site);
});
} else {
if (notification.contexturl && notification.contexturl.indexOf('user_competency_in_plan.php') != -1) {
// Open the competency.
const courseId = Number(notification.course),
competencyId = Number(contextUrlParams.competencyid),
planId = Number(contextUrlParams.planid),
userId = Number(contextUrlParams.userid);
return this.competencyProvider.invalidateCompetencyInPlan(planId, competencyId, notification.site).catch(() => {
// Ignore errors.
}).then(() => {
return this.loginHelper.redirect('AddonCompetencyCompetencyPage', {
planId: planId,
competencyId: competencyId,
courseId: courseId,
userId: userId
}, notification.site);
});
} else {
// Open the list of plans.
const userId = Number(contextUrlParams.userid);
return this.competencyProvider.invalidateLearningPlans(userId, notification.site).catch(() => {
// Ignore errors.
}).then(() => {
return this.loginHelper.redirect('AddonCompetencyPlanListPage', { userId: userId }, notification.site);
});
}
}
}
}

View File

@ -0,0 +1,68 @@
// (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 { CoreContentLinksHandlerBase } from '@core/contentlinks/classes/base-handler';
import { CoreContentLinksAction } from '@core/contentlinks/providers/delegate';
import { CoreContentLinksHelperProvider } from '@core/contentlinks/providers/helper';
import { AddonCompetencyProvider } from './competency';
/**
* Handler to treat links to a usr competency.
*/
@Injectable()
export class AddonCompetencyUserCompetencyLinkHandler extends CoreContentLinksHandlerBase {
name = 'AddonCompetencyUserCompetencyLinkHandler';
pattern = /\/admin\/tool\/lp\/user_competency\.php.*([\?\&]id=\d+)/;
constructor(private linkHelper: CoreContentLinksHelperProvider, private competencyProvider: AddonCompetencyProvider) {
super();
}
/**
* Get the list of actions for a link (url).
*
* @param {string[]} siteIds List of sites the URL belongs to.
* @param {string} url The URL to treat.
* @param {any} params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1}
* @param {number} [courseId] Course ID related to the URL. Optional but recommended.
* @return {CoreContentLinksAction[]|Promise<CoreContentLinksAction[]>} List of (or promise resolved with list of) actions.
*/
getActions(siteIds: string[], url: string, params: any, courseId?: number):
CoreContentLinksAction[] | Promise<CoreContentLinksAction[]> {
return [{
action: (siteId, navCtrl?): void => {
this.linkHelper.goInSite(navCtrl, 'AddonCompetencyCompetencySummaryPage', { competencyId: params.id }, siteId);
}
}];
}
/**
* Check if the handler is enabled for a certain site (site + user) and a URL.
* If not defined, defaults to true.
*
* @param {string} siteId The site ID.
* @param {string} url The URL to treat.
* @param {any} params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1}
* @param {number} [courseId] Course ID related to the URL. Optional but recommended.
* @return {boolean|Promise<boolean>} Whether the handler is enabled for the URL and site.
*/
isEnabled(siteId: string, url: string, params: any, courseId?: number): boolean | Promise<boolean> {
// Handler is disabled if all competency features are disabled.
return this.competencyProvider.allCompetenciesDisabled(siteId).then((disabled) => {
return !disabled;
});
}
}