From 88b5eb7af0651bf11e5874c21a1b59de20188ff5 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 25 Feb 2019 15:59:53 +0100 Subject: [PATCH] MOBILE-2850 survey: Prefetch data after syncing survey --- .../mod/survey/components/index/index.ts | 16 +++++++- .../mod/survey/providers/prefetch-handler.ts | 4 +- src/addon/mod/survey/providers/survey.ts | 41 +++++++++++++------ src/addon/mod/survey/providers/sync.ts | 17 ++++---- src/core/course/classes/activity-sync.ts | 2 +- 5 files changed, 54 insertions(+), 26 deletions(-) diff --git a/src/addon/mod/survey/components/index/index.ts b/src/addon/mod/survey/components/index/index.ts index 581064e0e..3a7eb4151 100644 --- a/src/addon/mod/survey/components/index/index.ts +++ b/src/addon/mod/survey/components/index/index.ts @@ -188,8 +188,20 @@ export class AddonModSurveyIndexComponent extends CoreCourseModuleMainActivityCo }); } - return this.surveyProvider.submitAnswers(this.survey.id, this.survey.name, this.courseId, answers).then(() => { - return this.showLoadingAndRefresh(false); + return this.surveyProvider.submitAnswers(this.survey.id, this.survey.name, this.courseId, answers).then((online) => { + if (online && this.isPrefetched()) { + // The survey is downloaded, update the data. + return this.surveySync.prefetchAfterUpdate(this.module, this.courseId).then(() => { + // Update the view. + this.showLoadingAndFetch(false, false); + }).catch((error) => { + // Prefetch failed, refresh the data. + return this.showLoadingAndRefresh(false); + }); + } else { + // Not downloaded, refresh the data. + return this.showLoadingAndRefresh(false); + } }).finally(() => { modal.dismiss(); }); diff --git a/src/addon/mod/survey/providers/prefetch-handler.ts b/src/addon/mod/survey/providers/prefetch-handler.ts index b3ac45ea6..e6b785486 100644 --- a/src/addon/mod/survey/providers/prefetch-handler.ts +++ b/src/addon/mod/survey/providers/prefetch-handler.ts @@ -111,7 +111,7 @@ export class AddonModSurveyPrefetchHandler extends CoreCourseActivityPrefetchHan * @return {Promise} Promise resolved when done. */ protected prefetchSurvey(module: any, courseId: number, single: boolean, siteId: string): Promise { - return this.surveyProvider.getSurvey(courseId, module.id).then((survey) => { + return this.surveyProvider.getSurvey(courseId, module.id, true, siteId).then((survey) => { const promises = [], files = this.getIntroFilesFromInstance(module, survey); @@ -120,7 +120,7 @@ export class AddonModSurveyPrefetchHandler extends CoreCourseActivityPrefetchHan // If survey isn't answered, prefetch the questions. if (!survey.surveydone) { - promises.push(this.surveyProvider.getQuestions(survey.id)); + promises.push(this.surveyProvider.getQuestions(survey.id, true, siteId)); } return Promise.all(promises); diff --git a/src/addon/mod/survey/providers/survey.ts b/src/addon/mod/survey/providers/survey.ts index 75bd970d0..ff376e2f8 100644 --- a/src/addon/mod/survey/providers/survey.ts +++ b/src/addon/mod/survey/providers/survey.ts @@ -20,6 +20,7 @@ import { CoreAppProvider } from '@providers/app'; import { CoreFilepoolProvider } from '@providers/filepool'; import { CoreCourseLogHelperProvider } from '@core/course/providers/log-helper'; import { AddonModSurveyOfflineProvider } from './offline'; +import { CoreSiteWSPreSets } from '@classes/site'; /** * Service that provides some features for surveys. @@ -41,18 +42,24 @@ export class AddonModSurveyProvider { * Get a survey's questions. * * @param {number} surveyId Survey ID. + * @param {boolean} [ignoreCache] True if it should ignore cached data (it will always fail in offline or server down). * @param {string} [siteId] Site ID. If not defined, current site. * @return {Promise} Promise resolved when the questions are retrieved. */ - getQuestions(surveyId: number, siteId?: string): Promise { + getQuestions(surveyId: number, ignoreCache?: boolean, siteId?: string): Promise { return this.sitesProvider.getSite(siteId).then((site) => { const params = { surveyid: surveyId }, - preSets = { + preSets: CoreSiteWSPreSets = { cacheKey: this.getQuestionsCacheKey(surveyId) }; + if (ignoreCache) { + preSets.getFromCache = false; + preSets.emergencyCache = false; + } + return site.read('mod_survey_get_questions', params, preSets).then((response) => { if (response.questions) { return response.questions; @@ -87,20 +94,26 @@ export class AddonModSurveyProvider { * Get a survey data. * * @param {number} courseId Course ID. - * @param {string} key Name of the property to check. - * @param {any} value Value to search. + * @param {string} key Name of the property to check. + * @param {any} value Value to search. + * @param {boolean} [ignoreCache] True if it should ignore cached data (it will always fail in offline or server down). * @param {string} [siteId] Site ID. If not defined, current site. * @return {Promise} Promise resolved when the survey is retrieved. */ - protected getSurveyDataByKey(courseId: number, key: string, value: any, siteId?: string): Promise { + protected getSurveyDataByKey(courseId: number, key: string, value: any, ignoreCache?: boolean, siteId?: string): Promise { return this.sitesProvider.getSite(siteId).then((site) => { const params = { courseids: [courseId] }, - preSets = { + preSets: CoreSiteWSPreSets = { cacheKey: this.getSurveyCacheKey(courseId) }; + if (ignoreCache) { + preSets.getFromCache = false; + preSets.emergencyCache = false; + } + return site.read('mod_survey_get_surveys_by_courses', params, preSets).then((response) => { if (response && response.surveys) { const currentSurvey = response.surveys.find((survey) => { @@ -120,24 +133,26 @@ export class AddonModSurveyProvider { * Get a survey by course module ID. * * @param {number} courseId Course ID. - * @param {number} cmId Course module ID. + * @param {number} cmId Course module ID. + * @param {boolean} [ignoreCache] True if it should ignore cached data (it will always fail in offline or server down). * @param {string} [siteId] Site ID. If not defined, current site. * @return {Promise} Promise resolved when the survey is retrieved. */ - getSurvey(courseId: number, cmId: number, siteId?: string): Promise { - return this.getSurveyDataByKey(courseId, 'coursemodule', cmId, siteId); + getSurvey(courseId: number, cmId: number, ignoreCache?: boolean, siteId?: string): Promise { + return this.getSurveyDataByKey(courseId, 'coursemodule', cmId, ignoreCache, siteId); } /** * Get a survey by ID. * - * @param {number} courseId Course ID. - * @param {number} id Survey ID. + * @param {number} courseId Course ID. + * @param {number} id Survey ID. + * @param {boolean} [ignoreCache] True if it should ignore cached data (it will always fail in offline or server down). * @param {string} [siteId] Site ID. If not defined, current site. * @return {Promise} Promise resolved when the survey is retrieved. */ - getSurveyById(courseId: number, id: number, siteId?: string): Promise { - return this.getSurveyDataByKey(courseId, 'id', id, siteId); + getSurveyById(courseId: number, id: number, ignoreCache?: boolean, siteId?: string): Promise { + return this.getSurveyDataByKey(courseId, 'id', id, ignoreCache, siteId); } /** diff --git a/src/addon/mod/survey/providers/sync.ts b/src/addon/mod/survey/providers/sync.ts index 2e301f5fa..7ade2bb70 100644 --- a/src/addon/mod/survey/providers/sync.ts +++ b/src/addon/mod/survey/providers/sync.ts @@ -15,7 +15,6 @@ import { Injectable } from '@angular/core'; import { CoreLoggerProvider } from '@providers/logger'; import { CoreSitesProvider } from '@providers/sites'; -import { CoreSyncBaseProvider } from '@classes/base-sync'; import { CoreAppProvider } from '@providers/app'; import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreTextUtilsProvider } from '@providers/utils/text'; @@ -26,13 +25,16 @@ import { CoreEventsProvider } from '@providers/events'; import { TranslateService } from '@ngx-translate/core'; import { CoreCourseProvider } from '@core/course/providers/course'; import { CoreCourseLogHelperProvider } from '@core/course/providers/log-helper'; +import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-prefetch-delegate'; +import { CoreCourseActivitySyncBaseProvider } from '@core/course/classes/activity-sync'; import { CoreSyncProvider } from '@providers/sync'; +import { AddonModSurveyPrefetchHandler } from './prefetch-handler'; /** * Service to sync surveys. */ @Injectable() -export class AddonModSurveySyncProvider extends CoreSyncBaseProvider { +export class AddonModSurveySyncProvider extends CoreCourseActivitySyncBaseProvider { static AUTO_SYNCED = 'addon_mod_survey_autom_synced'; protected componentTranslate: string; @@ -41,10 +43,11 @@ export class AddonModSurveySyncProvider extends CoreSyncBaseProvider { syncProvider: CoreSyncProvider, textUtils: CoreTextUtilsProvider, translate: TranslateService, courseProvider: CoreCourseProvider, private surveyOffline: AddonModSurveyOfflineProvider, private eventsProvider: CoreEventsProvider, private surveyProvider: AddonModSurveyProvider, - private utils: CoreUtilsProvider, timeUtils: CoreTimeUtilsProvider, private logHelper: CoreCourseLogHelperProvider) { + private utils: CoreUtilsProvider, timeUtils: CoreTimeUtilsProvider, private logHelper: CoreCourseLogHelperProvider, + prefetchDelegate: CoreCourseModulePrefetchDelegate, prefetchHandler: AddonModSurveyPrefetchHandler) { super('AddonModSurveySyncProvider', loggerProvider, sitesProvider, appProvider, syncProvider, textUtils, translate, - timeUtils); + timeUtils, prefetchDelegate, prefetchHandler); this.componentTranslate = courseProvider.translateModuleName('survey'); } @@ -57,7 +60,7 @@ export class AddonModSurveySyncProvider extends CoreSyncBaseProvider { * @return {string} Sync ID. * @protected */ - getSyncId (surveyId: number, userId: number): string { + getSyncId(surveyId: number, userId: number): string { return surveyId + '#' + userId; } @@ -192,9 +195,7 @@ export class AddonModSurveySyncProvider extends CoreSyncBaseProvider { }).then(() => { if (courseId) { // Data has been sent to server, update survey data. - return this.surveyProvider.invalidateSurveyData(courseId, siteId).then(() => { - return this.surveyProvider.getSurveyById(courseId, surveyId, siteId); - }).catch(() => { + return this.prefetchAfterUpdate(module, courseId, undefined, siteId).catch(() => { // Ignore errors. }); } diff --git a/src/core/course/classes/activity-sync.ts b/src/core/course/classes/activity-sync.ts index 2ccdb8552..e6bd7e96c 100644 --- a/src/core/course/classes/activity-sync.ts +++ b/src/core/course/classes/activity-sync.ts @@ -55,7 +55,7 @@ export class CoreCourseActivitySyncBaseProvider extends CoreSyncBaseProvider { if (result && result.updates) { // Only prefetch if files haven't changed. const fileChanged = !!result.updates.find((entry) => { - return entry.match(regex); + return entry.name.match(regex); }); if (!fileChanged) {