MOBILE-2675 course: Fallback for fetching module info in offline
parent
8a4563aa28
commit
d4b3496f79
|
@ -303,35 +303,18 @@ export class CoreCourseProvider {
|
||||||
siteId?: string, modName?: string): Promise<any> {
|
siteId?: string, modName?: string): Promise<any> {
|
||||||
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
||||||
|
|
||||||
let promise;
|
// Helper function to do the WS request without processing the result.
|
||||||
|
const doRequest = (site: CoreSite, moduleId: number, modName: string, includeStealth: boolean, preferCache: boolean):
|
||||||
if (!courseId) {
|
Promise<any> => {
|
||||||
// No courseId passed, try to retrieve it.
|
|
||||||
promise = this.getModuleBasicInfo(moduleId, siteId).then((module) => {
|
|
||||||
return module.course;
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
promise = Promise.resolve(courseId);
|
|
||||||
}
|
|
||||||
|
|
||||||
return promise.then((cid) => {
|
|
||||||
courseId = cid;
|
|
||||||
|
|
||||||
// Get the site.
|
|
||||||
return this.sitesProvider.getSite(siteId);
|
|
||||||
}).then((site) => {
|
|
||||||
// We have courseId, we can use core_course_get_contents for compatibility.
|
|
||||||
this.logger.debug(`Getting module ${moduleId} in course ${courseId}`);
|
|
||||||
|
|
||||||
const params: any = {
|
const params: any = {
|
||||||
courseid: courseId,
|
courseid: courseId,
|
||||||
options: []
|
options: []
|
||||||
},
|
};
|
||||||
preSets: any = {
|
const preSets: CoreSiteWSPreSets = {
|
||||||
omitExpires: preferCache
|
omitExpires: preferCache
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.canRequestStealthModules(site)) {
|
if (includeStealth) {
|
||||||
params.options.push({
|
params.options.push({
|
||||||
name: 'includestealthmodules',
|
name: 'includestealthmodules',
|
||||||
value: 1
|
value: 1
|
||||||
|
@ -354,23 +337,61 @@ export class CoreCourseProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!preferCache && ignoreCache) {
|
if (!preferCache && ignoreCache) {
|
||||||
preSets.getFromCache = 0;
|
preSets.getFromCache = false;
|
||||||
preSets.emergencyCache = 0;
|
preSets.emergencyCache = false;
|
||||||
}
|
|
||||||
|
|
||||||
if (sectionId) {
|
|
||||||
params.options.push({
|
|
||||||
name: 'sectionid',
|
|
||||||
value: sectionId
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return site.read('core_course_get_contents', params, preSets).catch(() => {
|
return site.read('core_course_get_contents', params, preSets).catch(() => {
|
||||||
|
// The module might still be cached by a request with different parameters.
|
||||||
|
if (!ignoreCache && !this.appProvider.isOnline()) {
|
||||||
|
if (includeStealth) {
|
||||||
|
// Older versions didn't include the includestealthmodules option.
|
||||||
|
return doRequest(site, moduleId, modName, false, true);
|
||||||
|
} else if (modName) {
|
||||||
|
// Falback to the request for the given moduleId only.
|
||||||
|
return doRequest(site, moduleId, undefined, this.canRequestStealthModules(site), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.reject(null);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
let promise;
|
||||||
|
if (!courseId) {
|
||||||
|
// No courseId passed, try to retrieve it.
|
||||||
|
promise = this.getModuleBasicInfo(moduleId, siteId).then((module) => {
|
||||||
|
courseId = module.course;
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
promise = Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
return promise.then(() => {
|
||||||
|
return this.sitesProvider.getSite(siteId);
|
||||||
|
}).then((site) => {
|
||||||
|
// We have courseId, we can use core_course_get_contents for compatibility.
|
||||||
|
this.logger.debug(`Getting module ${moduleId} in course ${courseId}`);
|
||||||
|
|
||||||
|
return doRequest(site, moduleId, modName, this.canRequestStealthModules(site), preferCache);
|
||||||
|
}).catch(() => {
|
||||||
// Error getting the module. Try to get all contents (without filtering by module).
|
// Error getting the module. Try to get all contents (without filtering by module).
|
||||||
|
const preSets: CoreSiteWSPreSets = {
|
||||||
|
omitExpires: preferCache
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!preferCache && ignoreCache) {
|
||||||
|
preSets.getFromCache = false;
|
||||||
|
preSets.emergencyCache = false;
|
||||||
|
}
|
||||||
|
|
||||||
return this.getSections(courseId, false, false, preSets, siteId);
|
return this.getSections(courseId, false, false, preSets, siteId);
|
||||||
}).then((sections) => {
|
}).then((sections) => {
|
||||||
for (let i = 0; i < sections.length; i++) {
|
for (let i = 0; i < sections.length; i++) {
|
||||||
const section = sections[i];
|
const section = sections[i];
|
||||||
|
if (sectionId != null && !isNaN(sectionId) && sectionId != section.id) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
for (let j = 0; j < section.modules.length; j++) {
|
for (let j = 0; j < section.modules.length; j++) {
|
||||||
const module = section.modules[j];
|
const module = section.modules[j];
|
||||||
if (module.id == moduleId) {
|
if (module.id == moduleId) {
|
||||||
|
@ -383,7 +404,6 @@ export class CoreCourseProvider {
|
||||||
|
|
||||||
return Promise.reject(null);
|
return Promise.reject(null);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue