MOBILE-2850 survey: Prefetch data after syncing survey

main
Dani Palou 2019-02-25 15:59:53 +01:00
parent ceca8cb5c6
commit 88b5eb7af0
5 changed files with 54 additions and 26 deletions

View File

@ -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.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();
});

View File

@ -111,7 +111,7 @@ export class AddonModSurveyPrefetchHandler extends CoreCourseActivityPrefetchHan
* @return {Promise<any>} Promise resolved when done.
*/
protected prefetchSurvey(module: any, courseId: number, single: boolean, siteId: string): Promise<any> {
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);

View File

@ -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<any>} Promise resolved when the questions are retrieved.
*/
getQuestions(surveyId: number, siteId?: string): Promise<any> {
getQuestions(surveyId: number, ignoreCache?: boolean, siteId?: string): Promise<any> {
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;
@ -89,18 +96,24 @@ export class AddonModSurveyProvider {
* @param {number} courseId Course ID.
* @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<any>} Promise resolved when the survey is retrieved.
*/
protected getSurveyDataByKey(courseId: number, key: string, value: any, siteId?: string): Promise<any> {
protected getSurveyDataByKey(courseId: number, key: string, value: any, ignoreCache?: boolean, siteId?: string): Promise<any> {
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) => {
@ -121,11 +134,12 @@ export class AddonModSurveyProvider {
*
* @param {number} courseId Course 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<any>} Promise resolved when the survey is retrieved.
*/
getSurvey(courseId: number, cmId: number, siteId?: string): Promise<any> {
return this.getSurveyDataByKey(courseId, 'coursemodule', cmId, siteId);
getSurvey(courseId: number, cmId: number, ignoreCache?: boolean, siteId?: string): Promise<any> {
return this.getSurveyDataByKey(courseId, 'coursemodule', cmId, ignoreCache, siteId);
}
/**
@ -133,11 +147,12 @@ export class AddonModSurveyProvider {
*
* @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<any>} Promise resolved when the survey is retrieved.
*/
getSurveyById(courseId: number, id: number, siteId?: string): Promise<any> {
return this.getSurveyDataByKey(courseId, 'id', id, siteId);
getSurveyById(courseId: number, id: number, ignoreCache?: boolean, siteId?: string): Promise<any> {
return this.getSurveyDataByKey(courseId, 'id', id, ignoreCache, siteId);
}
/**

View File

@ -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.
});
}

View File

@ -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) {