Ratings API - How to provide a custom RatingsConfiguration

Hi there, I have a simple question related to the new ratings api …

If I understand it correctly, I automatically get my own RatingsManager instance named ‘exercise’ by simply referring to it like this:

$services.ratings.exercise.setRating('test1', $xcontext.getUser(), 1)

But how and when and where could I instantiate a RatingsConfiguration in order to tweak it for my needs?

Thanks, Holger

Hi,

so for the when it’s easy: it should always be before you first call $services.ratings.exercise, basically the RatingsConfiguration for this RatingsManager is looked for when instantiating the RatingsManager which is then cached, so you need to pay attention to do it early enough.

The how and where are a bit linked, you have two options:

  1. You implement a Java extension with a provided RatingsConfiguration component which uses an annotation @Named('exercise') and you install it on your wiki. There’s info here on how to do it.
  2. You use a groovy script to implement a custom RatingsConfiguration instance, and you register it using the component script service and more specifically services.component.registerComponent​(ComponentDescriptor<T> componentDescriptor,T componentInstance)

many thanks for your hint!!

I implemented it using the Script Component Extension like so:

import java.util.Collections;
import java.util.Set;
import javax.inject.Named;
import javax.inject.Singleton;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.component.annotation.Component;
import org.xwiki.ratings.RatingsConfiguration;

@Component
@Named("aufgaben")
@Singleton
public class AufgabenBewertung implements RatingsConfiguration
{
    public boolean isZeroStored() { return false; }
    public int getScaleUpperBound() { return 5; }
    public boolean hasDedicatedCore() { return false; }
    public boolean isAverageStored() { return true; }
    public String getRatingsStorageHint() { return "solr"; }
    public String getAverageRatingStorageHint() { return "solr"; }
    public Set<EntityReference> getExcludedReferencesFromRatings() { return Collections.EMPTY_SAET; }
    public boolean isEnabled() { return true; }
}
1 Like

I’m a bit surprised with this choice if you want to store average rating: here it means you’ll basically compute average on a scale [1,5] and not on a scale [0,5].

Why? 1 … 5 of 5 stars and 0 for deletion of the vote? Is that unusual?

I don’t know if there’s such thing as an usual choice here as I only know 2 usages of this :slight_smile:
But for the standard Ratings configuration we do store the vote for “0” (see: xwiki-platform/xwiki-platform-core/xwiki-platform-ratings/xwiki-platform-ratings-api/src/main/java/org/xwiki/ratings/internal/DefaultRatingsConfiguration.java at master · xwiki/xwiki-platform · GitHub).

So if we have 3 ratings with 0, 4 and 5, the average will be (0+4+5) / 3 = 3 for us.
For you the 0 won’t be stored so you’ll only get 4 + 5 / 2 = 4.5

It mainly depends if you’re planning to have a UI to let your users set “0” or not basically.