You are here

function user_badges_get_badges in User Badges 7.2

Same name and namespace in other branches
  1. 5 user_badges.module \user_badges_get_badges()
  2. 6.2 user_badges.module \user_badges_get_badges()
  3. 6 user_badges.module \user_badges_get_badges()
  4. 7.4 user_badges.module \user_badges_get_badges()
  5. 7 user_badges.module \user_badges_get_badges()
  6. 7.3 user_badges.module \user_badges_get_badges()

Return array of user badges where keys are badge ids (bid) and values are object containing badge info.

Parameters

$uid: if $uid is a user id, returns badges for that user. if $uid is 'all', returns all badges. if $uid is 'select', returns badges for form_select options.

$options array of options.: $options['nolimit'] : if TRUE, the limit clause will not be applied for a user returned values for 'select' are just badge names.

9 calls to user_badges_get_badges()
user_badges_add_badge_action in ./user_badges.actions.inc
Implementsa Drupal action. Adds a badge to the current user.
user_badges_add_badge_action_form in ./user_badges.actions.inc
@todo Please document this function.
user_badges_for_uid in ./user_badges.module
Returns HTML representation of user badges for given uid
user_badges_remove_badge_action in ./user_badges.actions.inc
Implementsa Drupal action. Removes a badge to the current user.
user_badges_remove_badge_action_form in ./user_badges.actions.inc
@todo Please document this function.

... See full list

File

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

Code

function user_badges_get_badges($uid, $options = array()) {
  static $badges = array(), $past_uid, $past_options;
  $defaults = array(
    'nolimit' => FALSE,
  );
  $options = array_merge($defaults, $options);
  if (isset($badges[$uid])) {
    return $badges[$uid];
  }

  // Do this so we don't return NULL.
  $badges[$uid] = array();
  if (empty($past_uid) || $past_uid !== $uid || $past_options !== $options) {
    $past_uid = $uid;
    if ($uid == 'all' || $uid == 'select') {
      $sql = db_query('SELECT b.bid, b.weight, b.name, b.image, b.href,
        b.unhideable, b.fixedweight, b.doesnotcounttolimit, b.tid
        FROM {user_badges_badges} b
        ORDER BY b.weight, b.name');
    }
    else {
      $usr = db_query('SELECT COUNT(uid) FROM {users} WHERE uid = :uid AND status = :status', array(
        ':uid' => $uid,
        ':status' => 0,
      ))
        ->fetchField();
      if ($usr && variable_get('user_badges_showblocked', 0)) {
        $sql = db_query('SELECT DISTINCT b.bid, b.weight, b.name, b.image, b.href,
          b.unhideable, b.fixedweight, b.doesnotcounttolimit, u.userweight, b.tid,
          CASE WHEN b.fixedweight = 1 THEN b.weight ELSE COALESCE(u.userweight,b.weight) END coalescedweight
          FROM {user_badges_badges} b
            INNER JOIN {user_badges_user} u ON b.bid = u.bid
            INNER JOIN {user_badges_roles} r ON b.bid = r.bid
          WHERE u.uid = :uid AND r.rid = :rid
          ORDER BY coalescedweight, b.name', array(
          ':uid' => $uid,
          ':rid' => 0,
        ));
      }
      else {
        $sql = db_query('SELECT DISTINCT b.bid, b.weight, b.name, b.image, b.href,
          b.unhideable, b.fixedweight, b.doesnotcounttolimit, u.userweight, b.tid,
          CASE WHEN b.fixedweight = 1 THEN b.weight ELSE COALESCE(u.userweight,b.weight) END coalescedweight
          FROM {user_badges_badges} b
            INNER JOIN {user_badges_user} u ON b.bid = u.bid
          WHERE u.uid = :uid
          ORDER BY coalescedweight, b.name
          ', array(
          ':uid' => $uid,
        ));
      }
    }

    // Should we limit the badges returned?
    if (!$options['nolimit'] && variable_get('user_badges_showone', 0)) {
      $limit = variable_get('user_badges_showone', 1);
    }
    else {

      // Set to -1 for no limit.
      $limit = -1;
    }
    foreach ($sql as $badge) {

      // Display the badge if there's no limit or if the badge is unhideable or if we are within our limit.
      if ($limit != 0 || $badge->unhideable == 1) {
        if ($uid == 'select') {
          $badges[$uid][$badge->bid] = $badge->name;
          $badges[$badge->bid]['#attributes'] = array(
            'class' => array(
              "badge badge-{$badge->bid} " . drupal_html_class($badge->name),
            ),
          );
        }
        else {
          $badges[$uid][$badge->bid] = $badge;
          $badges[$uid][$badge->bid]->class = "badge badge-{$badge->bid} " . drupal_html_class($badge->name);
        }

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