2021-01-25 07:42:55 +01:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import { CoreConstants } from '@/core/constants';
|
|
|
|
import { CoreNetworkError } from '@classes/errors/network-error';
|
|
|
|
import { CoreFilterHelper } from '@features/filter/services/filter-helper';
|
2022-05-11 14:06:42 +02:00
|
|
|
import { CoreNetwork } from '@services/network';
|
2021-01-25 07:42:55 +01:00
|
|
|
import { CoreFilepool } from '@services/filepool';
|
|
|
|
import { CoreSites } from '@services/sites';
|
2021-02-01 12:28:37 +01:00
|
|
|
import { CoreCourse, CoreCourseAnyModuleData } from '../services/course';
|
2021-01-25 07:42:55 +01:00
|
|
|
import { CoreCourseModulePrefetchHandlerBase } from './module-prefetch-handler';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base prefetch handler to be registered in CoreCourseModulePrefetchDelegate. It is useful to minimize the amount of
|
|
|
|
* functions that handlers need to implement. It also provides some helper features like preventing a module to be
|
|
|
|
* downloaded twice at the same time.
|
|
|
|
*
|
|
|
|
* If your handler inherits from this service, you just need to override the functions that you want to change.
|
|
|
|
*
|
|
|
|
* This class should be used for ACTIVITIES. You must override the prefetch function, and it's recommended to call
|
|
|
|
* prefetchPackage in there since it handles the package status.
|
|
|
|
*/
|
|
|
|
export class CoreCourseActivityPrefetchHandlerBase extends CoreCourseModulePrefetchHandlerBase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download the module.
|
|
|
|
*
|
|
|
|
* @param module The module object returned by WS.
|
|
|
|
* @param courseId Course ID.
|
|
|
|
* @param dirPath Path of the directory where to store all the content files.
|
|
|
|
* @return Promise resolved when all content is downloaded.
|
|
|
|
*/
|
2021-02-01 12:28:37 +01:00
|
|
|
download(module: CoreCourseAnyModuleData, courseId: number, dirPath?: string): Promise<void> {
|
2021-01-25 07:42:55 +01:00
|
|
|
// Same implementation for download and prefetch.
|
|
|
|
return this.prefetch(module, courseId, false, dirPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prefetch a module.
|
|
|
|
*
|
|
|
|
* @param module Module.
|
|
|
|
* @param courseId Course ID the module belongs to.
|
|
|
|
* @param single True if we're downloading a single module, false if we're downloading a whole section.
|
|
|
|
* @param dirPath Path of the directory where to store all the content files.
|
|
|
|
* @return Promise resolved when done.
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2021-02-01 12:28:37 +01:00
|
|
|
async prefetch(module: CoreCourseAnyModuleData, courseId?: number, single?: boolean, dirPath?: string): Promise<void> {
|
2021-01-25 07:42:55 +01:00
|
|
|
// To be overridden. It should call prefetchPackage
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prefetch the module, setting package status at start and finish.
|
|
|
|
*
|
|
|
|
* Example usage from a child instance:
|
2022-10-04 15:06:14 +02:00
|
|
|
* return this.prefetchPackage(module, courseId, (siteId) => this.prefetchModule(module, otherParam, siteId), siteId);
|
2021-01-25 07:42:55 +01:00
|
|
|
*
|
|
|
|
* @param module Module.
|
|
|
|
* @param courseId Course ID the module belongs to.
|
2022-10-04 15:06:14 +02:00
|
|
|
* @param downloadFn Function to perform the prefetch. It can return a string to be stored as the package "extra" data.
|
2021-01-25 07:42:55 +01:00
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise resolved when the module has been downloaded. Data returned is not reliable.
|
|
|
|
*/
|
|
|
|
async prefetchPackage(
|
2021-02-01 12:28:37 +01:00
|
|
|
module: CoreCourseAnyModuleData,
|
2021-06-11 08:56:11 +02:00
|
|
|
courseId: number,
|
2022-10-04 15:06:14 +02:00
|
|
|
downloadFunction: (siteId: string) => Promise<string | void>,
|
2021-01-25 07:42:55 +01:00
|
|
|
siteId?: string,
|
|
|
|
): Promise<void> {
|
2021-03-02 11:41:04 +01:00
|
|
|
siteId = siteId || CoreSites.getCurrentSiteId();
|
2021-01-25 07:42:55 +01:00
|
|
|
|
2022-05-11 14:06:42 +02:00
|
|
|
if (!CoreNetwork.isOnline()) {
|
2021-01-25 07:42:55 +01:00
|
|
|
// Cannot prefetch in offline.
|
|
|
|
throw new CoreNetworkError();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isDownloading(module.id, siteId)) {
|
|
|
|
// There's already a download ongoing for this module, return the promise.
|
|
|
|
return this.getOngoingDownload(module.id, siteId);
|
|
|
|
}
|
|
|
|
|
|
|
|
const prefetchPromise = this.changeStatusAndPrefetch(module, courseId, downloadFunction, siteId);
|
|
|
|
|
|
|
|
return this.addOngoingDownload(module.id, prefetchPromise, siteId);
|
|
|
|
}
|
|
|
|
|
2021-02-01 12:28:37 +01:00
|
|
|
/**
|
|
|
|
* Change module status and call the prefetch function.
|
|
|
|
*
|
|
|
|
* @param module Module.
|
|
|
|
* @param courseId Course ID the module belongs to.
|
2022-10-04 15:06:14 +02:00
|
|
|
* @param downloadFn Function to perform the prefetch. It can return a string to be stored as the package "extra" data.
|
2021-02-01 12:28:37 +01:00
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise resolved when the module has been downloaded. Data returned is not reliable.
|
|
|
|
*/
|
2021-01-25 07:42:55 +01:00
|
|
|
protected async changeStatusAndPrefetch(
|
2021-02-01 12:28:37 +01:00
|
|
|
module: CoreCourseAnyModuleData,
|
|
|
|
courseId: number | undefined,
|
2022-10-04 15:06:14 +02:00
|
|
|
downloadFunction: (siteId: string) => Promise<string | void>,
|
2021-06-11 08:56:11 +02:00
|
|
|
siteId: string,
|
2021-01-25 07:42:55 +01:00
|
|
|
): Promise<void> {
|
|
|
|
try {
|
|
|
|
await this.setDownloading(module.id, siteId);
|
|
|
|
|
|
|
|
// Package marked as downloading, get module info to be able to handle links. Get module filters too.
|
|
|
|
await Promise.all([
|
2021-12-17 00:27:04 +01:00
|
|
|
CoreCourse.getModuleBasicInfo(module.id, { siteId }),
|
2021-03-02 11:41:04 +01:00
|
|
|
CoreCourse.getModule(module.id, courseId, undefined, false, true, siteId),
|
|
|
|
CoreFilterHelper.getFilters('module', module.id, { courseId }),
|
2021-01-25 07:42:55 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Call the download function.
|
2021-06-11 08:56:11 +02:00
|
|
|
let extra = await downloadFunction(siteId);
|
2021-01-25 07:42:55 +01:00
|
|
|
|
|
|
|
// Only accept string types.
|
|
|
|
if (typeof extra != 'string') {
|
|
|
|
extra = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefetch finished, mark as downloaded.
|
|
|
|
await this.setDownloaded(module.id, siteId, extra);
|
|
|
|
} catch (error) {
|
|
|
|
// Error prefetching, go back to previous status and reject the promise.
|
2021-06-07 17:21:54 +02:00
|
|
|
await this.setPreviousStatus(module.id, siteId);
|
2021-01-25 07:42:55 +01:00
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark the module as downloaded.
|
|
|
|
*
|
|
|
|
* @param id Unique identifier per component.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @param extra Extra data to store.
|
|
|
|
* @return Promise resolved when done.
|
|
|
|
*/
|
|
|
|
setDownloaded(id: number, siteId?: string, extra?: string): Promise<void> {
|
2021-03-02 11:41:04 +01:00
|
|
|
siteId = siteId || CoreSites.getCurrentSiteId();
|
2021-01-25 07:42:55 +01:00
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
return CoreFilepool.storePackageStatus(siteId, CoreConstants.DOWNLOADED, this.component, id, extra);
|
2021-01-25 07:42:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark the module as downloading.
|
|
|
|
*
|
|
|
|
* @param id Unique identifier per component.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise resolved when done.
|
|
|
|
*/
|
|
|
|
setDownloading(id: number, siteId?: string): Promise<void> {
|
2021-03-02 11:41:04 +01:00
|
|
|
siteId = siteId || CoreSites.getCurrentSiteId();
|
2021-01-25 07:42:55 +01:00
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
return CoreFilepool.storePackageStatus(siteId, CoreConstants.DOWNLOADING, this.component, id);
|
2021-01-25 07:42:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set previous status and return a rejected promise.
|
|
|
|
*
|
|
|
|
* @param id Unique identifier per component.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Rejected promise.
|
|
|
|
*/
|
|
|
|
async setPreviousStatus(id: number, siteId?: string): Promise<void> {
|
2021-03-02 11:41:04 +01:00
|
|
|
siteId = siteId || CoreSites.getCurrentSiteId();
|
2021-01-25 07:42:55 +01:00
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
await CoreFilepool.setPackagePreviousStatus(siteId, this.component, id);
|
2021-01-25 07:42:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set previous status and return a rejected promise.
|
|
|
|
*
|
|
|
|
* @param id Unique identifier per component.
|
|
|
|
* @param error Error to throw.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Rejected promise.
|
|
|
|
* @deprecated since 3.9.5. Use setPreviousStatus instead.
|
|
|
|
*/
|
|
|
|
async setPreviousStatusAndReject(id: number, error?: Error, siteId?: string): Promise<never> {
|
2021-03-02 11:41:04 +01:00
|
|
|
siteId = siteId || CoreSites.getCurrentSiteId();
|
2021-01-25 07:42:55 +01:00
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
await CoreFilepool.setPackagePreviousStatus(siteId, this.component, id);
|
2021-01-25 07:42:55 +01:00
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|