You are here

function userpoints_block in User Points 6

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

File

./userpoints.module, line 1530

Code

function userpoints_block($op = 'list', $delta = 0, $edit = array()) {
  global $user;
  switch ($op) {
    case 'list':
      $blocks[-1]['info'] = t('User\'s !points', userpoints_translation());

      //Grab a list of the available terms
      $terms = userpoints_get_categories();
      foreach ($terms as $key => $value) {
        $blocks[$key]['info'] = t("Highest {$value} !points", userpoints_translation());
      }

      //foreach
      return $blocks;
    case 'view':
      if ($delta == -1 && (user_access(USERPOINTS_PERM_VIEW) || user_access(USERPOINTS_PERM_VIEW_OWN))) {
        $title = t('@user\'s !points', array_merge(array(
          '@user' => $user->name,
        ), userpoints_translation()));
        if ($user->uid) {
          $points = userpoints_get_current_points($user->uid, variable_get(USERPOINTS_CATEGORY_PROFILE_DISPLAY_TID, 0));
          $show_points = format_plural($points, '!point', '!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());
        }
      }
      elseif (user_access(USERPOINTS_PERM_VIEW)) {

        //$delta is our tid for pulling the points

        //if 0 we pull 0 or NULL
        $title = t('Highest Users');
        $num = variable_get('userpoints_block_up_records_' . $delta, 5);
        $sql = 'SELECT p.uid, u.name, p.points FROM {userpoints} p INNER JOIN {users} u ON u.uid = p.uid AND u.status = 1';
        if ($delta == 0) {
          $sql .= ' WHERE p.tid = 0 OR p.tid IS NULL ORDER BY p.points DESC';
          $result = db_query_range($sql, 0, $num);
        }
        else {
          $sql .= ' WHERE p.tid = %d ORDER BY p.points DESC';
          $result = db_query_range($sql, $delta, 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/' . $delta, array(
          'attributes' => array(
            'title' => t('All users by !points', userpoints_translation()),
          ),
        )) . '</div>';
      }

      //if $delta
      $block['subject'] = $title;
      $block['content'] = $content;
      return $block;
    case 'configure':
      $form['up_records'] = array(
        '#type' => 'select',
        '#title' => t('Number of users to display'),
        '#default_value' => variable_get('userpoints_block_up_records_' . $delta, 10),
        '#options' => array(
          1 => 1,
          5 => 5,
          10 => 10,
          15 => 15,
          20 => 20,
          30 => 30,
          40 => 40,
          50 => 50,
          60 => 60,
          70 => 70,
          80 => 80,
          90 => 90,
          100 => 100,
          200 => 200,
        ),
        '#description' => t('Limit the number of users displayed to this value'),
      );
      return $form;
    case 'save':
      variable_set('userpoints_block_up_records_' . $delta, $edit['up_records']);
      break;
  }

  //switch $op
}