How to setup a block showing the "top ten" users who flagged the most items (Views 3.x)
Showing the "top ten" flagged nodes is easy (see the "Counters" section in the guide). On the other hand, showing the "top ten" flagging users is a different story, because this statistics is not immediately available in the database. Fortunately, Views is powerful enough to let us generate this kind of list as well:
Instructions for Views 2.x users
If you're using Views 2.x, you'll find instructions here.
Instructions for Views 3.x users
In the following recipe we take advantage of the new Grouping feature in Views 3.x:
- Start a user-type view (as opposed to a node-type view).
- You're starting with "Style: Unformatted". Change it to "Style: Table".
This step isn't required, but a table makes it easier to see what's going on.
- Add the "User: Name" field.
- Bring in the "Flags: User's flagged content" relationship.
This step links each user with all the nodes she has flagged, and the consequence is that each user is now listed multiple times: as the number of nodes she has flagged. In the next few steps we will "squash" (or "summarize", or "group") these duplicate records.
- Add the "User: Uid" field.
(You'll eventually see that we can actually pick almost any other user field here; we're using the field for counting and its contents doesn't interest us.)
- Click the "Use aggregation" setting (under "Advanced settings", of the view itself) and tick the "Aggregate" checkbox. (And click the "Apply" button.)
If you don't have this setting, you aren't using Views 3.x.
- Click the little gear icon (see pic) to the right of the "User: Uid" field. At the "Group type" listbox choose "Count". (And click the "Update" button.)
We've just asked Views to "squash" and count the number of duplicate rows. You'll notice that there are no duplicate records now and that instead of user IDs we see the number of times each user has flagged content. We're almost done!
- We want to sort the records by the counts. Click the "+" button in the "Sort criteria" box and add the "User: Uid" sort. You'll be directed to a "Group type" listbox (as you encountered in step 7), and here too choose "Count". (And click the "Update" button.) You'll want to sort it in descending order.
- You're done. You'll want to assign the view some URL, or make it a block. You'll also want to limit the view to, say, only 10 records. Don't forget to Save the view.
Caching
The SQL query executed for this view is an expensive one. So tweak the Caching setting of your view to your liking (e.g, cache the view for a day). You may want to add a footer message informing your users that "The list is updated daily" so they won't think your site is broken.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion