1

I am using the Message module to produce a user activity feed for my site.

I want to make a view for the front page that shows the latest user activity (the 10 most recent actions for which I have messages configured) like this:

  • Bill updated his profile.
  • Ted updated his profile.
  • Wayne added a picture.
  • Garth sent a message.

Making a view that shows this activity was fairly simple. However, if one user is very active, the results will look like this:

  • Wayne updated his profile.
  • Wayne added a picture.
  • Wayne sent a message.

This doesn't look very good on the front page, and I have many hyperactive users, so what I would like to do is "show the 10 most recent activity messages for 10 unique users." I experimented with the grouping feature of views, but I haven't figured out how to create a combination that allows me to achieve this.

1 Answer 1

2

A dull ax approach would be to create a View that returns 100 of the latest messages, and then using a views_pre_render() hook, go through and grab the messages uniquely per user, eg, something like this:

function YOURMODULE_views_pre_render(&$view) {

  if ($view->name=='THIS_VIEW') {

    $new_result=array();
    $unique_users=array();

    foreach ($view->result as $row) {
      if (!in_array($row->USER_FIELD_IN_VIEW, $unique_users)) {
        $new_result[]=$row
        $unique_users[]=$row->USER_FIELD_IN_VIEW;
        if (count($new_result)==10) {
          break;
        }
      }
    }

    $view->result=$new_result;

  }

  // any other pre_render()ings go here

}

where USER_FIELD_IN_VIEW is the field in your View corresponding to the uid of the owner of the message. A print_r() of $row or $view->result should give you that information. The other kicker is picking a small enough number of messages to give you those 10 users. I just pulled out 100 out as 10 users and 10 messages per "hyperactive" user to make sure you got at least 10 users without getting too many that you just throw away at the end.

EDIT/ADDITION: there were some variable name typos in my code that I've corrected :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.