You are here

function user_badges_get_badges in User Badges 7.3

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.2 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.

11 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 924
@brief User Badges module file

Code

function user_badges_get_badges($uid, $options = array()) {
  static $badges = array(), $past_uid;
  $defaults = array(
    'nolimit' => FALSE,
  );
  $options = array_merge($defaults, $options);

  // Sort and turn $options into a string, so that we can check if the same options are in the cache, and if so, just return that
  ksort($options);
  $stringified_options = json_encode($options);

  // If the uid with the same options are already set, then, just return that
  if (isset($badges[$uid][$stringified_options])) {
    return $badges[$uid][$stringified_options];
  }
  if (empty($past_uid) || $past_uid !== $uid || !isset($badges[$uid][$stringified_options])) {

    // Do this so we don't return NULL.
    $badges[$uid][$stringified_options] = array();
    $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, u.uid, b.weight, b.name, b.image, b.href,
          b.unhideable, b.fixedweight, b.doesnotcounttolimit, u.userweight, b.tid, r.rid,
          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
            LEFT 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, u.uid, b.weight, b.name, b.image, b.href,
          b.unhideable, b.fixedweight, b.doesnotcounttolimit, u.userweight, b.tid, r.rid,
          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
            LEFT JOIN {user_badges_roles} r ON b.bid = r.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', 0);
    }
    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][$stringified_options][$badge->bid] = $badge->name;
        }
        else {
          $badges[$uid][$stringified_options][$badge->bid] = $badge;
          $badges[$uid][$stringified_options][$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][$stringified_options];
}