One vote per user

Status
Not open for further replies.

rackem

Well-Known Member
I am looking for ideas on how to make a rating system where logged in users can vote on a list of items. Each item would have a rating that would be the average of all the user votes. Users would be able to view all the votes for each item, i.e. they could view all other user's votes as well. I am stuck on one thing. I would like each user to only have one vote per item which they would be able to edit their vote whenever they wish.

The primary list here would be the list of items. Users would click a link to add/edit their vote. Another link in the list would take them to the list of votes.

I realize there is a rating element that does something similar to this. However I need more control over what votes are counted and why.

I thought about using the Related Data functionality. That way users could see the number of votes the item has and then view the votes for an item when using the link to list. The link to form would let them add a vote. However the problem is that users would be able to add multiple votes this way instead of just editing their record one was already present. Is there a way to set a rowid = -1 for the link to form similar to how you would set up the juser plugin?

That is just one idea, I am open to other approaches to this. Has anyone ever done anything like this before?
 
You can build any custom form link with
&rowid=-1&usekey=your-user-element
but with multiple items you'll have multiple records per user.

To prevent the user of voting more than once you can add a areunique (user + item) validation.
 
but with multiple items you'll have multiple records per user.
There are multiple items so yes, each user can have many records. Just to clarify, each user can only have one vote per item but that vote can be updated at any time.

To prevent the user of voting more than once you can add a areunique (user + item) validation.
I would like to avoid using a validation if possible. I would like either A: a new form to open if the user has not voted on that item, or B: edit the existing if the user has already voted on that item.

All of a sudden this hit me. I recall that you have described to others how to create a dynamic link using a calc element. So perhaps I could use two calc elements to re-create the look of the Related Data but with customized functionality.

Calc Element #1: create a link to the list of votes that filters on the item_id of the record. The displayed text will be the number of votes already present for the item plus "view votes". I think there is a way to pass in a filter via the URL to also filter the list, yes? Not a pre-filter, just a regular one that the user could clear using the list button.

Calc Element #2: create a link to add/edit votes something like

Code:
if(no user votes exist for item)
   return link to new form with text "add vote"
else
   return link to existing form with text "edit vote"

Hmm, I wonder if that will work? I haven't tried creating links in a calc element. The links would need to open a form that pre-filled a new record or selected the right existing record based on item_id and the user_id.

You can build any custom form link with
&rowid=-1&usekey=your-user-element
Excellent! This is very helpful.

I will give this a try when I return unless there are any other suggestions?
 
&rowid=-1&usekey=your-user-element
Maybe you can use this with a filter on the item number, I didn't test (otherwise it will show the first record belonging to this user).

So something like
&rowid=-1&usekey=your-user-element &your-list___item={your-list___item_raw}
 
Just wanted to post back that I got this working (thanks troester! :)) and how I did it. Here is my commented code that I placed in some calc elements.

Calc Element #1: create a link to the list of votes that filters on the item_id of the record and also shows the number of votes the item has had.
Code:
$db =&JFactory::getDBO();
$db->setQuery("SELECT COUNT(*) FROM `mps_test` where `player` = '{mps_wi_ranking___id}'");
$num = $db->loadResult();  // Gets the number of votes the item has received
$url = JURI::base();  // Gets the root site URL
$url .= "index.php?option=com_fabrik&view=list&listid=16&resetfilters=1&mps_test___player={mps_wi_ranking___id}";  // Adds the Fabrik URL string to reset any filters present and then filter on the appropriate item
return '<a href="'.$url.'">('.$num.') View</a>';  // Presents the user with a hyperlink showing the number of votes for the item and "view"

Calc Element #2: create a link to add/edit votes
Code:
$user =& JFactory::getUser();  // Gets the current user as an array
$userid = $user->get('id');  // Pulls the user id from the user array
if($userid == 0) return "Please log in";  //Guest users are prompted to log in

$url = JURI::base();  // Gets the root site URL
$url .= "index.php?option=com_fabrik&view=form&formid=16";  // Adds on the Fabrik URL to edit form # 16

$db =&JFactory::getDBO();
$db->setQuery("SELECT COUNT(*) FROM `mps_test` where `player` = '{mps_wi_ranking___id}' AND `user` = $userid");
$exist = $db->loadResult();  // Finds whether the current user already has voted for the selected item

if($exist == 0){
$url .= "&mps_test___player={mps_wi_ranking___id}";
return '<a href="'.$url.'">Add Vote</a>';}  // If the user has not voted, the Fabrik URL string is added to create a new record with the appropriate value pre-filled.  The user is presented with a hyperlink to "Add vote"

else{
$url .= "&rowid={mps_wi_ranking___id}&usekey=player&mps_test___player={mps_wi_ranking___id}";
return '<a href="'.$url.'">Edit Vote</a>';} // If the user has already voted, the Fabrik URL string is added to open that record for edit.  The user is presented with a hyperlink to "Edit Vote"
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top