MOBILE-2921 course: Support course request push clicks
parent
ac96739e39
commit
a83ebdfec8
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<boolean> {
|
||||
// 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<any>} Promise resolved when done.
|
||||
*/
|
||||
handleClick(notification: any): Promise<any> {
|
||||
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();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue