My goal was to create a view that present to a user only content he never voted for.
To achieve that i have create a Filter: “VotingAPI percent vote user” -> “has not been voted by” -> “currently loged in user”, which works fine for registered users.
In order for it to work for anonymous users I have patched votingapi.module so it would record anonymous users ip's:
@@ -92,8 +92,18 @@ function votingapi_set_vote($content_typ
if (!isset($vote->tag)) {
$vote->tag = VOTINGAPI_VALUE_DEFAULT_TAG;
}
+
+ $hostname = $_SERVER['REMOTE_ADDR'];
+ if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
+ $hostname .= '-' . $_SERVER['HTTP_X_FORWARDED_FOR'];
+ }
+
+ if ($uid) { //registered user - check if vote exists from same user account
+ $result = db_query("SELECT * FROM {votingapi_vote} WHERE content_type='%s' AND content_id=%d AND tag='%s' AND value_type='%s' AND uid=%d", $content_type, $content_id, $vote->tag, $vote->value_type, $uid);
+ } else { //anonymous user - check if vote exists from anonymous user with same IP
+ $result = db_query("SELECT * FROM {votingapi_vote} WHERE content_type='%s' AND content_id=%d AND tag='%s' AND value_type='%s' AND uid=%d AND hostname='%s'", $content_type, $content_id, $vote->tag, $vote->value_type, $uid, $hostname);
+ }
- $result = db_query("SELECT * FROM {votingapi_vote} WHERE content_type='%s' AND content_id=%d AND tag='%s' AND value_type='%s' AND uid=%d", $content_type, $content_id, $vote->tag, $vote->value_type, $uid);
while ($vobj = db_fetch_object($result)) {
votingapi_change_vote($vobj, $vote->value);
$exists = TRUE;
I have also patched votingapi_views.inc so the filter checks the anonymous user ip before presenting content :
@@ -187,9 +187,17 @@
'content_type' => $filterinfo['content_type'],
'tag' => $filterinfo['tag'],
);
+ global $user;
if ($filter['value'] != '***ANY_USER***') {
$joininfo['extra']['uid'] = $filter['value'];
+ if ($user->uid == '0') {
+ $hostname = $_SERVER['REMOTE_ADDR'];
+ if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
+ $hostname .= '-' . $_SERVER['HTTP_X_FORWARDED_FOR'];
+ }
+ $joininfo['extra']['hostname'] = $hostname;
+ }
}
$tblnum = $query->add_table('votingapi_vote', false, 1, $joininfo);
The resault is that the filter treat all anonymous users as one and show only content that wasn't voted by any of the the anonymous users.
Any idea what's wrong?
| Comment | File | Size | Author |
|---|---|---|---|
| votingapi.module_2.patch | 1.35 KB | gabash | |
| votingapi_views.diff | 770 bytes | gabash |
Comments
Comment #1
eaton commentedViews integration in VotingAPI 5.x is fundamentally broken -- it works for a small number of cases but more complex cases like this are not supported, and this isn't being developed further. Views support was rewriten in the 2.x branch to fix these issues. If anyone wants to take a stab at rewriting the Views integration for D5, they're welcome to, but it's not on my radar at present.