From ff1a336fa66f563026cad6a252f182ed7b05c901 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 29 Apr 2019 10:14:12 +0200 Subject: [PATCH] MOBILE-2975 rating: Allow disabling rating --- .../rating/components/aggregate/aggregate.ts | 21 +++++++++++--- .../aggregate/core-rating-aggregate.html | 2 +- .../components/rate/core-rating-rate.html | 2 +- src/core/rating/components/rate/rate.ts | 28 ++++++++++++++++--- src/core/rating/providers/rating.ts | 26 ++++++++++++++++- 5 files changed, 68 insertions(+), 11 deletions(-) diff --git a/src/core/rating/components/aggregate/aggregate.ts b/src/core/rating/components/aggregate/aggregate.ts index 75148751a..f763ddfa3 100644 --- a/src/core/rating/components/aggregate/aggregate.ts +++ b/src/core/rating/components/aggregate/aggregate.ts @@ -12,10 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Component, Input, OnChanges, SimpleChange } from '@angular/core'; +import { Component, Input, OnChanges, SimpleChange, OnDestroy } from '@angular/core'; import { ModalController } from 'ionic-angular'; import { CoreEventsProvider } from '@providers/events'; import { CoreRatingProvider, CoreRatingInfo, CoreRatingInfoItem } from '@core/rating/providers/rating'; +import { CoreSitesProvider } from '@providers/sites'; /** * Component that displays the aggregation of a rating item. @@ -24,7 +25,7 @@ import { CoreRatingProvider, CoreRatingInfo, CoreRatingInfoItem } from '@core/ra selector: 'core-rating-aggregate', templateUrl: 'core-rating-aggregate.html' }) -export class CoreRatingAggregateComponent implements OnChanges { +export class CoreRatingAggregateComponent implements OnChanges, OnDestroy { @Input() ratingInfo: CoreRatingInfo; @Input() contextLevel: string; @Input() instanceId: number; @@ -33,12 +34,23 @@ export class CoreRatingAggregateComponent implements OnChanges { @Input() scaleId: number; @Input() courseId?: number; + disabled = false; protected labelKey: string; protected showCount: boolean; protected item: CoreRatingInfoItem; protected aggregateObserver; + protected updateSiteObserver; - constructor(private eventsProvider: CoreEventsProvider, private modalCtrl: ModalController) {} + constructor(private eventsProvider: CoreEventsProvider, private modalCtrl: ModalController, + private ratingProvider: CoreRatingProvider, sitesProvider: CoreSitesProvider) { + + this.disabled = this.ratingProvider.isRatingDisabledInSite(); + + // Update visibility if current site info is updated. + this.updateSiteObserver = eventsProvider.on(CoreEventsProvider.SITE_UPDATED, () => { + this.disabled = this.ratingProvider.isRatingDisabledInSite(); + }, sitesProvider.getCurrentSiteId()); + } /** * Detect changes on input properties. @@ -86,7 +98,7 @@ export class CoreRatingAggregateComponent implements OnChanges { * Open the individual ratings page. */ openRatings(): void { - if (!this.ratingInfo.canviewall || !this.item.count) { + if (!this.ratingInfo.canviewall || !this.item.count || this.disabled) { return; } @@ -108,5 +120,6 @@ export class CoreRatingAggregateComponent implements OnChanges { */ ngOnDestroy(): void { this.aggregateObserver && this.aggregateObserver.off(); + this.updateSiteObserver && this.updateSiteObserver.off(); } } diff --git a/src/core/rating/components/aggregate/core-rating-aggregate.html b/src/core/rating/components/aggregate/core-rating-aggregate.html index 2f3b3b063..16cd20ce9 100644 --- a/src/core/rating/components/aggregate/core-rating-aggregate.html +++ b/src/core/rating/components/aggregate/core-rating-aggregate.html @@ -1,4 +1,4 @@ - + {{ labelKey | translate }}{{ 'core.labelsep' | translate }} {{ item.aggregatestr || '-' }} ({{ item.count }}) diff --git a/src/core/rating/components/rate/core-rating-rate.html b/src/core/rating/components/rate/core-rating-rate.html index cbf5357b3..eafe8f6f1 100644 --- a/src/core/rating/components/rate/core-rating-rate.html +++ b/src/core/rating/components/rate/core-rating-rate.html @@ -1,4 +1,4 @@ - + {{ 'core.rating.rating' | translate }} {{ scaleItem.name }} diff --git a/src/core/rating/components/rate/rate.ts b/src/core/rating/components/rate/rate.ts index b2c897523..a8426ea78 100644 --- a/src/core/rating/components/rate/rate.ts +++ b/src/core/rating/components/rate/rate.ts @@ -12,11 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Component, EventEmitter, Input, OnChanges, Output, SimpleChange } from '@angular/core'; +import { Component, EventEmitter, Input, OnChanges, Output, SimpleChange, OnDestroy } from '@angular/core'; import { TranslateService } from '@ngx-translate/core'; import { CoreRatingProvider, CoreRatingInfo, CoreRatingInfoItem, CoreRatingScale } from '@core/rating/providers/rating'; import { CoreDomUtilsProvider } from '@providers/utils/dom'; import { CoreRatingOfflineProvider } from '@core/rating/providers/offline'; +import { CoreEventsProvider } from '@providers/events'; +import { CoreSitesProvider } from '@providers/sites'; /** * Component that displays the user rating select. @@ -25,7 +27,7 @@ import { CoreRatingOfflineProvider } from '@core/rating/providers/offline'; selector: 'core-rating-rate', templateUrl: 'core-rating-rate.html' }) -export class CoreRatingRateComponent implements OnChanges { +export class CoreRatingRateComponent implements OnChanges, OnDestroy { @Input() ratingInfo: CoreRatingInfo; @Input() contextLevel: string; // Context level: course, module, user, etc. @Input() instanceId: number; // Context instance id. @@ -41,11 +43,22 @@ export class CoreRatingRateComponent implements OnChanges { item: CoreRatingInfoItem; scale: CoreRatingScale; rating: number; + disabled = false; + protected updateSiteObserver; + + constructor(private domUtils: CoreDomUtilsProvider, private translate: TranslateService, eventsProvider: CoreEventsProvider, + private ratingProvider: CoreRatingProvider, private ratingOffline: CoreRatingOfflineProvider, + sitesProvider: CoreSitesProvider) { - constructor(private domUtils: CoreDomUtilsProvider, private translate: TranslateService, - private ratingProvider: CoreRatingProvider, private ratingOffline: CoreRatingOfflineProvider) { this.onLoading = new EventEmitter(); this.onUpdate = new EventEmitter(); + + this.disabled = this.ratingProvider.isRatingDisabledInSite(); + + // Update visibility if current site info is updated. + this.updateSiteObserver = eventsProvider.on(CoreEventsProvider.SITE_UPDATED, () => { + this.disabled = this.ratingProvider.isRatingDisabledInSite(); + }, sitesProvider.getCurrentSiteId()); } /** @@ -113,4 +126,11 @@ export class CoreRatingRateComponent implements OnChanges { modal.dismiss(); }); } + + /** + * Component being destroyed. + */ + ngOnDestroy(): void { + this.updateSiteObserver && this.updateSiteObserver.off(); + } } diff --git a/src/core/rating/providers/rating.ts b/src/core/rating/providers/rating.ts index 8756daec7..e14e4593f 100644 --- a/src/core/rating/providers/rating.ts +++ b/src/core/rating/providers/rating.ts @@ -13,7 +13,7 @@ // limitations under the License. import { Injectable } from '@angular/core'; -import { CoreSiteWSPreSets } from '@classes/site'; +import { CoreSite, CoreSiteWSPreSets } from '@classes/site'; import { CoreAppProvider } from '@providers/app'; import { CoreEventsProvider } from '@providers/events'; import { CoreSitesProvider } from '@providers/sites'; @@ -302,6 +302,30 @@ export class CoreRatingProvider { }); } + /** + * Check if rating is disabled in a certain site. + * + * @param {CoreSite} [site] Site. If not defined, use current site. + * @return {boolean} Whether it's disabled. + */ + isRatingDisabledInSite(site?: CoreSite): boolean { + site = site || this.sitesProvider.getCurrentSite(); + + return site.isFeatureDisabled('NoDelegate_CoreRating'); + } + + /** + * Check if rating 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. + */ + isRatingDisabled(siteId?: string): Promise { + return this.sitesProvider.getSite(siteId).then((site) => { + return this.isRatingDisabledInSite(site); + }); + } + /** * Prefetch individual ratings. *