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 { CoreSite, CoreSiteWSPreSets } from '@classes/site';
import { makeSingleton, Translate } from '@singletons/core.singletons';
import { CoreWSExternalWarning } from '@services/ws';
import { CoreCourseBase } from '@/types/global';
/*
* Service to handle groups.
*/
@Injectable()
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.
@ -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).
* @return Promise resolved with true if the activity has groups, resolved with false otherwise.
*/
activityHasGroups(cmId: number, siteId?: string, ignoreCache?: boolean): Promise<boolean> {
return this.getActivityGroupMode(cmId, siteId, ignoreCache).then((groupmode) => {
async activityHasGroups(cmId: number, siteId?: string, ignoreCache?: boolean): Promise<boolean> {
try {
const groupmode = await this.getActivityGroupMode(cmId, siteId, ignoreCache);
return groupmode === CoreGroupsProvider.SEPARATEGROUPS || groupmode === CoreGroupsProvider.VISIBLEGROUPS;
}).catch(() => {
} catch (error) {
return false;
});
}
}
/**
@ -56,8 +59,10 @@ export class CoreGroupsProvider {
* @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.
*/
getActivityAllowedGroups(cmId: number, userId?: number, siteId?: string, ignoreCache?: boolean): Promise<any> {
return CoreSites.instance.getSite(siteId).then((site) => {
async getActivityAllowedGroups(cmId: number, userId?: number, siteId?: string, ignoreCache?: boolean):
Promise<CoreGroupGetActivityAllowedGroupsResponse> {
const site = await CoreSites.instance.getSite(siteId);
userId = userId || site.getUserId();
const params = {
@ -74,14 +79,12 @@ export class CoreGroupsProvider {
preSets.emergencyCache = false;
}
return site.read('core_group_get_activity_allowed_groups', params, preSets).then((response) => {
const response = await site.read('core_group_get_activity_allowed_groups', params, preSets);
if (!response || !response.groups) {
return Promise.reject(null);
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).
* @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();
// 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) {
// Get the groups available for the user.
return this.getActivityAllowedGroups(cmId, userId, siteId, ignoreCache);
}
return {
groups: []
groups: [],
};
});
}
/**
@ -130,26 +133,26 @@ export class CoreGroupsProvider {
* @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.
*/
getActivityGroupInfo(cmId: number, addAllParts?: boolean, userId?: number, siteId?: string, ignoreCache?: boolean)
: Promise<CoreGroupInfo> {
async getActivityGroupInfo(cmId: number, addAllParts?: boolean, userId?: number, siteId?: string, ignoreCache?: boolean):
Promise<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;
let result: CoreGroupGetActivityAllowedGroupsResponse;
if (groupInfo.separateGroups || groupInfo.visibleGroups) {
return this.getActivityAllowedGroups(cmId, userId, siteId, ignoreCache);
result = await this.getActivityAllowedGroups(cmId, userId, siteId, ignoreCache);
} else {
result = {
groups: [],
};
}
return {
groups: [],
canaccessallgroups: false
};
}).then((result) => {
if (result.groups.length <= 0) {
groupInfo.separateGroups = false;
groupInfo.visibleGroups = false;
@ -167,7 +170,6 @@ export class CoreGroupsProvider {
}
return groupInfo;
});
}
/**
@ -178,8 +180,8 @@ export class CoreGroupsProvider {
* @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.
*/
getActivityGroupMode(cmId: number, siteId?: string, ignoreCache?: boolean): Promise<number> {
return CoreSites.instance.getSite(siteId).then((site) => {
async getActivityGroupMode(cmId: number, siteId?: string, ignoreCache?: boolean): Promise<number> {
const site = await CoreSites.instance.getSite(siteId);
const params = {
cmid: cmId,
};
@ -193,14 +195,12 @@ export class CoreGroupsProvider {
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') {
return Promise.reject(null);
throw null;
}
return response.groupmode;
});
});
}
/**
@ -219,8 +219,8 @@ export class CoreGroupsProvider {
* @param siteId Site to get the groups from. If not defined, use current site.
* @return Promise resolved when the groups are retrieved.
*/
getAllUserGroups(siteId?: string): Promise<any[]> {
return CoreSites.instance.getSite(siteId).then((site) => {
async getAllUserGroups(siteId?: string): Promise<CoreGroup[]> {
const site = await CoreSites.instance.getSite(siteId);
siteId = siteId || site.getId();
if (site.isVersionGreaterEqualThan('3.6')) {
@ -228,7 +228,6 @@ export class CoreGroupsProvider {
}
// @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.
* @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.
const promises = courses.map((course) => {
const courseId = typeof course == 'object' ? course.id : course;
const promises = this.getCourseIds(courses).map((courseId) => this.getUserGroupsInCourse(courseId, siteId, userId));
return this.getUserGroupsInCourse(courseId, siteId, userId);
});
const courseGroups = await Promise.all(promises);
return Promise.all(promises).then((courseGroups) => {
return [].concat(...courseGroups);
});
}
/**
@ -260,8 +255,8 @@ export class CoreGroupsProvider {
* @param userId ID of the user. If not defined, use ID related to siteid.
* @return Promise resolved when the groups are retrieved.
*/
getUserGroupsInCourse(courseId: number, siteId?: string, userId?: number): Promise<any[]> {
return CoreSites.instance.getSite(siteId).then((site) => {
async getUserGroupsInCourse(courseId: number, siteId?: string, userId?: number): Promise<CoreGroup[]> {
const site = await CoreSites.instance.getSite(siteId);
userId = userId || site.getUserId();
const data = {
userid: userId,
@ -272,14 +267,12 @@ export class CoreGroupsProvider {
updateFrequency: CoreSite.FREQUENCY_RARELY,
};
return site.read('core_group_get_course_user_groups', data, preSets).then((response) => {
if (response && response.groups) {
return response.groups;
} else {
return Promise.reject(null);
const response = await site.read('core_group_get_course_user_groups', data, preSets);
if (!response || !response.groups) {
throw null;
}
});
});
return response.groups;
}
/**
@ -310,12 +303,11 @@ export class CoreGroupsProvider {
* @param siteId Site ID. If not defined, current site.
* @return Promise resolved when the data is invalidated.
*/
invalidateActivityAllowedGroups(cmId: number, userId?: number, siteId?: string): Promise<any> {
return CoreSites.instance.getSite(siteId).then((site) => {
async invalidateActivityAllowedGroups(cmId: number, userId?: number, siteId?: string): Promise<void> {
const site = await CoreSites.instance.getSite(siteId);
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.
* @return Promise resolved when the data is invalidated.
*/
invalidateActivityGroupMode(cmId: number, siteId?: string): Promise<any> {
return CoreSites.instance.getSite(siteId).then((site) => {
return site.invalidateWsCacheForKey(this.getActivityGroupModeCacheKey(cmId));
});
async invalidateActivityGroupMode(cmId: number, siteId?: string): Promise<void> {
const site = await CoreSites.instance.getSite(siteId);
await site.invalidateWsCacheForKey(this.getActivityGroupModeCacheKey(cmId));
}
/**
@ -339,12 +331,12 @@ export class CoreGroupsProvider {
* @param siteId Site ID. If not defined, current site.
* @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 = [];
promises.push(this.invalidateActivityAllowedGroups(cmId, userId, 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.
* @return Promise resolved when the data is invalidated.
*/
invalidateAllUserGroups(siteId?: string): Promise<any> {
return CoreSites.instance.getSite(siteId).then((site) => {
async invalidateAllUserGroups(siteId?: string): Promise<void> {
const site = await CoreSites.instance.getSite(siteId);
if (site.isVersionGreaterEqualThan('3.6')) {
return this.invalidateUserGroupsInCourse(0, siteId);
}
return site.invalidateWsCacheForKeyStartingWith(this.getUserGroupsInCoursePrefixCacheKey());
});
await site.invalidateWsCacheForKeyStartingWith(this.getUserGroupsInCoursePrefixCacheKey());
}
/**
@ -371,18 +363,13 @@ export class CoreGroupsProvider {
* @param userId User ID. If not defined, use current user.
* @return Promise resolved when the data is invalidated.
*/
invalidateUserGroups(courses: any[], siteId?: string, userId?: number): Promise<any> {
return CoreSites.instance.getSite(siteId).then((site) => {
async invalidateUserGroups(courses: CoreCourseBase[] | number[], siteId?: string, userId?: number): Promise<void> {
const site = await CoreSites.instance.getSite(siteId);
userId = userId || site.getUserId();
const promises = courses.map((course) => {
const courseId = typeof course == 'object' ? course.id : course;
const promises = this.getCourseIds(courses).map((courseId) => this.invalidateUserGroupsInCourse(courseId, site.id, userId));
return this.invalidateUserGroupsInCourse(courseId, site.id, userId);
});
return Promise.all(promises);
});
await Promise.all(promises);
}
/**
@ -393,12 +380,11 @@ export class CoreGroupsProvider {
* @param userId User ID. If not defined, use current user.
* @return Promise resolved when the data is invalidated.
*/
invalidateUserGroupsInCourse(courseId: number, siteId?: string, userId?: number): Promise<any> {
return CoreSites.instance.getSite(siteId).then((site) => {
async invalidateUserGroupsInCourse(courseId: number, siteId?: string, userId?: number): Promise<void> {
const site = await CoreSites.instance.getSite(siteId);
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;
}
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) {}
/**
* 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.
*/
@ -429,7 +435,7 @@ export type CoreGroupInfo = {
/**
* List of groups.
*/
groups?: any[];
groups?: CoreGroup[];
/**
* Whether it's separate groups.
@ -446,3 +452,12 @@ export type CoreGroupInfo = {
*/
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.
};