MOBILE-2698 calendar: Reduce core_group_course_user_groups requests
parent
f651dc983b
commit
0f90be3926
|
@ -291,7 +291,7 @@ export class AddonCalendarListPage implements OnDestroy {
|
|||
refreshEvents(refresher: any): void {
|
||||
const promises = [];
|
||||
|
||||
promises.push(this.calendarProvider.invalidateEventsList(this.courses));
|
||||
promises.push(this.calendarProvider.invalidateEventsList());
|
||||
|
||||
if (this.categoriesRetrieved) {
|
||||
promises.push(this.coursesProvider.invalidateCategories(0, true));
|
||||
|
|
|
@ -297,11 +297,18 @@ export class AddonCalendarProvider {
|
|||
: Promise<any[]> {
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
siteId = site.getId();
|
||||
const promises = [];
|
||||
let courses, groups;
|
||||
|
||||
return this.coursesProvider.getUserCourses(false, siteId).then((courses) => {
|
||||
promises.push(this.coursesProvider.getUserCourses(false, siteId).then((data) => {
|
||||
courses = data;
|
||||
courses.push({ id: site.getSiteHomeId() }); // Add front page.
|
||||
}));
|
||||
promises.push(this.groupsProvider.getAllUserGroups(siteId).then((data) => {
|
||||
groups = data;
|
||||
}));
|
||||
|
||||
return this.groupsProvider.getUserGroups(courses, siteId).then((groups) => {
|
||||
return Promise.all(promises).then(() => {
|
||||
const now = this.timeUtils.timestamp(),
|
||||
start = now + (CoreConstants.SECONDS_DAY * daysToStart),
|
||||
end = start + (CoreConstants.SECONDS_DAY * daysInterval),
|
||||
|
@ -318,12 +325,11 @@ export class AddonCalendarProvider {
|
|||
}
|
||||
};
|
||||
|
||||
courses.forEach((course, index) => {
|
||||
data.events.courseids[index] = course.id;
|
||||
data.events.courseids = courses.map((course) => {
|
||||
return course.id;
|
||||
});
|
||||
|
||||
groups.forEach((group, index) => {
|
||||
data.events.groupids[index] = group.id;
|
||||
data.events.groupids = groups.map((group) => {
|
||||
return group.id;
|
||||
});
|
||||
|
||||
// We need to retrieve cached data using cache key because we have timestamp in the params.
|
||||
|
@ -339,7 +345,6 @@ export class AddonCalendarProvider {
|
|||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -365,18 +370,17 @@ export class AddonCalendarProvider {
|
|||
/**
|
||||
* Invalidates events list and all the single events and related info.
|
||||
*
|
||||
* @param {any[]} courses List of courses or course ids.
|
||||
* @param {string} [siteId] Site Id. If not defined, use current site.
|
||||
* @return {Promise<any[]>} Promise resolved when the list is invalidated.
|
||||
*/
|
||||
invalidateEventsList(courses: any[], siteId?: string): Promise<any[]> {
|
||||
invalidateEventsList(siteId?: string): Promise<any[]> {
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
siteId = site.getId();
|
||||
|
||||
const promises = [];
|
||||
|
||||
promises.push(this.coursesProvider.invalidateUserCourses(siteId));
|
||||
promises.push(this.groupsProvider.invalidateUserGroups(courses, siteId));
|
||||
promises.push(this.groupsProvider.invalidateAllUserGroups(siteId));
|
||||
promises.push(site.invalidateWsCacheForKeyStartingWith(this.getEventsListPrefixCacheKey()));
|
||||
|
||||
return Promise.all(promises);
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
import { CoreSitesProvider } from './sites';
|
||||
import { CoreCoursesProvider } from '@core/courses/providers/courses';
|
||||
|
||||
/**
|
||||
* Group info for an activity.
|
||||
|
@ -50,7 +51,8 @@ export class CoreGroupsProvider {
|
|||
static VISIBLEGROUPS = 2;
|
||||
protected ROOT_CACHE_KEY = 'mmGroups:';
|
||||
|
||||
constructor(private sitesProvider: CoreSitesProvider, private translate: TranslateService) { }
|
||||
constructor(private sitesProvider: CoreSitesProvider, private translate: TranslateService,
|
||||
private coursesProvider: CoreCoursesProvider) { }
|
||||
|
||||
/**
|
||||
* Check if group mode of an activity is enabled.
|
||||
|
@ -204,6 +206,28 @@ export class CoreGroupsProvider {
|
|||
return this.ROOT_CACHE_KEY + 'groupmode:' + cmId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user groups in all the user enrolled courses.
|
||||
*
|
||||
* @param {string} [siteId] Site to get the groups from. If not defined, use current site.
|
||||
* @return {Promise<any[]>} Promise resolved when the groups are retrieved.
|
||||
*/
|
||||
getAllUserGroups(siteId?: string): Promise<any[]> {
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
siteId = siteId || site.getId();
|
||||
|
||||
if (site.isVersionGreaterEqualThan('3.6')) {
|
||||
return this.getUserGroupsInCourse(0, siteId);
|
||||
}
|
||||
|
||||
return this.coursesProvider.getUserCourses(false, siteId).then((courses) => {
|
||||
courses.push({ id: site.getSiteHomeId() }); // Add front page.
|
||||
|
||||
return this.getUserGroups(courses, siteId);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user groups in all the supplied courses.
|
||||
*
|
||||
|
@ -213,33 +237,31 @@ export class CoreGroupsProvider {
|
|||
* @return {Promise<any[]>} Promise resolved when the groups are retrieved.
|
||||
*/
|
||||
getUserGroups(courses: any[], siteId?: string, userId?: number): Promise<any[]> {
|
||||
const promises = [];
|
||||
let groups = [];
|
||||
|
||||
courses.forEach((course) => {
|
||||
// Get all courses one by one.
|
||||
const promises = courses.map((course) => {
|
||||
const courseId = typeof course == 'object' ? course.id : course;
|
||||
promises.push(this.getUserGroupsInCourse(courseId, siteId, userId).then((courseGroups) => {
|
||||
groups = groups.concat(courseGroups);
|
||||
}));
|
||||
|
||||
return this.getUserGroupsInCourse(courseId, siteId, userId);
|
||||
});
|
||||
|
||||
return Promise.all(promises).then(() => {
|
||||
return groups;
|
||||
return Promise.all(promises).then((courseGroups) => {
|
||||
return [].concat(...courseGroups);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user groups in a course.
|
||||
*
|
||||
* @param {number} courseId ID of the course.
|
||||
* @param {number} courseId ID of the course. 0 to get all enrolled courses groups (Moodle version > 3.6).
|
||||
* @param {string} [siteId] Site to get the groups from. If not defined, use current site.
|
||||
* @param {number} [userId] ID of the user. If not defined, use ID related to siteid.
|
||||
* @return {Promise<any[]>} Promise resolved when the groups are retrieved.
|
||||
*/
|
||||
getUserGroupsInCourse(courseId: number, siteId?: string, userId?: number): Promise<any[]> {
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
userId = userId || site.getUserId();
|
||||
const data = {
|
||||
userid: userId || site.getUserId(),
|
||||
userid: userId,
|
||||
courseid: courseId
|
||||
},
|
||||
preSets = {
|
||||
|
@ -256,6 +278,15 @@ export class CoreGroupsProvider {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get prefix cache key for user groups in course WS calls.
|
||||
*
|
||||
* @return {string} Prefix Cache key.
|
||||
*/
|
||||
protected getUserGroupsInCoursePrefixCacheKey(): string {
|
||||
return this.ROOT_CACHE_KEY + 'courseGroups:';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache key for user groups in course WS calls.
|
||||
*
|
||||
|
@ -264,7 +295,7 @@ export class CoreGroupsProvider {
|
|||
* @return {string} Cache key.
|
||||
*/
|
||||
protected getUserGroupsInCourseCacheKey(courseId: number, userId: number): string {
|
||||
return this.ROOT_CACHE_KEY + 'courseGroups:' + courseId + ':' + userId;
|
||||
return this.getUserGroupsInCoursePrefixCacheKey() + courseId + ':' + userId;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -312,6 +343,22 @@ export class CoreGroupsProvider {
|
|||
return Promise.all(promises);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidates user groups in all user enrolled courses.
|
||||
*
|
||||
* @param {string} [siteId] Site ID. If not defined, current site.
|
||||
* @return {Promise<any>} Promise resolved when the data is invalidated.
|
||||
*/
|
||||
invalidateAllUserGroups(siteId?: string): Promise<any> {
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
if (site.isVersionGreaterEqualThan('3.6')) {
|
||||
return this.invalidateUserGroupsInCourse(0, siteId);
|
||||
}
|
||||
|
||||
return site.invalidateWsCacheForKeyStartingWith(this.getUserGroupsInCoursePrefixCacheKey());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidates user groups in courses.
|
||||
*
|
||||
|
@ -322,13 +369,12 @@ export class CoreGroupsProvider {
|
|||
*/
|
||||
invalidateUserGroups(courses: any[], siteId?: string, userId?: number): Promise<any> {
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
const promises = [];
|
||||
|
||||
userId = userId || site.getUserId();
|
||||
|
||||
courses.forEach((course) => {
|
||||
const promises = courses.map((course) => {
|
||||
const courseId = typeof course == 'object' ? course.id : course;
|
||||
promises.push(this.invalidateUserGroupsInCourse(courseId, site.id, userId));
|
||||
|
||||
return this.invalidateUserGroupsInCourse(courseId, site.id, userId);
|
||||
});
|
||||
|
||||
return Promise.all(promises);
|
||||
|
@ -338,7 +384,7 @@ export class CoreGroupsProvider {
|
|||
/**
|
||||
* Invalidates user groups in course.
|
||||
*
|
||||
* @param {number} courseId Course ID.
|
||||
* @param {number} courseId ID of the course. 0 to get all enrolled courses groups (Moodle version > 3.6).
|
||||
* @param {string} [siteId] Site ID. If not defined, current site.
|
||||
* @param {number} [userId] User ID. If not defined, use current user.
|
||||
* @return {Promise<any>} Promise resolved when the data is invalidated.
|
||||
|
|
Loading…
Reference in New Issue