Merge pull request #1578 from dpalou/MOBILE-2668

MOBILE-2668 course: Decrease calls to get by field
main
Juan Leyva 2018-10-25 15:20:02 +02:00 committed by GitHub
commit 39c0895eae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 11 deletions

View File

@ -96,20 +96,29 @@ export class CoreCourseFormatDefaultHandler implements CoreCourseFormatHandler {
* @return {any|Promise<any>} Current section (or promise resolved with current section). * @return {any|Promise<any>} Current section (or promise resolved with current section).
*/ */
getCurrentSection(course: any, sections: any[]): any | Promise<any> { getCurrentSection(course: any, sections: any[]): any | Promise<any> {
if (!this.coursesProvider.isGetCoursesByFieldAvailable()) { let promise;
// Cannot get the current section, return all of them.
return sections[0];
}
// We need the "marker" to determine the current section. // We need the "marker" to determine the current section.
return this.coursesProvider.getCoursesByField('id', course.id).catch(() => { if (typeof course.marker != 'undefined') {
// We already have it.
promise = Promise.resolve(course.marker);
} else if (!this.coursesProvider.isGetCoursesByFieldAvailable()) {
// Cannot get the current section, return all of them.
return sections[0];
} else {
// Try to retrieve the marker.
promise = this.coursesProvider.getCoursesByField('id', course.id).catch(() => {
// Ignore errors. // Ignore errors.
}).then((courses) => { }).then((courses) => {
if (courses && courses[0] && courses[0].marker > 0) { return courses && courses[0] && courses[0].marker;
});
}
return promise.then((marker) => {
if (marker > 0) {
// Find the marked section. // Find the marked section.
const course = courses[0], const section = sections.find((sect) => {
section = sections.find((sect) => { return sect.section == marker;
return sect.section == course.marker;
}); });
if (section) { if (section) {

View File

@ -54,6 +54,15 @@ export class CoreCoursesHelperProvider {
* @return {Promise<any>} Promise resolved when done. * @return {Promise<any>} Promise resolved when done.
*/ */
loadCoursesExtraInfo(courses: any[]): Promise<any> { loadCoursesExtraInfo(courses: any[]): Promise<any> {
if (courses[0] && typeof courses[0].overviewfiles != 'undefined' && typeof courses[0].displayname != 'undefined') {
// We already have the extra data. Call loadCourseExtraInfo to load the calculated fields.
courses.forEach((course) => {
this.loadCourseExtraInfo(course, course);
});
return Promise.resolve();
}
if (!courses.length || !this.coursesProvider.isGetCoursesByFieldAvailable()) { if (!courses.length || !this.coursesProvider.isGetCoursesByFieldAvailable()) {
// No courses or cannot get the data, stop. // No courses or cannot get the data, stop.
return Promise.resolve(); return Promise.resolve();