Merge pull request #1587 from crazyserver/MOBILE-2698
MOBILE-2698 calendar: Reduce core_group_course_user_groups requests
This commit is contained in:
		
						commit
						a5688bd1db
					
				| @ -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,46 +297,51 @@ 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) => { | ||||
|                     const now = this.timeUtils.timestamp(), | ||||
|                         start = now + (CoreConstants.SECONDS_DAY * daysToStart), | ||||
|                         end = start + (CoreConstants.SECONDS_DAY * daysInterval), | ||||
|                         data = { | ||||
|                             options: { | ||||
|                                 userevents: 1, | ||||
|                                 siteevents: 1, | ||||
|                                 timestart: start, | ||||
|                                 timeend: end | ||||
|                             }, | ||||
|                             events: { | ||||
|                                 courseids: [], | ||||
|                                 groupids: [] | ||||
|                             } | ||||
|                         }; | ||||
| 
 | ||||
|                     courses.forEach((course, index) => { | ||||
|                         data.events.courseids[index] = course.id; | ||||
|                     }); | ||||
| 
 | ||||
|                     groups.forEach((group, index) => { | ||||
|                         data.events.groupids[index] = group.id; | ||||
|                     }); | ||||
| 
 | ||||
|                     // We need to retrieve cached data using cache key because we have timestamp in the params.
 | ||||
|                     const preSets = { | ||||
|                         cacheKey: this.getEventsListCacheKey(daysToStart, daysInterval), | ||||
|                         getCacheUsingCacheKey: true | ||||
|             return Promise.all(promises).then(() => { | ||||
|                 const now = this.timeUtils.timestamp(), | ||||
|                     start = now + (CoreConstants.SECONDS_DAY * daysToStart), | ||||
|                     end = start + (CoreConstants.SECONDS_DAY * daysInterval), | ||||
|                     data = { | ||||
|                         options: { | ||||
|                             userevents: 1, | ||||
|                             siteevents: 1, | ||||
|                             timestart: start, | ||||
|                             timeend: end | ||||
|                         }, | ||||
|                         events: { | ||||
|                             courseids: [], | ||||
|                             groupids: [] | ||||
|                         } | ||||
|                     }; | ||||
| 
 | ||||
|                     return site.read('core_calendar_get_calendar_events', data, preSets).then((response) => { | ||||
|                         this.storeEventsInLocalDB(response.events, siteId); | ||||
|                 data.events.courseids = courses.map((course) => { | ||||
|                     return course.id; | ||||
|                 }); | ||||
|                 data.events.groupids = groups.map((group) => { | ||||
|                     return group.id; | ||||
|                 }); | ||||
| 
 | ||||
|                         return response.events; | ||||
|                     }); | ||||
|                 // We need to retrieve cached data using cache key because we have timestamp in the params.
 | ||||
|                 const preSets = { | ||||
|                     cacheKey: this.getEventsListCacheKey(daysToStart, daysInterval), | ||||
|                     getCacheUsingCacheKey: true | ||||
|                 }; | ||||
| 
 | ||||
|                 return site.read('core_calendar_get_calendar_events', data, preSets).then((response) => { | ||||
|                     this.storeEventsInLocalDB(response.events, siteId); | ||||
| 
 | ||||
|                     return response.events; | ||||
|                 }); | ||||
|             }); | ||||
|         }); | ||||
| @ -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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user