You are here

function user_badges_user_load in User Badges 7.2

Same name and namespace in other branches
  1. 7 user_badges.module \user_badges_user_load()
  2. 7.3 user_badges.module \user_badges_user_load()

Implements hook_user_load().

Gets all badges for the user and the limited badges.

File

./user_badges.module, line 241
@brief User Badges module file

Code

function user_badges_user_load($users) {
  static $badges = array();
  foreach ($users as $uid => $account) {

    // Only handle authenticated users.
    if ($uid > 0) {

      // Have we loaded this user before?
      if (isset($badges[$uid])) {
        $users[$uid]->badges = $badges[$uid];
      }

      // Get all user badges for this user, regardless of
      // whether we filter the ones we show.
      // @TODO: Is this the right function to use? Certainly nolimit is wrong.
      // @TODO: This also doesn't get role badges.
      $badges_all = user_badges_get_badges($uid, array(
        'nolimit' => TRUE,
      ));
      $users[$uid]->badges_all = $badges_all;
      $users[$uid]->badges_count = count($badges_all);
      $badges[$uid] = array();

      // Display the badge if there's no limit
      // or if the badge is unhideable
      // or if we are within our limit.
      if ($limit = variable_get('user_badges_showone', 0)) {

        // Loop through all potential badges and get the ones we can show.
        foreach ($badges_all as $bid => $badge) {
          $badge->class = "badge  badge-{$bid} " . drupal_html_class($badge->name);
          if ($limit > 0 || $badge->unhideable == 1) {
            $badges[$uid][$bid] = $badge;

            // Count down our limit, unless the badge doesn't count towards it.
            if (!$badge->doesnotcounttolimit) {
              $limit--;
            }
          }
        }
        $users[$uid]->badges = $badges[$uid];
      }
      else {
        $users[$uid]->badges = $badges_all;
      }
    }
  }
}