From a83ebdfec8f30b8deb9b79b7670d3ee521ce2505 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 19 Mar 2019 13:30:24 +0100 Subject: [PATCH] MOBILE-2921 course: Support course request push clicks --- src/core/courses/courses.module.ts | 10 +- .../providers/request-push-click-handler.ts | 97 +++++++++++++++++++ 2 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 src/core/courses/providers/request-push-click-handler.ts diff --git a/src/core/courses/courses.module.ts b/src/core/courses/courses.module.ts index 1a7294425..a166ad5db 100644 --- a/src/core/courses/courses.module.ts +++ b/src/core/courses/courses.module.ts @@ -21,6 +21,7 @@ import { CoreCoursesCourseLinkHandler } from './providers/course-link-handler'; import { CoreCoursesIndexLinkHandler } from './providers/courses-index-link-handler'; import { CoreCoursesDashboardLinkHandler } from './providers/dashboard-link-handler'; import { CoreCoursesEnrolPushClickHandler } from './providers/enrol-push-click-handler'; +import { CoreCoursesRequestPushClickHandler } from './providers/request-push-click-handler'; import { CoreMainMenuDelegate } from '@core/mainmenu/providers/delegate'; import { CoreContentLinksDelegate } from '@core/contentlinks/providers/delegate'; import { CorePushNotificationsDelegate } from '@core/pushnotifications/providers/delegate'; @@ -44,7 +45,8 @@ export const CORE_COURSES_PROVIDERS: any[] = [ CoreCoursesCourseLinkHandler, CoreCoursesIndexLinkHandler, CoreCoursesDashboardLinkHandler, - CoreCoursesEnrolPushClickHandler + CoreCoursesEnrolPushClickHandler, + CoreCoursesRequestPushClickHandler ], exports: [] }) @@ -52,12 +54,14 @@ export class CoreCoursesModule { constructor(mainMenuDelegate: CoreMainMenuDelegate, contentLinksDelegate: CoreContentLinksDelegate, mainMenuHandler: CoreDashboardMainMenuHandler, courseLinkHandler: CoreCoursesCourseLinkHandler, indexLinkHandler: CoreCoursesIndexLinkHandler, dashboardLinkHandler: CoreCoursesDashboardLinkHandler, - pushNotificationsDelegate: CorePushNotificationsDelegate, pushClickHandler: CoreCoursesEnrolPushClickHandler) { + pushNotificationsDelegate: CorePushNotificationsDelegate, enrolPushClickHandler: CoreCoursesEnrolPushClickHandler, + requestPushClickHandler: CoreCoursesRequestPushClickHandler) { mainMenuDelegate.registerHandler(mainMenuHandler); contentLinksDelegate.registerHandler(courseLinkHandler); contentLinksDelegate.registerHandler(indexLinkHandler); contentLinksDelegate.registerHandler(dashboardLinkHandler); - pushNotificationsDelegate.registerClickHandler(pushClickHandler); + pushNotificationsDelegate.registerClickHandler(enrolPushClickHandler); + pushNotificationsDelegate.registerClickHandler(requestPushClickHandler); } } diff --git a/src/core/courses/providers/request-push-click-handler.ts b/src/core/courses/providers/request-push-click-handler.ts new file mode 100644 index 000000000..af8688605 --- /dev/null +++ b/src/core/courses/providers/request-push-click-handler.ts @@ -0,0 +1,97 @@ +// (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 { CoreSitesProvider } from '@providers/sites'; +import { CoreDomUtilsProvider } from '@providers/utils/dom'; +import { CoreTextUtilsProvider } from '@providers/utils/text'; +import { CoreUtilsProvider } from '@providers/utils/utils'; +import { CorePushNotificationsClickHandler } from '@core/pushnotifications/providers/delegate'; +import { CoreCoursesProvider } from '@core/courses/providers/courses'; +import { CoreCourseHelperProvider } from '@core/course/providers/helper'; +import { CoreLoginHelperProvider } from '@core/login/providers/helper'; + +/** + * Handler for course request push notifications clicks. + */ +@Injectable() +export class CoreCoursesRequestPushClickHandler implements CorePushNotificationsClickHandler { + name = 'CoreCoursesRequestPushClickHandler'; + priority = 200; + + protected SUPPORTED_NAMES = ['courserequested', 'courserequestapproved', 'courserequestrejected']; + + constructor(private utils: CoreUtilsProvider, private domUtils: CoreDomUtilsProvider, private sitesProvider: CoreSitesProvider, + private courseHelper: CoreCourseHelperProvider, private loginHelper: CoreLoginHelperProvider, + private textUtils: CoreTextUtilsProvider, private coursesProvider: CoreCoursesProvider) {} + + /** + * 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 { + // Don't support 'courserequestrejected', that way the app will open the notifications page. + return this.utils.isTrueOrOne(notification.notif) && notification.moodlecomponent == 'moodle' && + (notification.name == 'courserequested' || notification.name == 'courserequestapproved'); + } + + /** + * Handle the notification click. + * + * @param {any} notification The notification to check. + * @return {Promise} Promise resolved when done. + */ + handleClick(notification: any): Promise { + const courseId = Number(notification.courseid); + + if (notification.name == 'courserequested') { + // Feature not supported in the app, open in browser. + return this.sitesProvider.getSite(notification.site).then((site) => { + const url = this.textUtils.concatenatePaths(site.getURL(), 'course/pending.php'); + + return site.openInBrowserWithAutoLogin(url); + }); + } else { + // Open the course. + const modal = this.domUtils.showModalLoading(); + + return this.coursesProvider.invalidateUserCourses(notification.site).catch(() => { + // Ignore errors. + }).then(() => { + return this.courseHelper.getCourse(courseId, notification.site); + }).then((result) => { + const params: any = { + course: result.course + }; + let page; + + if (result.enrolled) { + // User is still enrolled, open the course. + page = 'CoreCourseSectionPage'; + } else { + // User not enrolled (shouldn't happen), open the preview page. + page = 'CoreCoursesCoursePreviewPage'; + } + + return this.loginHelper.redirect(page, params, notification.site); + }).catch((error) => { + this.domUtils.showErrorModalDefault(error, 'Error getting course.'); + }).finally(() => { + modal.dismiss(); + }); + } + } +}