From 211342e7258a2511968ac7269e277dee7241a352 Mon Sep 17 00:00:00 2001 From: Albert Gasset Date: Wed, 7 Aug 2024 14:52:43 +0200 Subject: [PATCH] MOBILE-4580 courses: Use new WS parameter to fetch course by section id --- src/core/features/courses/services/courses.ts | 3 +++ .../courses/services/handlers/section-link.ts | 20 ++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/core/features/courses/services/courses.ts b/src/core/features/courses/services/courses.ts index b56755dab..35f34ceb4 100644 --- a/src/core/features/courses/services/courses.ts +++ b/src/core/features/courses/services/courses.ts @@ -413,6 +413,7 @@ export class CoreCoursesProvider { * shortname: course short name. * idnumber: course id number. * category: category id the course belongs to. + * sectionid: section id that belongs to a course, since 4.5. * @param value The value to match. * @param siteId Site ID. If not defined, use current site. * @returns Promise resolved with the first course. @@ -436,6 +437,7 @@ export class CoreCoursesProvider { * shortname: course short name. * idnumber: course id number. * category: category id the course belongs to. + * sectionid: section id that belongs to a course, since 4.5. * @param value The value to match. * @param siteId Site ID. If not defined, use current site. * @returns Promise resolved with the courses. @@ -457,6 +459,7 @@ export class CoreCoursesProvider { * shortname: course short name. * idnumber: course id number. * category: category id the course belongs to. + * sectionid: section id that belongs to a course, since 4.5. * @param value The value to match. * @param options Other options. * @returns Observable that returns the courses. diff --git a/src/core/features/courses/services/handlers/section-link.ts b/src/core/features/courses/services/handlers/section-link.ts index 4a86ffe1a..933debb7c 100644 --- a/src/core/features/courses/services/handlers/section-link.ts +++ b/src/core/features/courses/services/handlers/section-link.ts @@ -15,7 +15,7 @@ import { Injectable } from '@angular/core'; import { makeSingleton } from '@singletons'; import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate'; -import { CoreCourses, CoreEnrolledCourseData } from '@features/courses/services/courses'; +import { CoreCourseBasicData, CoreCourses } from '@features/courses/services/courses'; import { CoreSites, CoreSitesReadingStrategy } from '@services/sites'; import { CoreCourse } from '@features/course/services/course'; import { CoreCoursesLinksHandlerBase } from '@features/courses/services/handlers/base-link-handler'; @@ -66,14 +66,24 @@ export class CoreCoursesSectionLinkHandlerService extends CoreCoursesLinksHandle * @param siteId Site id. * @returns Course. */ - private async getSectionCourse(sectionId: number | false, siteId: string | false): Promise { + private async getSectionCourse(sectionId: number | false, siteId: string | false): Promise { if (!siteId || !sectionId) { return null; } - // Ideally, we would use a webservice to get this information; but such webservice doesn't exists. - // Given that getting all the courses from a user could be very network intensive, all the requests - // in this method will only use cache. + const site = await CoreSites.getSite(siteId); + + if (site.isVersionGreaterEqualThan('4.5')) { + try { + return CoreCourses.getCourseByField('sectionid', sectionId, siteId); + } catch { + // Fallback to searching courses stored in cache. + } + } + + // In 4.4 and previous versions, the web service does not allow fetching a course by section id. + // Given that getting all the courses from a user could be very network intensive, the following + // requests will only use cache. const courses = await CoreUtils.ignoreErrors( CoreCourses.getUserCourses(true, siteId, CoreSitesReadingStrategy.ONLY_CACHE),