Hello. First of all thanks for the awesome module.
I'm using Flag module to flag users. I have about 10 user flags (i.e. Editor, Admin, Critic, Commentator, Guru, etc.).

Tying to create a View to list all the flags user is flagged with and its count.

i.e. ->
Editor x3
Critic x5
Guru x1

Could someone please help!

Comments

mooffie’s picture

Status: Active » Fixed

If I understand correctly, all these flags are global flags. In other words, you want to count the number of editors, the number of gurus, etc.

This could be done very easily and elegantly once we have the #371432: Exposing the flaggings table: Alternative Views support feature.

But it's possible to do this even now (but not very elegantly):

  • You have to use Views 3.x.
  • You start a "User" view. Make it a "Table". Add the user name field.
  • Add all the relevant flag relationships to the view. But tick off the "Include only flagged content" checkbox. And choose "by Any user".
  • Add the "Flags: Flagged time" field for each relationship.
  • The result will be a nice "spreadsheet" (or "matrix"): one row for each user, one column for each "role" the user has. A cell will be filled (with a timestamp) if the user has the role, empty otherwise.
  • The next step is to "squash" the rows: Tell view to "Use grouping", and to "Count" all the timestamps. (This feature requires Views 3.x.)

This view isn't complicated, but the average person is likely to omit some detail(s). So here's a ready view you can "Import":

$view = new view;
$view->name = 'stats';
$view->description = '';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'users';
$view->is_cacheable = FALSE;
$view->api_version = 3.0;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Defaults */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->display->display_options['group_by'] = TRUE;
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '15';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['pager']['options']['id'] = '0';
$handler->display->display_options['style_plugin'] = 'table';
/* Relationship: Flags: editor */
$handler->display->display_options['relationships']['flag_content_rel']['id'] = 'flag_content_rel';
$handler->display->display_options['relationships']['flag_content_rel']['table'] = 'users';
$handler->display->display_options['relationships']['flag_content_rel']['field'] = 'flag_content_rel';
$handler->display->display_options['relationships']['flag_content_rel']['label'] = 'flag-edt';
$handler->display->display_options['relationships']['flag_content_rel']['required'] = 0;
$handler->display->display_options['relationships']['flag_content_rel']['flag'] = 'editor';
$handler->display->display_options['relationships']['flag_content_rel']['user_scope'] = 'any';
/* Relationship: Flags: commentator */
$handler->display->display_options['relationships']['flag_content_rel_1']['id'] = 'flag_content_rel_1';
$handler->display->display_options['relationships']['flag_content_rel_1']['table'] = 'users';
$handler->display->display_options['relationships']['flag_content_rel_1']['field'] = 'flag_content_rel';
$handler->display->display_options['relationships']['flag_content_rel_1']['label'] = 'flag-cmt';
$handler->display->display_options['relationships']['flag_content_rel_1']['required'] = 0;
$handler->display->display_options['relationships']['flag_content_rel_1']['flag'] = 'commentator';
$handler->display->display_options['relationships']['flag_content_rel_1']['user_scope'] = 'any';
/* Relationship: Flags: guru */
$handler->display->display_options['relationships']['flag_content_rel_2']['id'] = 'flag_content_rel_2';
$handler->display->display_options['relationships']['flag_content_rel_2']['table'] = 'users';
$handler->display->display_options['relationships']['flag_content_rel_2']['field'] = 'flag_content_rel';
$handler->display->display_options['relationships']['flag_content_rel_2']['label'] = 'flag-gru';
$handler->display->display_options['relationships']['flag_content_rel_2']['required'] = 0;
$handler->display->display_options['relationships']['flag_content_rel_2']['flag'] = 'guru';
$handler->display->display_options['relationships']['flag_content_rel_2']['user_scope'] = 'any';
/* Field: Flags: Flagged time */
$handler->display->display_options['fields']['timestamp']['id'] = 'timestamp';
$handler->display->display_options['fields']['timestamp']['table'] = 'flag_content';
$handler->display->display_options['fields']['timestamp']['field'] = 'timestamp';
$handler->display->display_options['fields']['timestamp']['relationship'] = 'flag_content_rel';
$handler->display->display_options['fields']['timestamp']['group_type'] = 'count';
$handler->display->display_options['fields']['timestamp']['label'] = 'Editors';
/* Field: Flags: Flagged time */
$handler->display->display_options['fields']['timestamp_1']['id'] = 'timestamp_1';
$handler->display->display_options['fields']['timestamp_1']['table'] = 'flag_content';
$handler->display->display_options['fields']['timestamp_1']['field'] = 'timestamp';
$handler->display->display_options['fields']['timestamp_1']['relationship'] = 'flag_content_rel_1';
$handler->display->display_options['fields']['timestamp_1']['group_type'] = 'count';
$handler->display->display_options['fields']['timestamp_1']['label'] = 'Commentators';
/* Field: Flags: Flagged time */
$handler->display->display_options['fields']['timestamp_2']['id'] = 'timestamp_2';
$handler->display->display_options['fields']['timestamp_2']['table'] = 'flag_content';
$handler->display->display_options['fields']['timestamp_2']['field'] = 'timestamp';
$handler->display->display_options['fields']['timestamp_2']['relationship'] = 'flag_content_rel_2';
$handler->display->display_options['fields']['timestamp_2']['group_type'] = 'count';
$handler->display->display_options['fields']['timestamp_2']['label'] = 'Gurus';
/* Field: User: Active */
$handler->display->display_options['fields']['status']['id'] = 'status';
$handler->display->display_options['fields']['status']['table'] = 'users';
$handler->display->display_options['fields']['status']['field'] = 'status';
$handler->display->display_options['fields']['status']['exclude'] = TRUE;
/* Filter: User: Active */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'users';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = '1';

Note the following lines:

$handler->display->display_options['relationships']['flag_content_rel']['flag'] = 'editor';
$handler->display->display_options['relationships']['flag_content_rel_1']['flag'] = 'commentator';
$handler->display->display_options['relationships']['flag_content_rel_2']['flag'] = 'guru';

Change "editor"/"commentator"/"guru" to the machine-names of the flags on your own system. Do this before you import the view.

Make sure to turn on caching for the view (because it isn't efficient).

(Again: #371432: Exposing the flaggings table: Alternative Views support would make things much, much easier. Alternatively, if you know PHP you can use it instead of Views.)

ranavaibhav’s picture

Title: List flags & its count - User falgged with » List flags & its count user is falgged with - Views & Flag

Hello mooffie,
wow! thanks for your time and putting the sample view and information together. really appreciated.
However, it's my bad. I should have been clear and provided more information in my initial post.

RE: If I understand correctly, all these flags are global flags. In other words, you want to count the number of editors, the number of gurus, etc.
Actually, i wanted almost opposite of this. My user flags are not-Global flags. User can be flagged with Guru, Editor, etc flag multiple times. So one user could have 3 Guru flags, 2 Editor flags, etc.
And what i wanted is, on the user profile page, in a block, i wanted to list all the flags user has been flagged with and the total number of each flags.

After leaning from your sample view, it turns out that what i wanted is very very simple. below is the view i came-up with.
In this view, I'm passing User ID as an argument. Added "Flags: User flag counter" relationship for all the flags i wanted and "Flag Count" fields with hidden label and custom prefix.

Now the only problem is that currently I have only handful of user flags, but eventually it will grow to over 50 flags. So i will have to add relationship for each flag and field for each flag. Not sure if #371432: Exposing the flaggings table: Alternative Views support will solve this issue too.

By the way, I'm building Question-Answer site similar to StackOverflow and with flag system I'm trying to achieve badge system they have - http://stackoverflow.com/badges

$view = new view;
$view->name = 'user_badges';
$view->description = '';
$view->tag = 'user.profile';
$view->base_table = 'users';
$view->api_version = '3.0-alpha1';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Defaults */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->display->display_options['use_ajax'] = TRUE;
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '0';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['pager']['options']['id'] = '0';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
$handler->display->display_options['row_options']['inline'] = array(
  'ops' => 'ops',
  'count' => 'count',
);
$handler->display->display_options['row_options']['hide_empty'] = 0;
/* Empty text: Global: Text area */
$handler->display->display_options['empty']['area']['id'] = 'area';
$handler->display->display_options['empty']['area']['table'] = 'views';
$handler->display->display_options['empty']['area']['field'] = 'area';
$handler->display->display_options['empty']['area']['empty'] = FALSE;
$handler->display->display_options['empty']['area']['content'] = 'User has not earn any badge yet.';
$handler->display->display_options['empty']['area']['format'] = '1';
/* Relationship: Flags: badge_editor counter */
$handler->display->display_options['relationships']['flag_count_rel']['id'] = 'flag_count_rel';
$handler->display->display_options['relationships']['flag_count_rel']['table'] = 'users';
$handler->display->display_options['relationships']['flag_count_rel']['field'] = 'flag_count_rel';
$handler->display->display_options['relationships']['flag_count_rel']['label'] = 'counter_editor';
$handler->display->display_options['relationships']['flag_count_rel']['required'] = 0;
/* Relationship: Flags: badge_critic counter */
$handler->display->display_options['relationships']['flag_count_rel_1']['id'] = 'flag_count_rel_1';
$handler->display->display_options['relationships']['flag_count_rel_1']['table'] = 'users';
$handler->display->display_options['relationships']['flag_count_rel_1']['field'] = 'flag_count_rel';
$handler->display->display_options['relationships']['flag_count_rel_1']['label'] = 'counter_critic';
$handler->display->display_options['relationships']['flag_count_rel_1']['required'] = 0;
$handler->display->display_options['relationships']['flag_count_rel_1']['flag'] = 'badge_critic';
/* Field: Flags: Flag counter */
$handler->display->display_options['fields']['count']['id'] = 'count';
$handler->display->display_options['fields']['count']['table'] = 'flag_counts';
$handler->display->display_options['fields']['count']['field'] = 'count';
$handler->display->display_options['fields']['count']['relationship'] = 'flag_count_rel';
$handler->display->display_options['fields']['count']['label'] = '';
$handler->display->display_options['fields']['count']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['count']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['count']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['count']['alter']['trim'] = 0;
$handler->display->display_options['fields']['count']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['count']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['count']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['count']['alter']['html'] = 0;
$handler->display->display_options['fields']['count']['hide_empty'] = 1;
$handler->display->display_options['fields']['count']['empty_zero'] = 0;
$handler->display->display_options['fields']['count']['format_plural'] = 0;
$handler->display->display_options['fields']['count']['prefix'] = 'Editor x';
/* Field: Flags: Flag counter */
$handler->display->display_options['fields']['count_1']['id'] = 'count_1';
$handler->display->display_options['fields']['count_1']['table'] = 'flag_counts';
$handler->display->display_options['fields']['count_1']['field'] = 'count';
$handler->display->display_options['fields']['count_1']['relationship'] = 'flag_count_rel_1';
$handler->display->display_options['fields']['count_1']['label'] = '';
$handler->display->display_options['fields']['count_1']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['count_1']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['count_1']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['count_1']['alter']['trim'] = 0;
$handler->display->display_options['fields']['count_1']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['count_1']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['count_1']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['count_1']['alter']['html'] = 0;
$handler->display->display_options['fields']['count_1']['hide_empty'] = 1;
$handler->display->display_options['fields']['count_1']['empty_zero'] = 0;
$handler->display->display_options['fields']['count_1']['format_plural'] = 0;
$handler->display->display_options['fields']['count_1']['prefix'] = 'Critic x';
/* Argument: User: Uid */
$handler->display->display_options['arguments']['uid']['id'] = 'uid';
$handler->display->display_options['arguments']['uid']['table'] = 'users';
$handler->display->display_options['arguments']['uid']['field'] = 'uid';
$handler->display->display_options['arguments']['uid']['default_action'] = 'default';
$handler->display->display_options['arguments']['uid']['style_plugin'] = 'default_summary';
$handler->display->display_options['arguments']['uid']['default_argument_type'] = 'user';
$handler->display->display_options['arguments']['uid']['break_phrase'] = 0;
$handler->display->display_options['arguments']['uid']['not'] = 0;
mooffie’s picture

Status: Fixed » Active

Now the only problem is that currently I have only handful of user flags, but eventually it will grow to over 50 flags. So i will have to add relationship for each flag and field for each flag. Not sure if #371432: Exposing the flaggings table: Alternative Views support will solve this issue too.

It will.

I'll have good news soon.

I'm building Question-Answer site similar to StackOverflow [...] http://stackoverflow.com/badges

Interesting. (It seems the only challenge will be to sort the flags by the titles. I'll have to think about this.)

mooffie’s picture

Status: Active » Closed (duplicate)

(It seems the only challenge will be to sort the flags by the titles. I'll have to think about this.)

It turns out there isn't a challenge here: I had the impression the titles were in a PHP serialized DB column (which makes them unavailable to Views), but they aren't: they are in a normal column. So all is well.

I'm marking this a dup of #371432: Exposing the flaggings table: Alternative Views support.