From ac08c758cecde68e9168a5e5a13f1cb2cb30a5ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Fri, 3 Dec 2021 11:10:44 +0100 Subject: [PATCH] MOBILE-3923 blocks: Fix recently accessed courses 3.7 downwards --- .../recentlyaccessedcourses.ts | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/addons/block/recentlyaccessedcourses/components/recentlyaccessedcourses/recentlyaccessedcourses.ts b/src/addons/block/recentlyaccessedcourses/components/recentlyaccessedcourses/recentlyaccessedcourses.ts index b6aef6614..6bbad0d5d 100644 --- a/src/addons/block/recentlyaccessedcourses/components/recentlyaccessedcourses/recentlyaccessedcourses.ts +++ b/src/addons/block/recentlyaccessedcourses/components/recentlyaccessedcourses/recentlyaccessedcourses.ts @@ -21,11 +21,16 @@ import { CoreCourses, CoreCourseSummaryData, } from '@features/courses/services/courses'; -import { CoreCourseSearchedDataWithExtraInfoAndOptions } from '@features/courses/services/courses-helper'; +import { + CoreCourseSearchedDataWithExtraInfoAndOptions, + CoreCoursesHelper, + CoreEnrolledCourseDataWithOptions, +} from '@features/courses/services/courses-helper'; import { CoreCourseOptionsDelegate } from '@features/course/services/course-options-delegate'; import { AddonCourseCompletion } from '@/addons/coursecompletion/services/coursecompletion'; import { CoreBlockBaseComponent } from '@features/block/classes/base-block-component'; import { CoreUtils } from '@services/utils/utils'; +import { CoreSite } from '@classes/site'; /** * Component to render a recent courses block. @@ -38,11 +43,12 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom @Input() downloadEnabled = false; - courses: (Omit & CoreCourseSearchedDataWithExtraInfoAndOptions)[] = []; + courses: AddonBlockRecentlyAccessedCourse[] = []; downloadCourseEnabled = false; scrollElementId!: string; + protected site!: CoreSite; protected isDestroyed = false; protected coursesObserver?: CoreEventObserver; protected updateSiteObserver?: CoreEventObserver; @@ -50,6 +56,8 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom constructor() { super('AddonBlockRecentlyAccessedCoursesComponent'); + + this.site = CoreSites.getRequiredCurrentSite(); } /** @@ -68,14 +76,14 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom this.updateSiteObserver = CoreEvents.on(CoreEvents.SITE_UPDATED, () => { this.downloadCourseEnabled = !CoreCourses.isDownloadCourseDisabledInSite(); - }, CoreSites.getCurrentSiteId()); + }, this.site.getId()); this.coursesObserver = CoreEvents.on( CoreCoursesProvider.EVENT_MY_COURSES_UPDATED, (data) => { this.refreshCourseList(data); }, - CoreSites.getCurrentSiteId(), + this.site.getId(), ); super.ngOnInit(); @@ -99,8 +107,12 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom protected async invalidateCourses(courseIds: number[]): Promise { const promises: Promise[] = []; + const invalidateCoursePromise = this.site.isVersionGreaterEqualThan('3.8') + ? CoreCourses.invalidateRecentCourses() + : CoreCourses.invalidateUserCourses(); + // Invalidate course completion data. - promises.push(CoreCourses.invalidateRecentCourses().finally(() => + promises.push(invalidateCoursePromise.finally(() => CoreUtils.allPromises(courseIds.map((courseId) => AddonCourseCompletion.invalidateCourseCompletion(courseId))))); @@ -123,6 +135,13 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom const showCategories = this.block.configsRecord && this.block.configsRecord.displaycategories && this.block.configsRecord.displaycategories.value == '1'; + // WS is failing on 3.7 and 3.6, use a fallback. + if (!this.site.isVersionGreaterEqualThan('3.8')) { + this.courses = await CoreCoursesHelper.getUserCoursesWithOptions('lastaccess', 10, undefined, showCategories); + + return; + } + const recentCourses = await CoreCourses.getRecentCourses(); const courseIds = recentCourses.map((course) => course.id); @@ -191,3 +210,9 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom } } + +type AddonBlockRecentlyAccessedCourse = + (Omit & CoreCourseSearchedDataWithExtraInfoAndOptions) | + (CoreEnrolledCourseDataWithOptions & { + categoryname?: string; // Category name, + });