MOBILE-2975 rating: Allow disabling rating

main
Dani Palou 2019-04-29 10:14:12 +02:00
parent e449496b45
commit ff1a336fa6
5 changed files with 68 additions and 11 deletions

View File

@ -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();
}
}

View File

@ -1,4 +1,4 @@
<a *ngIf="item && item.canviewaggregate && labelKey" ion-item text-wrap [attr.detail-none]="ratingInfo.canviewall && item.count ? null : true" (click)="openRatings()">
<a *ngIf="item && item.canviewaggregate && labelKey && !disabled" ion-item text-wrap [attr.detail-none]="ratingInfo.canviewall && item.count ? null : true" (click)="openRatings()">
{{ labelKey | translate }}{{ 'core.labelsep' | translate }} {{ item.aggregatestr || '-' }}
<span *ngIf="showCount && item.count > 0">({{ item.count }})</span>
</a>

View File

@ -1,4 +1,4 @@
<ion-item text-wrap *ngIf="item && (item.canrate || item.rating != null)">
<ion-item text-wrap *ngIf="item && (item.canrate || item.rating != null) && !disabled">
<ion-label>{{ 'core.rating.rating' | translate }}</ion-label>
<ion-select text-start [(ngModel)]="rating" (ngModelChange)="userRatingChanged()" interface="action-sheet" [disabled]="!item.canrate">
<ion-option *ngFor="let scaleItem of scale.items" [value]="scaleItem.value">{{ scaleItem.name }}</ion-option>

View File

@ -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<boolean>();
this.onUpdate = new EventEmitter<void>();
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();
}
}

View File

@ -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<boolean>} Promise resolved with true if disabled, rejected or resolved with false otherwise.
*/
isRatingDisabled(siteId?: string): Promise<boolean> {
return this.sitesProvider.getSite(siteId).then((site) => {
return this.isRatingDisabledInSite(site);
});
}
/**
* Prefetch individual ratings.
*