From 0efa423bf6dc9c9ce498ef50a9ef05d7d217ee10 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 9 Dec 2019 10:24:22 +0100 Subject: [PATCH] MOBILE-3233 course: Implement prefetch handlers for URL and LTI These prefetch handlers won't download anything, but they will be used to invalidate data when the user performs a PTR in the course page. --- src/addon/mod/lti/lti.module.ts | 16 +++- .../mod/lti/providers/prefetch-handler.ts | 96 +++++++++++++++++++ .../mod/url/providers/prefetch-handler.ts | 95 ++++++++++++++++++ src/addon/mod/url/url.module.ts | 16 +++- 4 files changed, 215 insertions(+), 8 deletions(-) create mode 100644 src/addon/mod/lti/providers/prefetch-handler.ts create mode 100644 src/addon/mod/url/providers/prefetch-handler.ts diff --git a/src/addon/mod/lti/lti.module.ts b/src/addon/mod/lti/lti.module.ts index 08d670c13..4e2c83878 100644 --- a/src/addon/mod/lti/lti.module.ts +++ b/src/addon/mod/lti/lti.module.ts @@ -18,8 +18,10 @@ import { AddonModLtiModuleHandler } from './providers/module-handler'; import { AddonModLtiProvider } from './providers/lti'; import { AddonModLtiLinkHandler } from './providers/link-handler'; import { AddonModLtiListLinkHandler } from './providers/list-link-handler'; +import { AddonModLtiPrefetchHandler } from './providers/prefetch-handler'; import { CoreContentLinksDelegate } from '@core/contentlinks/providers/delegate'; import { CoreCourseModuleDelegate } from '@core/course/providers/module-delegate'; +import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-prefetch-delegate'; // List of providers (without handlers). export const ADDON_MOD_LTI_PROVIDERS: any[] = [ @@ -36,16 +38,22 @@ export const ADDON_MOD_LTI_PROVIDERS: any[] = [ AddonModLtiProvider, AddonModLtiModuleHandler, AddonModLtiLinkHandler, - AddonModLtiListLinkHandler + AddonModLtiListLinkHandler, + AddonModLtiPrefetchHandler ] }) export class AddonModLtiModule { - constructor(moduleDelegate: CoreCourseModuleDelegate, moduleHandler: AddonModLtiModuleHandler, - contentLinksDelegate: CoreContentLinksDelegate, linkHandler: AddonModLtiLinkHandler, - listLinkHandler: AddonModLtiListLinkHandler) { + constructor(moduleDelegate: CoreCourseModuleDelegate, + moduleHandler: AddonModLtiModuleHandler, + contentLinksDelegate: CoreContentLinksDelegate, + linkHandler: AddonModLtiLinkHandler, + listLinkHandler: AddonModLtiListLinkHandler, + prefetchDelegate: CoreCourseModulePrefetchDelegate, + prefetchHandler: AddonModLtiPrefetchHandler) { moduleDelegate.registerHandler(moduleHandler); contentLinksDelegate.registerHandler(linkHandler); contentLinksDelegate.registerHandler(listLinkHandler); + prefetchDelegate.registerHandler(prefetchHandler); } } diff --git a/src/addon/mod/lti/providers/prefetch-handler.ts b/src/addon/mod/lti/providers/prefetch-handler.ts new file mode 100644 index 000000000..14d3c2584 --- /dev/null +++ b/src/addon/mod/lti/providers/prefetch-handler.ts @@ -0,0 +1,96 @@ +// (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 { Injectable } from '@angular/core'; +import { TranslateService } from '@ngx-translate/core'; +import { CoreAppProvider } from '@providers/app'; +import { CoreFilepoolProvider } from '@providers/filepool'; +import { CoreSitesProvider } from '@providers/sites'; +import { CoreDomUtilsProvider } from '@providers/utils/dom'; +import { CoreUtilsProvider } from '@providers/utils/utils'; +import { CoreCourseProvider } from '@core/course/providers/course'; +import { CoreCourseActivityPrefetchHandlerBase } from '@core/course/classes/activity-prefetch-handler'; +import { AddonModLtiProvider } from './lti'; +import { CoreFilterHelperProvider } from '@core/filter/providers/helper'; + +/** + * Handler to prefetch LTIs. LTIs cannot be prefetched, but the handler will be used to invalidate some data on course PTR. + */ +@Injectable() +export class AddonModLtiPrefetchHandler extends CoreCourseActivityPrefetchHandlerBase { + name = 'AddonModLti'; + modName = 'lti'; + component = AddonModLtiProvider.COMPONENT; + + constructor(translate: TranslateService, + appProvider: CoreAppProvider, + utils: CoreUtilsProvider, + courseProvider: CoreCourseProvider, + filepoolProvider: CoreFilepoolProvider, + sitesProvider: CoreSitesProvider, + domUtils: CoreDomUtilsProvider, + filterHelper: CoreFilterHelperProvider, + protected ltiProvider: AddonModLtiProvider) { + + super(translate, appProvider, utils, courseProvider, filepoolProvider, sitesProvider, domUtils, filterHelper); + } + + /** + * 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. + */ + download(module: any, courseId: number, dirPath?: string): Promise { + return Promise.resolve(); + } + + /** + * Invalidate WS calls needed to determine module status (usually, to check if module is downloadable). + * It doesn't need to invalidate check updates. It should NOT invalidate files nor all the prefetched data. + * + * @param module Module. + * @param courseId Course ID the module belongs to. + * @return Promise resolved when invalidated. + */ + invalidateModule(module: any, courseId: number): Promise { + return this.ltiProvider.invalidateLti(courseId); + } + + /** + * Check if a module can be downloaded. + * + * @param module Module. + * @param courseId Course ID the module belongs to. + * @return Whether the module can be downloaded. The promise should never be rejected. + */ + isDownloadable(module: any, courseId: number): boolean | Promise { + return false; // LTIs aren't downloadable. + } + + /** + * 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. + */ + prefetch(module: any, courseId?: number, single?: boolean, dirPath?: string): Promise { + return Promise.resolve(); + } +} diff --git a/src/addon/mod/url/providers/prefetch-handler.ts b/src/addon/mod/url/providers/prefetch-handler.ts new file mode 100644 index 000000000..f904fc429 --- /dev/null +++ b/src/addon/mod/url/providers/prefetch-handler.ts @@ -0,0 +1,95 @@ +// (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 { Injectable } from '@angular/core'; +import { TranslateService } from '@ngx-translate/core'; +import { CoreAppProvider } from '@providers/app'; +import { CoreFilepoolProvider } from '@providers/filepool'; +import { CoreSitesProvider } from '@providers/sites'; +import { CoreDomUtilsProvider } from '@providers/utils/dom'; +import { CoreUtilsProvider } from '@providers/utils/utils'; +import { CoreCourseProvider } from '@core/course/providers/course'; +import { CoreCourseResourcePrefetchHandlerBase } from '@core/course/classes/resource-prefetch-handler'; +import { AddonModUrlProvider } from './url'; +import { CoreFilterHelperProvider } from '@core/filter/providers/helper'; + +/** + * Handler to prefetch URLs. URLs cannot be prefetched, but the handler will be used to invalidate some data on course PTR. + */ +@Injectable() +export class AddonModUrlPrefetchHandler extends CoreCourseResourcePrefetchHandlerBase { + name = 'AddonModUrl'; + modName = 'url'; + component = AddonModUrlProvider.COMPONENT; + + constructor(translate: TranslateService, + appProvider: CoreAppProvider, + utils: CoreUtilsProvider, + courseProvider: CoreCourseProvider, + filepoolProvider: CoreFilepoolProvider, + sitesProvider: CoreSitesProvider, + domUtils: CoreDomUtilsProvider, + filterHelper: CoreFilterHelperProvider) { + + super(translate, appProvider, utils, courseProvider, filepoolProvider, sitesProvider, domUtils, filterHelper); + } + + /** + * 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. + */ + download(module: any, courseId: number, dirPath?: string): Promise { + return Promise.resolve(); + } + + /** + * Invalidate WS calls needed to determine module status (usually, to check if module is downloadable). + * It doesn't need to invalidate check updates. It should NOT invalidate files nor all the prefetched data. + * + * @param module Module. + * @param courseId Course ID the module belongs to. + * @return Promise resolved when invalidated. + */ + invalidateModule(module: any, courseId: number): Promise { + return this.courseProvider.invalidateModule(module.id, undefined, this.modName); + } + + /** + * Check if a module can be downloaded. + * + * @param module Module. + * @param courseId Course ID the module belongs to. + * @return Whether the module can be downloaded. The promise should never be rejected. + */ + isDownloadable(module: any, courseId: number): boolean | Promise { + return false; // URLs aren't downloadable. + } + + /** + * 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. + */ + prefetch(module: any, courseId?: number, single?: boolean, dirPath?: string): Promise { + return Promise.resolve(); + } +} diff --git a/src/addon/mod/url/url.module.ts b/src/addon/mod/url/url.module.ts index 1e9c7231b..1066155af 100644 --- a/src/addon/mod/url/url.module.ts +++ b/src/addon/mod/url/url.module.ts @@ -18,9 +18,11 @@ import { AddonModUrlModuleHandler } from './providers/module-handler'; import { AddonModUrlProvider } from './providers/url'; import { AddonModUrlLinkHandler } from './providers/link-handler'; import { AddonModUrlListLinkHandler } from './providers/list-link-handler'; +import { AddonModUrlPrefetchHandler } from './providers/prefetch-handler'; import { AddonModUrlHelperProvider } from './providers/helper'; import { CoreContentLinksDelegate } from '@core/contentlinks/providers/delegate'; import { CoreCourseModuleDelegate } from '@core/course/providers/module-delegate'; +import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-prefetch-delegate'; // List of providers (without handlers). export const ADDON_MOD_URL_PROVIDERS: any[] = [ @@ -39,16 +41,22 @@ export const ADDON_MOD_URL_PROVIDERS: any[] = [ AddonModUrlHelperProvider, AddonModUrlModuleHandler, AddonModUrlLinkHandler, - AddonModUrlListLinkHandler + AddonModUrlListLinkHandler, + AddonModUrlPrefetchHandler ] }) export class AddonModUrlModule { - constructor(moduleDelegate: CoreCourseModuleDelegate, moduleHandler: AddonModUrlModuleHandler, - contentLinksDelegate: CoreContentLinksDelegate, linkHandler: AddonModUrlLinkHandler, - listLinkHandler: AddonModUrlListLinkHandler) { + constructor(moduleDelegate: CoreCourseModuleDelegate, + moduleHandler: AddonModUrlModuleHandler, + contentLinksDelegate: CoreContentLinksDelegate, + linkHandler: AddonModUrlLinkHandler, + listLinkHandler: AddonModUrlListLinkHandler, + prefetchDelegate: CoreCourseModulePrefetchDelegate, + prefetchHandler: AddonModUrlPrefetchHandler) { moduleDelegate.registerHandler(moduleHandler); contentLinksDelegate.registerHandler(linkHandler); contentLinksDelegate.registerHandler(listLinkHandler); + prefetchDelegate.registerHandler(prefetchHandler); } }