Skip to content

Commit 0c7dfca

Browse files
committed
2
1 parent 310a35c commit 0c7dfca

File tree

2 files changed

+99
-39
lines changed

2 files changed

+99
-39
lines changed

src/core/features/user/services/user-helper.ts

+64-34
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { CoreNavigator } from '@services/navigator';
1717
import { CoreSites } from '@services/sites';
1818

1919
import { makeSingleton, Translate } from '@singletons';
20-
import { CoreUser, CoreUserProfile, CoreUserRole } from './user';
20+
import { CoreUser, CoreUserBasicData, CoreUserProfile, CoreUserRole } from './user';
2121
import { CoreTime } from '@singletons/time';
2222

2323
/**
@@ -90,24 +90,8 @@ export class CoreUserHelperProvider {
9090
*
9191
* @param user User object.
9292
* @returns User initials.
93-
* @deprecated since 4.4. Use getUserInitialsFromParts instead.
9493
*/
9594
getUserInitials(user: Partial<CoreUserProfile>): string {
96-
if (!user.firstname && !user.lastname) {
97-
// @TODO: Use local info or check WS to get initials from.
98-
return '';
99-
}
100-
101-
return (user.firstname?.charAt(0) || '') + (user.lastname?.charAt(0) || '');
102-
}
103-
104-
/**
105-
* Get the user initials.
106-
*
107-
* @param parts User name parts. Containing firstname, lastname, fullname and userId.
108-
* @returns User initials.
109-
*/
110-
async getUserInitialsFromParts(parts: CoreUserNameParts): Promise<string> {
11195
const nameFields = ['firstname', 'lastname'];
11296
const dummyUser = {
11397
firstname: 'firstname',
@@ -118,28 +102,30 @@ export class CoreUserHelperProvider {
118102
.filter((field) => nameFormat.indexOf(field) >= 0)
119103
.sort((a, b) => nameFormat.indexOf(a) - nameFormat.indexOf(b));
120104

121-
if (!parts.firstname && !parts.lastname) {
122-
if (!parts.fullname && parts.userId) {
123-
const user = await CoreUser.getProfile(parts.userId, undefined, true);
124-
parts.fullname = user.fullname || '';
125-
}
126-
127-
if (parts.fullname) {
128-
// It's a complete workaround.
129-
const split = parts.fullname.split(' ');
130-
parts.firstname = split[0];
131-
if (split.length > 1) {
132-
parts.lastname = split[split.length - 1];
133-
}
134-
}
135-
}
136-
137105
const initials = availableFieldsSorted.reduce((initials, fieldName) =>
138-
initials + (parts[fieldName]?.charAt(0) ?? ''), '');
106+
initials + (user[fieldName]?.charAt(0) ?? ''), '');
139107

140108
return initials || 'UNK';
141109
}
142110

111+
/**
112+
* Get the user initials.
113+
*
114+
* @param parts User name parts. Containing firstname, lastname, fullname and userId.
115+
* @returns User initials.
116+
*/
117+
async getUserInitialsFromParts(parts: CoreUserNameParts): Promise<string> {
118+
const initials = this.getUserInitials(parts);
119+
if (initials !== 'UNK' || !parts.userId) {
120+
return initials;
121+
}
122+
const user = await CoreUser.getProfile(parts.userId, undefined, false);
123+
console.error(user, parts.userId);
124+
125+
return user.initials || 'UNK';
126+
127+
}
128+
143129
/**
144130
* Translates legacy timezone names.
145131
*
@@ -151,8 +137,52 @@ export class CoreUserHelperProvider {
151137
return CoreTime.translateLegacyTimezone(tz);
152138
}
153139

140+
normalizeBasicFields<T extends CoreUserBasicData = CoreUserBasicData>(profile: CoreUserDenormalized): T {
141+
let normalized = {
142+
id: profile.id ?? profile.userid ?? 0,
143+
fullname: profile.fullname ?? profile.userfullname ?? '',
144+
profileimageurl: profile.profileimageurl ?? profile.userprofileimageurl ??
145+
profile.userpictureurl ?? profile.profileimageurlsmall ?? profile.urls?.profileimage ?? '',
146+
} as T;
147+
148+
delete profile.userid;
149+
delete profile.userfullname;
150+
delete profile.userpictureurl;
151+
delete profile.userprofileimageurl;
152+
delete profile.profileimageurlsmall;
153+
delete profile.urls;
154+
155+
normalized = { ...profile, ...normalized };
156+
157+
if (normalized.id === 0) {
158+
throw new Error('Invalid user ID');
159+
}
160+
161+
normalized.initials = CoreUserHelper.getUserInitials(profile);
162+
163+
return normalized;
164+
}
165+
154166
}
155167

156168
export const CoreUserHelper = makeSingleton(CoreUserHelperProvider);
157169

158170
type CoreUserNameParts = { firstname?: string; lastname?: string; fullname?: string; userId?: number };
171+
172+
type CoreUserDenormalized = CoreUserBasicData & {
173+
id?: number;
174+
userid?: number;
175+
176+
initials?: string; // Initials.
177+
178+
fullname?: string;
179+
userfullname?: string;
180+
181+
profileimageurl?: string;
182+
userpictureurl?: string;
183+
userprofileimageurl?: string;
184+
profileimageurlsmall?: string;
185+
urls?: {
186+
profileimage?: string;
187+
};
188+
};

src/core/features/user/services/user.ts

+35-5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { CorePromiseUtils } from '@singletons/promise-utils';
3333
import { CoreTextFormat } from '@singletons/text';
3434
import { CORE_USER_PROFILE_REFRESHED, CORE_USER_PROFILE_PICTURE_UPDATED, CORE_USER_PARTICIPANTS_LIST_LIMIT } from '../constants';
3535
import { CoreStoredCache } from '@classes/stored-cache';
36+
import { CoreUserHelper } from './user-helper';
3637

3738
declare module '@singletons/events' {
3839

@@ -349,7 +350,7 @@ export class CoreUserProvider {
349350
let users: CoreUserDescriptionExporter[] | CoreUserCourseProfile[] | undefined;
350351

351352
// Determine WS and data to use.
352-
if (courseId && courseId != site.getSiteHomeId()) {
353+
if (courseId && courseId !== site.getSiteHomeId()) {
353354
this.logger.debug(`Get participant with ID '${userId}' in course '${courseId}`);
354355

355356
const params: CoreUserGetCourseUserProfilesWSParams = {
@@ -362,6 +363,7 @@ export class CoreUserProvider {
362363
};
363364

364365
users = await site.read<CoreUserGetCourseUserProfilesWSResponse>('core_user_get_course_user_profiles', params, preSets);
366+
console.error('core_user_get_course_user_profiles', users);
365367
} else {
366368
this.logger.debug(`Get user with ID '${userId}'`);
367369

@@ -371,15 +373,16 @@ export class CoreUserProvider {
371373
};
372374

373375
users = await site.read<CoreUserGetUsersByFieldWSResponse>('core_user_get_users_by_field', params, preSets);
376+
console.error('core_user_get_users_by_field', users);
374377
}
375378

376379
if (users.length === 0) {
377380
// Shouldn't happen.
378381
throw new CoreError('Cannot retrieve user info.');
379382
}
380383

381-
const user = users[0];
382-
if ('country' in user && user.country) {
384+
const user = CoreUserHelper.normalizeBasicFields<CoreUserData | CoreUserCourseProfile>(users[0]);
385+
if (user.country) {
383386
user.country = CoreCountries.getCountryName(user.country);
384387
}
385388

@@ -725,6 +728,7 @@ export class CoreUserProvider {
725728
initials: user.initials,
726729
};
727730

731+
console.error(userRecord);
728732

729733
await this.userCache.setEntry(user.id, userRecord, siteId);
730734
}
@@ -854,10 +858,9 @@ export type CoreUserProfilePictureUpdatedData = {
854858
export type CoreUserBasicData = {
855859
id: number; // ID of the user.
856860
fullname: string; // The fullname of the user.
857-
profileimageurl: string; // User image profile URL - big version.
861+
profileimageurl?: string; // User image profile URL - big version.
858862
firstname?: string; // The first name(s) of the user.
859863
lastname?: string; // The family name of the user.
860-
lastaccess?: number;
861864
initials?: string; // Initials.
862865
};
863866

@@ -911,6 +914,33 @@ export type CoreUserEnrolledCourse = {
911914
shortname: string; // Shortname of the course.
912915
};
913916

917+
export type CoreUserNormalized = {
918+
id: number; // ID of the user.
919+
fullname: string; // The fullname of the user.
920+
profileimageurl: string; // User image profile URL - big version.
921+
username?: string; // The username.
922+
firstname?: string; // The first name(s) of the user.
923+
lastname?: string; // The family name of the user.
924+
initials?: string; // Initials, added by the app.
925+
email?: string; // An email address - allow email as root@localhost.
926+
address?: string; // Postal address.
927+
phone1?: string; // Phone 1.
928+
phone2?: string; // Phone 2.
929+
department?: string; // Department.
930+
idnumber?: string; // An arbitrary ID code number perhaps from the institution.
931+
interests?: string; // User interests (separated by commas).
932+
firstaccess?: number; // First access to the site (0 if never).
933+
lastaccess?: number; // Last access to the site (0 if never).
934+
timezone?: string; // Timezone code such as Australia/Perth, or 99 for default.
935+
trackforums?: number; // @since 4.4. Whether the user is tracking forums.
936+
description?: string; // User profile description.
937+
city?: string; // Home city of the user.
938+
url?: string; // URL of the user.
939+
country?: string; // Home country code of the user, such as AU or CZ.
940+
customfields?: CoreUserProfileField[]; // User custom fields (also known as user profile fields).
941+
preferences?: CoreUserPreference[]; // Users preferences.
942+
};
943+
914944
/**
915945
* Type for exporting a user description.
916946
* This relates to LMS core_user_external::user_description, do not modify unless the exporter changes.

0 commit comments

Comments
 (0)