From 2799bcdd5edb1b9e6fce98948fe859679fa2e4c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Tue, 18 Sep 2018 14:51:21 +0200 Subject: [PATCH] MOBILE-2554 course: Show all sections if non is defined --- .../course/formats/weeks/providers/handler.ts | 8 ++--- src/core/course/providers/default-format.ts | 30 +++++++------------ src/core/course/providers/format-delegate.ts | 10 ++----- src/core/courses/providers/courses.ts | 2 ++ 4 files changed, 19 insertions(+), 31 deletions(-) diff --git a/src/core/course/formats/weeks/providers/handler.ts b/src/core/course/formats/weeks/providers/handler.ts index 675e343b1..ccaef9d44 100644 --- a/src/core/course/formats/weeks/providers/handler.ts +++ b/src/core/course/formats/weeks/providers/handler.ts @@ -47,8 +47,8 @@ export class CoreCourseFormatWeeksHandler implements CoreCourseFormatHandler { const now = this.timeUtils.timestamp(); if (now < course.startdate || (course.enddate && now > course.enddate)) { - // Course hasn't started yet or it has ended already. Return the first section. - return sections[1]; + // Course hasn't started yet or it has ended already. Return all sections. + return sections[0]; } for (let i = 0; i < sections.length; i++) { @@ -63,8 +63,8 @@ export class CoreCourseFormatWeeksHandler implements CoreCourseFormatHandler { } } - // The section wasn't found, return the first section. - return sections[1]; + // The section wasn't found, return all sections. + return sections[0]; } /** diff --git a/src/core/course/providers/default-format.ts b/src/core/course/providers/default-format.ts index 45bcfe551..146f44232 100644 --- a/src/core/course/providers/default-format.ts +++ b/src/core/course/providers/default-format.ts @@ -98,38 +98,28 @@ export class CoreCourseFormatDefaultHandler implements CoreCourseFormatHandler { */ getCurrentSection(course: any, sections: any[]): any | Promise { if (!this.coursesProvider.isGetCoursesByFieldAvailable()) { - // Cannot get the current section, return the first one. - if (sections[0].id != CoreCourseProvider.ALL_SECTIONS_ID) { - return sections[0]; - } - - return sections[1]; + // Cannot get the current section, return all of them. + return sections[0]; } // 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]) { + if (courses && courses[0] && courses[0].marker > 0) { // Find the marked section. - const course = courses[0]; - for (let i = 0; i < sections.length; i++) { - const section = sections[i]; - if (section.section == course.marker) { - return section; - } - } - } + const course = courses[0], + section = sections.find((sect) => { + return sect.section == course.marker; + }); - // Marked section not found or we couldn't retrieve the marker. Return the first section. - for (let i = 0; i < sections.length; i++) { - const section = sections[i]; - if (section.id != CoreCourseProvider.ALL_SECTIONS_ID) { + if (section) { return section; } } - return Promise.reject(null); + // Marked section not found or we couldn't retrieve the marker. Return all sections. + return sections[0]; }); } diff --git a/src/core/course/providers/format-delegate.ts b/src/core/course/providers/format-delegate.ts index 4a2b476c4..40a2668d4 100644 --- a/src/core/course/providers/format-delegate.ts +++ b/src/core/course/providers/format-delegate.ts @@ -17,7 +17,6 @@ import { NavController } from 'ionic-angular'; import { CoreEventsProvider } from '@providers/events'; import { CoreLoggerProvider } from '@providers/logger'; import { CoreSitesProvider } from '@providers/sites'; -import { CoreCourseProvider } from './course'; import { CoreCourseFormatDefaultHandler } from './default-format'; import { CoreDelegate, CoreDelegateHandler } from '@classes/delegate'; @@ -285,14 +284,11 @@ export class CoreCourseFormatDelegate extends CoreDelegate { * @return {Promise} Promise resolved with current section. */ getCurrentSection(course: any, sections: any[]): Promise { + // Convert the result to a Promise if it isn't. return Promise.resolve(this.executeFunctionOnEnabled(course.format, 'getCurrentSection', [course, sections])).catch(() => { - // This function should never fail. Just return the first section. - if (sections[0].id != CoreCourseProvider.ALL_SECTIONS_ID) { - return sections[0]; - } - - return sections[1]; + // This function should never fail. Just return all the sections. + return sections[0]; }); } diff --git a/src/core/courses/providers/courses.ts b/src/core/courses/providers/courses.ts index b1adb7cb6..bf5345fb1 100644 --- a/src/core/courses/providers/courses.ts +++ b/src/core/courses/providers/courses.ts @@ -390,6 +390,7 @@ export class CoreCoursesProvider { * @param {any} [value] The value to match. * @param {string} [siteId] Site ID. If not defined, use current site. * @return {Promise} Promise resolved with the courses. + * @since 3.2 */ getCoursesByField(field?: string, value?: any, siteId?: string): Promise { siteId = siteId || this.sitesProvider.getCurrentSiteId(); @@ -473,6 +474,7 @@ export class CoreCoursesProvider { * Check if get courses by field WS is available. * * @return {boolean} Whether get courses by field is available. + * @since 3.2 */ isGetCoursesByFieldAvailable(): boolean { return this.sitesProvider.wsAvailableInCurrentSite('core_course_get_courses_by_field');