Skip to content

Commit 93a6680

Browse files
authored
Merge pull request #3960 from NoelDeMartin/MOBILE-4529
MOBILE-4529: Decouple Survey
2 parents 8a1774b + b39dc1b commit 93a6680

File tree

20 files changed

+326
-149
lines changed

20 files changed

+326
-149
lines changed

src/addons/mod/survey/components/index/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { CoreDomUtils } from '@services/utils/dom';
2323
import { CoreTextUtils } from '@services/utils/text';
2424
import { Translate } from '@singletons';
2525
import { CoreEvents } from '@singletons/events';
26-
import { AddonModSurveyPrefetchHandler } from '../../services/handlers/prefetch';
26+
import { getPrefetchHandlerInstance } from '../../services/handlers/prefetch';
2727
import {
2828
AddonModSurveyProvider,
2929
AddonModSurveySurvey,
@@ -215,7 +215,7 @@ export class AddonModSurveyIndexComponent extends CoreCourseModuleMainActivityCo
215215
// The survey is downloaded, update the data.
216216
try {
217217
const prefetched = await AddonModSurveySync.prefetchAfterUpdate(
218-
AddonModSurveyPrefetchHandler.instance,
218+
getPrefetchHandlerInstance(),
219219
this.module,
220220
this.courseId,
221221
);

src/addons/mod/survey/constants.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
export const ADDON_MOD_SURVEY_COMPONENT = 'mmaModSurvey';
16+
17+
// Routing.
18+
export const ADDON_MOD_SURVEY_PAGE_NAME = 'mod_survey';
19+
20+
// Handlers.
21+
export const ADDON_MOD_SURVEY_PREFETCH_NAME = 'AddonModSurvey';
22+
export const ADDON_MOD_SURVEY_PREFETCH_MODNAME = 'survey';
23+
export const ADDON_MOD_SURVEY_PREFETCH_COMPONENT = ADDON_MOD_SURVEY_COMPONENT;
24+
export const ADDON_MOD_SURVEY_PREFETCH_UPDATE_NAMES = /^configuration$|^.*files$|^answers$/;
25+
26+
export const ADDON_MOD_SURVEY_SYNC_CRON_NAME = 'AddonModSurveySyncCronHandler';

src/addons/mod/survey/services/handlers/module.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,21 @@
1313
// limitations under the License.
1414

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

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

28-
static readonly PAGE_NAME = 'mod_survey';
29-
3028
name = 'AddonModSurvey';
3129
modName = 'survey';
32-
protected pageName = AddonModSurveyModuleHandlerService.PAGE_NAME;
30+
protected pageName = ADDON_MOD_SURVEY_PAGE_NAME;
3331

3432
supportedFeatures = {
3533
[CoreConstants.FEATURE_GROUPS]: true,
@@ -48,6 +46,8 @@ export class AddonModSurveyModuleHandlerService extends CoreModuleHandlerBase im
4846
* @inheritdoc
4947
*/
5048
async getMainComponent(): Promise<Type<unknown>> {
49+
const { AddonModSurveyIndexComponent } = await import('@addons/mod/survey/components/index');
50+
5151
return AddonModSurveyIndexComponent;
5252
}
5353

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 { CoreCourseAnyModuleData } from '@features/course/services/course';
17+
import { CoreFilepool } from '@services/filepool';
18+
import { CoreSitesReadingStrategy } from '@services/sites';
19+
import { CoreUtils } from '@services/utils/utils';
20+
import { CoreWSFile } from '@services/ws';
21+
import { makeSingleton } from '@singletons';
22+
import { AddonModSurvey, AddonModSurveyProvider } from '../survey';
23+
import { AddonModSurveySync, AddonModSurveySyncResult } from '../survey-sync';
24+
import { AddonModSurveyPrefetchHandlerService } from '@addons/mod/survey/services/handlers/prefetch';
25+
26+
/**
27+
* Handler to prefetch surveys.
28+
*/
29+
@Injectable( { providedIn: 'root' })
30+
export class AddonModSurveyPrefetchHandlerLazyService extends AddonModSurveyPrefetchHandlerService {
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
async getIntroFiles(module: CoreCourseAnyModuleData, courseId: number): Promise<CoreWSFile[]> {
36+
const survey = await CoreUtils.ignoreErrors(AddonModSurvey.getSurvey(courseId, module.id));
37+
38+
return this.getIntroFilesFromInstance(module, survey);
39+
}
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
async invalidateContent(moduleId: number, courseId: number): Promise<void> {
45+
return AddonModSurvey.invalidateContent(moduleId, courseId);
46+
}
47+
48+
/**
49+
* @inheritdoc
50+
*/
51+
async invalidateModule(module: CoreCourseAnyModuleData, courseId: number): Promise<void> {
52+
await AddonModSurvey.invalidateSurveyData(courseId);
53+
}
54+
55+
/**
56+
* @inheritdoc
57+
*/
58+
async isEnabled(): Promise<boolean> {
59+
return true;
60+
}
61+
62+
/**
63+
* @inheritdoc
64+
*/
65+
prefetch(module: CoreCourseAnyModuleData, courseId: number): Promise<void> {
66+
return this.prefetchPackage(module, courseId, (siteId) => this.prefetchSurvey(module, courseId, siteId));
67+
}
68+
69+
/**
70+
* Prefetch a survey.
71+
*
72+
* @param module Module.
73+
* @param courseId Course ID the module belongs to.
74+
* @param siteId SiteId or current site.
75+
* @returns Promise resolved when done.
76+
*/
77+
protected async prefetchSurvey(module: CoreCourseAnyModuleData, courseId: number, siteId: string): Promise<void> {
78+
const survey = await AddonModSurvey.getSurvey(courseId, module.id, {
79+
readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK,
80+
siteId,
81+
});
82+
83+
const promises: Promise<unknown>[] = [];
84+
const files = this.getIntroFilesFromInstance(module, survey);
85+
86+
// Prefetch files.
87+
promises.push(CoreFilepool.addFilesToQueue(siteId, files, AddonModSurveyProvider.COMPONENT, module.id));
88+
89+
// If survey isn't answered, prefetch the questions.
90+
if (!survey.surveydone) {
91+
promises.push(AddonModSurvey.getQuestions(survey.id, {
92+
cmId: module.id,
93+
readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK,
94+
siteId,
95+
}));
96+
}
97+
98+
await Promise.all(promises);
99+
}
100+
101+
/**
102+
* @inheritdoc
103+
*/
104+
sync(module: CoreCourseAnyModuleData, courseId: number, siteId?: string): Promise<AddonModSurveySyncResult> {
105+
return AddonModSurveySync.syncSurvey(module.instance, undefined, siteId);
106+
}
107+
108+
}
109+
export const AddonModSurveyPrefetchHandler = makeSingleton(AddonModSurveyPrefetchHandlerLazyService);

src/addons/mod/survey/services/handlers/prefetch.ts

Lines changed: 39 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -12,103 +12,54 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

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

26-
/**
27-
* Handler to prefetch surveys.
28-
*/
29-
@Injectable( { providedIn: 'root' })
30-
export class AddonModSurveyPrefetchHandlerService extends CoreCourseActivityPrefetchHandlerBase {
31-
32-
name = 'AddonModSurvey';
33-
modName = 'survey';
34-
component = AddonModSurveyProvider.COMPONENT;
35-
updatesNames = /^configuration$|^.*files$|^answers$/;
36-
37-
/**
38-
* @inheritdoc
39-
*/
40-
async getIntroFiles(module: CoreCourseAnyModuleData, courseId: number): Promise<CoreWSFile[]> {
41-
const survey = await CoreUtils.ignoreErrors(AddonModSurvey.getSurvey(courseId, module.id));
26+
let prefetchHandlerInstance: AsyncInstance<
27+
AddonModSurveyPrefetchHandlerLazyService,
28+
AddonModSurveyPrefetchHandlerService
29+
> | null = null;
4230

43-
return this.getIntroFilesFromInstance(module, survey);
44-
}
45-
46-
/**
47-
* @inheritdoc
48-
*/
49-
async invalidateContent(moduleId: number, courseId: number): Promise<void> {
50-
return AddonModSurvey.invalidateContent(moduleId, courseId);
51-
}
31+
export class AddonModSurveyPrefetchHandlerService extends CoreCourseActivityPrefetchHandlerBase {
5232

53-
/**
54-
* @inheritdoc
55-
*/
56-
async invalidateModule(module: CoreCourseAnyModuleData, courseId: number): Promise<void> {
57-
await AddonModSurvey.invalidateSurveyData(courseId);
58-
}
33+
name = ADDON_MOD_SURVEY_PREFETCH_NAME;
34+
modName = ADDON_MOD_SURVEY_PREFETCH_MODNAME;
35+
component = ADDON_MOD_SURVEY_PREFETCH_COMPONENT;
36+
updatesNames = ADDON_MOD_SURVEY_PREFETCH_UPDATE_NAMES;
5937

60-
/**
61-
* @inheritdoc
62-
*/
63-
async isEnabled(): Promise<boolean> {
64-
return true;
65-
}
38+
}
6639

67-
/**
68-
* @inheritdoc
69-
*/
70-
prefetch(module: CoreCourseAnyModuleData, courseId: number): Promise<void> {
71-
return this.prefetchPackage(module, courseId, (siteId) => this.prefetchSurvey(module, courseId, siteId));
72-
}
40+
/**
41+
* Get prefetch handler instance.
42+
*
43+
* @returns Prefetch handler.
44+
*/
45+
export function getPrefetchHandlerInstance(): CoreCourseModulePrefetchHandler {
46+
if (!prefetchHandlerInstance) {
47+
prefetchHandlerInstance = asyncInstance(async () => {
48+
const { AddonModSurveyPrefetchHandler } = await import('./prefetch-lazy');
7349

74-
/**
75-
* Prefetch a survey.
76-
*
77-
* @param module Module.
78-
* @param courseId Course ID the module belongs to.
79-
* @param siteId SiteId or current site.
80-
* @returns Promise resolved when done.
81-
*/
82-
protected async prefetchSurvey(module: CoreCourseAnyModuleData, courseId: number, siteId: string): Promise<void> {
83-
const survey = await AddonModSurvey.getSurvey(courseId, module.id, {
84-
readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK,
85-
siteId,
50+
return AddonModSurveyPrefetchHandler.instance;
8651
});
8752

88-
const promises: Promise<unknown>[] = [];
89-
const files = this.getIntroFilesFromInstance(module, survey);
90-
91-
// Prefetch files.
92-
promises.push(CoreFilepool.addFilesToQueue(siteId, files, AddonModSurveyProvider.COMPONENT, module.id));
93-
94-
// If survey isn't answered, prefetch the questions.
95-
if (!survey.surveydone) {
96-
promises.push(AddonModSurvey.getQuestions(survey.id, {
97-
cmId: module.id,
98-
readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK,
99-
siteId,
100-
}));
101-
}
102-
103-
await Promise.all(promises);
104-
}
105-
106-
/**
107-
* @inheritdoc
108-
*/
109-
sync(module: CoreCourseAnyModuleData, courseId: number, siteId?: string): Promise<AddonModSurveySyncResult> {
110-
return AddonModSurveySync.syncSurvey(module.instance, undefined, siteId);
53+
prefetchHandlerInstance.setEagerInstance(new AddonModSurveyPrefetchHandlerService());
54+
prefetchHandlerInstance.setLazyMethods(['sync']);
55+
prefetchHandlerInstance.setLazyOverrides([
56+
'prefetch',
57+
'isEnabled',
58+
'invalidateModule',
59+
'invalidateContent',
60+
'getIntroFiles',
61+
]);
11162
}
11263

64+
return prefetchHandlerInstance;
11365
}
114-
export const AddonModSurveyPrefetchHandler = makeSingleton(AddonModSurveyPrefetchHandlerService);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 { CoreCronHandler } from '@services/cron';
17+
import { makeSingleton } from '@singletons';
18+
import { AddonModSurveySync } from '../survey-sync';
19+
import { AddonModSurveySyncCronHandlerService } from '@addons/mod/survey/services/handlers/sync-cron';
20+
21+
/**
22+
* Synchronization cron handler.
23+
*/
24+
@Injectable( { providedIn: 'root' })
25+
export class AddonModSurveySyncCronHandlerLazyService
26+
extends AddonModSurveySyncCronHandlerService
27+
implements CoreCronHandler {
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
async execute(siteId?: string, force?: boolean): Promise<void> {
33+
await AddonModSurveySync.syncAllSurveys(siteId, force);
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
getInterval(): number {
40+
return AddonModSurveySync.syncInterval;
41+
}
42+
43+
}
44+
export const AddonModSurveySyncCronHandler = makeSingleton(AddonModSurveySyncCronHandlerLazyService);

0 commit comments

Comments
 (0)