lost password?

home
•  xaraya +
•  rails
•  django
•  webdev
•  xamp
•  musings

rss
Tag this page
   

ยป Blogs that link here
last modified: Aug 20, 2006
(first posted: Aug 19, 2006)
(1795 Reads)
keywords: articles ratings
Permalink

Adding ratings to articles

I decided to finally add user ratiings to articles on the news-oriented IWA site (http://www.windaction.org). Here's the steps (and some missteps) I went through.

Enable Ratings module and hook it to articles

Simple enough, you'd think. Well, I already had Ratings enabled, I guess I had played with it before, and had toyed with various configurations like should it be 5-stars, or x-out-of-ten, or x%, so when I settled on 5-stars, some existing rated articles should have been 100 but were actually 10's. I dont know, but deactivating/uninstalling, then re-installing the module cleared the db table and took care of that anomoly.

Then I hooked Ratings to just specific pubtypes in the Ariticles module. Well, that seemed to work fine for a while. Eventually though I had to change it to hook to all pubtypes. Why? I wanted a mix of any rated pubtypes in a topitems block. Xaraya apparently wont let you do that, either hook it to all pubtypes and enable all pubtypes in the block, or you're limited to one pubtype in the block. I opted for the former and just made sure the display template for each pubtype that does not support ratings skips that hook. See the xaraya bug i filed.

I configured it for medium security- users can vote once per session. Actually I dont really care, but while testing I noticed that after someone votes, it may not be obvious that the vote was recorded so they might click it multiple times. But with the security, the voting widget disappears once the vote is recorded. Cool!

Setup Ratings Config and Template

For this I configured it as 5 star.

The default template handles all the options. One thing that bothered me about the star display is it's not necessarily obvious that the value is "x out of 5" when you only display x stars. Also the star icons that come with the module are the wrong color, so I created 3 new icons:

  • star.gif
  • starhalf.gif
  • starnone.gif

I also thought the default icons (16 x 16) were too big for my site, so I made these 12 x 12 pixels.

So the next step was modifying the ratings user display template so it shows the extra empty stars. Here's the template, stripped of the styles I know I'm not using:

Notice I had to eliminate nice formatting in the loop with the <img> tag so no extra spaces were placed between images.

I also don't display any stars if the item is not rated (actually if the rating is zero, which is good enough). That's what's going on in the first if-statements.

As a result, i get something like this:

For the form to vote a new rating, the default template has the user pick a value from the dropdown list and then click a submit button. I thought many users would not realize they need to click the button so their vote would not get recorded. Also, the dropdown shows a default value of "3", not sure why but some users might confuse that with the current total ratings value. So I eliminated the zero choice and the submit button, and added onchange="submit()" to the <select>. That works very nicely :)

Articles Templates

The next step is to update the Articles templates to show the current ratings and allow visitors to rate items.

For each pubtype which I wanted to allow rating, i edited its user-display-xxx.xt template. The current rating should be after the title, on the line where I show the date, author, and publication information. It turned out to look best if I floated it to the right, thus,

and then at the bottom of the template, skip the default location for this hook:

Be sure the Articles Config for each pubtype has Show Ratings checked.

Enable for Anon Visitors

By default anonymous visitors don't have permission to rate articles. I added a priv as follows to my priv group named "CasualAccess":

name: RatingsVote
module: Ratings
Component: Item
Level: Comment

That should do it so users can rate the articles and see the ratings on article display.

Ratings in the Article Summary Template

Well, I also want the ratings to show in the articles view / summary page. This wasn't as obvious. It seems the ratings user-display template is not called for item summaries (only item display). However, fortunately, the rating value is available.

So, to format the value, I created a new template in mytheme/modules/ratings/includes/displayratings.xt that shows the stars like the ratings/user-display.xt template, without the input form:

Then in each pubtype's summary template, I insert the following line where I want it to go:

Top Rated Items Block

The last step is to create a topitems block with the top rated items.

I created a block, type articles/topitems, as follows:

name: toprated
title: Top Rated
number of items: 30
all publication types: checked
link to publication type: checked
all categories: checked
dynamic block title: unchecked (doesnt matter)
sort by: Rating
show sort value: checked
show summary: (doesnt matter)
status: All pubished

and

Instance Template: ;ratings

Then, I modified the default topitems.xd template, saved as mytheme/modules/articles/blocks/topitems-ratings.xt as follows:

 

This is probably more detail than anyone cares about but briefly, walking through the code,

  • dont do anything if there's no items
  • loop through each item
  • commented out the dump item data
  • define a variable $rating from the current item
  • if the rating is 0, then skip it (we only want to show rated items in this block)
  • display the rating stars, using <xar:template > tag
  • the rest is not related to this article but is (will be) discussed in another posting

See the results at http://www.windaction.org

 

 

There are no comments attached to this item.

Post a new comment

How many days in a week?

Name :