MOBILE-2850 survey: Prefetch data after syncing survey
parent
ceca8cb5c6
commit
88b5eb7af0
|
@ -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);
|
return this.showLoadingAndRefresh(false);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Not downloaded, refresh the data.
|
||||||
|
return this.showLoadingAndRefresh(false);
|
||||||
|
}
|
||||||
}).finally(() => {
|
}).finally(() => {
|
||||||
modal.dismiss();
|
modal.dismiss();
|
||||||
});
|
});
|
||||||
|
|
|
@ -111,7 +111,7 @@ export class AddonModSurveyPrefetchHandler extends CoreCourseActivityPrefetchHan
|
||||||
* @return {Promise<any>} Promise resolved when done.
|
* @return {Promise<any>} Promise resolved when done.
|
||||||
*/
|
*/
|
||||||
protected prefetchSurvey(module: any, courseId: number, single: boolean, siteId: string): Promise<any> {
|
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 = [],
|
const promises = [],
|
||||||
files = this.getIntroFilesFromInstance(module, survey);
|
files = this.getIntroFilesFromInstance(module, survey);
|
||||||
|
|
||||||
|
@ -120,7 +120,7 @@ export class AddonModSurveyPrefetchHandler extends CoreCourseActivityPrefetchHan
|
||||||
|
|
||||||
// If survey isn't answered, prefetch the questions.
|
// If survey isn't answered, prefetch the questions.
|
||||||
if (!survey.surveydone) {
|
if (!survey.surveydone) {
|
||||||
promises.push(this.surveyProvider.getQuestions(survey.id));
|
promises.push(this.surveyProvider.getQuestions(survey.id, true, siteId));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.all(promises);
|
return Promise.all(promises);
|
||||||
|
|
|
@ -20,6 +20,7 @@ import { CoreAppProvider } from '@providers/app';
|
||||||
import { CoreFilepoolProvider } from '@providers/filepool';
|
import { CoreFilepoolProvider } from '@providers/filepool';
|
||||||
import { CoreCourseLogHelperProvider } from '@core/course/providers/log-helper';
|
import { CoreCourseLogHelperProvider } from '@core/course/providers/log-helper';
|
||||||
import { AddonModSurveyOfflineProvider } from './offline';
|
import { AddonModSurveyOfflineProvider } from './offline';
|
||||||
|
import { CoreSiteWSPreSets } from '@classes/site';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Service that provides some features for surveys.
|
* Service that provides some features for surveys.
|
||||||
|
@ -41,18 +42,24 @@ export class AddonModSurveyProvider {
|
||||||
* Get a survey's questions.
|
* Get a survey's questions.
|
||||||
*
|
*
|
||||||
* @param {number} surveyId Survey ID.
|
* @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.
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
||||||
* @return {Promise<any>} Promise resolved when the questions are retrieved.
|
* @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) => {
|
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||||
const params = {
|
const params = {
|
||||||
surveyid: surveyId
|
surveyid: surveyId
|
||||||
},
|
},
|
||||||
preSets = {
|
preSets: CoreSiteWSPreSets = {
|
||||||
cacheKey: this.getQuestionsCacheKey(surveyId)
|
cacheKey: this.getQuestionsCacheKey(surveyId)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (ignoreCache) {
|
||||||
|
preSets.getFromCache = false;
|
||||||
|
preSets.emergencyCache = false;
|
||||||
|
}
|
||||||
|
|
||||||
return site.read('mod_survey_get_questions', params, preSets).then((response) => {
|
return site.read('mod_survey_get_questions', params, preSets).then((response) => {
|
||||||
if (response.questions) {
|
if (response.questions) {
|
||||||
return response.questions;
|
return response.questions;
|
||||||
|
@ -89,18 +96,24 @@ export class AddonModSurveyProvider {
|
||||||
* @param {number} courseId Course ID.
|
* @param {number} courseId Course ID.
|
||||||
* @param {string} key Name of the property to check.
|
* @param {string} key Name of the property to check.
|
||||||
* @param {any} value Value to search.
|
* @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.
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
||||||
* @return {Promise<any>} Promise resolved when the survey is retrieved.
|
* @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) => {
|
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||||
const params = {
|
const params = {
|
||||||
courseids: [courseId]
|
courseids: [courseId]
|
||||||
},
|
},
|
||||||
preSets = {
|
preSets: CoreSiteWSPreSets = {
|
||||||
cacheKey: this.getSurveyCacheKey(courseId)
|
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) => {
|
return site.read('mod_survey_get_surveys_by_courses', params, preSets).then((response) => {
|
||||||
if (response && response.surveys) {
|
if (response && response.surveys) {
|
||||||
const currentSurvey = response.surveys.find((survey) => {
|
const currentSurvey = response.surveys.find((survey) => {
|
||||||
|
@ -121,11 +134,12 @@ export class AddonModSurveyProvider {
|
||||||
*
|
*
|
||||||
* @param {number} courseId Course 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.
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
||||||
* @return {Promise<any>} Promise resolved when the survey is retrieved.
|
* @return {Promise<any>} Promise resolved when the survey is retrieved.
|
||||||
*/
|
*/
|
||||||
getSurvey(courseId: number, cmId: number, siteId?: string): Promise<any> {
|
getSurvey(courseId: number, cmId: number, ignoreCache?: boolean, siteId?: string): Promise<any> {
|
||||||
return this.getSurveyDataByKey(courseId, 'coursemodule', cmId, siteId);
|
return this.getSurveyDataByKey(courseId, 'coursemodule', cmId, ignoreCache, siteId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -133,11 +147,12 @@ export class AddonModSurveyProvider {
|
||||||
*
|
*
|
||||||
* @param {number} courseId Course ID.
|
* @param {number} courseId Course ID.
|
||||||
* @param {number} id Survey 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.
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
||||||
* @return {Promise<any>} Promise resolved when the survey is retrieved.
|
* @return {Promise<any>} Promise resolved when the survey is retrieved.
|
||||||
*/
|
*/
|
||||||
getSurveyById(courseId: number, id: number, siteId?: string): Promise<any> {
|
getSurveyById(courseId: number, id: number, ignoreCache?: boolean, siteId?: string): Promise<any> {
|
||||||
return this.getSurveyDataByKey(courseId, 'id', id, siteId);
|
return this.getSurveyDataByKey(courseId, 'id', id, ignoreCache, siteId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -15,7 +15,6 @@
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { CoreLoggerProvider } from '@providers/logger';
|
import { CoreLoggerProvider } from '@providers/logger';
|
||||||
import { CoreSitesProvider } from '@providers/sites';
|
import { CoreSitesProvider } from '@providers/sites';
|
||||||
import { CoreSyncBaseProvider } from '@classes/base-sync';
|
|
||||||
import { CoreAppProvider } from '@providers/app';
|
import { CoreAppProvider } from '@providers/app';
|
||||||
import { CoreUtilsProvider } from '@providers/utils/utils';
|
import { CoreUtilsProvider } from '@providers/utils/utils';
|
||||||
import { CoreTextUtilsProvider } from '@providers/utils/text';
|
import { CoreTextUtilsProvider } from '@providers/utils/text';
|
||||||
|
@ -26,13 +25,16 @@ import { CoreEventsProvider } from '@providers/events';
|
||||||
import { TranslateService } from '@ngx-translate/core';
|
import { TranslateService } from '@ngx-translate/core';
|
||||||
import { CoreCourseProvider } from '@core/course/providers/course';
|
import { CoreCourseProvider } from '@core/course/providers/course';
|
||||||
import { CoreCourseLogHelperProvider } from '@core/course/providers/log-helper';
|
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 { CoreSyncProvider } from '@providers/sync';
|
||||||
|
import { AddonModSurveyPrefetchHandler } from './prefetch-handler';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Service to sync surveys.
|
* Service to sync surveys.
|
||||||
*/
|
*/
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AddonModSurveySyncProvider extends CoreSyncBaseProvider {
|
export class AddonModSurveySyncProvider extends CoreCourseActivitySyncBaseProvider {
|
||||||
|
|
||||||
static AUTO_SYNCED = 'addon_mod_survey_autom_synced';
|
static AUTO_SYNCED = 'addon_mod_survey_autom_synced';
|
||||||
protected componentTranslate: string;
|
protected componentTranslate: string;
|
||||||
|
@ -41,10 +43,11 @@ export class AddonModSurveySyncProvider extends CoreSyncBaseProvider {
|
||||||
syncProvider: CoreSyncProvider, textUtils: CoreTextUtilsProvider, translate: TranslateService,
|
syncProvider: CoreSyncProvider, textUtils: CoreTextUtilsProvider, translate: TranslateService,
|
||||||
courseProvider: CoreCourseProvider, private surveyOffline: AddonModSurveyOfflineProvider,
|
courseProvider: CoreCourseProvider, private surveyOffline: AddonModSurveyOfflineProvider,
|
||||||
private eventsProvider: CoreEventsProvider, private surveyProvider: AddonModSurveyProvider,
|
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,
|
super('AddonModSurveySyncProvider', loggerProvider, sitesProvider, appProvider, syncProvider, textUtils, translate,
|
||||||
timeUtils);
|
timeUtils, prefetchDelegate, prefetchHandler);
|
||||||
|
|
||||||
this.componentTranslate = courseProvider.translateModuleName('survey');
|
this.componentTranslate = courseProvider.translateModuleName('survey');
|
||||||
}
|
}
|
||||||
|
@ -57,7 +60,7 @@ export class AddonModSurveySyncProvider extends CoreSyncBaseProvider {
|
||||||
* @return {string} Sync ID.
|
* @return {string} Sync ID.
|
||||||
* @protected
|
* @protected
|
||||||
*/
|
*/
|
||||||
getSyncId (surveyId: number, userId: number): string {
|
getSyncId(surveyId: number, userId: number): string {
|
||||||
return surveyId + '#' + userId;
|
return surveyId + '#' + userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,9 +195,7 @@ export class AddonModSurveySyncProvider extends CoreSyncBaseProvider {
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
if (courseId) {
|
if (courseId) {
|
||||||
// Data has been sent to server, update survey data.
|
// Data has been sent to server, update survey data.
|
||||||
return this.surveyProvider.invalidateSurveyData(courseId, siteId).then(() => {
|
return this.prefetchAfterUpdate(module, courseId, undefined, siteId).catch(() => {
|
||||||
return this.surveyProvider.getSurveyById(courseId, surveyId, siteId);
|
|
||||||
}).catch(() => {
|
|
||||||
// Ignore errors.
|
// Ignore errors.
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,7 @@ export class CoreCourseActivitySyncBaseProvider extends CoreSyncBaseProvider {
|
||||||
if (result && result.updates) {
|
if (result && result.updates) {
|
||||||
// Only prefetch if files haven't changed.
|
// Only prefetch if files haven't changed.
|
||||||
const fileChanged = !!result.updates.find((entry) => {
|
const fileChanged = !!result.updates.find((entry) => {
|
||||||
return entry.match(regex);
|
return entry.name.match(regex);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!fileChanged) {
|
if (!fileChanged) {
|
||||||
|
|
Loading…
Reference in New Issue