diff --git a/src/core/features/course/components/module/module.ts b/src/core/features/course/components/module/module.ts index 5a1d42fb7..3f02e900d 100644 --- a/src/core/features/course/components/module/module.ts +++ b/src/core/features/course/components/module/module.ts @@ -115,7 +115,7 @@ export class CoreCourseModuleComponent implements OnInit, OnDestroy { } if (this.showDownloadStatus && this.module.handlerData.showDownloadButton) { - const status = await CoreCourseModulePrefetchDelegate.getModuleStatus(this.module, this.module.course); + const status = await CoreCourseModulePrefetchDelegate.getDownloadedModuleStatus(this.module, this.module.course); this.updateModuleStatus(status); // Listen for changes on this module status, even if download isn't enabled. @@ -146,7 +146,7 @@ export class CoreCourseModuleComponent implements OnInit, OnDestroy { * * @param prefetchStatus Module status. */ - protected updateModuleStatus(prefetchStatus: TDownloadStatus): void { + protected updateModuleStatus(prefetchStatus: TDownloadStatus | null): void { if (!prefetchStatus) { return; } diff --git a/src/core/features/course/services/module-prefetch-delegate.ts b/src/core/features/course/services/module-prefetch-delegate.ts index fa5166b06..0a56dc30d 100644 --- a/src/core/features/course/services/module-prefetch-delegate.ts +++ b/src/core/features/course/services/module-prefetch-delegate.ts @@ -25,7 +25,7 @@ import { CoreUtils } from '@services/utils/utils'; import { CoreCourse, CoreCourseAnyModuleData, CoreCourseModuleContentFile } from './course'; import { CoreCache } from '@classes/cache'; import { CoreSiteWSPreSets } from '@classes/sites/authenticated-site'; -import { DownloadStatus, TDownloadStatus } from '@/core/constants'; +import { DownloadStatus, TDownloadStatus, TDownloadedStatus } from '@/core/constants'; import { CoreDelegate, CoreDelegateHandler } from '@classes/delegate'; import { makeSingleton } from '@singletons'; import { CoreEvents, CoreEventSectionStatusChangedData } from '@singletons/events'; @@ -554,6 +554,52 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate { + const handler = this.getPrefetchHandlerFor(module.modname); + if (!handler) { + // No handler found, module not downloadable. + return null; + } + + // Check if the status is cached. + const packageId = CoreFilepool.getPackageId(handler.component, module.id); + const status = this.statusCache.getValue(packageId, 'status'); + + if (!refresh && status !== undefined) { + this.storeCourseAndSection(packageId, courseId, sectionId); + + return this.filterDownloadedStatus(this.determineModuleStatus(module, status)); + } + + const result = await this.calculateDownloadedModuleStatus(handler, module, courseId, updates, sectionId); + if (result.updateStatus && result.status) { + this.updateStatusCache(result.status, handler.component, module.id, courseId, sectionId); + } + + return this.filterDownloadedStatus( + this.determineModuleStatus(module, result.status ?? DownloadStatus.DOWNLOADABLE_NOT_DOWNLOADED), + ); + } + /** * Calculate a module status. * @@ -581,14 +627,42 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate { // Get the saved package status. const siteId = CoreSites.getCurrentSiteId(); const currentStatus = await CoreFilepool.getPackageStatus(siteId, handler.component, module.id); let status = handler.determineStatus ? handler.determineStatus(module, currentStatus, true) : currentStatus; - if (status != DownloadStatus.DOWNLOADED || updates === false) { + if (status !== DownloadStatus.DOWNLOADED || updates === false) { return { - status, + status: this.filterDownloadedStatus(status), updateStatus: true, }; } @@ -604,7 +678,7 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate(packageId, 'status', true); return { - status: this.determineModuleStatus(module, status || DownloadStatus.DOWNLOADABLE_NOT_DOWNLOADED), + status: status ? this.filterDownloadedStatus(status) : null, updateStatus: true, }; } } + /** + * Given a download status, filter it to return only the downloaded statuses. + * + * @param status Status. + * @returns Filtered status, null for not downloaded statuses. + */ + protected filterDownloadedStatus(status: TDownloadStatus): TDownloadedStatus | null { + return status === DownloadStatus.NOT_DOWNLOADABLE || status === DownloadStatus.DOWNLOADABLE_NOT_DOWNLOADED ? + null : status; + } + /** * Get the status of a list of modules, along with the lists of modules for each status. *