MOBILE-3227 filter: Get all enrolled courses filters at once

main
Dani Palou 2019-12-04 11:32:04 +01:00
parent dac9ba4de9
commit 466da5c7af
2 changed files with 117 additions and 53 deletions

View File

@ -99,42 +99,7 @@ export class CoreCoursesProvider {
if (courseIds.length == 1) { if (courseIds.length == 1) {
// Only 1 course, check if it belongs to the user courses. If so, use all user courses. // Only 1 course, check if it belongs to the user courses. If so, use all user courses.
return this.getUserCourses(true, siteId).then((courses) => { return this.getCourseIdsIfEnrolled(courseIds[0], siteId);
const courseId = courseIds[0];
let useAllCourses = false;
if (courseId == siteHomeId) {
// It's site home, use all courses.
useAllCourses = true;
} else {
for (let i = 0; i < courses.length; i++) {
if (courses[i].id == courseId) {
useAllCourses = true;
break;
}
}
}
if (useAllCourses) {
// User is enrolled, retrieve all the courses.
courseIds = courses.map((course) => {
return course.id;
});
// Always add the site home ID.
courseIds.push(siteHomeId);
// Sort the course IDs.
courseIds.sort((a, b) => {
return b - a;
});
}
return courseIds;
}).catch(() => {
// Ignore errors.
return courseIds;
});
} else { } else {
if (courseIds.length > 1 && courseIds.indexOf(siteHomeId) == -1) { if (courseIds.length > 1 && courseIds.indexOf(siteHomeId) == -1) {
courseIds.push(siteHomeId); courseIds.push(siteHomeId);
@ -150,6 +115,56 @@ export class CoreCoursesProvider {
}); });
} }
/**
* Given a course ID, if user is enrolled in the course it will return the IDs of all enrolled courses and site home.
* Return only the course ID otherwise.
*
* @param courseIds Course IDs.
* @param siteId Site Id. If not defined, use current site.
* @return Promise resolved with the list of course IDs.
*/
getCourseIdsIfEnrolled(courseId: number, siteId?: string): Promise<number[]> {
return this.sitesProvider.getSite(siteId).then((site) => {
const siteHomeId = site.getSiteHomeId();
// Check if user is enrolled in the course.
return this.getUserCourses(true, siteId).then((courses) => {
let useAllCourses = false;
if (courseId == siteHomeId) {
// It's site home, use all courses.
useAllCourses = true;
} else {
useAllCourses = !!courses.find((course) => {
return course.id == courseId;
});
}
if (useAllCourses) {
// User is enrolled, return all the courses.
const courseIds = courses.map((course) => {
return course.id;
});
// Always add the site home ID.
courseIds.push(siteHomeId);
// Sort the course IDs.
courseIds.sort((a, b) => {
return b - a;
});
return courseIds;
}
return [courseId];
}).catch(() => {
// Ignore errors.
return [courseId];
});
});
}
/** /**
* Check if download a whole course is disabled in a certain site. * Check if download a whole course is disabled in a certain site.
* *

View File

@ -20,6 +20,7 @@ import { CoreSitesProvider } from '@providers/sites';
import { CoreFilterDelegate } from './delegate'; import { CoreFilterDelegate } from './delegate';
import { CoreFilterProvider, CoreFilterFilter, CoreFilterFormatTextOptions, CoreFilterClassifiedFilters } from './filter'; import { CoreFilterProvider, CoreFilterFilter, CoreFilterFormatTextOptions, CoreFilterClassifiedFilters } from './filter';
import { CoreCourseProvider } from '@core/course/providers/course'; import { CoreCourseProvider } from '@core/course/providers/course';
import { CoreCoursesProvider } from '@core/courses/providers/courses';
import { CoreSite } from '@classes/site'; import { CoreSite } from '@classes/site';
/** /**
@ -45,11 +46,12 @@ export class CoreFilterHelperProvider {
constructor(logger: CoreLoggerProvider, constructor(logger: CoreLoggerProvider,
eventsProvider: CoreEventsProvider, eventsProvider: CoreEventsProvider,
private appProvider: CoreAppProvider, protected appProvider: CoreAppProvider,
private sitesProvider: CoreSitesProvider, protected sitesProvider: CoreSitesProvider,
private filterDelegate: CoreFilterDelegate, protected filterDelegate: CoreFilterDelegate,
private courseProvider: CoreCourseProvider, protected courseProvider: CoreCourseProvider,
private filterProvider: CoreFilterProvider) { protected filterProvider: CoreFilterProvider,
protected coursesProvider: CoreCoursesProvider) {
this.logger = logger.getInstance('CoreFilterHelperProvider'); this.logger = logger.getInstance('CoreFilterHelperProvider');
@ -62,6 +64,59 @@ export class CoreFilterHelperProvider {
}); });
} }
/**
* Get some filters from memory cache. If not in cache, get them and store them in cache.
*
* @param contextLevel The context level.
* @param instanceId Instance ID related to the context.
* @param options Options for format text.
* @param siteId Site ID. If not defined, current site.
* @return Promise resolved with the filters.
*/
protected getCacheableFilters(contextLevel: string, instanceId: number, getFilters: () => Promise<{contextlevel: string,
instanceid: number}[]>, options?: CoreFilterFormatTextOptions, site?: CoreSite): Promise<CoreFilterFilter[]> {
// Check the memory cache first.
const result = this.getFromMemoryCache(options.courseId, contextLevel, instanceId, site);
if (result) {
return Promise.resolve(result);
}
const siteId = site.getId();
return getFilters().then((contexts) => {
return this.filterProvider.getAvailableInContexts(contexts, siteId).then((filters) => {
this.storeInMemoryCache(options.courseId, filters, siteId);
return filters[contextLevel][instanceId] || [];
});
});
}
/**
* If user is enrolled in the course, return contexts of all enrolled courses to decrease number of WS requests.
*
* @param courseId Course ID.
* @param siteId Site ID. If not defined, current site.
* @return Promise resolved with the contexts.
*/
getCourseContexts(courseId: number, siteId?: string): Promise<{contextlevel: string, instanceid: number}[]> {
return this.coursesProvider.getCourseIdsIfEnrolled(courseId, siteId).then((courseIds) => {
const contexts: {contextlevel: string, instanceid: number}[] = [];
courseIds.forEach((courseId) => {
contexts.push({
contextlevel: 'course',
instanceid: courseId
});
});
return contexts;
});
}
/** /**
* Get the contexts of all course modules in a course. * Get the contexts of all course modules in a course.
* *
@ -137,21 +192,15 @@ export class CoreFilterHelperProvider {
if (contextLevel == 'module' && options.courseId) { if (contextLevel == 'module' && options.courseId) {
// Get all the modules filters with a single call to decrease the number of WS calls. // Get all the modules filters with a single call to decrease the number of WS calls.
// Check the memory cache first to speed up the process. const getFilters = this.getCourseModulesContexts.bind(this, options.courseId, siteId);
const result = this.getFromMemoryCache(options.courseId, contextLevel, instanceId, site); return this.getCacheableFilters(contextLevel, instanceId, getFilters, options, site);
if (result) {
return result;
}
return this.getCourseModulesContexts(options.courseId, siteId).then((contexts) => { } else if (contextLevel == 'course') {
// If enrolled, get all enrolled courses filters with a single call to decrease number of WS calls.
const getFilters = this.getCourseContexts.bind(this, instanceId, siteId);
return this.filterProvider.getAvailableInContexts(contexts, siteId).then((filters) => { return this.getCacheableFilters(contextLevel, instanceId, getFilters, options, site);
this.storeInMemoryCache(options.courseId, filters, siteId);
return filters[contextLevel][instanceId] || [];
});
});
} }
return this.filterProvider.getAvailableInContext(contextLevel, instanceId, siteId); return this.filterProvider.getAvailableInContext(contextLevel, instanceId, siteId);