You are here

function userpoints_block_view in User Points 7

Same name and namespace in other branches
  1. 7.2 userpoints.module \userpoints_block_view()

Implements hook_block_view().

File

./userpoints.module, line 1444

Code

function userpoints_block_view($delta) {
  global $user;
  if ($delta == -1 && (user_access('view userpoints') || user_access('view own userpoints'))) {
    $title = t('My !points balance', userpoints_translation());
    if ($user->uid) {
      $content = userpoints_get_points_list();
    }
    else {
      $content = t('!Points are visible to logged in users only', userpoints_translation());
    }
  }
  elseif (user_access('view userpoints')) {

    // $delta is our tid for pulling the points.
    // If 0 we pull 0 or NULL.
    $title = t('Highest Users');
    $query = db_select('userpoints', 'p')
      ->fields('p', array(
      'uid',
      'points',
    ))
      ->orderBy('p.points', 'DESC')
      ->range(0, variable_get('userpoints_block_up_records_' . $delta, 5));
    if ($delta == 0) {
      $query
        ->condition(db_or()
        ->condition('p.tid', 0)
        ->isNull('p.tid'));
    }
    else {
      $query
        ->condition('p.tid', $delta);
    }

    // Exclude blocked users.
    $query
      ->join('users', 'u', 'u.uid = p.uid AND u.status = 1');
    $rows = array();
    foreach ($query
      ->execute() as $data) {
      $rows[] = array(
        array(
          'data' => theme('username', array(
            'account' => user_load($data->uid),
          )),
        ),
        array(
          'data' => $data->points,
          'align' => 'right',
        ),
      );
    }
    $header = array(
      t('User'),
      t('!Points', userpoints_translation()),
    );
    $content = theme('table', array(
      'header' => $header,
      'rows' => $rows,
    ));
    $content .= '<div class="more-link">' . l(t('more'), 'userpoints/' . $delta, array(
      'attributes' => array(
        'title' => t('All users by !points', userpoints_translation()),
      ),
    )) . '</div>';
  }
  if (!empty($title) && !empty($content)) {
    $block['subject'] = $title;
    $block['content'] = $content;
    return $block;
  }
}