MOBILE-3565 services: Fix group service linting

main
Pau Ferrer Ocaña 2020-10-08 11:17:07 +02:00
parent 1cf1a4a016
commit ddb5db51f2
2 changed files with 180 additions and 158 deletions

View File

@ -17,19 +17,20 @@ import { Injectable } from '@angular/core';
import { CoreSites } from '@services/sites'; import { CoreSites } from '@services/sites';
import { CoreSite, CoreSiteWSPreSets } from '@classes/site'; import { CoreSite, CoreSiteWSPreSets } from '@classes/site';
import { makeSingleton, Translate } from '@singletons/core.singletons'; import { makeSingleton, Translate } from '@singletons/core.singletons';
import { CoreWSExternalWarning } from '@services/ws';
import { CoreCourseBase } from '@/types/global';
/* /*
* Service to handle groups. * Service to handle groups.
*/ */
@Injectable() @Injectable()
export class CoreGroupsProvider { export class CoreGroupsProvider {
// Group mode constants.
static NOGROUPS = 0;
static SEPARATEGROUPS = 1;
static VISIBLEGROUPS = 2;
protected ROOT_CACHE_KEY = 'mmGroups:';
constructor() { } // Group mode constants.
static readonly NOGROUPS = 0;
static readonly SEPARATEGROUPS = 1;
static readonly VISIBLEGROUPS = 2;
protected readonly ROOT_CACHE_KEY = 'mmGroups:';
/** /**
* Check if group mode of an activity is enabled. * Check if group mode of an activity is enabled.
@ -39,12 +40,14 @@ export class CoreGroupsProvider {
* @param ignoreCache True if it should ignore cached data (it will always fail in offline or server down). * @param ignoreCache True if it should ignore cached data (it will always fail in offline or server down).
* @return Promise resolved with true if the activity has groups, resolved with false otherwise. * @return Promise resolved with true if the activity has groups, resolved with false otherwise.
*/ */
activityHasGroups(cmId: number, siteId?: string, ignoreCache?: boolean): Promise<boolean> { async activityHasGroups(cmId: number, siteId?: string, ignoreCache?: boolean): Promise<boolean> {
return this.getActivityGroupMode(cmId, siteId, ignoreCache).then((groupmode) => { try {
const groupmode = await this.getActivityGroupMode(cmId, siteId, ignoreCache);
return groupmode === CoreGroupsProvider.SEPARATEGROUPS || groupmode === CoreGroupsProvider.VISIBLEGROUPS; return groupmode === CoreGroupsProvider.SEPARATEGROUPS || groupmode === CoreGroupsProvider.VISIBLEGROUPS;
}).catch(() => { } catch (error) {
return false; return false;
}); }
} }
/** /**
@ -56,32 +59,32 @@ export class CoreGroupsProvider {
* @param ignoreCache True if it should ignore cached data (it will always fail in offline or server down). * @param ignoreCache True if it should ignore cached data (it will always fail in offline or server down).
* @return Promise resolved when the groups are retrieved. * @return Promise resolved when the groups are retrieved.
*/ */
getActivityAllowedGroups(cmId: number, userId?: number, siteId?: string, ignoreCache?: boolean): Promise<any> { async getActivityAllowedGroups(cmId: number, userId?: number, siteId?: string, ignoreCache?: boolean):
return CoreSites.instance.getSite(siteId).then((site) => { Promise<CoreGroupGetActivityAllowedGroupsResponse> {
userId = userId || site.getUserId(); const site = await CoreSites.instance.getSite(siteId);
const params = { userId = userId || site.getUserId();
cmid: cmId,
userid: userId,
};
const preSets: CoreSiteWSPreSets = {
cacheKey: this.getActivityAllowedGroupsCacheKey(cmId, userId),
updateFrequency: CoreSite.FREQUENCY_RARELY,
};
if (ignoreCache) { const params = {
preSets.getFromCache = false; cmid: cmId,
preSets.emergencyCache = false; userid: userId,
} };
const preSets: CoreSiteWSPreSets = {
cacheKey: this.getActivityAllowedGroupsCacheKey(cmId, userId),
updateFrequency: CoreSite.FREQUENCY_RARELY,
};
return site.read('core_group_get_activity_allowed_groups', params, preSets).then((response) => { if (ignoreCache) {
if (!response || !response.groups) { preSets.getFromCache = false;
return Promise.reject(null); preSets.emergencyCache = false;
} }
return response; const response = await site.read('core_group_get_activity_allowed_groups', params, preSets);
}); if (!response || !response.groups) {
}); throw null;
}
return response;
} }
/** /**
@ -104,20 +107,20 @@ export class CoreGroupsProvider {
* @param ignoreCache True if it should ignore cached data (it will always fail in offline or server down). * @param ignoreCache True if it should ignore cached data (it will always fail in offline or server down).
* @return Promise resolved when the groups are retrieved. If not allowed, empty array will be returned. * @return Promise resolved when the groups are retrieved. If not allowed, empty array will be returned.
*/ */
getActivityAllowedGroupsIfEnabled(cmId: number, userId?: number, siteId?: string, ignoreCache?: boolean): Promise<any[]> { async getActivityAllowedGroupsIfEnabled(cmId: number, userId?: number, siteId?: string, ignoreCache?: boolean):
Promise<CoreGroupGetActivityAllowedGroupsResponse> {
siteId = siteId || CoreSites.instance.getCurrentSiteId(); siteId = siteId || CoreSites.instance.getCurrentSiteId();
// Get real groupmode, in case it's forced by the course. // Get real groupmode, in case it's forced by the course.
return this.activityHasGroups(cmId, siteId, ignoreCache).then((hasGroups) => { const hasGroups = await this.activityHasGroups(cmId, siteId, ignoreCache);
if (hasGroups) { if (hasGroups) {
// Get the groups available for the user. // Get the groups available for the user.
return this.getActivityAllowedGroups(cmId, userId, siteId, ignoreCache); return this.getActivityAllowedGroups(cmId, userId, siteId, ignoreCache);
} }
return { return {
groups: [] groups: [],
}; };
});
} }
/** /**
@ -130,44 +133,43 @@ export class CoreGroupsProvider {
* @param ignoreCache True if it should ignore cached data (it will always fail in offline or server down). * @param ignoreCache True if it should ignore cached data (it will always fail in offline or server down).
* @return Promise resolved with the group info. * @return Promise resolved with the group info.
*/ */
getActivityGroupInfo(cmId: number, addAllParts?: boolean, userId?: number, siteId?: string, ignoreCache?: boolean) async getActivityGroupInfo(cmId: number, addAllParts?: boolean, userId?: number, siteId?: string, ignoreCache?: boolean):
: Promise<CoreGroupInfo> { Promise<CoreGroupInfo> {
const groupInfo: CoreGroupInfo = { const groupInfo: CoreGroupInfo = {
groups: [] groups: [],
}; };
return this.getActivityGroupMode(cmId, siteId, ignoreCache).then((groupMode) => { const groupMode = await this.getActivityGroupMode(cmId, siteId, ignoreCache);
groupInfo.separateGroups = groupMode === CoreGroupsProvider.SEPARATEGROUPS;
groupInfo.visibleGroups = groupMode === CoreGroupsProvider.VISIBLEGROUPS;
if (groupInfo.separateGroups || groupInfo.visibleGroups) { groupInfo.separateGroups = groupMode === CoreGroupsProvider.SEPARATEGROUPS;
return this.getActivityAllowedGroups(cmId, userId, siteId, ignoreCache); groupInfo.visibleGroups = groupMode === CoreGroupsProvider.VISIBLEGROUPS;
}
return { let result: CoreGroupGetActivityAllowedGroupsResponse;
if (groupInfo.separateGroups || groupInfo.visibleGroups) {
result = await this.getActivityAllowedGroups(cmId, userId, siteId, ignoreCache);
} else {
result = {
groups: [], groups: [],
canaccessallgroups: false
}; };
}).then((result) => { }
if (result.groups.length <= 0) {
groupInfo.separateGroups = false; if (result.groups.length <= 0) {
groupInfo.visibleGroups = false; groupInfo.separateGroups = false;
groupInfo.visibleGroups = false;
groupInfo.defaultGroupId = 0;
} else {
// The "canaccessallgroups" field was added in 3.4. Add all participants for visible groups in previous versions.
if (result.canaccessallgroups || (typeof result.canaccessallgroups == 'undefined' && groupInfo.visibleGroups)) {
groupInfo.groups.push({ id: 0, name: Translate.instance.instant('core.allparticipants') });
groupInfo.defaultGroupId = 0; groupInfo.defaultGroupId = 0;
} else { } else {
// The "canaccessallgroups" field was added in 3.4. Add all participants for visible groups in previous versions. groupInfo.defaultGroupId = result.groups[0].id;
if (result.canaccessallgroups || (typeof result.canaccessallgroups == 'undefined' && groupInfo.visibleGroups)) {
groupInfo.groups.push({ id: 0, name: Translate.instance.instant('core.allparticipants') });
groupInfo.defaultGroupId = 0;
} else {
groupInfo.defaultGroupId = result.groups[0].id;
}
groupInfo.groups = groupInfo.groups.concat(result.groups);
} }
return groupInfo; groupInfo.groups = groupInfo.groups.concat(result.groups);
}); }
return groupInfo;
} }
/** /**
@ -178,29 +180,27 @@ export class CoreGroupsProvider {
* @param ignoreCache True if it should ignore cached data (it will always fail in offline or server down). * @param ignoreCache True if it should ignore cached data (it will always fail in offline or server down).
* @return Promise resolved when the group mode is retrieved. * @return Promise resolved when the group mode is retrieved.
*/ */
getActivityGroupMode(cmId: number, siteId?: string, ignoreCache?: boolean): Promise<number> { async getActivityGroupMode(cmId: number, siteId?: string, ignoreCache?: boolean): Promise<number> {
return CoreSites.instance.getSite(siteId).then((site) => { const site = await CoreSites.instance.getSite(siteId);
const params = { const params = {
cmid: cmId, cmid: cmId,
}; };
const preSets: CoreSiteWSPreSets = { const preSets: CoreSiteWSPreSets = {
cacheKey: this.getActivityGroupModeCacheKey(cmId), cacheKey: this.getActivityGroupModeCacheKey(cmId),
updateFrequency: CoreSite.FREQUENCY_RARELY, updateFrequency: CoreSite.FREQUENCY_RARELY,
}; };
if (ignoreCache) { if (ignoreCache) {
preSets.getFromCache = false; preSets.getFromCache = false;
preSets.emergencyCache = false; preSets.emergencyCache = false;
} }
return site.read('core_group_get_activity_groupmode', params, preSets).then((response) => { const response = await site.read('core_group_get_activity_groupmode', params, preSets);
if (!response || typeof response.groupmode == 'undefined') { if (!response || typeof response.groupmode == 'undefined') {
return Promise.reject(null); throw null;
} }
return response.groupmode; return response.groupmode;
});
});
} }
/** /**
@ -219,16 +219,15 @@ export class CoreGroupsProvider {
* @param siteId Site to get the groups from. If not defined, use current site. * @param siteId Site to get the groups from. If not defined, use current site.
* @return Promise resolved when the groups are retrieved. * @return Promise resolved when the groups are retrieved.
*/ */
getAllUserGroups(siteId?: string): Promise<any[]> { async getAllUserGroups(siteId?: string): Promise<CoreGroup[]> {
return CoreSites.instance.getSite(siteId).then((site) => { const site = await CoreSites.instance.getSite(siteId);
siteId = siteId || site.getId(); siteId = siteId || site.getId();
if (site.isVersionGreaterEqualThan('3.6')) { if (site.isVersionGreaterEqualThan('3.6')) {
return this.getUserGroupsInCourse(0, siteId); return this.getUserGroupsInCourse(0, siteId);
} }
// @todo Get courses. // @todo Get courses.
});
} }
/** /**
@ -239,17 +238,13 @@ export class CoreGroupsProvider {
* @param userId ID of the user. If not defined, use the userId related to siteId. * @param userId ID of the user. If not defined, use the userId related to siteId.
* @return Promise resolved when the groups are retrieved. * @return Promise resolved when the groups are retrieved.
*/ */
getUserGroups(courses: any[], siteId?: string, userId?: number): Promise<any[]> { async getUserGroups(courses: CoreCourseBase[] | number[], siteId?: string, userId?: number): Promise<CoreGroup[]> {
// Get all courses one by one. // Get all courses one by one.
const promises = courses.map((course) => { const promises = this.getCourseIds(courses).map((courseId) => this.getUserGroupsInCourse(courseId, siteId, userId));
const courseId = typeof course == 'object' ? course.id : course;
return this.getUserGroupsInCourse(courseId, siteId, userId); const courseGroups = await Promise.all(promises);
});
return Promise.all(promises).then((courseGroups) => { return [].concat(...courseGroups);
return [].concat(...courseGroups);
});
} }
/** /**
@ -260,26 +255,24 @@ export class CoreGroupsProvider {
* @param userId ID of the user. If not defined, use ID related to siteid. * @param userId ID of the user. If not defined, use ID related to siteid.
* @return Promise resolved when the groups are retrieved. * @return Promise resolved when the groups are retrieved.
*/ */
getUserGroupsInCourse(courseId: number, siteId?: string, userId?: number): Promise<any[]> { async getUserGroupsInCourse(courseId: number, siteId?: string, userId?: number): Promise<CoreGroup[]> {
return CoreSites.instance.getSite(siteId).then((site) => { const site = await CoreSites.instance.getSite(siteId);
userId = userId || site.getUserId(); userId = userId || site.getUserId();
const data = { const data = {
userid: userId, userid: userId,
courseid: courseId, courseid: courseId,
}; };
const preSets = { const preSets = {
cacheKey: this.getUserGroupsInCourseCacheKey(courseId, userId), cacheKey: this.getUserGroupsInCourseCacheKey(courseId, userId),
updateFrequency: CoreSite.FREQUENCY_RARELY, updateFrequency: CoreSite.FREQUENCY_RARELY,
}; };
return site.read('core_group_get_course_user_groups', data, preSets).then((response) => { const response = await site.read('core_group_get_course_user_groups', data, preSets);
if (response && response.groups) { if (!response || !response.groups) {
return response.groups; throw null;
} else { }
return Promise.reject(null);
} return response.groups;
});
});
} }
/** /**
@ -310,12 +303,11 @@ export class CoreGroupsProvider {
* @param siteId Site ID. If not defined, current site. * @param siteId Site ID. If not defined, current site.
* @return Promise resolved when the data is invalidated. * @return Promise resolved when the data is invalidated.
*/ */
invalidateActivityAllowedGroups(cmId: number, userId?: number, siteId?: string): Promise<any> { async invalidateActivityAllowedGroups(cmId: number, userId?: number, siteId?: string): Promise<void> {
return CoreSites.instance.getSite(siteId).then((site) => { const site = await CoreSites.instance.getSite(siteId);
userId = userId || site.getUserId(); userId = userId || site.getUserId();
return site.invalidateWsCacheForKey(this.getActivityAllowedGroupsCacheKey(cmId, userId)); await site.invalidateWsCacheForKey(this.getActivityAllowedGroupsCacheKey(cmId, userId));
});
} }
/** /**
@ -325,10 +317,10 @@ export class CoreGroupsProvider {
* @param siteId Site ID. If not defined, current site. * @param siteId Site ID. If not defined, current site.
* @return Promise resolved when the data is invalidated. * @return Promise resolved when the data is invalidated.
*/ */
invalidateActivityGroupMode(cmId: number, siteId?: string): Promise<any> { async invalidateActivityGroupMode(cmId: number, siteId?: string): Promise<void> {
return CoreSites.instance.getSite(siteId).then((site) => { const site = await CoreSites.instance.getSite(siteId);
return site.invalidateWsCacheForKey(this.getActivityGroupModeCacheKey(cmId));
}); await site.invalidateWsCacheForKey(this.getActivityGroupModeCacheKey(cmId));
} }
/** /**
@ -339,12 +331,12 @@ export class CoreGroupsProvider {
* @param siteId Site ID. If not defined, current site. * @param siteId Site ID. If not defined, current site.
* @return Promise resolved when the data is invalidated. * @return Promise resolved when the data is invalidated.
*/ */
invalidateActivityGroupInfo(cmId: number, userId?: number, siteId?: string): Promise<any> { async invalidateActivityGroupInfo(cmId: number, userId?: number, siteId?: string): Promise<void> {
const promises = []; const promises = [];
promises.push(this.invalidateActivityAllowedGroups(cmId, userId, siteId)); promises.push(this.invalidateActivityAllowedGroups(cmId, userId, siteId));
promises.push(this.invalidateActivityGroupMode(cmId, siteId)); promises.push(this.invalidateActivityGroupMode(cmId, siteId));
return Promise.all(promises); await Promise.all(promises);
} }
/** /**
@ -353,14 +345,14 @@ export class CoreGroupsProvider {
* @param siteId Site ID. If not defined, current site. * @param siteId Site ID. If not defined, current site.
* @return Promise resolved when the data is invalidated. * @return Promise resolved when the data is invalidated.
*/ */
invalidateAllUserGroups(siteId?: string): Promise<any> { async invalidateAllUserGroups(siteId?: string): Promise<void> {
return CoreSites.instance.getSite(siteId).then((site) => { const site = await CoreSites.instance.getSite(siteId);
if (site.isVersionGreaterEqualThan('3.6')) {
return this.invalidateUserGroupsInCourse(0, siteId);
}
return site.invalidateWsCacheForKeyStartingWith(this.getUserGroupsInCoursePrefixCacheKey()); if (site.isVersionGreaterEqualThan('3.6')) {
}); return this.invalidateUserGroupsInCourse(0, siteId);
}
await site.invalidateWsCacheForKeyStartingWith(this.getUserGroupsInCoursePrefixCacheKey());
} }
/** /**
@ -371,18 +363,13 @@ export class CoreGroupsProvider {
* @param userId User ID. If not defined, use current user. * @param userId User ID. If not defined, use current user.
* @return Promise resolved when the data is invalidated. * @return Promise resolved when the data is invalidated.
*/ */
invalidateUserGroups(courses: any[], siteId?: string, userId?: number): Promise<any> { async invalidateUserGroups(courses: CoreCourseBase[] | number[], siteId?: string, userId?: number): Promise<void> {
return CoreSites.instance.getSite(siteId).then((site) => { const site = await CoreSites.instance.getSite(siteId);
userId = userId || site.getUserId(); userId = userId || site.getUserId();
const promises = courses.map((course) => { const promises = this.getCourseIds(courses).map((courseId) => this.invalidateUserGroupsInCourse(courseId, site.id, userId));
const courseId = typeof course == 'object' ? course.id : course;
return this.invalidateUserGroupsInCourse(courseId, site.id, userId); await Promise.all(promises);
});
return Promise.all(promises);
});
} }
/** /**
@ -393,12 +380,11 @@ export class CoreGroupsProvider {
* @param userId User ID. If not defined, use current user. * @param userId User ID. If not defined, use current user.
* @return Promise resolved when the data is invalidated. * @return Promise resolved when the data is invalidated.
*/ */
invalidateUserGroupsInCourse(courseId: number, siteId?: string, userId?: number): Promise<any> { async invalidateUserGroupsInCourse(courseId: number, siteId?: string, userId?: number): Promise<void> {
return CoreSites.instance.getSite(siteId).then((site) => { const site = await CoreSites.instance.getSite(siteId);
userId = userId || site.getUserId(); userId = userId || site.getUserId();
return site.invalidateWsCacheForKey(this.getUserGroupsInCourseCacheKey(courseId, userId)); await site.invalidateWsCacheForKey(this.getUserGroupsInCourseCacheKey(courseId, userId));
});
} }
/** /**
@ -418,10 +404,30 @@ export class CoreGroupsProvider {
return groupInfo.defaultGroupId; return groupInfo.defaultGroupId;
} }
protected getCourseIds(courses: CoreCourseBase[] | number[]): number[] {
return courses.length > 0 && typeof courses[0] === 'object'
? (courses as CoreCourseBase[]).map((course) => course.id)
: courses as number[];
}
} }
export class CoreGroups extends makeSingleton(CoreGroupsProvider) {} export class CoreGroups extends makeSingleton(CoreGroupsProvider) {}
/**
* Specific group info.
*/
export type CoreGroup = {
id: number; // Group ID.
name: string; // Multilang compatible name, course unique'.
description?: string; // Group description text.
descriptionformat?: number; // Description format (1 = HTML, 0 = MOODLE, 2 = PLAIN or 4 = MARKDOWN).
idnumber?: string; // Id number.
courseid?: number; // Coure Id.
};
/** /**
* Group info for an activity. * Group info for an activity.
*/ */
@ -429,7 +435,7 @@ export type CoreGroupInfo = {
/** /**
* List of groups. * List of groups.
*/ */
groups?: any[]; groups?: CoreGroup[];
/** /**
* Whether it's separate groups. * Whether it's separate groups.
@ -446,3 +452,12 @@ export type CoreGroupInfo = {
*/ */
defaultGroupId?: number; defaultGroupId?: number;
}; };
/**
* WS core_group_get_activity_allowed_groups response type.
*/
export type CoreGroupGetActivityAllowedGroupsResponse = {
groups: CoreGroup[]; // List of groups.
canaccessallgroups?: boolean; // Whether the user will be able to access all the activity groups.
warnings?: CoreWSExternalWarning[];
};

View File

@ -24,3 +24,10 @@ declare global {
} }
} }
/**
* Course base definition.
*/
export type CoreCourseBase = {
id: number; // Course Id.
};