MOBILE-2339 survey: Sync if needed

main
Pau Ferrer Ocaña 2018-04-05 09:37:41 +02:00
parent 1346f86372
commit b58ff46b3c
1 changed files with 21 additions and 1 deletions

View File

@ -78,7 +78,7 @@ export class AddonModSurveySyncProvider extends CoreSyncBaseProvider {
return this.surveyOffline.getAllData(siteId).then((entries) => {
// Sync all surveys.
const promises = entries.map((entry) => {
return this.syncSurvey(entry.surveyid, entry.userid, siteId).then((result) => {
return this.syncSurveyIfNeeded(entry.surveyid, entry.userid, siteId).then((result) => {
if (result && result.answersSent) {
// Sync successful, send event.
this.eventsProvider.trigger(AddonModSurveySyncProvider.AUTO_SYNCED, {
@ -94,6 +94,26 @@ export class AddonModSurveySyncProvider extends CoreSyncBaseProvider {
});
}
/**
* Sync a survey only if a certain time has passed since the last time.
*
* @param {Number} surveyId Survey ID.
* @param {Number} userId User the answers belong to.
* @param {String} [siteId] Site ID. If not defined, current site.
* @return {Promise<any>} Promise resolved when the survey is synced or if it doesn't need to be synced.
*/
syncSurveyIfNeeded(surveyId: number, userId: number, siteId?: string): Promise<any> {
siteId = siteId || this.sitesProvider.getCurrentSiteId();
const syncId = this.getSyncId(surveyId, userId);
return this.isSyncNeeded(syncId, siteId).then((needed) => {
if (needed) {
return this.syncSurvey(surveyId, userId, siteId);
}
});
}
/**
* Synchronize a survey.
*