Get username or user id for "Ratings" Module

MCBurner

Joined: 2012-12-31
Posts: 5
Posted: Fri, 2013-05-10 16:17

Hi.

Please trust me - I did a google search and als a search in this forum. But without ANY luck! Maybe not my day...

My problem is the following: I would like to use the "Ratings" module with a Gallery 3 test installation. But there are a few problems with the module which I'm trying to fix within the modules code. For example, the base design is limited to allow just one vote per IP address. That's not what I want to do. Each user should be able to vote for each pic in the gallery at least one time. The setting in "Ratings" to allow that does not really work.

My plan is to add the users ID to each vote that is saved in the database. And that's the problem or the answer that I was looking for: how can I get the logged in username or the id within the module code???? I tried serveral code snippets but without success.

Any help would be appreciated. Must be definetly my fault that I was not able to find the answer in this forum. I can't be the first who asks this....

 
jnash
jnash's picture

Joined: 2004-08-02
Posts: 814
Posted: Fri, 2013-05-10 16:40

The restriction is placed on the itemid of the image. A user -should- be able to vote -once- on any image (and every image) within an album.

I just tried myself, and can vote on any image within an album.

Perhaps something I'm missing from your description?

Regardless for user id it's: $theuserid = identity::active_user()->id;

 
MCBurner

Joined: 2012-12-31
Posts: 5
Posted: Fri, 2013-05-10 17:24

Hi jnash. Thanks for your help.

I've tried your tip and it doesn't work. I get this error " Class 'identity' not found in ....\modules\ratings\vendor\ratings.php". Looks like I have to include something of the G3 core before?

And with the vote problem - I failed to add some further details. The problem only occurs, if the voters source IP address is the same.
So you are right - one user can vote one time (as soon as I did note change the multiple vote option) per image. But if there are two users coming from the same IP address, it fails.
I think that's because the vote is just saved with the item info, the vote and the IP address ($SQL_INSERT_VOTE = "INSERT INTO $this->ratings(ratable_id, ip_address, rating) VALUES ('$id', '$ip', '$rating')").
I would like to fix that and make use of the user's ID. But I don't want to change the complete module code so I thought it should be simple - change the value of the IP address this way: "<IP ADDRESS>_<USERID>"

Regards.

 
jnash
jnash's picture

Joined: 2004-08-02
Posts: 814
Posted: Fri, 2013-05-10 18:38

You're correct, you can't call that class from the vendor/ratings.php script as it's not part of the Kohana package for Gallery - it's a third party app altogether.

Ah, I see what you mean by two users via the same IP address.

(I'm the author of the ratings module, btw)

Let me take a look at my code again, and I think I may be able to adjust this. (I've included other gallery variables into the ratings script, so I think I can do it again easily enough)

(Where I call the ratings script I convert the gallery variable to a javascript variable and pass it that way if I recall, or it's added to the url, I really can't remember clearly) so feel free to jump on it first if you'd like.

 
jnash
jnash's picture

Joined: 2004-08-02
Posts: 814
Posted: Sun, 2013-05-12 20:37

@MCBurner: I started working on this, but realized that the ratings script has a rather crude way of checking for a duplicate vote... It just looks to see if the insert failed (which on a duplicate would because id, rating, and ip would be the same)

I did the leg work for adding the userid, but then realized that this crude way of checking for a duplicate would then allow the same userid on different IP addresses vote again. This isn't the behavior.

So, give me a bit longer, and I'll see I can whip up a better method for checking for the dupes...

I'm almost there, and this was a quick fix until I found that 'gotcha' above.

Stay tuned...

 
jnash
jnash's picture

Joined: 2004-08-02
Posts: 814
Posted: Sun, 2013-05-12 22:30

@MCBurner - give this version a try (warning, I wouldn't recommend it yet on a production gallery or one with votes already unless you have a backup) I tested it, but still, better to warn you.

http://codex.galleryproject.org/images/5/56/Ratings-temp.zip

It will now check:
(If set for registered users only voting)
* if username matches - reject duplicate vote
* if ip matches, but usernames don't - then vote is okay

(If set for all can vote)
* if ip matches - reject duplicate vote

Let me know how it works out so I can publish this to the codex/modules page.

 
EagleTG

Joined: 2008-06-11
Posts: 3
Posted: Mon, 2013-05-13 22:48

This almost worked... I had to modify the following:

Line 264 in vendor\ratings.php changed to:

Quote:
UNIQUE ( `ratable_id` , `userid`),

Line 187 and 192 both had a typo of SELETE instead of SELECT, changed to:

Quote:
$SQL = "SELECT * from $this->ratings where 'userid' = $userid";

and

Quote:
$SQL = "SELECT * from $this->ratings where 'ip_address' = $ip";

I also dropped my ratings and ratables tables and started over.

 
jnash
jnash's picture

Joined: 2004-08-02
Posts: 814
Posted: Tue, 2013-05-14 00:34

ooops on that... did that fix it up though?

 
MCBurner

Joined: 2012-12-31
Posts: 5
Posted: Tue, 2013-05-21 22:13

Hi jnash, EagleTG

I was a bit busy with other stuff the last days but I'm now going to test your new script/changes. I'll inform you asap about the results.

 
MCBurner

Joined: 2012-12-31
Posts: 5
Posted: Wed, 2013-05-22 00:03

Doesn't look good. Even if both tables are deleted and I try to start with first new vote, I get "Duplicate votes not allowed". But in fact, I changed that setting in the script so it is allowed. I'm trying to add some line for SQL output to check were the problem is.

UPDATE 1: looks like no data is added to the ratings table. still investigating
UPDATE 2: the insert statement for new data looks fine and if I add it manually (copy n paste) via phpmyadmin it does the job. but it's not added via the ratings script.
UPDATE 3: for some reason it looks like rating is not correctly connected to the DB when it tries to push the "INSERT INTO" sql line. i forced the output of error message while changing the line "if (!mysql_query($SQL_INSERT_VOTE))" to "if (mysql_query($SQL_INSERT_VOTE))" and it gives the "RabidRatings couldn't connect to the database. Please make sure your configuration is correct" message.
UPDATE 4: I did some really "quick and dirty" changes to the ratings.php file. they work - but I think it shouldn't be done that way. What I've changed is:
- deactivated line 55-60 (the db settings)
- changed the line "$this->dbConnection = mysql_connect($this->dbHost, $this->dbUser,$this->dbPassword);" and the line "if (!mysql_select_db($this->dbDatabase))" to use my mysql database credentials and settings instead of getting them from another file.

I think I have a problem with original line 55-60. In my case the files included there don't exist. Did I miss something in the installation instructions?
But beside of that it's really strange that before my "quick and dirty" changes the insert for the "ratables" table worked fine. So there was a connection to the db?! Why does it work for the keys but not for the ratings?

And to make sure I didn't miss that: many thanks for your great job! I really like this module and it's good to know that people like you help others with knowledge and time.

 
jnash
jnash's picture

Joined: 2004-08-02
Posts: 814
Posted: Wed, 2013-05-22 05:34

Hmmm, I did make some changes to the script and uploaded a new version to the codex site.

I think if you drop both ratings and ratables and delete the row for the ratings module in the modules table, then reinstall, it may be working right...

I had to do this as I messed up my versioning and stuff, then realized that the activate and install functions were sort off poorly done.

I -seem- to have it working after redoing all this. The script was a real hack, and I'm not a coder truly! (As you may have seen by my coding :) )

Let me know what you come up with after the above suggestions. Unfortunately, doing so will lose any ratings you have stored... but with some clever backing up and restoring the info itself, one -should- be able to 'recreate' the data...