You are here

function user_badges_block_view in User Badges 7

Same name and namespace in other branches
  1. 6.2 user_badges.module \user_badges_block_view()
  2. 6 user_badges.module \user_badges_block_view()
  3. 7.2 user_badges.module \user_badges_block_view()
  4. 7.3 user_badges.module \user_badges_block_view()

Implements hook_block_view().

File

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

Code

function user_badges_block_view($delta = 0) {
  $block = array();
  switch ($delta) {
    case 'current_node':
      $arg2 = arg(2);
      if (arg(0) == 'node' && is_numeric(arg(1)) && empty($arg2)) {
        $node = menu_get_object();

        // Make sure we actually got a node.
        if (!$node || !is_object($node)) {
          watchdog('user_badges', 'Block failed to get proper node, got: @type.', array(
            '@type' => gettype($node),
          ), WATCHDOG_WARNING);
          break;
        }
        if (in_array($node->type, variable_get('user_badges_current_node_types', array()))) {
          $account = user_load($node->uid);
          $result = isset($account->badges) ? $account->badges : array();
          $images = array();
          foreach ($result as $badge) {
            $images[] = theme('user_badge', array(
              'badge' => $badge,
            ));
          }
          $block['subject'] = t("@name's Badges", array(
            '@name' => $node->name,
          ));
          $block['content'] = theme('user_badge_group', array(
            'badgeimages' => $images,
          ));
        }
      }
      break;
    case 'my_badges':
      global $user;
      if (!isset($user->badges_all)) {
        $user = user_load($user->uid);
      }
      if (variable_get('user_badges_my_badges_limit', 1)) {
        $result = $user->badges;
      }
      else {
        $result = $user->badges_all;
      }
      $images = array();
      foreach ($result as $badge) {
        $images[] = theme('user_badge', array(
          'badge' => $badge,
        ));
      }
      $block['subject'] = t('My Badges');
      $block['content'] = theme('user_badge_group', array(
        'badgeimages' => $images,
      ));
      break;
    case 'all_badges':
      $result = db_select('user_badges_badges', 'ubb')
        ->fields('ubb')
        ->orderBy('ubb.weight', 'asc')
        ->orderBy('ubb.name', 'asc')
        ->execute();
      $items = array();
      foreach ($result as $badge) {
        $items[] = theme('user_badge', array(
          'badge' => $badge,
        )) . ' <span class="all-badges-name">' . check_plain($badge->name) . '</span>';
      }
      $block['content'] = theme('item_list', array(
        'items' => $items,
      ));
      break;
  }
  return $block;
}