Rating Assets

Liferay’s asset framework supports a rating system that lets your users rate content in plugins. You’ve probably seen this in many of Liferay’s built-in plugins. A great example is the Blogs portlet. On viewing a blogs entry, a row of stars appears, letting the user rate the entry on a scale from one to five stars. Ratings help users figure out what content is popular. The feature also fosters a sense of community and collaboration among content producers and consumers. Even better, once your plugin is asset enabled, implementing ratings is a snap.

Figure 1: Users can now rate instances of your custom entities.

Figure 1: Users can now rate instances of your custom entities.

This tutorial shows you how to add ratings to an asset enabled portlet. The tutorial uses code from a custom Insults portlet as an example. The Insults portlet seemed appropriate, since a truly distinguished writer of insults needs to know how good his or her insults really are.

In order to implement ratings on your custom entity, it must be asset enabled. The completed Insults portlet code that uses this feature is on GitHub, here.

Now go ahead and get started learning how to add ratings to your portlets!

If you’ve implemented asset rendering for your custom entity, you can display ratings in the full content view of your entity. You can, of course, also display this asset feature in any view JSP you create for viewing the entity.

As an example, the Insult portlet’s view JSP file view_insult.jsp shows an insult entity and its ratings. This tutorial shows you how to associate the ratings component with your custom entity.

In your view JSP you can use ParamUtil to get the ID of the entity from the render request. Then you can create an entity object using your -LocalServiceUtil class.

long insultId = ParamUtil.getLong(renderRequest, "insultId");
Insult ins = InsultLocalServiceUtil.getInsult(insultId);

After the code that displays your entity, you can add the ratings component using the liferay-ui:ratings tag. Note that the object’s ID is used to tie the ratings to the entity:

<liferay-ui:ratings className="<%=Insult.class.getName()%>"
    classPK="<%=ins.getInsultId()%>" type="stars" />

In the code above, the type attribute is given the value "stars" to use the five star rating system. You can optionally replace the star ratings with a thumbs-up or thumbs-down rating system by changing this value to "thumbs".

If you haven’t already connected your portlet’s view to the JSP for your entity, you can refer here to see how to connect your portlet’s main view JSP to your entity’s view JSP.

Great! Now you have the JSP that lets your users rate content.

Now redeploy your portlet and refresh the page so that the your plugin’s UI reloads. The ratings UI component now appears in your entity’s view.

Great! Now you know how to add ratings for content in your asset enabled portlets.

Another thing you might want to do is perform permissions checks to make sure only the proper users can rate content. For example, the Add Insult and Permissions buttons of the Insults portlet are wrapped in a permissions check in its view.jsp. For more information on this, see the learning path Checking for Permissions in the UI.

Related Topics

Asset Enabling Custom Entities

Implementing Asset Renderers

Liferay UI Taglibs

User Interfaces with AlloyUI

« Implementing Asset Categorization and TaggingCommenting on Assets »
Was this article helpful?
0 out of 0 found this helpful