Allow users to rate a record?

A place for users and developers of the Xataface to discuss and receive support.

Postby Aahz » Mon Jul 02, 2007 8:59 pm

Now that I've managed to figure out most of setting up Dataface I love it even more. So do my users :) However, they're already asking for more functionality and I'm not even sure if this is possible.

My database consists of game components (miniature figures and cards) and my users would like to be able to "click to rate" individual game components (records). The ultimate would be a rollover, single-click voting like they have at imdb.com, but even a less 'fancy' interface would be much appreciated.

Basically, I realize I'd need to add a new field to y tables in order to contain the rating. And it would clearly have to be a calculated field with the formula created to integrate the latest vote. Output would be something like "Rating: x out of y with z votes", preferably graphical (I can easily create the graphics), but text would be acceptable as well.

So, is this possible with Dataface? If so, where do I start?
Aahz
 
Posts: 17
Joined: Wed Dec 31, 1969 5:00 pm

Postby shannah » Tue Jul 03, 2007 3:48 pm

Glad it is working out. Firstly I'd like to say that *anything* is possible with dataface. Using blocks and slots you are able to add custom HTML anywhere you want in the interface.

The implementation of your rating system will depend on your requirements. Mainly, whether or not you want to keep track of who rated which record, and which rating they gave the record.

A generic system for keeping this rating might be to have a single table called 'ratings', with columns: record_id, username, date_of_rating, and score.

Then in each table that you want to have a rating for, you would add a dummy calculated field which will serve as a placeholder for the record's rating.

e.g.
__fields__ = "select *, foocol as rating from footable"

(Where foocol is any of the columns in footable). The reason for using a placeholder here rather than actually performing the rating calculation is because the SQL parser in dataface can't handle arithmetic operators yet.

Then you can override the display value and html values for the rating field in the delegate class.
The display value might be something like:
function rating__display(&$record){
$sql = "select avg(score) from ratings where record_id='".addslashes($record->getId())."'";
list($score) = mysql_fetch_row(mysql_query($sql, df_db()));
return $score;
}

And you can be even more creative with the HTML value for the field. The HTML value is how the value is displayed in the details view. Here you could add your little javascript function to perform the rating.

e.g.

function rating__htmlValue(&$record){
$auth =& Dataface_AuthenticationTool::getInstance();
$user =& $auth->getLoggedInUser();
if ( $user ){
// User is logged in so he can rate the record
// See if the user has already rated this record
list($num_ratings) = mysql_fetch_row(mysql_query("select count(*) from ratings where record_id='".addslashes($record->getId())."' and username='".addslashes($user->val('username'))."'", df_db()));
if ( $num_ratings > 0 ){
// This user has already rated this record.. he can't rate it again
return $record->display('rating');
} else {
return '.. {put a little web form here to allow the user to rate this record} ..';
}
}
}

Finally, you would need to implement a custom action that handles input from your webform to add a rating.
Alternatively if you didn't want anything fancy, you could just put a link to the new record form for the ratings table to allow the user to fill in their rating that way.

When linking to the new record form, you can submit default values in the GET string.

e.g.
?-table=ratings&-action=new&record_id=foobar
Would like to the new record form for the ratings table, and it would put a default value of foobar in the record_id field.

One thing to note about the examples above. I make use of the getId() method of the Dataface_Record class. This is a handy method for obtaining a unique identifier for a record. It is in the form:
tablename?key1=val1&key2=val2, etc.. where key1..keyn are the primary keys of the record.

-Steve
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby Aahz » Wed Jul 04, 2007 1:25 am

Ya know, Steve, your accessibilty here at the site is just amazing. Thank you.

Of course, I only understood about a third of your message, but that's totally on me, nothing to do with you. I did understand the two most important things: 1) Dataface can actually do this, and 2) *I* can actually do this, it's just gonna take a little more studying. I trust that I'm up to the challenge, but need to push it back a bit on the schedule.

As always, thanks for all your hard work,

Aahz
Aahz
 
Posts: 17
Joined: Wed Dec 31, 1969 5:00 pm

Postby shannah » Wed Jul 04, 2007 2:29 pm

*** Correction ***

In my post I have:
__fields__ = "select *, foocol as rating from footable"


This should be
__sql__ = "select *, foocol as rating from footable"

Good luck.

-Steve
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 28 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved