From 96d548e706d8837535b6a7ed8cfbc9e98a3a9daa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Wed, 24 Jan 2018 13:49:53 +0100 Subject: [PATCH] MOBILE-2317 user: Add user links handler --- .../pages/course-preview/course-preview.html | 13 ++-- src/core/user/providers/helper.ts | 6 +- src/core/user/providers/user-link-handler.ts | 69 +++++++++++++++++++ src/core/user/user.module.ts | 9 ++- 4 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 src/core/user/providers/user-link-handler.ts diff --git a/src/core/courses/pages/course-preview/course-preview.html b/src/core/courses/pages/course-preview/course-preview.html index 452b2e79f..ec071512d 100644 --- a/src/core/courses/pages/course-preview/course-preview.html +++ b/src/core/courses/pages/course-preview/course-preview.html @@ -21,19 +21,20 @@ - -

{{ 'core.teachers' | translate }}

-

{{contact.fullname}}

-
+ + {{ 'core.teachers' | translate }} + {{contact.fullname}} + +
-

{{ instance.name }}

+

{{ instance.name }}

-

{{ 'core.courses.paypalaccepted' | translate }}

+

{{ 'core.courses.paypalaccepted' | translate }}

{{ 'core.paymentinstant' | translate }}

diff --git a/src/core/user/providers/helper.ts b/src/core/user/providers/helper.ts index e4d6eeb05..ba62a3f00 100644 --- a/src/core/user/providers/helper.ts +++ b/src/core/user/providers/helper.ts @@ -59,11 +59,9 @@ export class CoreUserHelperProvider { let separator = this.translate.instant('core.listsep'); - roles.map((value) => { + return roles.map((value) => { let translation = this.translate.instant('core.user.' + value.shortname); return translation.indexOf('core.user.') < 0 ? translation : value.shortname; - }); - - return roles.join(separator + " "); + }).join(separator + " "); } } diff --git a/src/core/user/providers/user-link-handler.ts b/src/core/user/providers/user-link-handler.ts new file mode 100644 index 000000000..bd2515783 --- /dev/null +++ b/src/core/user/providers/user-link-handler.ts @@ -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 '../../contentlinks/classes/base-handler'; +import { CoreContentLinksAction } from '../../contentlinks/providers/delegate'; +import { CoreLoginHelperProvider } from '../../login/providers/helper'; + +/** + * Handler to treat links to user profiles. + */ +@Injectable() +export class CoreUserProfileLinkHandler extends CoreContentLinksHandlerBase { + name = 'CoreUserProfileLinkHandler'; + // Match user/view.php and user/profile.php but NOT grade/report/user/. + pattern = /((\/user\/view\.php)|(\/user\/profile\.php)).*([\?\&]id=\d+)/; + + constructor(private loginHelper: CoreLoginHelperProvider) { + 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} List of (or promise resolved with list of) actions. + */ + getActions(siteIds: string[], url: string, params: any, courseId?: number) : + CoreContentLinksAction[]|Promise { + return [{ + action: (siteId, navCtrl?) => { + let stateParams = { + courseId: params.course, + userId: parseInt(params.id, 10) + }; + // Always use redirect to make it the new history root (to avoid "loops" in history). + this.loginHelper.redirect('CoreUserProfilePage', stateParams, 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} Whether the handler is enabled for the URL and site. + */ + isEnabled(siteId: string, url: string, params: any, courseId?: number) : boolean|Promise { + return url.indexOf('/grade/report/') == -1; + } +} diff --git a/src/core/user/user.module.ts b/src/core/user/user.module.ts index 89f35804b..62fb1af70 100644 --- a/src/core/user/user.module.ts +++ b/src/core/user/user.module.ts @@ -20,6 +20,8 @@ import { CoreUserHelperProvider } from './providers/helper'; import { CoreUserProfileMailHandler } from './providers/user-handler'; import { CoreEventsProvider } from '../../providers/events'; import { CoreSitesProvider } from '../../providers/sites'; +import { CoreContentLinksDelegate } from '../contentlinks/providers/delegate'; +import { CoreUserProfileLinkHandler } from './providers/user-link-handler'; @NgModule({ declarations: [ @@ -31,13 +33,16 @@ import { CoreSitesProvider } from '../../providers/sites'; CoreUserProfileFieldDelegate, CoreUserProfileMailHandler, CoreUserProvider, - CoreUserHelperProvider + CoreUserHelperProvider, + CoreUserProfileLinkHandler ] }) export class CoreUserModule { constructor(userDelegate: CoreUserDelegate, userProfileMailHandler: CoreUserProfileMailHandler, - eventsProvider: CoreEventsProvider, sitesProvider: CoreSitesProvider, userProvider: CoreUserProvider) { + eventsProvider: CoreEventsProvider, sitesProvider: CoreSitesProvider, userProvider: CoreUserProvider, + contentLinksDelegate: CoreContentLinksDelegate, userLinkHandler: CoreUserProfileLinkHandler) { userDelegate.registerHandler(userProfileMailHandler); + contentLinksDelegate.registerHandler(userLinkHandler); eventsProvider.on(CoreEventsProvider.USER_DELETED, (data) => { // Search for userid in params.