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';
|
2023-06-12 10:48:41 +02:00
|
|
|
import { CoreUserProfile, CoreUserRole } from './user';
|
2020-11-12 09:18:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
|
|
|
|
2023-10-06 11:37:13 +02:00
|
|
|
protected static readonly LEGACY_TIMEZONES = {
|
|
|
|
'-13.0': 'Australia/Perth',
|
|
|
|
'-12.5': 'Etc/GMT+12',
|
|
|
|
'-12.0': 'Etc/GMT+12',
|
|
|
|
'-11.5': 'Etc/GMT+11',
|
|
|
|
'-11.0': 'Etc/GMT+11',
|
|
|
|
'-10.5': 'Etc/GMT+10',
|
|
|
|
'-10.0': 'Etc/GMT+10',
|
|
|
|
'-9.5': 'Etc/GMT+9',
|
|
|
|
'-9.0': 'Etc/GMT+9',
|
|
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10.5': 'Etc/GMT-10',
|
|
|
|
'11.0': 'Etc/GMT-11',
|
|
|
|
'11.5': 'Etc/GMT-11',
|
|
|
|
'12.0': 'Etc/GMT-12',
|
|
|
|
'12.5': 'Etc/GMT-12',
|
|
|
|
'13.0': 'Etc/GMT-13',
|
|
|
|
};
|
|
|
|
|
2020-11-12 09:18:44 +01:00
|
|
|
/**
|
|
|
|
* 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 });
|
|
|
|
}
|
|
|
|
|
2023-06-12 10:48:41 +02:00
|
|
|
/**
|
|
|
|
* Get the user initials.
|
|
|
|
*
|
|
|
|
* @param user User object.
|
|
|
|
* @returns Promise resolved with the user data.
|
|
|
|
*/
|
|
|
|
getUserInitials(user: Partial<CoreUserProfile>): string {
|
|
|
|
if (!user.firstname && !user.lastname) {
|
|
|
|
// @TODO: Use local info or check WS to get initials from.
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return (user.firstname?.charAt(0) || '') + (user.lastname?.charAt(0) || '');
|
|
|
|
}
|
|
|
|
|
2023-10-06 11:37:13 +02:00
|
|
|
/**
|
|
|
|
* Translates legacy timezone names.
|
|
|
|
*
|
|
|
|
* @param tz Timezone name.
|
|
|
|
* @returns Readable timezone name.
|
|
|
|
*/
|
|
|
|
translateLegacyTimezone(tz: string): string {
|
|
|
|
return CoreUserHelperProvider.LEGACY_TIMEZONES[tz] ?? tz;
|
|
|
|
}
|
|
|
|
|
2020-11-12 09:18:44 +01:00
|
|
|
}
|
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
export const CoreUserHelper = makeSingleton(CoreUserHelperProvider);
|