From cc1b4acb8e129d6977ee9bff2f7fc6cf74e046da Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 6 Jul 2020 11:39:38 +0200 Subject: [PATCH] MOBILE-3475 core: Prevent errors when calling isVersionGreaterEqualThan --- src/addon/block/myoverview/providers/block-handler.ts | 2 +- src/addon/block/timeline/providers/block-handler.ts | 4 +++- src/addon/calendar/providers/calendar.ts | 2 +- src/addon/filter/multilang/providers/handler.ts | 2 +- src/addon/mod/assign/components/index/index.ts | 7 +++++-- .../mod/assign/submission/onlinetext/providers/handler.ts | 2 +- src/addon/mod/forum/providers/forum.ts | 5 +++-- src/addon/mod/forum/providers/prefetch-handler.ts | 3 ++- src/addon/mod/glossary/providers/prefetch-handler.ts | 3 ++- src/addon/mod/quiz/providers/prefetch-handler.ts | 5 +++-- src/addon/mod/resource/providers/helper.ts | 4 +++- src/addon/mod/resource/providers/prefetch-handler.ts | 2 +- src/core/course/providers/course.ts | 4 ++-- src/core/course/providers/sync.ts | 4 +++- src/core/courses/pages/course-preview/course-preview.ts | 4 ++-- src/core/courses/providers/helper.ts | 2 +- src/directives/format-text.ts | 2 +- 17 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/addon/block/myoverview/providers/block-handler.ts b/src/addon/block/myoverview/providers/block-handler.ts index 976c3c046..089bfb058 100644 --- a/src/addon/block/myoverview/providers/block-handler.ts +++ b/src/addon/block/myoverview/providers/block-handler.ts @@ -37,7 +37,7 @@ export class AddonBlockMyOverviewHandler extends CoreBlockBaseHandler { * @return Whether or not the handler is enabled on a site level. */ isEnabled(): boolean | Promise { - return this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.6') || + return (this.sitesProvider.getCurrentSite() && this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.6')) || !this.coursesProvider.isMyCoursesDisabledInSite(); } diff --git a/src/addon/block/timeline/providers/block-handler.ts b/src/addon/block/timeline/providers/block-handler.ts index 1b5044146..55b201bc3 100644 --- a/src/addon/block/timeline/providers/block-handler.ts +++ b/src/addon/block/timeline/providers/block-handler.ts @@ -41,7 +41,9 @@ export class AddonBlockTimelineHandler extends CoreBlockBaseHandler { */ isEnabled(): boolean | Promise { return this.timelineProvider.isAvailable().then((enabled) => { - return enabled && (this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.6') || + const currentSite = this.sitesProvider.getCurrentSite(); + + return enabled && ((currentSite && currentSite.isVersionGreaterEqualThan('3.6')) || !this.coursesProvider.isMyCoursesDisabledInSite()); }); } diff --git a/src/addon/calendar/providers/calendar.ts b/src/addon/calendar/providers/calendar.ts index ed3891a00..5d40bbd31 100644 --- a/src/addon/calendar/providers/calendar.ts +++ b/src/addon/calendar/providers/calendar.ts @@ -393,7 +393,7 @@ export class AddonCalendarProvider { site = site || this.sitesProvider.getCurrentSite(); // The WS to create/edit events requires a fix that was integrated in 3.7.1. - return site.isVersionGreaterEqualThan('3.7.1'); + return site && site.isVersionGreaterEqualThan('3.7.1'); } /** diff --git a/src/addon/filter/multilang/providers/handler.ts b/src/addon/filter/multilang/providers/handler.ts index 8f809624a..229182791 100644 --- a/src/addon/filter/multilang/providers/handler.ts +++ b/src/addon/filter/multilang/providers/handler.ts @@ -83,6 +83,6 @@ export class AddonFilterMultilangHandler extends CoreFilterDefaultHandler { */ shouldBeApplied(options: CoreFilterFormatTextOptions, site?: CoreSite): boolean { // The filter should be applied if site is older than 3.7 or the WS didn't filter the text. - return options.wsNotFiltered || !site.isVersionGreaterEqualThan('3.7'); + return options.wsNotFiltered || (site && !site.isVersionGreaterEqualThan('3.7')); } } diff --git a/src/addon/mod/assign/components/index/index.ts b/src/addon/mod/assign/components/index/index.ts index 8d8fb9985..b47535c64 100644 --- a/src/addon/mod/assign/components/index/index.ts +++ b/src/addon/mod/assign/components/index/index.ts @@ -207,9 +207,10 @@ export class AddonModAssignIndexComponent extends CoreCourseModuleMainActivityCo // Check if groupmode is enabled to avoid showing wrong numbers. return this.groupsProvider.getActivityGroupInfo(this.assign.cmid, false).then((groupInfo) => { + const currentSite = this.sitesProvider.getCurrentSite(); this.groupInfo = groupInfo; this.showNumbers = groupInfo.groups.length == 0 || - this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.5'); + (currentSite && currentSite.isVersionGreaterEqualThan('3.5')); return this.setGroup(this.groupsProvider.validateGroupId(this.group, groupInfo)); }); @@ -258,8 +259,10 @@ export class AddonModAssignIndexComponent extends CoreCourseModuleMainActivityCo } } + const currentSite = this.sitesProvider.getCurrentSite(); + this.needsGradingAvalaible = response.gradingsummary && response.gradingsummary.submissionsneedgradingcount > 0 && - this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.2'); + currentSite && currentSite.isVersionGreaterEqualThan('3.2'); }); } diff --git a/src/addon/mod/assign/submission/onlinetext/providers/handler.ts b/src/addon/mod/assign/submission/onlinetext/providers/handler.ts index c2b80fc2d..572d61735 100644 --- a/src/addon/mod/assign/submission/onlinetext/providers/handler.ts +++ b/src/addon/mod/assign/submission/onlinetext/providers/handler.ts @@ -225,7 +225,7 @@ export class AddonModAssignSubmissionOnlineTextHandler implements AddonModAssign // Bug was fixed in 3.1.1 minor release and in 3.2. const currentSite = this.sitesProvider.getCurrentSite(); - return currentSite.isVersionGreaterEqualThan('3.1.1') || currentSite.checkIfAppUsesLocalMobile(); + return currentSite && (currentSite.isVersionGreaterEqualThan('3.1.1') || currentSite.checkIfAppUsesLocalMobile()); } /** diff --git a/src/addon/mod/forum/providers/forum.ts b/src/addon/mod/forum/providers/forum.ts index 3e5f6f956..2c9d03cdb 100644 --- a/src/addon/mod/forum/providers/forum.ts +++ b/src/addon/mod/forum/providers/forum.ts @@ -291,7 +291,8 @@ export class AddonModForumProvider { * @return True if fixed, false otherwise. */ isAllParticipantsFixed(): boolean { - return this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan(['3.1.5', '3.2.2']); + return this.sitesProvider.getCurrentSite() && + this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan(['3.1.5', '3.2.2']); } /** @@ -543,7 +544,7 @@ export class AddonModForumProvider { isDiscussionListSortingAvailable(site?: CoreSite): boolean { site = site || this.sitesProvider.getCurrentSite(); - return site.isVersionGreaterEqualThan('3.7'); + return site && site.isVersionGreaterEqualThan('3.7'); } /** diff --git a/src/addon/mod/forum/providers/prefetch-handler.ts b/src/addon/mod/forum/providers/prefetch-handler.ts index bf741d780..00af16f83 100644 --- a/src/addon/mod/forum/providers/prefetch-handler.ts +++ b/src/addon/mod/forum/providers/prefetch-handler.ts @@ -87,7 +87,8 @@ export class AddonModForumPrefetchHandler extends CoreCourseActivityPrefetchHand */ protected getPostsFiles(posts: any[]): any[] { let files = []; - const getInlineFiles = this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.2'); + const getInlineFiles = this.sitesProvider.getCurrentSite() && + this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.2'); posts.forEach((post) => { if (post.attachments && post.attachments.length) { diff --git a/src/addon/mod/glossary/providers/prefetch-handler.ts b/src/addon/mod/glossary/providers/prefetch-handler.ts index 30bdf4903..c202a88e6 100644 --- a/src/addon/mod/glossary/providers/prefetch-handler.ts +++ b/src/addon/mod/glossary/providers/prefetch-handler.ts @@ -86,7 +86,8 @@ export class AddonModGlossaryPrefetchHandler extends CoreCourseActivityPrefetchH */ protected getFilesFromGlossaryAndEntries(module: any, glossary: any, entries: any[]): any[] { let files = this.getIntroFilesFromInstance(module, glossary); - const getInlineFiles = this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.2'); + const getInlineFiles = this.sitesProvider.getCurrentSite() && + this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.2'); // Get entries files. entries.forEach((entry) => { diff --git a/src/addon/mod/quiz/providers/prefetch-handler.ts b/src/addon/mod/quiz/providers/prefetch-handler.ts index 5ec4d9cbf..f1832e11d 100644 --- a/src/addon/mod/quiz/providers/prefetch-handler.ts +++ b/src/addon/mod/quiz/providers/prefetch-handler.ts @@ -110,8 +110,9 @@ export class AddonModQuizPrefetchHandler extends CoreCourseActivityPrefetchHandl */ protected getAttemptsFeedbackFiles(quiz: any, attempts: any[]): Promise { // We have quiz data, now we'll get specific data for each attempt. - const promises = [], - getInlineFiles = this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.2'); + const promises = []; + const getInlineFiles = this.sitesProvider.getCurrentSite() && + this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.2'); let files = []; attempts.forEach((attempt) => { diff --git a/src/addon/mod/resource/providers/helper.ts b/src/addon/mod/resource/providers/helper.ts index cdbf17270..62a00e511 100644 --- a/src/addon/mod/resource/providers/helper.ts +++ b/src/addon/mod/resource/providers/helper.ts @@ -104,8 +104,10 @@ export class AddonModResourceHelperProvider { * @return Whether the resource should be displayed embeded. */ isDisplayedEmbedded(module: any, display: number): boolean { + const currentSite = this.sitesProvider.getCurrentSite(); + if ((!module.contents.length && !module.contentsinfo) || !this.fileProvider.isAvailable() || - (!this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.7') && this.isNextcloudFile(module))) { + (currentSite && !currentSite.isVersionGreaterEqualThan('3.7') && this.isNextcloudFile(module))) { return false; } diff --git a/src/addon/mod/resource/providers/prefetch-handler.ts b/src/addon/mod/resource/providers/prefetch-handler.ts index 27c52989d..17f775cce 100644 --- a/src/addon/mod/resource/providers/prefetch-handler.ts +++ b/src/addon/mod/resource/providers/prefetch-handler.ts @@ -147,7 +147,7 @@ export class AddonModResourcePrefetchHandler extends CoreCourseResourcePrefetchH * @return Promise resolved with true if downloadable, resolved with false otherwise. */ isDownloadable(module: any, courseId: number): Promise { - if (this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.7')) { + if (this.sitesProvider.getCurrentSite() && this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.7')) { // Nextcloud files are downloadable from 3.7 onwards. return Promise.resolve(true); } diff --git a/src/core/course/providers/course.ts b/src/core/course/providers/course.ts index 98c551149..5a734cd76 100644 --- a/src/core/course/providers/course.ts +++ b/src/core/course/providers/course.ts @@ -122,7 +122,7 @@ export class CoreCourseProvider { canGetCourseBlocks(site?: CoreSite): boolean { site = site || this.sitesProvider.getCurrentSite(); - return site.isVersionGreaterEqualThan('3.7') && site.wsAvailable('core_block_get_course_blocks'); + return site && site.isVersionGreaterEqualThan('3.7') && site.wsAvailable('core_block_get_course_blocks'); } /** @@ -135,7 +135,7 @@ export class CoreCourseProvider { canRequestStealthModules(site?: CoreSite): boolean { site = site || this.sitesProvider.getCurrentSite(); - return site.isVersionGreaterEqualThan(['3.4.6', '3.5.3']); + return site && site.isVersionGreaterEqualThan(['3.4.6', '3.5.3']); } /** diff --git a/src/core/course/providers/sync.ts b/src/core/course/providers/sync.ts index ef5ea7462..f62b9c48a 100644 --- a/src/core/course/providers/sync.ts +++ b/src/core/course/providers/sync.ts @@ -197,7 +197,9 @@ export class CoreCourseSyncProvider extends CoreSyncBaseProvider { if (result.updated) { // Update data. return this.courseProvider.invalidateSections(courseId, siteId).then(() => { - if (this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.6')) { + const currentSite = this.sitesProvider.getCurrentSite(); + + if (currentSite && currentSite.isVersionGreaterEqualThan('3.6')) { return this.courseProvider.getSections(courseId, false, true, undefined, siteId); } else { return this.courseProvider.getActivitiesCompletionStatus(courseId, siteId); diff --git a/src/core/courses/pages/course-preview/course-preview.ts b/src/core/courses/pages/course-preview/course-preview.ts index 4ca797ecf..28f6d62ee 100644 --- a/src/core/courses/pages/course-preview/course-preview.ts +++ b/src/core/courses/pages/course-preview/course-preview.ts @@ -235,7 +235,7 @@ export class CoreCoursesCoursePreviewPage implements OnDestroy { }); }); }).finally(() => { - if (!this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.7')) { + if (this.sitesProvider.getCurrentSite() && !this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.7')) { return this.coursesProvider.isGetCoursesByFieldAvailableInSite().then((available) => { if (available) { return this.coursesProvider.getCourseByField('id', this.course.id).then((course) => { @@ -402,7 +402,7 @@ export class CoreCoursesCoursePreviewPage implements OnDestroy { promises.push(this.coursesProvider.invalidateCourse(this.course.id)); promises.push(this.coursesProvider.invalidateCourseEnrolmentMethods(this.course.id)); promises.push(this.courseOptionsDelegate.clearAndInvalidateCoursesOptions(this.course.id)); - if (this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.7')) { + if (this.sitesProvider.getCurrentSite() && !this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.7')) { promises.push(this.coursesProvider.invalidateCoursesByField('id', this.course.id)); } if (this.guestInstanceId) { diff --git a/src/core/courses/providers/helper.ts b/src/core/courses/providers/helper.ts index d7ac68a4b..5898d00a3 100644 --- a/src/core/courses/providers/helper.ts +++ b/src/core/courses/providers/helper.ts @@ -114,7 +114,7 @@ export class CoreCoursesHelperProvider { promises = [], colors = []; - if (site.isVersionGreaterEqualThan('3.8')) { + if (site && site.isVersionGreaterEqualThan('3.8')) { promises.push(site.getConfig().then((configs) => { for (let x = 0; x < 10; x++) { colors[x] = configs['core_admin_coursecolor' + (x + 1)] || null; diff --git a/src/directives/format-text.ts b/src/directives/format-text.ts index f5c9f8f4b..996fab7bf 100644 --- a/src/directives/format-text.ts +++ b/src/directives/format-text.ts @@ -711,7 +711,7 @@ export class CoreFormatTextDirective implements OnChanges { } // Width and height parameters are required in 3.6 and older sites. - if (!site.isVersionGreaterEqualThan('3.7')) { + if (site && !site.isVersionGreaterEqualThan('3.7')) { newUrl += '&width=' + width + '&height=' + height; } iframe.src = newUrl;