2020-11-12 09:18:44 +01:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// 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';
|
2022-07-20 14:44:07 +02:00
|
|
|
import { CoreNavigator } from '@services/navigator';
|
|
|
|
import { CoreSites } from '@services/sites';
|
2020-11-12 09:18:44 +01:00
|
|
|
|
2020-11-23 11:25:13 +01:00
|
|
|
import { makeSingleton, Translate } from '@singletons';
|
2020-11-12 09:18:44 +01:00
|
|
|
import { CoreUserRole } from './user';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Service that provides some features regarding users information.
|
|
|
|
*/
|
2020-12-10 13:21:05 +01:00
|
|
|
@Injectable({ providedIn: 'root' })
|
2020-11-12 09:18:44 +01:00
|
|
|
export class CoreUserHelperProvider {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats a user address, concatenating address, city and country.
|
|
|
|
*
|
|
|
|
* @param address Address.
|
|
|
|
* @param city City.
|
|
|
|
* @param country Country.
|
2022-12-01 12:31:00 +01:00
|
|
|
* @returns Formatted address.
|
2020-11-12 09:18:44 +01:00
|
|
|
*/
|
2020-11-13 09:08:58 +01:00
|
|
|
formatAddress(address?: string, city?: string, country?: string): string {
|
2021-03-02 11:41:04 +01:00
|
|
|
const separator = Translate.instant('core.listsep');
|
2020-11-12 09:18:44 +01:00
|
|
|
let values = [address, city, country];
|
|
|
|
|
2020-11-13 09:08:58 +01:00
|
|
|
values = values.filter((value) => value && value.length > 0);
|
2020-11-12 09:18:44 +01:00
|
|
|
|
|
|
|
return values.join(separator + ' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats a user role list, translating and concatenating them.
|
|
|
|
*
|
|
|
|
* @param roles List of user roles.
|
2022-12-01 12:31:00 +01:00
|
|
|
* @returns The formatted roles.
|
2020-11-12 09:18:44 +01:00
|
|
|
*/
|
|
|
|
formatRoleList(roles?: CoreUserRole[]): string {
|
|
|
|
if (!roles || roles.length <= 0) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
const separator = Translate.instant('core.listsep');
|
2020-11-12 09:18:44 +01:00
|
|
|
|
|
|
|
return roles.map((value) => {
|
2021-03-02 11:41:04 +01:00
|
|
|
const translation = Translate.instant('core.user.' + value.shortname);
|
2020-11-12 09:18:44 +01:00
|
|
|
|
|
|
|
return translation.indexOf('core.user.') < 0 ? translation : value.shortname;
|
|
|
|
}).join(separator + ' ');
|
|
|
|
}
|
|
|
|
|
2022-07-20 14:44:07 +02:00
|
|
|
/**
|
|
|
|
* Open a page with instructions on how to complete profile.
|
|
|
|
*
|
2022-08-04 09:54:09 +02:00
|
|
|
* @param siteId The site ID. Undefined for current site.
|
2022-07-20 14:44:07 +02:00
|
|
|
*/
|
2022-08-04 09:54:09 +02:00
|
|
|
async openCompleteProfile(siteId?: string): Promise<void> {
|
2022-07-20 14:44:07 +02:00
|
|
|
const currentSite = CoreSites.getCurrentSite();
|
2022-08-04 09:54:09 +02:00
|
|
|
siteId = siteId ?? currentSite?.getId();
|
|
|
|
|
2022-07-20 14:44:07 +02:00
|
|
|
if (!currentSite || siteId !== currentSite.getId()) {
|
|
|
|
return; // Site that triggered the event is not current site.
|
|
|
|
}
|
|
|
|
|
|
|
|
// If current page is already complete profile, stop.
|
|
|
|
if (CoreNavigator.isCurrent('/user/completeprofile')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await CoreNavigator.navigate('/user/completeprofile', { params: { siteId }, reset: true });
|
|
|
|
}
|
|
|
|
|
2020-11-12 09:18:44 +01:00
|
|
|
}
|
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
export const CoreUserHelper = makeSingleton(CoreUserHelperProvider);
|