You are here

function userpoints_block in User Points 5

Same name and namespace in other branches
  1. 5.3 userpoints.module \userpoints_block()
  2. 5.2 userpoints.module \userpoints_block()
  3. 6 userpoints.module \userpoints_block()

File

./userpoints.module, line 638

Code

function userpoints_block($op = 'list', $delta = 0, $edit = array()) {
  global $user;
  $num = 5;
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = t('User\'s !points', userpoints_translation());
      $blocks[1]['info'] = t('Highest Users');
      return $blocks;
    case 'view':
      if (user_access(USERPOINTS_PERM_VIEW)) {
        switch ($delta) {
          case 0:
            $title = t('@user\'s !points', array_merge(array(
              '@user' => $user->name,
            ), userpoints_translation()));
            if ($user->uid) {
              $points = (int) db_result(db_query('SELECT points FROM {userpoints} WHERE uid = %d', $user->uid));
              $show_points = format_plural($points, t('!point', userpoints_translation()), t('!points', userpoints_translation()));
              $content = t('You have %p %c', array(
                '%p' => $points,
                '%c' => $show_points,
              ));
            }
            else {
              $content = t('!Points are visible to logged in users only', userpoints_translation());
            }
            break;
          case 1:
            $title = t('Highest Users');
            $result = db_query_range('SELECT p.uid, u.name, p.points
              FROM {userpoints} p INNER JOIN {users} u USING (uid)
              ORDER BY p.points DESC', 0, $num);
            while ($data = db_fetch_object($result)) {
              $rows[] = array(
                array(
                  'data' => theme('username', $data),
                ),
                array(
                  'data' => $data->points,
                  'align' => 'right',
                ),
              );
            }
            $header = array(
              t('User'),
              t('!Points', userpoints_translation()),
            );
            $content = theme('table', $header, $rows);
            $content .= '<div class="more-link">' . l(t('more'), 'userpoints', array(
              'title' => t('All users by !points', userpoints_translation()),
            )) . '</div>';
            break;
        }
        $block['subject'] = $title;
        $block['content'] = $content;
        return $block;
      }
  }
}