diff --git a/src/addons/calendar/services/calendar-sync.ts b/src/addons/calendar/services/calendar-sync.ts index e0e44e6f6..c167865d3 100644 --- a/src/addons/calendar/services/calendar-sync.ts +++ b/src/addons/calendar/services/calendar-sync.ts @@ -28,7 +28,6 @@ import { AddonCalendarOffline } from './calendar-offline'; import { AddonCalendarHelper } from './calendar-helper'; import { makeSingleton, Translate } from '@singletons'; import { CoreSync } from '@services/sync'; -import { CoreTextUtils } from '@services/utils/text'; import { CoreNetworkError } from '@classes/errors/network-error'; /** @@ -41,6 +40,8 @@ export class AddonCalendarSyncProvider extends CoreSyncBaseProvider { siteId = siteId || CoreSites.getCurrentSiteId(); - this.componentTranslate = this.componentTranslate || CoreCourse.translateModuleName('assign'); if (this.isSyncing(assignId, siteId)) { // There's already a sync ongoing for this assign, return the promise. @@ -328,7 +325,6 @@ export class AddonModAssignSyncProvider extends CoreCourseActivitySyncBaseProvid // The submission was modified in Moodle, discard the submission. this.addOfflineDataDeletedWarning( warnings, - this.componentTranslate, assign.name, Translate.instant('addon.mod_assign.warningsubmissionmodified'), ); @@ -369,12 +365,7 @@ export class AddonModAssignSyncProvider extends CoreCourseActivitySyncBaseProvid } // A WebService has thrown an error, this means it cannot be submitted. Discard the submission. - this.addOfflineDataDeletedWarning( - warnings, - this.componentTranslate, - assign.name, - CoreTextUtils.getErrorMessageFromError(error) || '', - ); + this.addOfflineDataDeletedWarning(warnings, assign.name, error); } // Delete the offline data. @@ -458,7 +449,6 @@ export class AddonModAssignSyncProvider extends CoreCourseActivitySyncBaseProvid // The submission grade was modified in Moodle, discard it. this.addOfflineDataDeletedWarning( warnings, - this.componentTranslate, assign.name, Translate.instant('addon.mod_assign.warningsubmissiongrademodified'), ); @@ -527,12 +517,7 @@ export class AddonModAssignSyncProvider extends CoreCourseActivitySyncBaseProvid } // A WebService has thrown an error, this means it cannot be submitted. Discard the submission. - this.addOfflineDataDeletedWarning( - warnings, - this.componentTranslate, - assign.name, - CoreTextUtils.getErrorMessageFromError(error) || '', - ); + this.addOfflineDataDeletedWarning(warnings, assign.name, error); } // Delete the offline data. diff --git a/src/addons/mod/forum/services/forum-sync.ts b/src/addons/mod/forum/services/forum-sync.ts index 4d5eb3c08..479fe33a7 100644 --- a/src/addons/mod/forum/services/forum-sync.ts +++ b/src/addons/mod/forum/services/forum-sync.ts @@ -14,8 +14,7 @@ import { ContextLevel } from '@/core/constants'; import { Injectable } from '@angular/core'; -import { CoreSyncBaseProvider } from '@classes/base-sync'; -import { CoreCourse } from '@features/course/services/course'; +import { CoreCourseActivitySyncBaseProvider } from '@features/course/classes/activity-sync'; import { CoreCourseLogHelper } from '@features/course/services/log-helper'; import { CoreFileUploader } from '@features/fileuploader/services/fileuploader'; import { CoreRatingSync } from '@features/rating/services/rating-sync'; @@ -23,7 +22,6 @@ import { CoreApp } from '@services/app'; import { CoreGroups } from '@services/groups'; import { CoreSites } from '@services/sites'; import { CoreSync } from '@services/sync'; -import { CoreTextUtils } from '@services/utils/text'; import { CoreUtils } from '@services/utils/utils'; import { makeSingleton, Translate } from '@singletons'; import { CoreArray } from '@singletons/array'; @@ -55,25 +53,17 @@ declare module '@singletons/events' { * Service to sync forums. */ @Injectable({ providedIn: 'root' }) -export class AddonModForumSyncProvider extends CoreSyncBaseProvider { +export class AddonModForumSyncProvider extends CoreCourseActivitySyncBaseProvider { static readonly AUTO_SYNCED = 'addon_mod_forum_autom_synced'; static readonly MANUAL_SYNCED = 'addon_mod_forum_manual_synced'; - private _componentTranslate?: string; + protected componentTranslatableString = 'forum'; constructor() { super('AddonModForumSyncProvider'); } - protected get componentTranslate(): string { - if (!this._componentTranslate) { - this._componentTranslate = CoreCourse.translateModuleName('forum'); - } - - return this._componentTranslate; - } - /** * Try to synchronize all the forums in a certain site or in all sites. * @@ -291,11 +281,7 @@ export class AddonModForumSyncProvider extends CoreSyncBaseProvider { result.warnings.forEach((warning) => { - warnings.push(Translate.instant('core.warningofflinedatadeleted', { - component: this.componentTranslate, - name: forum.name, - error: warning, - })); + this.addOfflineDataDeletedWarning(warnings, forum.name, warning); }); return; @@ -512,11 +494,8 @@ export class AddonModForumSyncProvider extends CoreSyncBaseProvider { siteId = siteId || CoreSites.getCurrentSiteId(); - this.componentTranslate = this.componentTranslate || CoreCourse.translateModuleName('lesson'); let syncPromise = this.getOngoingSync(lessonId, siteId); if (syncPromise) { @@ -317,11 +315,12 @@ export class AddonModLessonSyncProvider extends CoreCourseActivitySyncBaseProvid if (attempts.length != attemptsLength) { // Some attempts won't be sent, add a warning. - result.warnings.push(Translate.instant('core.warningofflinedatadeleted', { - component: this.componentTranslate, - name: lesson.name, - error: Translate.instant('addon.mod_lesson.warningretakefinished'), - })); + this.addOfflineDataDeletedWarning( + result.warnings, + lesson.name, + Translate.instant('addon.mod_lesson.warningretakefinished'), + ); + } await Promise.all(promises); @@ -386,11 +385,7 @@ export class AddonModLessonSyncProvider extends CoreCourseActivitySyncBaseProvid await AddonModLessonOffline.deleteAttempt(lesson.id, retake, pageId, timemodified, siteId); // Attempt deleted, add a warning. - result.warnings.push(Translate.instant('core.warningofflinedatadeleted', { - component: this.componentTranslate, - name: lesson.name, - error: CoreTextUtils.getErrorMessageFromError(error), - })); + this.addOfflineDataDeletedWarning(result.warnings, lesson.name, error); } } @@ -447,11 +442,11 @@ export class AddonModLessonSyncProvider extends CoreCourseActivitySyncBaseProvid if (retake.retake != passwordData.accessInfo.attemptscount) { // The retake changed, add a warning if it isn't there already. if (!result.warnings.length) { - result.warnings.push(Translate.instant('core.warningofflinedatadeleted', { - component: this.componentTranslate, - name: passwordData.lesson.name, - error: Translate.instant('addon.mod_lesson.warningretakefinished'), - })); + this.addOfflineDataDeletedWarning( + result.warnings, + passwordData.lesson.name, + Translate.instant('addon.mod_lesson.warningretakefinished'), + ); } await AddonModLessonOffline.deleteRetake(lessonId, siteId); @@ -488,11 +483,7 @@ export class AddonModLessonSyncProvider extends CoreCourseActivitySyncBaseProvid await AddonModLessonOffline.deleteRetake(lessonId, siteId); // Retake deleted, add a warning. - result.warnings.push(Translate.instant('core.warningofflinedatadeleted', { - component: this.componentTranslate, - name: passwordData.lesson.name, - error: CoreTextUtils.getErrorMessageFromError(error), - })); + this.addOfflineDataDeletedWarning(result.warnings, passwordData.lesson.name, error); } } diff --git a/src/addons/mod/quiz/services/quiz-sync.ts b/src/addons/mod/quiz/services/quiz-sync.ts index a7f1bb273..77ce8beff 100644 --- a/src/addons/mod/quiz/services/quiz-sync.ts +++ b/src/addons/mod/quiz/services/quiz-sync.ts @@ -40,7 +40,7 @@ export class AddonModQuizSyncProvider extends CoreCourseActivitySyncBaseProvider static readonly AUTO_SYNCED = 'addon_mod_quiz_autom_synced'; - protected componentTranslate?: string; + protected componentTranslatableString = 'quiz'; constructor() { super('AddonModQuizSyncProvider'); @@ -271,7 +271,6 @@ export class AddonModQuizSyncProvider extends CoreCourseActivitySyncBaseProvider // Verify that quiz isn't blocked. if (CoreSync.isBlocked(AddonModQuizProvider.COMPONENT, quiz.id, siteId)) { this.logger.debug('Cannot sync quiz ' + quiz.id + ' because it is blocked.'); - this.componentTranslate = this.componentTranslate || CoreCourse.translateModuleName('quiz'); throw new CoreError(Translate.instant('core.errorsyncblocked', { $a: this.componentTranslate })); } diff --git a/src/addons/mod/survey/services/survey-sync.ts b/src/addons/mod/survey/services/survey-sync.ts index c2c10cb39..f9c70bf91 100644 --- a/src/addons/mod/survey/services/survey-sync.ts +++ b/src/addons/mod/survey/services/survey-sync.ts @@ -19,9 +19,8 @@ import { CoreCourse } from '@features/course/services/course'; import { CoreCourseLogHelper } from '@features/course/services/log-helper'; import { CoreApp } from '@services/app'; import { CoreSites } from '@services/sites'; -import { CoreTextUtils } from '@services/utils/text'; import { CoreUtils } from '@services/utils/utils'; -import { makeSingleton, Translate } from '@singletons'; +import { makeSingleton } from '@singletons'; import { CoreEvents } from '@singletons/events'; import { AddonModSurveyPrefetchHandler } from './handlers/prefetch'; import { AddonModSurvey, AddonModSurveyProvider } from './survey'; @@ -35,11 +34,10 @@ export class AddonModSurveySyncProvider extends CoreCourseActivitySyncBaseProvid static readonly AUTO_SYNCED = 'addon_mod_survey_autom_synced'; - protected componentTranslate: string; + protected componentTranslatableString = 'survey'; constructor() { super('AddonModSurveySyncProvider'); - this.componentTranslate = CoreCourse.translateModuleName('survey'); } /** @@ -196,11 +194,7 @@ export class AddonModSurveySyncProvider extends CoreCourseActivitySyncBaseProvid await AddonModSurveyOffline.deleteSurveyAnswers(surveyId, siteId, userId); // Answers deleted, add a warning. - result.warnings.push(Translate.instant('core.warningofflinedatadeleted', { - component: this.componentTranslate, - name: data.name, - error: CoreTextUtils.getErrorMessageFromError(error), - })); + this.addOfflineDataDeletedWarning(result.warnings, data.name, error); } if (result.courseId) { diff --git a/src/core/classes/base-sync.ts b/src/core/classes/base-sync.ts index 7146f1504..c34516719 100644 --- a/src/core/classes/base-sync.ts +++ b/src/core/classes/base-sync.ts @@ -19,7 +19,7 @@ import { CoreTextUtils } from '@services/utils/text'; import { CoreTimeUtils } from '@services/utils/time'; import { Translate } from '@singletons'; import { CoreLogger } from '@singletons/logger'; -import { CoreError } from '@classes/errors/error'; +import { CoreAnyError, CoreError } from '@classes/errors/error'; /** * Blocked sync error. @@ -41,6 +41,16 @@ export class CoreSyncBaseProvider { */ component = 'core'; + /** + * Translatable component name string. + */ + protected componentTranslatableString = 'generic component'; + + /** + * Translated name of the component. + */ + protected componentTranslateInternal?: string; + /** * Sync provider's interval. */ @@ -58,15 +68,14 @@ export class CoreSyncBaseProvider { * Add an offline data deleted warning to a list of warnings. * * @param warnings List of warnings. - * @param component Component. * @param name Instance name. * @param error Specific error message. */ - protected addOfflineDataDeletedWarning(warnings: string[], component: string, name: string, error: string): void { + protected addOfflineDataDeletedWarning(warnings: string[], name: string, error: CoreAnyError): void { const warning = Translate.instant('core.warningofflinedatadeleted', { - component: component, + component: this.componentTranslate, name: name, - error: error, + error: CoreTextUtils.getErrorMessageFromError(error), }); if (warnings.indexOf(warning) == -1) { @@ -304,4 +313,17 @@ export class CoreSyncBaseProvider { } } + /** + * Get component name translated. + * + * @return Component name translated. + */ + protected get componentTranslate(): string { + if (!this.componentTranslateInternal) { + this.componentTranslateInternal = Translate.instant(this.componentTranslatableString); + } + + return this.componentTranslateInternal!; + } + } diff --git a/src/core/classes/errors/error.ts b/src/core/classes/errors/error.ts index 2bbb0b737..e046ea3d1 100644 --- a/src/core/classes/errors/error.ts +++ b/src/core/classes/errors/error.ts @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +import { CoreTextErrorObject } from '@services/utils/text'; + /** * Base Error class. * @@ -31,3 +33,5 @@ export class CoreError extends Error { } } + +export type CoreAnyError = string | CoreError | CoreTextErrorObject | null | undefined; diff --git a/src/core/features/course/classes/activity-sync.ts b/src/core/features/course/classes/activity-sync.ts index 0bfaf2254..8cebdddd6 100644 --- a/src/core/features/course/classes/activity-sync.ts +++ b/src/core/features/course/classes/activity-sync.ts @@ -13,7 +13,7 @@ // limitations under the License. import { CoreSyncBaseProvider } from '@classes/base-sync'; -import { CoreCourseAnyModuleData } from '../services/course'; +import { CoreCourse, CoreCourseAnyModuleData } from '../services/course'; import { CoreCourseModulePrefetchDelegate } from '../services/module-prefetch-delegate'; import { CoreCourseModulePrefetchHandlerBase } from './module-prefetch-handler'; @@ -22,6 +22,8 @@ import { CoreCourseModulePrefetchHandlerBase } from './module-prefetch-handler'; */ export class CoreCourseActivitySyncBaseProvider extends CoreSyncBaseProvider { + protected componentTranslatableString = 'activity'; + /** * Conveniece function to prefetch data after an update. * @@ -54,4 +56,15 @@ export class CoreCourseActivitySyncBaseProvider extends CoreSyncBasePr } } + /** + * @inheritdoc + */ + protected get componentTranslate(): string { + if (!this.componentTranslateInternal) { + this.componentTranslateInternal = CoreCourse.translateModuleName(this.componentTranslatableString); + } + + return this.componentTranslateInternal; + } + } diff --git a/src/core/services/utils/text.ts b/src/core/services/utils/text.ts index 462c28f69..34682af36 100644 --- a/src/core/services/utils/text.ts +++ b/src/core/services/utils/text.ts @@ -18,7 +18,7 @@ import { ModalOptions } from '@ionic/core'; import { CoreApp } from '@services/app'; import { CoreLang } from '@services/lang'; -import { CoreError } from '@classes/errors/error'; +import { CoreAnyError, CoreError } from '@classes/errors/error'; import { makeSingleton, ModalController, Translate } from '@singletons'; import { CoreWSExternalFile } from '@services/ws'; import { Locutus } from '@singletons/locutus'; @@ -533,7 +533,7 @@ export class CoreTextUtilsProvider { * @param error Error. * @return Error message, undefined if not found. */ - getErrorMessageFromError(error?: string | CoreError | CoreTextErrorObject | null): string | undefined { + getErrorMessageFromError(error?: CoreAnyError): string | undefined { if (typeof error == 'string') { return error; }