|
| 1 | +// (C) Copyright 2015 Moodle Pty Ltd. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import { Injectable } from '@angular/core'; |
| 16 | +import { CoreCourseResourcePrefetchHandlerBase } from '@features/course/classes/resource-prefetch-handler'; |
| 17 | +import { CoreCourse, CoreCourseAnyModuleData, CoreCourseWSSection } from '@features/course/services/course'; |
| 18 | +import { makeSingleton } from '@singletons'; |
| 19 | +import { ADDON_MOD_SUBSECTION_COMPONENT } from '../../constants'; |
| 20 | +import { CoreCourseHelper, CoreCourseModuleData } from '@features/course/services/course-helper'; |
| 21 | +import { CoreFileSizeSum } from '@services/plugin-file-delegate'; |
| 22 | +import { CoreCourseModulePrefetchDelegate } from '@features/course/services/module-prefetch-delegate'; |
| 23 | +import { CoreSites } from '@services/sites'; |
| 24 | + |
| 25 | +/** |
| 26 | + * Handler to prefetch subsections. |
| 27 | + */ |
| 28 | +@Injectable({ providedIn: 'root' }) |
| 29 | +export class AddonModSubsectionPrefetchHandlerService extends CoreCourseResourcePrefetchHandlerBase { |
| 30 | + |
| 31 | + name = 'AddonModSubsection'; |
| 32 | + modName = 'subsection'; |
| 33 | + component = ADDON_MOD_SUBSECTION_COMPONENT; |
| 34 | + |
| 35 | + /** |
| 36 | + * @inheritdoc |
| 37 | + */ |
| 38 | + protected async performDownloadOrPrefetch( |
| 39 | + siteId: string, |
| 40 | + module: CoreCourseModuleData, |
| 41 | + courseId: number, |
| 42 | + ): Promise<void> { |
| 43 | + const section = await this.getSection(module, courseId, siteId); |
| 44 | + if (!section) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + await CoreCourseHelper.prefetchSections([section], courseId); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @inheritdoc |
| 53 | + */ |
| 54 | + async getDownloadSize(module: CoreCourseAnyModuleData, courseId: number): Promise<CoreFileSizeSum> { |
| 55 | + const section = await this.getSection(module, courseId); |
| 56 | + if (!section) { |
| 57 | + return { size: 0, total: true }; |
| 58 | + } |
| 59 | + |
| 60 | + return await CoreCourseModulePrefetchDelegate.getDownloadSize(section.modules, courseId); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @inheritdoc |
| 65 | + */ |
| 66 | + async getDownloadedSize(module: CoreCourseAnyModuleData, courseId: number): Promise<number> { |
| 67 | + const section = await this.getSection(module, courseId); |
| 68 | + if (!section) { |
| 69 | + return 0; |
| 70 | + } |
| 71 | + |
| 72 | + return CoreCourseHelper.getModulesDownloadedSize(section.modules, courseId); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get the section of a module. |
| 78 | + * |
| 79 | + * @param module Module. |
| 80 | + * @param courseId Course ID. |
| 81 | + * @param siteId Site ID. If not defined, current site. |
| 82 | + * @returns Promise resolved with the section if found. |
| 83 | + */ |
| 84 | + protected async getSection( |
| 85 | + module: CoreCourseAnyModuleData, |
| 86 | + courseId: number, |
| 87 | + siteId?: string, |
| 88 | + ): Promise<CoreCourseWSSection | undefined> { |
| 89 | + siteId = siteId ?? CoreSites.getCurrentSiteId(); |
| 90 | + |
| 91 | + const sections = await CoreCourse.getSections(courseId, false, true, undefined, siteId); |
| 92 | + |
| 93 | + return sections.find((section) => |
| 94 | + section.component === 'mod_subsection' && section.itemid === module.instance); |
| 95 | + } |
| 96 | + |
| 97 | +} |
| 98 | +export const AddonModSubsectionPrefetchHandler = makeSingleton(AddonModSubsectionPrefetchHandlerService); |
0 commit comments