Skip to content

Commit

Permalink
Merge pull request #3960 from NoelDeMartin/MOBILE-4529
Browse files Browse the repository at this point in the history
MOBILE-4529: Decouple Survey
  • Loading branch information
dpalou authored Mar 7, 2024
2 parents 8a1774b + b39dc1b commit 93a6680
Show file tree
Hide file tree
Showing 20 changed files with 326 additions and 149 deletions.
4 changes: 2 additions & 2 deletions src/addons/mod/survey/components/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { CoreDomUtils } from '@services/utils/dom';
import { CoreTextUtils } from '@services/utils/text';
import { Translate } from '@singletons';
import { CoreEvents } from '@singletons/events';
import { AddonModSurveyPrefetchHandler } from '../../services/handlers/prefetch';
import { getPrefetchHandlerInstance } from '../../services/handlers/prefetch';
import {
AddonModSurveyProvider,
AddonModSurveySurvey,
Expand Down Expand Up @@ -215,7 +215,7 @@ export class AddonModSurveyIndexComponent extends CoreCourseModuleMainActivityCo
// The survey is downloaded, update the data.
try {
const prefetched = await AddonModSurveySync.prefetchAfterUpdate(
AddonModSurveyPrefetchHandler.instance,
getPrefetchHandlerInstance(),
this.module,
this.courseId,
);
Expand Down
26 changes: 26 additions & 0 deletions src/addons/mod/survey/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// (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.

export const ADDON_MOD_SURVEY_COMPONENT = 'mmaModSurvey';

// Routing.
export const ADDON_MOD_SURVEY_PAGE_NAME = 'mod_survey';

// Handlers.
export const ADDON_MOD_SURVEY_PREFETCH_NAME = 'AddonModSurvey';
export const ADDON_MOD_SURVEY_PREFETCH_MODNAME = 'survey';
export const ADDON_MOD_SURVEY_PREFETCH_COMPONENT = ADDON_MOD_SURVEY_COMPONENT;
export const ADDON_MOD_SURVEY_PREFETCH_UPDATE_NAMES = /^configuration$|^.*files$|^answers$/;

export const ADDON_MOD_SURVEY_SYNC_CRON_NAME = 'AddonModSurveySyncCronHandler';
8 changes: 4 additions & 4 deletions src/addons/mod/survey/services/handlers/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,21 @@
// limitations under the License.

import { CoreConstants, ModPurpose } from '@/core/constants';
import { ADDON_MOD_SURVEY_PAGE_NAME } from '@addons/mod/survey/constants';
import { Injectable, Type } from '@angular/core';
import { CoreModuleHandlerBase } from '@features/course/classes/module-base-handler';
import { CoreCourseModuleHandler } from '@features/course/services/module-delegate';
import { makeSingleton } from '@singletons';
import { AddonModSurveyIndexComponent } from '../../components/index';

/**
* Handler to support survey modules.
*/
@Injectable( { providedIn: 'root' })
export class AddonModSurveyModuleHandlerService extends CoreModuleHandlerBase implements CoreCourseModuleHandler {

static readonly PAGE_NAME = 'mod_survey';

name = 'AddonModSurvey';
modName = 'survey';
protected pageName = AddonModSurveyModuleHandlerService.PAGE_NAME;
protected pageName = ADDON_MOD_SURVEY_PAGE_NAME;

supportedFeatures = {
[CoreConstants.FEATURE_GROUPS]: true,
Expand All @@ -48,6 +46,8 @@ export class AddonModSurveyModuleHandlerService extends CoreModuleHandlerBase im
* @inheritdoc
*/
async getMainComponent(): Promise<Type<unknown>> {
const { AddonModSurveyIndexComponent } = await import('@addons/mod/survey/components/index');

return AddonModSurveyIndexComponent;
}

Expand Down
109 changes: 109 additions & 0 deletions src/addons/mod/survey/services/handlers/prefetch-lazy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// (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 { CoreCourseAnyModuleData } from '@features/course/services/course';
import { CoreFilepool } from '@services/filepool';
import { CoreSitesReadingStrategy } from '@services/sites';
import { CoreUtils } from '@services/utils/utils';
import { CoreWSFile } from '@services/ws';
import { makeSingleton } from '@singletons';
import { AddonModSurvey, AddonModSurveyProvider } from '../survey';
import { AddonModSurveySync, AddonModSurveySyncResult } from '../survey-sync';
import { AddonModSurveyPrefetchHandlerService } from '@addons/mod/survey/services/handlers/prefetch';

/**
* Handler to prefetch surveys.
*/
@Injectable( { providedIn: 'root' })
export class AddonModSurveyPrefetchHandlerLazyService extends AddonModSurveyPrefetchHandlerService {

/**
* @inheritdoc
*/
async getIntroFiles(module: CoreCourseAnyModuleData, courseId: number): Promise<CoreWSFile[]> {
const survey = await CoreUtils.ignoreErrors(AddonModSurvey.getSurvey(courseId, module.id));

return this.getIntroFilesFromInstance(module, survey);
}

/**
* @inheritdoc
*/
async invalidateContent(moduleId: number, courseId: number): Promise<void> {
return AddonModSurvey.invalidateContent(moduleId, courseId);
}

/**
* @inheritdoc
*/
async invalidateModule(module: CoreCourseAnyModuleData, courseId: number): Promise<void> {
await AddonModSurvey.invalidateSurveyData(courseId);
}

/**
* @inheritdoc
*/
async isEnabled(): Promise<boolean> {
return true;
}

/**
* @inheritdoc
*/
prefetch(module: CoreCourseAnyModuleData, courseId: number): Promise<void> {
return this.prefetchPackage(module, courseId, (siteId) => this.prefetchSurvey(module, courseId, siteId));
}

/**
* Prefetch a survey.
*
* @param module Module.
* @param courseId Course ID the module belongs to.
* @param siteId SiteId or current site.
* @returns Promise resolved when done.
*/
protected async prefetchSurvey(module: CoreCourseAnyModuleData, courseId: number, siteId: string): Promise<void> {
const survey = await AddonModSurvey.getSurvey(courseId, module.id, {
readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK,
siteId,
});

const promises: Promise<unknown>[] = [];
const files = this.getIntroFilesFromInstance(module, survey);

// Prefetch files.
promises.push(CoreFilepool.addFilesToQueue(siteId, files, AddonModSurveyProvider.COMPONENT, module.id));

// If survey isn't answered, prefetch the questions.
if (!survey.surveydone) {
promises.push(AddonModSurvey.getQuestions(survey.id, {
cmId: module.id,
readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK,
siteId,
}));
}

await Promise.all(promises);
}

/**
* @inheritdoc
*/
sync(module: CoreCourseAnyModuleData, courseId: number, siteId?: string): Promise<AddonModSurveySyncResult> {
return AddonModSurveySync.syncSurvey(module.instance, undefined, siteId);
}

}
export const AddonModSurveyPrefetchHandler = makeSingleton(AddonModSurveyPrefetchHandlerLazyService);
127 changes: 39 additions & 88 deletions src/addons/mod/survey/services/handlers/prefetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,103 +12,54 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { Injectable } from '@angular/core';
import { AsyncInstance, asyncInstance } from '@/core/utils/async-instance';
import {
ADDON_MOD_SURVEY_PREFETCH_COMPONENT,
ADDON_MOD_SURVEY_PREFETCH_MODNAME,
ADDON_MOD_SURVEY_PREFETCH_NAME,
ADDON_MOD_SURVEY_PREFETCH_UPDATE_NAMES,
} from '@addons/mod/survey/constants';
import { CoreCourseActivityPrefetchHandlerBase } from '@features/course/classes/activity-prefetch-handler';
import { CoreCourseAnyModuleData } from '@features/course/services/course';
import { CoreFilepool } from '@services/filepool';
import { CoreSitesReadingStrategy } from '@services/sites';
import { CoreUtils } from '@services/utils/utils';
import { CoreWSFile } from '@services/ws';
import { makeSingleton } from '@singletons';
import { AddonModSurvey, AddonModSurveyProvider } from '../survey';
import { AddonModSurveySync, AddonModSurveySyncResult } from '../survey-sync';
import { CoreCourseModulePrefetchHandler } from '@features/course/services/module-prefetch-delegate';
import type { AddonModSurveyPrefetchHandlerLazyService } from './prefetch-lazy';

/**
* Handler to prefetch surveys.
*/
@Injectable( { providedIn: 'root' })
export class AddonModSurveyPrefetchHandlerService extends CoreCourseActivityPrefetchHandlerBase {

name = 'AddonModSurvey';
modName = 'survey';
component = AddonModSurveyProvider.COMPONENT;
updatesNames = /^configuration$|^.*files$|^answers$/;

/**
* @inheritdoc
*/
async getIntroFiles(module: CoreCourseAnyModuleData, courseId: number): Promise<CoreWSFile[]> {
const survey = await CoreUtils.ignoreErrors(AddonModSurvey.getSurvey(courseId, module.id));
let prefetchHandlerInstance: AsyncInstance<
AddonModSurveyPrefetchHandlerLazyService,
AddonModSurveyPrefetchHandlerService
> | null = null;

return this.getIntroFilesFromInstance(module, survey);
}

/**
* @inheritdoc
*/
async invalidateContent(moduleId: number, courseId: number): Promise<void> {
return AddonModSurvey.invalidateContent(moduleId, courseId);
}
export class AddonModSurveyPrefetchHandlerService extends CoreCourseActivityPrefetchHandlerBase {

/**
* @inheritdoc
*/
async invalidateModule(module: CoreCourseAnyModuleData, courseId: number): Promise<void> {
await AddonModSurvey.invalidateSurveyData(courseId);
}
name = ADDON_MOD_SURVEY_PREFETCH_NAME;
modName = ADDON_MOD_SURVEY_PREFETCH_MODNAME;
component = ADDON_MOD_SURVEY_PREFETCH_COMPONENT;
updatesNames = ADDON_MOD_SURVEY_PREFETCH_UPDATE_NAMES;

/**
* @inheritdoc
*/
async isEnabled(): Promise<boolean> {
return true;
}
}

/**
* @inheritdoc
*/
prefetch(module: CoreCourseAnyModuleData, courseId: number): Promise<void> {
return this.prefetchPackage(module, courseId, (siteId) => this.prefetchSurvey(module, courseId, siteId));
}
/**
* Get prefetch handler instance.
*
* @returns Prefetch handler.
*/
export function getPrefetchHandlerInstance(): CoreCourseModulePrefetchHandler {
if (!prefetchHandlerInstance) {
prefetchHandlerInstance = asyncInstance(async () => {
const { AddonModSurveyPrefetchHandler } = await import('./prefetch-lazy');

/**
* Prefetch a survey.
*
* @param module Module.
* @param courseId Course ID the module belongs to.
* @param siteId SiteId or current site.
* @returns Promise resolved when done.
*/
protected async prefetchSurvey(module: CoreCourseAnyModuleData, courseId: number, siteId: string): Promise<void> {
const survey = await AddonModSurvey.getSurvey(courseId, module.id, {
readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK,
siteId,
return AddonModSurveyPrefetchHandler.instance;
});

const promises: Promise<unknown>[] = [];
const files = this.getIntroFilesFromInstance(module, survey);

// Prefetch files.
promises.push(CoreFilepool.addFilesToQueue(siteId, files, AddonModSurveyProvider.COMPONENT, module.id));

// If survey isn't answered, prefetch the questions.
if (!survey.surveydone) {
promises.push(AddonModSurvey.getQuestions(survey.id, {
cmId: module.id,
readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK,
siteId,
}));
}

await Promise.all(promises);
}

/**
* @inheritdoc
*/
sync(module: CoreCourseAnyModuleData, courseId: number, siteId?: string): Promise<AddonModSurveySyncResult> {
return AddonModSurveySync.syncSurvey(module.instance, undefined, siteId);
prefetchHandlerInstance.setEagerInstance(new AddonModSurveyPrefetchHandlerService());
prefetchHandlerInstance.setLazyMethods(['sync']);
prefetchHandlerInstance.setLazyOverrides([
'prefetch',
'isEnabled',
'invalidateModule',
'invalidateContent',
'getIntroFiles',
]);
}

return prefetchHandlerInstance;
}
export const AddonModSurveyPrefetchHandler = makeSingleton(AddonModSurveyPrefetchHandlerService);
44 changes: 44 additions & 0 deletions src/addons/mod/survey/services/handlers/sync-cron-lazy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// (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 { CoreCronHandler } from '@services/cron';
import { makeSingleton } from '@singletons';
import { AddonModSurveySync } from '../survey-sync';
import { AddonModSurveySyncCronHandlerService } from '@addons/mod/survey/services/handlers/sync-cron';

/**
* Synchronization cron handler.
*/
@Injectable( { providedIn: 'root' })
export class AddonModSurveySyncCronHandlerLazyService
extends AddonModSurveySyncCronHandlerService
implements CoreCronHandler {

/**
* @inheritdoc
*/
async execute(siteId?: string, force?: boolean): Promise<void> {
await AddonModSurveySync.syncAllSurveys(siteId, force);
}

/**
* @inheritdoc
*/
getInterval(): number {
return AddonModSurveySync.syncInterval;
}

}
export const AddonModSurveySyncCronHandler = makeSingleton(AddonModSurveySyncCronHandlerLazyService);
Loading

0 comments on commit 93a6680

Please sign in to comment.