|
| 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, Injector } from '@angular/core'; |
| 16 | +import { TranslateService } from '@ngx-translate/core'; |
| 17 | +import { CoreAppProvider } from '@providers/app'; |
| 18 | +import { CoreFilepoolProvider } from '@providers/filepool'; |
| 19 | +import { CorePluginFileDelegate } from '@providers/plugin-file-delegate'; |
| 20 | +import { CoreSitesProvider } from '@providers/sites'; |
| 21 | +import { CoreWSExternalFile } from '@providers/ws'; |
| 22 | +import { CoreDomUtilsProvider } from '@providers/utils/dom'; |
| 23 | +import { CoreUtilsProvider } from '@providers/utils/utils'; |
| 24 | +import { CoreCourseProvider } from '@core/course/providers/course'; |
| 25 | +import { CoreCourseActivityPrefetchHandlerBase } from '@core/course/classes/activity-prefetch-handler'; |
| 26 | +import { CoreFilterHelperProvider } from '@core/filter/providers/helper'; |
| 27 | +import { CoreH5PHelper } from '@core/h5p/classes/helper'; |
| 28 | +import { CoreH5P } from '@core/h5p/providers/h5p'; |
| 29 | +import { CoreUserProvider } from '@core/user/providers/user'; |
| 30 | +import { AddonModH5PActivity, AddonModH5PActivityProvider, AddonModH5PActivityData } from './h5pactivity'; |
| 31 | + |
| 32 | +/** |
| 33 | + * Handler to prefetch h5p activity. |
| 34 | + */ |
| 35 | +@Injectable() |
| 36 | +export class AddonModH5PActivityPrefetchHandler extends CoreCourseActivityPrefetchHandlerBase { |
| 37 | + name = 'AddonModH5PActivity'; |
| 38 | + modName = 'h5pactivity'; |
| 39 | + component = AddonModH5PActivityProvider.COMPONENT; |
| 40 | + updatesNames = /^configuration$|^.*files$|^tracks$|^usertracks$/; |
| 41 | + |
| 42 | + constructor(translate: TranslateService, |
| 43 | + appProvider: CoreAppProvider, |
| 44 | + utils: CoreUtilsProvider, |
| 45 | + courseProvider: CoreCourseProvider, |
| 46 | + filepoolProvider: CoreFilepoolProvider, |
| 47 | + sitesProvider: CoreSitesProvider, |
| 48 | + domUtils: CoreDomUtilsProvider, |
| 49 | + filterHelper: CoreFilterHelperProvider, |
| 50 | + pluginFileDelegate: CorePluginFileDelegate, |
| 51 | + protected userProvider: CoreUserProvider, |
| 52 | + protected injector: Injector) { |
| 53 | + |
| 54 | + super(translate, appProvider, utils, courseProvider, filepoolProvider, sitesProvider, domUtils, filterHelper, |
| 55 | + pluginFileDelegate); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Get list of files. |
| 60 | + * |
| 61 | + * @param module Module. |
| 62 | + * @param courseId Course ID the module belongs to. |
| 63 | + * @param single True if we're downloading a single module, false if we're downloading a whole section. |
| 64 | + * @return Promise resolved with the list of files. |
| 65 | + */ |
| 66 | + async getFiles(module: any, courseId: number, single?: boolean): Promise<CoreWSExternalFile[]> { |
| 67 | + |
| 68 | + const h5pActivity = await AddonModH5PActivity.instance.getH5PActivity(courseId, module.id); |
| 69 | + |
| 70 | + const displayOptions = CoreH5PHelper.decodeDisplayOptions(h5pActivity.displayoptions); |
| 71 | + |
| 72 | + const deployedFile = await AddonModH5PActivity.instance.getDeployedFile(h5pActivity, { |
| 73 | + displayOptions: displayOptions, |
| 74 | + }); |
| 75 | + |
| 76 | + return [deployedFile].concat(this.getIntroFilesFromInstance(module, h5pActivity)); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Invalidate WS calls needed to determine module status (usually, to check if module is downloadable). |
| 81 | + * It doesn't need to invalidate check updates. It should NOT invalidate files nor all the prefetched data. |
| 82 | + * |
| 83 | + * @param module Module. |
| 84 | + * @param courseId Course ID the module belongs to. |
| 85 | + * @return Promise resolved when invalidated. |
| 86 | + */ |
| 87 | + async invalidateModule(module: any, courseId: number): Promise<void> { |
| 88 | + // No need to invalidate anything. |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Check if a module can be downloaded. If the function is not defined, we assume that all modules are downloadable. |
| 93 | + * |
| 94 | + * @param module Module. |
| 95 | + * @param courseId Course ID the module belongs to. |
| 96 | + * @return Whether the module can be downloaded. The promise should never be rejected. |
| 97 | + */ |
| 98 | + isDownloadable(module: any, courseId: number): boolean | Promise<boolean> { |
| 99 | + return this.sitesProvider.getCurrentSite().canDownloadFiles() && !CoreH5P.instance.isOfflineDisabledInSite(); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Whether or not the handler is enabled on a site level. |
| 104 | + * |
| 105 | + * @return A boolean, or a promise resolved with a boolean, indicating if the handler is enabled. |
| 106 | + */ |
| 107 | + isEnabled(): boolean | Promise<boolean> { |
| 108 | + return AddonModH5PActivity.instance.isPluginEnabled(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Prefetch a module. |
| 113 | + * |
| 114 | + * @param module Module. |
| 115 | + * @param courseId Course ID the module belongs to. |
| 116 | + * @param single True if we're downloading a single module, false if we're downloading a whole section. |
| 117 | + * @param dirPath Path of the directory where to store all the content files. |
| 118 | + * @return Promise resolved when done. |
| 119 | + */ |
| 120 | + prefetch(module: any, courseId?: number, single?: boolean, dirPath?: string): Promise<any> { |
| 121 | + return this.prefetchPackage(module, courseId, single, this.prefetchActivity.bind(this)); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Prefetch an H5P activity. |
| 126 | + * |
| 127 | + * @param module Module. |
| 128 | + * @param courseId Course ID the module belongs to. |
| 129 | + * @param single True if we're downloading a single module, false if we're downloading a whole section. |
| 130 | + * @param siteId Site ID. |
| 131 | + * @return Promise resolved when done. |
| 132 | + */ |
| 133 | + protected async prefetchActivity(module: any, courseId: number, single: boolean, siteId: string): Promise<void> { |
| 134 | + |
| 135 | + const h5pActivity = await AddonModH5PActivity.instance.getH5PActivity(courseId, module.id, true, siteId); |
| 136 | + |
| 137 | + const introFiles = this.getIntroFilesFromInstance(module, h5pActivity); |
| 138 | + |
| 139 | + await Promise.all([ |
| 140 | + AddonModH5PActivity.instance.getAccessInformation(h5pActivity.id, true, siteId), |
| 141 | + this.filepoolProvider.addFilesToQueue(siteId, introFiles, AddonModH5PActivityProvider.COMPONENT, module.id), |
| 142 | + this.prefetchMainFile(module, h5pActivity, siteId), |
| 143 | + ]); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Prefetch the deployed file of the activity. |
| 148 | + * |
| 149 | + * @param module Module. |
| 150 | + * @param h5pActivity Activity instance. |
| 151 | + * @param siteId Site ID. |
| 152 | + * @return Promise resolved when done. |
| 153 | + */ |
| 154 | + protected async prefetchMainFile(module: any, h5pActivity: AddonModH5PActivityData, siteId: string): Promise<void> { |
| 155 | + |
| 156 | + const displayOptions = CoreH5PHelper.decodeDisplayOptions(h5pActivity.displayoptions); |
| 157 | + |
| 158 | + const deployedFile = await AddonModH5PActivity.instance.getDeployedFile(h5pActivity, { |
| 159 | + displayOptions: displayOptions, |
| 160 | + ignoreCache: true, |
| 161 | + siteId: siteId, |
| 162 | + }); |
| 163 | + |
| 164 | + await this.filepoolProvider.addFilesToQueue(siteId, [deployedFile], AddonModH5PActivityProvider.COMPONENT, module.id); |
| 165 | + } |
| 166 | +} |
0 commit comments