Merge pull request #1578 from dpalou/MOBILE-2668
MOBILE-2668 course: Decrease calls to get by fieldmain
commit
39c0895eae
|
@ -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') {
|
||||||
// Ignore errors.
|
// We already have it.
|
||||||
}).then((courses) => {
|
promise = Promise.resolve(course.marker);
|
||||||
if (courses && courses[0] && courses[0].marker > 0) {
|
} 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.
|
// 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) {
|
||||||
|
|
|
@ -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();
|
||||||
|
|
Loading…
Reference in New Issue