From fa46daeabc20072af1d5659e008ab1f27e80c1a3 Mon Sep 17 00:00:00 2001 From: dpalou Date: Thu, 25 Oct 2018 12:51:47 +0200 Subject: [PATCH] MOBILE-2668 course: Decrease calls to get by field --- src/core/course/providers/default-format.ts | 31 +++++++++++++-------- src/core/courses/providers/helper.ts | 9 ++++++ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/core/course/providers/default-format.ts b/src/core/course/providers/default-format.ts index 50ae2701c..19143d2a3 100644 --- a/src/core/course/providers/default-format.ts +++ b/src/core/course/providers/default-format.ts @@ -96,20 +96,29 @@ export class CoreCourseFormatDefaultHandler implements CoreCourseFormatHandler { * @return {any|Promise} Current section (or promise resolved with current section). */ getCurrentSection(course: any, sections: any[]): any | Promise { - if (!this.coursesProvider.isGetCoursesByFieldAvailable()) { - // Cannot get the current section, return all of them. - return sections[0]; - } + let promise; // We need the "marker" to determine the current section. - return this.coursesProvider.getCoursesByField('id', course.id).catch(() => { - // Ignore errors. - }).then((courses) => { - if (courses && courses[0] && courses[0].marker > 0) { + 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. + }).then((courses) => { + return courses && courses[0] && courses[0].marker; + }); + } + + return promise.then((marker) => { + if (marker > 0) { // Find the marked section. - const course = courses[0], - section = sections.find((sect) => { - return sect.section == course.marker; + const section = sections.find((sect) => { + return sect.section == marker; }); if (section) { diff --git a/src/core/courses/providers/helper.ts b/src/core/courses/providers/helper.ts index 078cd3e37..87d168465 100644 --- a/src/core/courses/providers/helper.ts +++ b/src/core/courses/providers/helper.ts @@ -54,6 +54,15 @@ export class CoreCoursesHelperProvider { * @return {Promise} Promise resolved when done. */ loadCoursesExtraInfo(courses: any[]): Promise { + 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()) { // No courses or cannot get the data, stop. return Promise.resolve();