The flag_get_user_flag_counts() does not work correctly for anonymous users because it counts all database entries per user id. But the user id of all anonymous users is 0. For anonymous users the sid provided by session_api needs to be considered.
Patch follows ...

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mkalkbrenner’s picture

Added patch.

joachim’s picture

Hmm this makes sense but the problem is that it would be an API Change.

mkalkbrenner’s picture

From my point of view it's rather a bug fix than an API change. The flag module itself does not use this call. It's only part of flag's API to be used by different modules (or rules). Therefor it should deliver what is documented: "Get the user's flag count."
Currently it's like "Get the authenticated user's flag count or the sum of the flag counts of all anonymous users." That second one doesn't make sense.

ciss’s picture

I agree that this rather seems to be a bug than a feature. While user 0 is technically a user, I can't think of a common use case where counting the flags of all anonymous users vs the current anonymous user is preferrable.
If the API change (and I agree that it is one, since it changes the function result) should turn out to be a blocker on this issue, maybe we can introduce a third argument like $count_anon_by_session (defaulting to false)?

Btw, patch works well for Flag 3.5 + 9 commits (revision c0f427e).

joachim’s picture

joachim’s picture

Status: Needs review » Needs work

> Therefor it should deliver what is documented: "Get the user's flag count."
Currently it's like "Get the authenticated user's flag count or the sum of the flag counts of all anonymous users." That second one doesn't make sens

On the one hand, I see what you mean. On the other, 'the anonymous user', uid 0, is a single Drupal user, and so in that sense we are doing what the documentation says.

Thing is, we've no idea what people are using it for in their custom code.

I think the best way to tackle this is to add an optional parameter to the function, eg $use_anon_sid, so we don't change existing behaviour.

gaurav.bajpai’s picture

Assigned: Unassigned » gaurav.bajpai
gaurav.bajpai’s picture

Assigned: gaurav.bajpai » Unassigned
Status: Needs work » Needs review
FileSize
1.58 KB

Hi All,

Please check attached patch with optional parameter to the function.

Thanks,
Gaurav

Status: Needs review » Needs work

The last submitted patch, 8: flag-anonymous_users_count-2232333-8.patch, failed testing.

gaurav.bajpai’s picture

Assigned: Unassigned » gaurav.bajpai
gaurav.bajpai’s picture

Assigned: gaurav.bajpai » Unassigned
Status: Needs work » Needs review
FileSize
1.59 KB

Attached wrong file.

joachim’s picture

Status: Needs review » Needs work

Thanks for working on this. Looks pretty good, just a couple of things:

+++ b/flag.module
@@ -1770,20 +1770,22 @@ function flag_get_entity_flag_counts($flag, $entity_type) {
+function flag_get_user_flag_counts($flag, $user, $use_anon_sid = NULL) {

New param needs documentation.

Also, this is a boolean, so default should be FALSE.

gaurav.bajpai’s picture

Assigned: Unassigned » gaurav.bajpai
gaurav.bajpai’s picture

Assigned: gaurav.bajpai » Unassigned
Status: Needs work » Needs review
FileSize
1.76 KB

Hi joachim,

Please find updated patch with given suggestions.

Thanks,
Gaurav