From de410c89b449cece3152b6f5e1cc0c2e798aee86 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 18 May 2018 15:32:43 +0200 Subject: [PATCH] MOBILE-2380 course: Allow disabling download course and all courses --- src/core/course/pages/section/section.html | 2 +- src/core/course/pages/section/section.ts | 21 +++++--- .../course-progress/course-progress.html | 2 +- .../course-progress/course-progress.ts | 54 +++++++++++++++---- .../pages/course-preview/course-preview.html | 2 +- .../pages/course-preview/course-preview.ts | 22 +++++--- .../courses/pages/my-courses/my-courses.html | 2 +- .../courses/pages/my-courses/my-courses.ts | 13 ++++- .../pages/my-overview/my-overview.html | 2 +- .../courses/pages/my-overview/my-overview.ts | 24 ++++++++- src/core/courses/providers/courses.ts | 48 +++++++++++++++++ 11 files changed, 161 insertions(+), 31 deletions(-) diff --git a/src/core/course/pages/section/section.html b/src/core/course/pages/section/section.html index 16a95b37b..496249479 100644 --- a/src/core/course/pages/section/section.html +++ b/src/core/course/pages/section/section.html @@ -13,7 +13,7 @@ - + diff --git a/src/core/course/pages/section/section.ts b/src/core/course/pages/section/section.ts index 27f983028..d5fcd1ad9 100644 --- a/src/core/course/pages/section/section.ts +++ b/src/core/course/pages/section/section.ts @@ -52,6 +52,7 @@ export class CoreCourseSectionPage implements OnDestroy { prefetchCourseIcon: 'spinner', title: 'core.course.downloadcourse' }; + downloadCourseEnabled: boolean; moduleId: number; displayEnableDownload: boolean; displayRefresher: boolean; @@ -75,6 +76,7 @@ export class CoreCourseSectionPage implements OnDestroy { // Get the title to display. We dont't have sections yet. this.title = courseFormatDelegate.getCourseTitle(this.course); this.displayEnableDownload = courseFormatDelegate.displayEnableDownload(this.course); + this.downloadCourseEnabled = !this.coursesProvider.isDownloadCourseDisabledInSite(); this.completionObserver = eventsProvider.on(CoreEventsProvider.COMPLETION_MODULE_VIEWED, (data) => { if (data && data.courseId == this.course.id) { @@ -82,12 +84,14 @@ export class CoreCourseSectionPage implements OnDestroy { } }); - // Listen for changes in course status. - this.courseStatusObserver = eventsProvider.on(CoreEventsProvider.COURSE_STATUS_CHANGED, (data) => { - if (data.courseId == this.course.id) { - this.updateCourseStatus(data.status); - } - }, sitesProvider.getCurrentSiteId()); + if (this.downloadCourseEnabled) { + // Listen for changes in course status. + this.courseStatusObserver = eventsProvider.on(CoreEventsProvider.COURSE_STATUS_CHANGED, (data) => { + if (data.courseId == this.course.id) { + this.updateCourseStatus(data.status); + } + }, sitesProvider.getCurrentSiteId()); + } } /** @@ -103,6 +107,11 @@ export class CoreCourseSectionPage implements OnDestroy { this.loadData().finally(() => { this.dataLoaded = true; + if (!this.downloadCourseEnabled) { + // Cannot download the whole course, stop. + return; + } + // Determine the course prefetch status. this.determineCoursePrefetchIcon().then(() => { if (this.prefetchCourseData.prefetchCourseIcon == 'spinner') { diff --git a/src/core/courses/components/course-progress/course-progress.html b/src/core/courses/components/course-progress/course-progress.html index 2560cc0a8..73002477a 100644 --- a/src/core/courses/components/course-progress/course-progress.html +++ b/src/core/courses/components/course-progress/course-progress.html @@ -2,7 +2,7 @@

-
+
- + diff --git a/src/core/courses/pages/my-courses/my-courses.ts b/src/core/courses/pages/my-courses/my-courses.ts index f59d1051b..da0bd3dae 100644 --- a/src/core/courses/pages/my-courses/my-courses.ts +++ b/src/core/courses/pages/my-courses/my-courses.ts @@ -37,6 +37,7 @@ export class CoreCoursesMyCoursesPage implements OnDestroy { showFilter = false; coursesLoaded = false; prefetchCoursesData: any = {}; + downloadAllCoursesEnabled: boolean; protected prefetchIconInitialized = false; protected myCoursesObserver; @@ -53,6 +54,7 @@ export class CoreCoursesMyCoursesPage implements OnDestroy { */ ionViewDidLoad(): void { this.searchEnabled = !this.coursesProvider.isSearchCoursesDisabledInSite(); + this.downloadAllCoursesEnabled = !this.coursesProvider.isDownloadCoursesDisabledInSite(); this.fetchCourses().finally(() => { this.coursesLoaded = true; @@ -62,8 +64,17 @@ export class CoreCoursesMyCoursesPage implements OnDestroy { this.fetchCourses(); }, this.sitesProvider.getCurrentSiteId()); + // Refresh the enabled flags if site is updated. this.siteUpdatedObserver = this.eventsProvider.on(CoreEventsProvider.SITE_UPDATED, () => { + const wasEnabled = this.downloadAllCoursesEnabled; + this.searchEnabled = !this.coursesProvider.isSearchCoursesDisabledInSite(); + this.downloadAllCoursesEnabled = !this.coursesProvider.isDownloadCoursesDisabledInSite(); + + if (!wasEnabled && this.downloadAllCoursesEnabled && this.coursesLoaded) { + // Download all courses is enabled now, initialize it. + this.initPrefetchCoursesIcon(); + } }, this.sitesProvider.getCurrentSiteId()); } @@ -176,7 +187,7 @@ export class CoreCoursesMyCoursesPage implements OnDestroy { * Initialize the prefetch icon for the list of courses. */ protected initPrefetchCoursesIcon(): void { - if (this.prefetchIconInitialized) { + if (this.prefetchIconInitialized || !this.downloadAllCoursesEnabled) { // Already initialized. return; } diff --git a/src/core/courses/pages/my-overview/my-overview.html b/src/core/courses/pages/my-overview/my-overview.html index a6a5e8481..8771c672a 100644 --- a/src/core/courses/pages/my-overview/my-overview.html +++ b/src/core/courses/pages/my-overview/my-overview.html @@ -71,7 +71,7 @@ {{ 'core.courses.past' | translate }} -
+
diff --git a/src/core/courses/pages/my-overview/my-overview.ts b/src/core/courses/pages/my-overview/my-overview.ts index b24c5b184..956267823 100644 --- a/src/core/courses/pages/my-overview/my-overview.ts +++ b/src/core/courses/pages/my-overview/my-overview.ts @@ -14,6 +14,7 @@ import { Component, OnDestroy } from '@angular/core'; import { IonicPage, NavController } from 'ionic-angular'; +import { CoreEventsProvider } from '@providers/events'; import { CoreSitesProvider } from '@providers/sites'; import { CoreDomUtilsProvider } from '@providers/utils/dom'; import { CoreCoursesProvider } from '../../providers/courses'; @@ -64,20 +65,38 @@ export class CoreCoursesMyOverviewPage implements OnDestroy { past: {}, future: {} }; + downloadAllCoursesEnabled: boolean; protected prefetchIconsInitialized = false; protected isDestroyed; + protected updateSiteObserver; constructor(private navCtrl: NavController, private coursesProvider: CoreCoursesProvider, private domUtils: CoreDomUtilsProvider, private myOverviewProvider: CoreCoursesMyOverviewProvider, private courseHelper: CoreCourseHelperProvider, private sitesProvider: CoreSitesProvider, - private siteHomeProvider: CoreSiteHomeProvider, private courseOptionsDelegate: CoreCourseOptionsDelegate) { } + private siteHomeProvider: CoreSiteHomeProvider, private courseOptionsDelegate: CoreCourseOptionsDelegate, + private eventsProvider: CoreEventsProvider) { + } /** * View loaded. */ ionViewDidLoad(): void { this.searchEnabled = !this.coursesProvider.isSearchCoursesDisabledInSite(); + this.downloadAllCoursesEnabled = !this.coursesProvider.isDownloadCoursesDisabledInSite(); + + // Refresh the enabled flags if site is updated. + this.updateSiteObserver = this.eventsProvider.on(CoreEventsProvider.SITE_UPDATED, () => { + const wasEnabled = this.downloadAllCoursesEnabled; + + this.searchEnabled = !this.coursesProvider.isSearchCoursesDisabledInSite(); + this.downloadAllCoursesEnabled = !this.coursesProvider.isDownloadCoursesDisabledInSite(); + + if (!wasEnabled && this.downloadAllCoursesEnabled && this.courses.loaded) { + // Download all courses is enabled now, initialize it. + this.initPrefetchCoursesIcons(); + } + }); // Decide which tab to load first. this.siteHomeProvider.isAvailable().then((enabled) => { @@ -378,7 +397,7 @@ export class CoreCoursesMyOverviewPage implements OnDestroy { * Initialize the prefetch icon for selected courses. */ protected initPrefetchCoursesIcons(): void { - if (this.prefetchIconsInitialized) { + if (this.prefetchIconsInitialized || !this.downloadAllCoursesEnabled) { // Already initialized. return; } @@ -410,5 +429,6 @@ export class CoreCoursesMyOverviewPage implements OnDestroy { */ ngOnDestroy(): void { this.isDestroyed = true; + this.updateSiteObserver && this.updateSiteObserver.off(); } } diff --git a/src/core/courses/providers/courses.ts b/src/core/courses/providers/courses.ts index 84a8ba485..8f825e8a3 100644 --- a/src/core/courses/providers/courses.ts +++ b/src/core/courses/providers/courses.ts @@ -120,6 +120,54 @@ export class CoreCoursesProvider { }); } + /** + * Check if download a whole course is disabled in a certain site. + * + * @param {string} [siteId] Site Id. If not defined, use current site. + * @return {Promise} Promise resolved with true if disabled, rejected or resolved with false otherwise. + */ + isDownloadCourseDisabled(siteId?: string): Promise { + return this.sitesProvider.getSite(siteId).then((site) => { + return this.isDownloadCoursesDisabledInSite(site); + }); + } + + /** + * Check if download a whole course is disabled in a certain site. + * + * @param {CoreSite} [site] Site. If not defined, use current site. + * @return {boolean} Whether it's disabled. + */ + isDownloadCourseDisabledInSite(site?: CoreSite): boolean { + site = site || this.sitesProvider.getCurrentSite(); + + return site.isFeatureDisabled('NoDelegate_CoreCourseDownload'); + } + + /** + * Check if download all courses is disabled in a certain site. + * + * @param {string} [siteId] Site Id. If not defined, use current site. + * @return {Promise} Promise resolved with true if disabled, rejected or resolved with false otherwise. + */ + isDownloadCoursesDisabled(siteId?: string): Promise { + return this.sitesProvider.getSite(siteId).then((site) => { + return this.isDownloadCoursesDisabledInSite(site); + }); + } + + /** + * Check if download all courses is disabled in a certain site. + * + * @param {CoreSite} [site] Site. If not defined, use current site. + * @return {boolean} Whether it's disabled. + */ + isDownloadCoursesDisabledInSite(site?: CoreSite): boolean { + site = site || this.sitesProvider.getCurrentSite(); + + return site.isFeatureDisabled('NoDelegate_CoreCoursesDownload'); + } + /** * Check if My Courses is disabled in a certain site. *