diff --git a/src/addons/block/myoverview/components/myoverview/myoverview.ts b/src/addons/block/myoverview/components/myoverview/myoverview.ts index 6bbe6b2a9..1d71653d0 100644 --- a/src/addons/block/myoverview/components/myoverview/myoverview.ts +++ b/src/addons/block/myoverview/components/myoverview/myoverview.ts @@ -16,8 +16,8 @@ import { Component, OnInit, Input, OnDestroy, ViewChild, OnChanges, SimpleChange import { IonSearchbar } from '@ionic/angular'; import { CoreEventObserver, CoreEvents } from '@singletons/events'; import { CoreTimeUtils } from '@services/utils/time'; -import { CoreSites } from '@services/sites'; -import { CoreCoursesProvider, CoreCourses } from '@features/courses/services/courses'; +import { CoreSites, CoreSitesReadingStrategy } from '@services/sites'; +import { CoreCoursesProvider, CoreCourses, CoreCoursesMyCoursesUpdatedEventData } from '@features/courses/services/courses'; import { CoreCoursesHelper, CoreEnrolledCourseDataWithOptions } from '@features/courses/services/courses-helper'; import { CoreCourseHelper, CorePrefetchStatusInfo } from '@features/course/services/course-helper'; import { CoreCourseOptionsDelegate } from '@features/course/services/course-options-delegate'; @@ -162,7 +162,7 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem CoreCoursesProvider.EVENT_MY_COURSES_UPDATED, (data) => { - if (data.action == CoreCoursesProvider.ACTION_ENROL || data.action == CoreCoursesProvider.ACTION_STATE_CHANGED) { + if (this.shouldRefreshOnUpdatedEvent(data)) { this.refreshCourseList(); } }, @@ -224,16 +224,16 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem } /** - * Fetch the courses for my overview. - * - * @return Promise resolved when done. + * @inheritdoc */ - protected async fetchContent(): Promise { + protected async fetchContent(refresh?: boolean): Promise { const config = this.block.configsRecord; const showCategories = config?.displaycategories?.value == '1'; - const courses = await CoreCoursesHelper.getUserCoursesWithOptions(this.sort, undefined, undefined, showCategories); + const courses = await CoreCoursesHelper.getUserCoursesWithOptions(this.sort, undefined, undefined, showCategories, { + readingStrategy: refresh ? CoreSitesReadingStrategy.PREFER_NETWORK : undefined, + }); // Check to show sort by short name only if the text is visible. if (courses.length > 0) { @@ -335,6 +335,41 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem return showCondition ? (disabledCondition ? 'disabled' : 'show') : 'hidden'; } + /** + * Whether list should be refreshed based on a EVENT_MY_COURSES_UPDATED event. + * + * @param data Event data. + * @return Whether to refresh. + */ + protected shouldRefreshOnUpdatedEvent(data: CoreCoursesMyCoursesUpdatedEventData): boolean { + if (data.action == CoreCoursesProvider.ACTION_ENROL) { + // Always update if user enrolled in a course. + return true; + } + + if (data.action == CoreCoursesProvider.ACTION_STATE_CHANGED) { + // Update list when course state changes (favourite, hidden). + return true; + } + + if (data.action == CoreCoursesProvider.ACTION_VIEW && data.courseId != CoreSites.getCurrentSiteHomeId()) { + // User viewed a course. If it isn't the most recent accessed course, update the list. + let recentAccessedCourse: CoreEnrolledCourseDataWithOptions | undefined; + if (this.sort == 'lastaccess') { + recentAccessedCourse = this.courses.allincludinghidden[0]; + } else { + recentAccessedCourse = Array.from(this.courses.allincludinghidden) + .sort((a, b) => (b.lastaccess || 0) - (a.lastaccess || 0))[0]; + } + + if (recentAccessedCourse && data.courseId != recentAccessedCourse.id) { + return true; + } + } + + return false; + } + /** * The filter has changed. * @@ -403,12 +438,6 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem protected async refreshCourseList(): Promise { CoreEvents.trigger(CoreCoursesProvider.EVENT_MY_COURSES_REFRESHED); - try { - await CoreCourses.invalidateUserCourses(); - } catch (error) { - // Ignore errors. - } - await this.loadContent(true); } diff --git a/src/core/features/courses/services/courses-helper.ts b/src/core/features/courses/services/courses-helper.ts index 150bfb05a..89034e716 100644 --- a/src/core/features/courses/services/courses-helper.ts +++ b/src/core/features/courses/services/courses-helper.ts @@ -14,7 +14,7 @@ import { Injectable } from '@angular/core'; import { CoreUtils } from '@services/utils/utils'; -import { CoreSites } from '@services/sites'; +import { CoreSites, CoreSitesCommonWSOptions } from '@services/sites'; import { CoreCourseAnyCourseDataWithOptions, CoreCourses, @@ -216,9 +216,14 @@ export class CoreCoursesHelperProvider { slice: number = 0, filter?: string, loadCategoryNames: boolean = false, + options: CoreSitesCommonWSOptions = {}, ): Promise { - let courses: CoreEnrolledCourseDataWithOptions[] = await CoreCourses.getUserCourses(); + let courses: CoreEnrolledCourseDataWithOptions[] = await CoreCourses.getUserCourses( + false, + options.siteId, + options.readingStrategy, + ); if (courses.length <= 0) { return []; } @@ -227,7 +232,7 @@ export class CoreCoursesHelperProvider { const courseIds = courses.map((course) => course.id); // Load course options of the course. - promises.push(CoreCourses.getCoursesAdminAndNavOptions(courseIds).then((options) => { + promises.push(CoreCourses.getCoursesAdminAndNavOptions(courseIds, options.siteId).then((options) => { courses.forEach((course) => { course.navOptions = options.navOptions[course.id]; course.admOptions = options.admOptions[course.id]; @@ -290,7 +295,7 @@ export class CoreCoursesHelperProvider { } try { - const completion = await AddonCourseCompletion.getCompletion(course.id); + const completion = await AddonCourseCompletion.getCompletion(course.id, undefined, undefined, options.siteId); course.completed = completion?.completed; } catch {