diff --git a/src/addon/messages/providers/mainmenu-handler.ts b/src/addon/messages/providers/mainmenu-handler.ts index 170d86082..150b6c994 100644 --- a/src/addon/messages/providers/mainmenu-handler.ts +++ b/src/addon/messages/providers/mainmenu-handler.ts @@ -165,9 +165,10 @@ export class AddonMessagesMainMenuHandler implements CoreMainMenuHandler, CoreCr * Receives the ID of the site affected, undefined for all sites. * * @param {string} [siteId] ID of the site affected, undefined for all sites. + * @param {boolean} [force] Wether the execution is forced (manual sync). * @return {Promise} Promise resolved when done, rejected if failure. */ - execute(siteId?: string): Promise { + execute(siteId?: string, force?: boolean): Promise { if (this.sitesProvider.isCurrentSite(siteId)) { this.refreshBadge(); } diff --git a/src/addon/messages/providers/sync-cron-handler.ts b/src/addon/messages/providers/sync-cron-handler.ts index 79dc464a3..466a593c3 100644 --- a/src/addon/messages/providers/sync-cron-handler.ts +++ b/src/addon/messages/providers/sync-cron-handler.ts @@ -30,9 +30,10 @@ export class AddonMessagesSyncCronHandler implements CoreCronHandler { * Receives the ID of the site affected, undefined for all sites. * * @param {string} [siteId] ID of the site affected, undefined for all sites. + * @param {boolean} [force] Wether the execution is forced (manual sync). * @return {Promise} Promise resolved when done, rejected if failure. */ - execute(siteId?: string): Promise { + execute(siteId?: string, force?: boolean): Promise { return this.messagesSync.syncAllDiscussions(siteId); } diff --git a/src/addon/mod/assign/providers/assign-sync.ts b/src/addon/mod/assign/providers/assign-sync.ts index cff8c421a..f65b2f393 100644 --- a/src/addon/mod/assign/providers/assign-sync.ts +++ b/src/addon/mod/assign/providers/assign-sync.ts @@ -109,27 +109,29 @@ export class AddonModAssignSyncProvider extends CoreSyncBaseProvider { /** * Try to synchronize all the assignments in a certain site or in all sites. * + * @param {boolean} force Wether to force sync not depending on last execution. * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. * @return {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - syncAllAssignments(siteId?: string): Promise { - return this.syncOnSites('all assignments', this.syncAllAssignmentsFunc.bind(this), [], siteId); + syncAllAssignments(siteId?: string, force?: boolean): Promise { + return this.syncOnSites('all assignments', this.syncAllAssignmentsFunc.bind(this), [force], siteId); } /** * Sync all assignments on a site. * + * @param {boolean} [force] Wether to force sync not depending on last execution. * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. * @param {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - protected syncAllAssignmentsFunc(siteId?: string): Promise { + protected syncAllAssignmentsFunc(siteId?: string, force?: boolean): Promise { // Get all assignments that have offline data. return this.assignOfflineProvider.getAllAssigns(siteId).then((assignIds) => { - const promises = []; - // Sync all assignments that haven't been synced for a while. - assignIds.forEach((assignId) => { - promises.push(this.syncAssignIfNeeded(assignId, siteId).then((data) => { + const promises = assignIds.map((assignId) => { + const promise = force ? this.syncAssign(assignId, siteId) : this.syncAssignIfNeeded(assignId, siteId); + + return promise.then((data) => { if (data && data.updated) { // Sync done. Send event. this.eventsProvider.trigger(AddonModAssignSyncProvider.AUTO_SYNCED, { @@ -137,7 +139,7 @@ export class AddonModAssignSyncProvider extends CoreSyncBaseProvider { warnings: data.warnings }, siteId); } - })); + }); }); return Promise.all(promises); diff --git a/src/addon/mod/assign/providers/sync-cron-handler.ts b/src/addon/mod/assign/providers/sync-cron-handler.ts index fe68ccddd..fbfb6600c 100644 --- a/src/addon/mod/assign/providers/sync-cron-handler.ts +++ b/src/addon/mod/assign/providers/sync-cron-handler.ts @@ -30,10 +30,11 @@ export class AddonModAssignSyncCronHandler implements CoreCronHandler { * Receives the ID of the site affected, undefined for all sites. * * @param {string} [siteId] ID of the site affected, undefined for all sites. + * @param {boolean} [force] Wether the execution is forced (manual sync). * @return {Promise} Promise resolved when done, rejected if failure. */ - execute(siteId?: string): Promise { - return this.assignSync.syncAllAssignments(siteId); + execute(siteId?: string, force?: boolean): Promise { + return this.assignSync.syncAllAssignments(siteId, force); } /** diff --git a/src/addon/mod/choice/providers/sync-cron-handler.ts b/src/addon/mod/choice/providers/sync-cron-handler.ts index af6cb8e81..92d88b52b 100644 --- a/src/addon/mod/choice/providers/sync-cron-handler.ts +++ b/src/addon/mod/choice/providers/sync-cron-handler.ts @@ -30,10 +30,11 @@ export class AddonModChoiceSyncCronHandler implements CoreCronHandler { * Receives the ID of the site affected, undefined for all sites. * * @param {string} [siteId] ID of the site affected, undefined for all sites. - * @return {Promise} Promise resolved when done, rejected if failure. + * @param {boolean} [force] Wether the execution is forced (manual sync). + * @return {Promise} Promise resolved when done, rejected if failure. */ - execute(siteId?: string): Promise { - return this.choiceSync.syncAllChoices(siteId); + execute(siteId?: string, force?: boolean): Promise { + return this.choiceSync.syncAllChoices(siteId, force); } /** diff --git a/src/addon/mod/choice/providers/sync.ts b/src/addon/mod/choice/providers/sync.ts index 9a14b71d4..bcdc0b848 100644 --- a/src/addon/mod/choice/providers/sync.ts +++ b/src/addon/mod/choice/providers/sync.ts @@ -67,26 +67,29 @@ export class AddonModChoiceSyncProvider extends CoreCourseActivitySyncBaseProvid /** * Try to synchronize all the choices in a certain site or in all sites. * + * @param {boolean} force Wether to force sync not depending on last execution. * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. * @return {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - syncAllChoices(siteId?: string): Promise { - return this.syncOnSites('choices', this.syncAllChoicesFunc.bind(this), undefined, siteId); + syncAllChoices(siteId?: string, force?: boolean): Promise { + return this.syncOnSites('choices', this.syncAllChoicesFunc.bind(this), [force], siteId); } /** * Sync all pending choices on a site. * - * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {boolean} force Wether to force sync not depending on last execution. * @return {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - protected syncAllChoicesFunc(siteId?: string): Promise { + protected syncAllChoicesFunc(siteId?: string, force?: boolean): Promise { return this.choiceOffline.getResponses(siteId).then((responses) => { - const promises = []; - // Sync all responses. - responses.forEach((response) => { - promises.push(this.syncChoiceIfNeeded(response.choiceid, response.userid, siteId).then((result) => { + const promises = responses.map((response) => { + const promise = force ? this.syncChoice(response.choiceid, response.userid, siteId) : + this.syncChoiceIfNeeded(response.choiceid, response.userid, siteId); + + return promise.then((result) => { if (result && result.updated) { // Sync successful, send event. this.eventsProvider.trigger(AddonModChoiceSyncProvider.AUTO_SYNCED, { @@ -95,8 +98,10 @@ export class AddonModChoiceSyncProvider extends CoreCourseActivitySyncBaseProvid warnings: result.warnings }, siteId); } - })); + }); }); + + return Promise.all(promises); }); } diff --git a/src/addon/mod/data/providers/sync-cron-handler.ts b/src/addon/mod/data/providers/sync-cron-handler.ts index 31a3db30a..c67e7a102 100644 --- a/src/addon/mod/data/providers/sync-cron-handler.ts +++ b/src/addon/mod/data/providers/sync-cron-handler.ts @@ -30,10 +30,11 @@ export class AddonModDataSyncCronHandler implements CoreCronHandler { * Receives the ID of the site affected, undefined for all sites. * * @param {string} [siteId] ID of the site affected, undefined for all sites. + * @param {boolean} [force] Wether the execution is forced (manual sync). * @return {Promise} Promise resolved when done, rejected if failure. */ - execute(siteId?: string): Promise { - return this.dataSync.syncAllDatabases(siteId); + execute(siteId?: string, force?: boolean): Promise { + return this.dataSync.syncAllDatabases(siteId, force); } /** diff --git a/src/addon/mod/data/providers/sync.ts b/src/addon/mod/data/providers/sync.ts index aeb16b190..6b5768adf 100644 --- a/src/addon/mod/data/providers/sync.ts +++ b/src/addon/mod/data/providers/sync.ts @@ -66,19 +66,22 @@ export class AddonModDataSyncProvider extends CoreSyncBaseProvider { /** * Try to synchronize all the databases in a certain site or in all sites. * + * @param {boolean} force Wether to force sync not depending on last execution. * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. * @return {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - syncAllDatabases(siteId?: string): Promise { - return this.syncOnSites('all databases', this.syncAllDatabasesFunc.bind(this), undefined, siteId); + syncAllDatabases(siteId?: string, force?: boolean): Promise { + return this.syncOnSites('all databases', this.syncAllDatabasesFunc.bind(this), [force], siteId); } /** * Sync all pending databases on a site. - * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * + * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {boolean} force Wether to force sync not depending on last execution. * @param {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - protected syncAllDatabasesFunc(siteId?: string): Promise { + protected syncAllDatabasesFunc(siteId?: string, force?: boolean): Promise { siteId = siteId || this.sitesProvider.getCurrentSiteId(); const promises = []; @@ -93,8 +96,10 @@ export class AddonModDataSyncProvider extends CoreSyncBaseProvider { return; } - promises[action.dataid] = this.syncDatabaseIfNeeded(action.dataid, siteId) - .then((result) => { + promises[action.dataid] = force ? this.syncDatabase(action.dataid, siteId) : + this.syncDatabaseIfNeeded(action.dataid, siteId); + + promises[action.dataid].then((result) => { if (result && result.updated) { // Sync done. Send event. this.eventsProvider.trigger(AddonModDataSyncProvider.AUTO_SYNCED, { diff --git a/src/addon/mod/feedback/providers/sync-cron-handler.ts b/src/addon/mod/feedback/providers/sync-cron-handler.ts index 6ace28013..94d43e905 100644 --- a/src/addon/mod/feedback/providers/sync-cron-handler.ts +++ b/src/addon/mod/feedback/providers/sync-cron-handler.ts @@ -30,10 +30,11 @@ export class AddonModFeedbackSyncCronHandler implements CoreCronHandler { * Receives the ID of the site affected, undefined for all sites. * * @param {string} [siteId] ID of the site affected, undefined for all sites. + * @param {boolean} [force] Wether the execution is forced (manual sync). * @return {Promise} Promise resolved when done, rejected if failure. */ - execute(siteId?: string): Promise { - return this.feedbackSync.syncAllFeedbacks(siteId); + execute(siteId?: string, force?: boolean): Promise { + return this.feedbackSync.syncAllFeedbacks(siteId, force); } /** diff --git a/src/addon/mod/feedback/providers/sync.ts b/src/addon/mod/feedback/providers/sync.ts index 6c6e008a4..08604869a 100644 --- a/src/addon/mod/feedback/providers/sync.ts +++ b/src/addon/mod/feedback/providers/sync.ts @@ -71,20 +71,22 @@ export class AddonModFeedbackSyncProvider extends CoreCourseActivitySyncBaseProv /** * Try to synchronize all the feedbacks in a certain site or in all sites. * + * @param {boolean} force Wether to force sync not depending on last execution. * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. * @return {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - syncAllFeedbacks(siteId?: string): Promise { - return this.syncOnSites('all feedbacks', this.syncAllFeedbacksFunc.bind(this), undefined, siteId); + syncAllFeedbacks(siteId?: string, force?: boolean): Promise { + return this.syncOnSites('all feedbacks', this.syncAllFeedbacksFunc.bind(this), [force], siteId); } /** * Sync all pending feedbacks on a site. * - * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {boolean} force Wether to force sync not depending on last execution. * @param {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - protected syncAllFeedbacksFunc(siteId?: string): Promise { + protected syncAllFeedbacksFunc(siteId?: string, force?: boolean): Promise { // Sync all new responses. return this.feedbackOffline.getAllFeedbackResponses(siteId).then((responses) => { const promises = {}; @@ -97,7 +99,10 @@ export class AddonModFeedbackSyncProvider extends CoreCourseActivitySyncBaseProv continue; } - promises[response.feedbackid] = this.syncFeedbackIfNeeded(response.feedbackid, siteId).then((result) => { + promises[response.feedbackid] = force ? this.syncFeedback(response.feedbackid, siteId) : + this.syncFeedbackIfNeeded(response.feedbackid, siteId); + + promises[response.feedbackid].then((result) => { if (result && result.updated) { // Sync successful, send event. this.eventsProvider.trigger(AddonModFeedbackSyncProvider.AUTO_SYNCED, { diff --git a/src/addon/mod/forum/providers/sync-cron-handler.ts b/src/addon/mod/forum/providers/sync-cron-handler.ts index 860368b25..53a2406e7 100644 --- a/src/addon/mod/forum/providers/sync-cron-handler.ts +++ b/src/addon/mod/forum/providers/sync-cron-handler.ts @@ -30,10 +30,11 @@ export class AddonModForumSyncCronHandler implements CoreCronHandler { * Receives the ID of the site affected, undefined for all sites. * * @param {string} [siteId] ID of the site affected, undefined for all sites. + * @param {boolean} [force] Wether the execution is forced (manual sync). * @return {Promise} Promise resolved when done, rejected if failure. */ - execute(siteId?: string): Promise { - return this.forumSync.syncAllForums(siteId); + execute(siteId?: string, force?: boolean): Promise { + return this.forumSync.syncAllForums(siteId, force); } /** diff --git a/src/addon/mod/forum/providers/sync.ts b/src/addon/mod/forum/providers/sync.ts index 8d63ccfc5..a829d2662 100644 --- a/src/addon/mod/forum/providers/sync.ts +++ b/src/addon/mod/forum/providers/sync.ts @@ -69,19 +69,21 @@ export class AddonModForumSyncProvider extends CoreSyncBaseProvider { * Try to synchronize all the forums in a certain site or in all sites. * * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {boolean} [force] Wether to force sync not depending on last execution. * @return {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - syncAllForums(siteId?: string): Promise { - return this.syncOnSites('all forums', this.syncAllForumsFunc.bind(this), [], siteId); + syncAllForums(siteId?: string, force?: boolean): Promise { + return this.syncOnSites('all forums', this.syncAllForumsFunc.bind(this), [force], siteId); } /** * Sync all forums on a site. * - * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {string} siteId Site ID to sync. + * @param {boolean} [force] Wether to force sync not depending on last execution. * @return {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - protected syncAllForumsFunc(siteId?: string): Promise { + protected syncAllForumsFunc(siteId: string, force?: boolean): Promise { const sitePromises = []; // Sync all new discussions. @@ -94,8 +96,10 @@ export class AddonModForumSyncProvider extends CoreSyncBaseProvider { return; } - promises[discussion.forumid] = this.syncForumDiscussionsIfNeeded(discussion.forumid, discussion.userid, siteId) - .then((result) => { + promises[discussion.forumid] = force ? this.syncForumDiscussions(discussion.forumid, discussion.userid, siteId) : + this.syncForumDiscussionsIfNeeded(discussion.forumid, discussion.userid, siteId); + + promises[discussion.forumid].then((result) => { if (result && result.updated) { // Sync successful, send event. this.eventsProvider.trigger(AddonModForumSyncProvider.AUTO_SYNCED, { @@ -120,8 +124,10 @@ export class AddonModForumSyncProvider extends CoreSyncBaseProvider { return; } - promises[reply.discussionid] = this.syncDiscussionRepliesIfNeeded(reply.discussionid, reply.userid, siteId) - .then((result) => { + promises[reply.discussionid] = force ? this.syncDiscussionReplies(reply.discussionid, reply.userid, siteId) : + this.syncDiscussionRepliesIfNeeded(reply.discussionid, reply.userid, siteId); + + promises[reply.discussionid].then((result) => { if (result && result.updated) { // Sync successful, send event. this.eventsProvider.trigger(AddonModForumSyncProvider.AUTO_SYNCED, { @@ -137,7 +143,7 @@ export class AddonModForumSyncProvider extends CoreSyncBaseProvider { return Promise.all(this.utils.objectToArray(promises)); })); - sitePromises.push(this.syncRatings(undefined, undefined, siteId)); + sitePromises.push(this.syncRatings(undefined, undefined, force, siteId)); return Promise.all(sitePromises); } @@ -282,13 +288,14 @@ export class AddonModForumSyncProvider extends CoreSyncBaseProvider { * * @param {number} [cmId] Course module to be synced. If not defined, sync all forums. * @param {number} [discussionId] Discussion id to be synced. If not defined, sync all discussions. + * @param {boolean} [force] Wether to force sync not depending on last execution. * @param {string} [siteId] Site ID. If not defined, current site. * @return {Promise} Promise resolved if sync is successful, rejected otherwise. */ - syncRatings(cmId?: number, discussionId?: number, siteId?: string): Promise { + syncRatings(cmId?: number, discussionId?: number, force?: boolean, siteId?: string): Promise { siteId = siteId || this.sitesProvider.getCurrentSiteId(); - return this.ratingSync.syncRatings('mod_forum', 'post', 'module', cmId, discussionId, siteId).then((results) => { + return this.ratingSync.syncRatings('mod_forum', 'post', 'module', cmId, discussionId, force, siteId).then((results) => { let updated = false; const warnings = []; const promises = []; diff --git a/src/addon/mod/glossary/providers/sync-cron-handler.ts b/src/addon/mod/glossary/providers/sync-cron-handler.ts index 77ea930d8..d952ce356 100644 --- a/src/addon/mod/glossary/providers/sync-cron-handler.ts +++ b/src/addon/mod/glossary/providers/sync-cron-handler.ts @@ -30,10 +30,11 @@ export class AddonModGlossarySyncCronHandler implements CoreCronHandler { * Receives the ID of the site affected, undefined for all sites. * * @param {string} [siteId] ID of the site affected, undefined for all sites. + * @param {boolean} [force] Wether the execution is forced (manual sync). * @return {Promise} Promise resolved when done, rejected if failure. */ - execute(siteId?: string): Promise { - return this.glossarySync.syncAllGlossaries(siteId); + execute(siteId?: string, force?: boolean): Promise { + return this.glossarySync.syncAllGlossaries(siteId, force); } /** diff --git a/src/addon/mod/glossary/providers/sync.ts b/src/addon/mod/glossary/providers/sync.ts index 3a0dbe0e2..c3eefdafd 100644 --- a/src/addon/mod/glossary/providers/sync.ts +++ b/src/addon/mod/glossary/providers/sync.ts @@ -68,19 +68,21 @@ export class AddonModGlossarySyncProvider extends CoreSyncBaseProvider { * Try to synchronize all the glossaries in a certain site or in all sites. * * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {boolean} [force] Wether to force sync not depending on last execution. * @return {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - syncAllGlossaries(siteId?: string): Promise { - return this.syncOnSites('all glossaries', this.syncAllGlossariesFunc.bind(this), [], siteId); + syncAllGlossaries(siteId?: string, force?: boolean): Promise { + return this.syncOnSites('all glossaries', this.syncAllGlossariesFunc.bind(this), [force], siteId); } /** * Sync all glossaries on a site. * - * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {string} siteId Site ID to sync. + * @param {boolean} [force] Wether to force sync not depending on last execution. * @return {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - protected syncAllGlossariesFunc(siteId?: string): Promise { + protected syncAllGlossariesFunc(siteId: string, force?: boolean): Promise { siteId = siteId || this.sitesProvider.getCurrentSiteId(); const promises = []; @@ -97,8 +99,10 @@ export class AddonModGlossarySyncProvider extends CoreSyncBaseProvider { continue; } - promises[entry.glossaryid] = this.syncGlossaryEntriesIfNeeded(entry.glossaryid, entry.userid, siteId) - .then((result) => { + promises[entry.glossaryid] = force ? this.syncGlossaryEntries(entry.glossaryid, entry.userid, siteId) : + this.syncGlossaryEntriesIfNeeded(entry.glossaryid, entry.userid, siteId); + + promises[entry.glossaryid].then((result) => { if (result && result.updated) { // Sync successful, send event. this.eventsProvider.trigger(AddonModGlossarySyncProvider.AUTO_SYNCED, { diff --git a/src/addon/mod/lesson/providers/lesson-sync.ts b/src/addon/mod/lesson/providers/lesson-sync.ts index 907e8f4d3..1a1abdaf6 100644 --- a/src/addon/mod/lesson/providers/lesson-sync.ts +++ b/src/addon/mod/lesson/providers/lesson-sync.ts @@ -186,26 +186,31 @@ export class AddonModLessonSyncProvider extends CoreCourseActivitySyncBaseProvid * Try to synchronize all the lessons in a certain site or in all sites. * * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {boolean} [force] Wether to force sync not depending on last execution. * @return {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - syncAllLessons(siteId?: string): Promise { - return this.syncOnSites('all lessons', this.syncAllLessonsFunc.bind(this), [], siteId); + syncAllLessons(siteId?: string, force?: boolean): Promise { + return this.syncOnSites('all lessons', this.syncAllLessonsFunc.bind(this), [force], siteId); } /** * Sync all lessons on a site. * - * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {string} siteId Site ID to sync. + * @param {boolean} [force] Wether to force sync not depending on last execution. * @param {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - protected syncAllLessonsFunc(siteId?: string): Promise { + protected syncAllLessonsFunc(siteId: string, force?: boolean): Promise { // Get all the lessons that have something to be synchronized. return this.lessonOfflineProvider.getAllLessonsWithData(siteId).then((lessons) => { // Sync all lessons that haven't been synced for a while. const promises = []; - lessons.forEach((lesson) => { - promises.push(this.syncLessonIfNeeded(lesson.id, false, siteId).then((result) => { + lessons.map((lesson) => { + const promise = force ? this.syncLesson(lesson.id, false, false, siteId) : + this.syncLessonIfNeeded(lesson.id, false, siteId); + + return promise.then((result) => { if (result && result.updated) { // Sync successful, send event. this.eventsProvider.trigger(AddonModLessonSyncProvider.AUTO_SYNCED, { @@ -213,7 +218,7 @@ export class AddonModLessonSyncProvider extends CoreCourseActivitySyncBaseProvid warnings: result.warnings }, siteId); } - })); + }); }); return Promise.all(promises); diff --git a/src/addon/mod/lesson/providers/sync-cron-handler.ts b/src/addon/mod/lesson/providers/sync-cron-handler.ts index 0ede470f1..3d204645a 100644 --- a/src/addon/mod/lesson/providers/sync-cron-handler.ts +++ b/src/addon/mod/lesson/providers/sync-cron-handler.ts @@ -30,10 +30,11 @@ export class AddonModLessonSyncCronHandler implements CoreCronHandler { * Receives the ID of the site affected, undefined for all sites. * * @param {string} [siteId] ID of the site affected, undefined for all sites. + * @param {boolean} [force] Wether the execution is forced (manual sync). * @return {Promise} Promise resolved when done, rejected if failure. */ - execute(siteId?: string): Promise { - return this.lessonSync.syncAllLessons(siteId); + execute(siteId?: string, force?: boolean): Promise { + return this.lessonSync.syncAllLessons(siteId, force); } /** diff --git a/src/addon/mod/quiz/providers/quiz-sync.ts b/src/addon/mod/quiz/providers/quiz-sync.ts index 854fb8a83..e14b3f1d6 100644 --- a/src/addon/mod/quiz/providers/quiz-sync.ts +++ b/src/addon/mod/quiz/providers/quiz-sync.ts @@ -190,19 +190,21 @@ export class AddonModQuizSyncProvider extends CoreCourseActivitySyncBaseProvider * Try to synchronize all the quizzes in a certain site or in all sites. * * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {boolean} [force] Wether to force sync not depending on last execution. * @return {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - syncAllQuizzes(siteId?: string): Promise { - return this.syncOnSites('all quizzes', this.syncAllQuizzesFunc.bind(this), [], siteId); + syncAllQuizzes(siteId?: string, force?: boolean): Promise { + return this.syncOnSites('all quizzes', this.syncAllQuizzesFunc.bind(this), [force], siteId); } /** * Sync all quizzes on a site. * - * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {string} siteId Site ID to sync. + * @param {boolean} [force] Wether to force sync not depending on last execution. * @param {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - protected syncAllQuizzesFunc(siteId?: string): Promise { + protected syncAllQuizzesFunc(siteId?: string, force?: boolean): Promise { // Get all offline attempts. return this.quizOfflineProvider.getAllAttempts(siteId).then((attempts) => { const quizzes = [], @@ -227,7 +229,9 @@ export class AddonModQuizSyncProvider extends CoreCourseActivitySyncBaseProvider // Quiz not blocked, try to synchronize it. promises.push(this.quizProvider.getQuizById(quiz.courseid, quiz.id, false, false, siteId).then((quiz) => { - return this.syncQuizIfNeeded(quiz, false, siteId).then((data) => { + const promise = force ? this.syncQuiz(quiz, false, siteId) : this.syncQuizIfNeeded(quiz, false, siteId); + + return promise.then((data) => { if (data && data.warnings && data.warnings.length) { // Store the warnings to show them when the user opens the quiz. return this.setSyncWarnings(quiz.id, data.warnings, siteId).then(() => { diff --git a/src/addon/mod/quiz/providers/sync-cron-handler.ts b/src/addon/mod/quiz/providers/sync-cron-handler.ts index 2866f3a08..2c3c1bcbe 100644 --- a/src/addon/mod/quiz/providers/sync-cron-handler.ts +++ b/src/addon/mod/quiz/providers/sync-cron-handler.ts @@ -30,10 +30,11 @@ export class AddonModQuizSyncCronHandler implements CoreCronHandler { * Receives the ID of the site affected, undefined for all sites. * * @param {string} [siteId] ID of the site affected, undefined for all sites. + * @param {boolean} [force] Wether the execution is forced (manual sync). * @return {Promise} Promise resolved when done, rejected if failure. */ - execute(siteId?: string): Promise { - return this.quizSync.syncAllQuizzes(siteId); + execute(siteId?: string, force?: boolean): Promise { + return this.quizSync.syncAllQuizzes(siteId, force); } /** diff --git a/src/addon/mod/scorm/providers/scorm-sync.ts b/src/addon/mod/scorm/providers/scorm-sync.ts index 683cfd450..163c4cd7a 100644 --- a/src/addon/mod/scorm/providers/scorm-sync.ts +++ b/src/addon/mod/scorm/providers/scorm-sync.ts @@ -443,20 +443,22 @@ export class AddonModScormSyncProvider extends CoreCourseActivitySyncBaseProvide /** * Try to synchronize all the SCORMs in a certain site or in all sites. * + * @param {boolean} force Wether to force sync not depending on last execution. * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. * @return {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - syncAllScorms(siteId?: string): Promise { - return this.syncOnSites('all SCORMs', this.syncAllScormsFunc.bind(this), [], siteId); + syncAllScorms(siteId?: string, force?: boolean): Promise { + return this.syncOnSites('all SCORMs', this.syncAllScormsFunc.bind(this), [force], siteId); } /** * Sync all SCORMs on a site. * - * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {string} siteId Site ID to sync. + * @param {boolean} [force] Wether to force sync not depending on last execution. * @param {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - protected syncAllScormsFunc(siteId?: string): Promise { + protected syncAllScormsFunc(siteId: string, force?: boolean): Promise { // Get all offline attempts. return this.scormOfflineProvider.getAllAttempts(siteId).then((attempts) => { @@ -481,7 +483,9 @@ export class AddonModScormSyncProvider extends CoreCourseActivitySyncBaseProvide if (!this.syncProvider.isBlocked(AddonModScormProvider.COMPONENT, scorm.id, siteId)) { promises.push(this.scormProvider.getScormById(scorm.courseId, scorm.id, '', false, siteId).then((scorm) => { - return this.syncScormIfNeeded(scorm, siteId).then((data) => { + const promise = force ? this.syncScorm(scorm, siteId) : this.syncScormIfNeeded(scorm, siteId); + + return promise.then((data) => { if (typeof data != 'undefined') { // We tried to sync. Send event. this.eventsProvider.trigger(AddonModScormSyncProvider.AUTO_SYNCED, { diff --git a/src/addon/mod/scorm/providers/sync-cron-handler.ts b/src/addon/mod/scorm/providers/sync-cron-handler.ts index 797eeb4bb..33999d0cb 100644 --- a/src/addon/mod/scorm/providers/sync-cron-handler.ts +++ b/src/addon/mod/scorm/providers/sync-cron-handler.ts @@ -30,10 +30,11 @@ export class AddonModScormSyncCronHandler implements CoreCronHandler { * Receives the ID of the site affected, undefined for all sites. * * @param {string} [siteId] ID of the site affected, undefined for all sites. + * @param {boolean} [force] Wether the execution is forced (manual sync). * @return {Promise} Promise resolved when done, rejected if failure. */ - execute(siteId?: string): Promise { - return this.scormSync.syncAllScorms(siteId); + execute(siteId?: string, force?: boolean): Promise { + return this.scormSync.syncAllScorms(siteId, force); } /** diff --git a/src/addon/mod/survey/providers/sync-cron-handler.ts b/src/addon/mod/survey/providers/sync-cron-handler.ts index 1949d9e14..f3e2e9552 100644 --- a/src/addon/mod/survey/providers/sync-cron-handler.ts +++ b/src/addon/mod/survey/providers/sync-cron-handler.ts @@ -30,10 +30,11 @@ export class AddonModSurveySyncCronHandler implements CoreCronHandler { * Receives the ID of the site affected, undefined for all sites. * * @param {string} [siteId] ID of the site affected, undefined for all sites. + * @param {boolean} [force] Wether the execution is forced (manual sync). * @return {Promise} Promise resolved when done, rejected if failure. */ - execute(siteId?: string): Promise { - return this.surveySync.syncAllSurveys(siteId); + execute(siteId?: string, force?: boolean): Promise { + return this.surveySync.syncAllSurveys(siteId, force); } /** diff --git a/src/addon/mod/survey/providers/sync.ts b/src/addon/mod/survey/providers/sync.ts index d38b0a898..f9b070175 100644 --- a/src/addon/mod/survey/providers/sync.ts +++ b/src/addon/mod/survey/providers/sync.ts @@ -68,23 +68,29 @@ export class AddonModSurveySyncProvider extends CoreCourseActivitySyncBaseProvid * Try to synchronize all the surveys in a certain site or in all sites. * * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {boolean} [force] Wether to force sync not depending on last execution. * @return {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - syncAllSurveys(siteId?: string): Promise { - return this.syncOnSites('all surveys', this.syncAllSurveysFunc.bind(this), undefined, siteId); + syncAllSurveys(siteId?: string, force?: boolean): Promise { + return this.syncOnSites('all surveys', this.syncAllSurveysFunc.bind(this), [force], siteId); } /** * Sync all pending surveys on a site. - * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * + * @param {string} siteId Site ID to sync. + * @param {boolean} [force] Wether to force sync not depending on last execution. * @param {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - protected syncAllSurveysFunc(siteId?: string): Promise { + protected syncAllSurveysFunc(siteId: string, force?: boolean): Promise { // Get all survey answers pending to be sent in the site. return this.surveyOffline.getAllData(siteId).then((entries) => { // Sync all surveys. const promises = entries.map((entry) => { - return this.syncSurveyIfNeeded(entry.surveyid, entry.userid, siteId).then((result) => { + const promise = force ? this.syncSurvey(entry.surveyid, entry.userid, siteId) : + this.syncSurveyIfNeeded(entry.surveyid, entry.userid, siteId); + + return promise.then((result) => { if (result && result.answersSent) { // Sync successful, send event. this.eventsProvider.trigger(AddonModSurveySyncProvider.AUTO_SYNCED, { diff --git a/src/addon/mod/wiki/providers/sync-cron-handler.ts b/src/addon/mod/wiki/providers/sync-cron-handler.ts index ce1fa6980..c74a439fe 100644 --- a/src/addon/mod/wiki/providers/sync-cron-handler.ts +++ b/src/addon/mod/wiki/providers/sync-cron-handler.ts @@ -30,10 +30,11 @@ export class AddonModWikiSyncCronHandler implements CoreCronHandler { * Receives the ID of the site affected, undefined for all sites. * * @param {string} [siteId] ID of the site affected, undefined for all sites. + * @param {boolean} [force] Wether the execution is forced (manual sync). * @return {Promise} Promise resolved when done, rejected if failure. */ - execute(siteId?: string): Promise { - return this.wikiSync.syncAllWikis(siteId); + execute(siteId?: string, force?: boolean): Promise { + return this.wikiSync.syncAllWikis(siteId, force); } /** diff --git a/src/addon/mod/wiki/providers/wiki-sync.ts b/src/addon/mod/wiki/providers/wiki-sync.ts index cfd37e1a0..9fa0200c9 100644 --- a/src/addon/mod/wiki/providers/wiki-sync.ts +++ b/src/addon/mod/wiki/providers/wiki-sync.ts @@ -143,19 +143,21 @@ export class AddonModWikiSyncProvider extends CoreSyncBaseProvider { * Try to synchronize all the wikis in a certain site or in all sites. * * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {boolean} [force] Wether to force sync not depending on last execution. * @return {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - syncAllWikis(siteId?: string): Promise { - return this.syncOnSites('all wikis', this.syncAllWikisFunc.bind(this), [], siteId); + syncAllWikis(siteId?: string, force?: boolean): Promise { + return this.syncOnSites('all wikis', this.syncAllWikisFunc.bind(this), [force], siteId); } /** * Sync all wikis on a site. * - * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {string} siteId Site ID to sync. + * @param {boolean} [force] Wether to force sync not depending on last execution. * @param {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - protected syncAllWikisFunc(siteId?: string): Promise { + protected syncAllWikisFunc(siteId: string, force?: boolean): Promise { // Get all the pages created in offline. return this.wikiOfflineProvider.getAllNewPages(siteId).then((pages) => { const promises = [], @@ -171,8 +173,10 @@ export class AddonModWikiSyncProvider extends CoreSyncBaseProvider { for (const id in subwikis) { const subwiki = subwikis[id]; - promises.push(this.syncSubwikiIfNeeded(subwiki.subwikiid, subwiki.wikiid, subwiki.userid, subwiki.groupid, - siteId).then((result) => { + const promise = force ? this.syncSubwiki(subwiki.subwikiid, subwiki.wikiid, subwiki.userid, subwiki.groupid, siteId) + : this.syncSubwikiIfNeeded(subwiki.subwikiid, subwiki.wikiid, subwiki.userid, subwiki.groupid, siteId); + + promises.push(promise.then((result) => { if (result && result.updated) { // Sync successful, send event. diff --git a/src/addon/mod/workshop/providers/sync-cron-handler.ts b/src/addon/mod/workshop/providers/sync-cron-handler.ts index 05f6a011f..3202ff253 100644 --- a/src/addon/mod/workshop/providers/sync-cron-handler.ts +++ b/src/addon/mod/workshop/providers/sync-cron-handler.ts @@ -30,10 +30,11 @@ export class AddonModWorkshopSyncCronHandler implements CoreCronHandler { * Receives the ID of the site affected, undefined for all sites. * * @param {string} [siteId] ID of the site affected, undefined for all sites. + * @param {boolean} [force] Wether the execution is forced (manual sync). * @return {Promise} Promise resolved when done, rejected if failure. */ - execute(siteId?: string): Promise { - return this.workshopSync.syncAllWorkshops(siteId); + execute(siteId?: string, force?: boolean): Promise { + return this.workshopSync.syncAllWorkshops(siteId, force); } /** diff --git a/src/addon/mod/workshop/providers/sync.ts b/src/addon/mod/workshop/providers/sync.ts index 74f7619cf..e30f6c387 100644 --- a/src/addon/mod/workshop/providers/sync.ts +++ b/src/addon/mod/workshop/providers/sync.ts @@ -76,25 +76,27 @@ export class AddonModWorkshopSyncProvider extends CoreSyncBaseProvider { * Try to synchronize all workshops that need it and haven't been synchronized in a while. * * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {boolean} [force] Wether to force sync not depending on last execution. * @return {Promise} Promise resolved when the sync is done. */ - syncAllWorkshops(siteId?: string): Promise { - return this.syncOnSites('all workshops', this.syncAllWorkshopsFunc.bind(this), [], siteId); + syncAllWorkshops(siteId?: string, force?: boolean): Promise { + return this.syncOnSites('all workshops', this.syncAllWorkshopsFunc.bind(this), [force], siteId); } /** * Sync all workshops on a site. * - * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {string} siteId Site ID to sync. + * @param {boolean} [force] Wether to force sync not depending on last execution. * @return {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - protected syncAllWorkshopsFunc(siteId?: string): Promise { + protected syncAllWorkshopsFunc(siteId: string, force?: boolean): Promise { return this.workshopOffline.getAllWorkshops(siteId).then((workshopIds) => { - const promises = []; - // Sync all workshops that haven't been synced for a while. - workshopIds.forEach((workshopId) => { - promises.push(this.syncWorkshopIfNeeded(workshopId, siteId).then((data) => { + const promises = workshopIds.map((workshopId) => { + const promise = force ? this.syncWorkshop(workshopId, siteId) : this.syncWorkshopIfNeeded(workshopId, siteId); + + return promise.then((data) => { if (data && data.updated) { // Sync done. Send event. this.eventsProvider.trigger(AddonModWorkshopSyncProvider.AUTO_SYNCED, { @@ -102,7 +104,7 @@ export class AddonModWorkshopSyncProvider extends CoreSyncBaseProvider { warnings: data.warnings }, siteId); } - })); + }); }); return Promise.all(promises); diff --git a/src/addon/notes/providers/notes-sync.ts b/src/addon/notes/providers/notes-sync.ts index 376e5b673..77b7039c3 100644 --- a/src/addon/notes/providers/notes-sync.ts +++ b/src/addon/notes/providers/notes-sync.ts @@ -48,19 +48,21 @@ export class AddonNotesSyncProvider extends CoreSyncBaseProvider { * Try to synchronize all the notes in a certain site or in all sites. * * @param {string} [siteId] Site ID to sync. If not defined, sync all sites. + * @param {boolean} [force] Wether to force sync not depending on last execution. * @return {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - syncAllNotes(siteId?: string): Promise { - return this.syncOnSites('all notes', this.syncAllNotesFunc.bind(this), [], siteId); + syncAllNotes(siteId?: string, force?: boolean): Promise { + return this.syncOnSites('all notes', this.syncAllNotesFunc.bind(this), [force], siteId); } /** * Synchronize all the notes in a certain site * * @param {string} siteId Site ID to sync. + * @param {boolean} force Wether to force sync not depending on last execution. * @return {Promise} Promise resolved if sync is successful, rejected if sync fails. */ - private syncAllNotesFunc(siteId: string): Promise { + private syncAllNotesFunc(siteId: string, force: boolean): Promise { return this.notesOffline.getAllNotes(siteId).then((notes) => { // Get all the courses to be synced. const courseIds = []; @@ -72,7 +74,9 @@ export class AddonNotesSyncProvider extends CoreSyncBaseProvider { // Sync all courses. const promises = courseIds.map((courseId) => { - return this.syncNotesIfNeeded(courseId, siteId).then((warnings) => { + const promise = force ? this.syncNotes(courseId, siteId) : this.syncNotesIfNeeded(courseId, siteId); + + return promise.then((warnings) => { if (typeof warnings != 'undefined') { // Sync successful, send event. this.eventsProvider.trigger(AddonNotesSyncProvider.AUTO_SYNCED, { diff --git a/src/addon/notes/providers/sync-cron-handler.ts b/src/addon/notes/providers/sync-cron-handler.ts index 25d5d2c24..dcd1ad5d6 100644 --- a/src/addon/notes/providers/sync-cron-handler.ts +++ b/src/addon/notes/providers/sync-cron-handler.ts @@ -30,10 +30,11 @@ export class AddonNotesSyncCronHandler implements CoreCronHandler { * Receives the ID of the site affected, undefined for all sites. * * @param {string} [siteId] ID of the site affected, undefined for all sites. - * @return {Promise} Promise resolved when done, rejected if failure. + * @param {boolean} [force] Wether the execution is forced (manual sync). + * @return {Promise} Promise resolved when done, rejected if failure. */ - execute(siteId?: string): Promise { - return this.notesSync.syncAllNotes(siteId); + execute(siteId?: string, force?: boolean): Promise { + return this.notesSync.syncAllNotes(siteId, force); } /** diff --git a/src/addon/notifications/providers/cron-handler.ts b/src/addon/notifications/providers/cron-handler.ts index 78f317c41..e58c11898 100644 --- a/src/addon/notifications/providers/cron-handler.ts +++ b/src/addon/notifications/providers/cron-handler.ts @@ -65,12 +65,14 @@ export class AddonNotificationsCronHandler implements CoreCronHandler { /** * Execute the process. + * Receives the ID of the site affected, undefined for all sites. * - * @param {string} [siteId] ID of the site affected. If not defined, all sites. - * @return {Promise} Promise resolved when done. If the promise is rejected, this function will be called again often, - * it shouldn't be abused. + * @param {string} [siteId] ID of the site affected, undefined for all sites. + * @param {boolean} [force] Wether the execution is forced (manual sync). + * @return {Promise} Promise resolved when done, rejected if failure. If the promise is rejected, this function + * will be called again often, it shouldn't be abused. */ - execute(siteId?: string): Promise { + execute(siteId?: string, force?: boolean): Promise { if (this.sitesProvider.isCurrentSite(siteId)) { this.eventsProvider.trigger(AddonNotificationsProvider.READ_CRON_EVENT, {}, this.sitesProvider.getCurrentSiteId()); } diff --git a/src/core/course/providers/log-cron-handler.ts b/src/core/course/providers/log-cron-handler.ts index c04dea1b7..063a37ea6 100644 --- a/src/core/course/providers/log-cron-handler.ts +++ b/src/core/course/providers/log-cron-handler.ts @@ -28,12 +28,13 @@ export class CoreCourseLogCronHandler implements CoreCronHandler { /** * Execute the process. - * Receives the ID of the site affected, undefined for the current site. + * Receives the ID of the site affected, undefined for all sites. * - * @param {string} [siteId] ID of the site affected, undefined for the current site. + * @param {string} [siteId] ID of the site affected, undefined for all sites. + * @param {boolean} [force] Wether the execution is forced (manual sync). * @return {Promise} Promise resolved when done, rejected if failure. */ - execute(siteId?: string): Promise { + execute(siteId?: string, force?: boolean): Promise { return this.sitesProvider.getSite(siteId).then((site) => { return this.courseProvider.logView(site.getSiteHomeId(), undefined, site.getId()); }); diff --git a/src/core/course/providers/sync-cron-handler.ts b/src/core/course/providers/sync-cron-handler.ts index 4e85dd398..1ea0b3784 100644 --- a/src/core/course/providers/sync-cron-handler.ts +++ b/src/core/course/providers/sync-cron-handler.ts @@ -30,10 +30,11 @@ export class CoreCourseSyncCronHandler implements CoreCronHandler { * Execute the process. * Receives the ID of the site affected, undefined for all sites. * - * @param {string} [siteId] ID of the site affected, undefined for all sites. - * @return {Promise} Promise resolved when done, rejected if failure. + * @param {string} [siteId] ID of the site affected, undefined for all sites. + * @param {boolean} [force] Wether the execution is forced (manual sync). + * @return {Promise} Promise resolved when done, rejected if failure. */ - execute(siteId?: string): Promise { + execute(siteId?: string, force?: boolean): Promise { const promises = []; // Sync activity logs even if the activity does not have sync handler. // This will sync all the activity logs even if there's nothing else to sync and also recources. diff --git a/src/core/course/providers/sync.ts b/src/core/course/providers/sync.ts index 058fba729..14c66dd08 100644 --- a/src/core/course/providers/sync.ts +++ b/src/core/course/providers/sync.ts @@ -62,11 +62,9 @@ export class CoreCourseSyncProvider extends CoreSyncBaseProvider { */ protected syncAllCoursesFunc(siteId?: string): Promise { return this.courseOffline.getAllManualCompletions(siteId).then((completions) => { - const promises = []; - // Sync all courses. - completions.forEach((completion) => { - promises.push(this.syncCourseIfNeeded(completion.courseid, siteId).then((result) => { + const promises = completions.map((completion) => { + return this.syncCourseIfNeeded(completion.courseid, siteId).then((result) => { if (result && result.updated) { // Sync successful, send event. this.eventsProvider.trigger(CoreCourseSyncProvider.AUTO_SYNCED, { @@ -74,8 +72,10 @@ export class CoreCourseSyncProvider extends CoreSyncBaseProvider { warnings: result.warnings }, siteId); } - })); + }); }); + + return Promise.all(promises); }); } diff --git a/src/core/rating/providers/sync.ts b/src/core/rating/providers/sync.ts index 0f34ce359..341797fe2 100644 --- a/src/core/rating/providers/sync.ts +++ b/src/core/rating/providers/sync.ts @@ -59,19 +59,23 @@ export class CoreRatingSyncProvider extends CoreSyncBaseProvider { * @param {string} [contextLevel] Context level: course, module, user, etc. * @param {numnber} [instanceId] Context instance id. * @param {number} [itemSetId] Item set id. + * @param {boolean} [force] Wether to force sync not depending on last execution. * @param {string} [siteId] Site ID. If not defined, current site. * @return {Promise} Promise resolved if sync is successful, rejected if sync fails. */ syncRatings(component: string, ratingArea: string, contextLevel?: string, instanceId?: number, itemSetId?: number, - siteId?: string): Promise<{itemSet: CoreRatingItemSet, updated: number[], warnings: string[]}[]> { + force?: boolean, siteId?: string): Promise<{itemSet: CoreRatingItemSet, updated: number[], warnings: string[]}[]> { siteId = siteId || this.sitesProvider.getCurrentSiteId(); return this.ratingOffline.getItemSets(component, ratingArea, contextLevel, instanceId, itemSetId, siteId) .then((itemSets) => { const results = []; const promises = itemSets.map((itemSet) => { - return this.syncItemSetIfNeeded(component, ratingArea, itemSet.contextLevel, itemSet.instanceId, - itemSet.itemSetId, siteId).then((result) => { + const promise = force ? this.syncItemSet(component, ratingArea, itemSet.contextLevel, itemSet.instanceId, + itemSet.itemSetId, siteId) : this.syncItemSetIfNeeded(component, ratingArea, itemSet.contextLevel, + itemSet.instanceId, itemSet.itemSetId, siteId); + + return promise.then((result) => { if (result.updated) { // Sync successful, send event. this.eventsProvider.trigger(CoreRatingSyncProvider.SYNCED_EVENT, { diff --git a/src/providers/cron.ts b/src/providers/cron.ts index 99d12b95d..b29bc654f 100644 --- a/src/providers/cron.ts +++ b/src/providers/cron.ts @@ -63,10 +63,11 @@ export interface CoreCronHandler { * Execute the process. * * @param {string} [siteId] ID of the site affected. If not defined, all sites. + * @param {boolean} [force] Determines if it's a forced execution. * @return {Promise} Promise resolved when done. If the promise is rejected, this function will be called again often, * it shouldn't be abused. */ - execute?(siteId?: string): Promise; + execute?(siteId?: string, force?: boolean): Promise; /** * Whether the handler is running. Used internally by the provider, there's no need to set it. @@ -181,7 +182,7 @@ export class CoreCronDelegate { this.queuePromise = this.queuePromise.catch(() => { // Ignore errors in previous handlers. }).then(() => { - return this.executeHandler(name, siteId).then(() => { + return this.executeHandler(name, force, siteId).then(() => { this.logger.debug(`Execution of handler '${name}' was a success.`); return this.setHandlerLastExecutionTime(name, Date.now()).then(() => { @@ -204,16 +205,18 @@ export class CoreCronDelegate { * Run a handler, cancelling the execution if it takes more than MAX_TIME_PROCESS. * * @param {string} name Name of the handler. + * @param {boolean} [force] Wether the execution is forced (manual sync). * @param {string} [siteId] Site ID. If not defined, all sites. * @return {Promise} Promise resolved when the handler finishes or reaches max time, rejected if it fails. */ - protected executeHandler(name: string, siteId?: string): Promise { + protected executeHandler(name: string, force?: boolean, siteId?: string): Promise { return new Promise((resolve, reject): void => { let cancelTimeout; this.logger.debug('Executing handler: ' + name); + // Wrap the call in Promise.resolve to make sure it's a promise. - Promise.resolve(this.handlers[name].execute(siteId)).then(resolve).catch(reject).finally(() => { + Promise.resolve(this.handlers[name].execute(siteId, force)).then(resolve).catch(reject).finally(() => { clearTimeout(cancelTimeout); });