diff --git a/src/core/course/pages/section/section.ts b/src/core/course/pages/section/section.ts index 65adf6fb4..7246d3fd9 100644 --- a/src/core/course/pages/section/section.ts +++ b/src/core/course/pages/section/section.ts @@ -169,7 +169,9 @@ export class CoreCourseSectionPage implements OnDestroy { */ protected loadData(refresh?: boolean, sync?: boolean): Promise { // First of all, get the course because the data might have changed. - return this.coursesProvider.getUserCourse(this.course.id).catch(() => { + return this.courseHelper.getCourse(this.course.id).then((result) => { + return result.course; + }).catch(() => { // Error getting the course, probably guest access. }).then((course) => { if (course) { diff --git a/src/core/course/providers/helper.ts b/src/core/course/providers/helper.ts index 941a2f9c4..aa57300b6 100644 --- a/src/core/course/providers/helper.ts +++ b/src/core/course/providers/helper.ts @@ -767,6 +767,33 @@ export class CoreCourseHelperProvider { }); } + /** + * Get a course. It will first check the user courses, and fallback to another WS if not enrolled. + * + * @param {number} courseId Course ID. + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise<{enrolled: boolean, course: any}>} Promise resolved with the course. + */ + getCourse(courseId: number, siteId?: string): Promise<{enrolled: boolean, course: any}> { + siteId = siteId || this.sitesProvider.getCurrentSiteId(); + + // Try with enrolled courses first. + return this.coursesProvider.getUserCourse(courseId, false, siteId).then((course) => { + return { enrolled: true, course: course }; + }).catch(() => { + // Not enrolled or an error happened. Try to use another WebService. + return this.coursesProvider.isGetCoursesByFieldAvailableInSite(siteId).then((available) => { + if (available) { + return this.coursesProvider.getCourseByField('id', courseId, siteId); + } else { + return this.coursesProvider.getCourse(courseId, siteId); + } + }).then((course) => { + return { enrolled: false, course: course }; + }); + }); + } + /** * Check if the course has a block with that name. * diff --git a/src/core/courses/providers/courses.ts b/src/core/courses/providers/courses.ts index d4e492aa8..2760b2f0c 100644 --- a/src/core/courses/providers/courses.ts +++ b/src/core/courses/providers/courses.ts @@ -389,6 +389,30 @@ export class CoreCoursesProvider { } } + /** + * Get the first course returned by getCoursesByField. + * + * @param {string} [field] The field to search. Can be left empty for all courses or: + * id: course id. + * ids: comma separated course ids. + * shortname: course short name. + * idnumber: course id number. + * category: category id the course belongs to. + * @param {any} [value] The value to match. + * @param {string} [siteId] Site ID. If not defined, use current site. + * @return {Promise} Promise resolved with the first course. + * @since 3.2 + */ + getCourseByField(field?: string, value?: any, siteId?: string): Promise { + return this.getCoursesByField(field, value, siteId).then((courses) => { + if (courses && courses.length > 0) { + return courses[0]; + } + + return Promise.reject(null); + }); + } + /** * Get courses. They can be filtered by field. * @@ -482,13 +506,29 @@ export class CoreCoursesProvider { } /** - * Check if get courses by field WS is available. + * Check if get courses by field WS is available in a certain site. * + * @param {CoreSite} [site] Site to check. * @return {boolean} Whether get courses by field is available. * @since 3.2 */ - isGetCoursesByFieldAvailable(): boolean { - return this.sitesProvider.wsAvailableInCurrentSite('core_course_get_courses_by_field'); + isGetCoursesByFieldAvailable(site?: CoreSite): boolean { + site = site || this.sitesProvider.getCurrentSite(); + + return site.wsAvailable('core_course_get_courses_by_field'); + } + + /** + * Check if get courses by field WS is available in a certain site, by site ID. + * + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise} Promise resolved with boolean: whether get courses by field is available. + * @since 3.2 + */ + isGetCoursesByFieldAvailableInSite(siteId?: string): Promise { + return this.sitesProvider.getSite(siteId).then((site) => { + return this.isGetCoursesByFieldAvailable(site); + }); } /**