You are here

function userpoints_block in User Points 5.3

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

File

./userpoints.module, line 1381

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 (user_access(USERPOINTS_PERM_VIEW)) {
        if ($delta == -1) {
          $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());
          }
        }
        else {

          //$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 USING (uid)';
          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(
            'title' => t('All users by !points', userpoints_translation()),
          )) . '</div>';
        }

        //if $delta
        $block['subject'] = $title;
        $block['content'] = $content;
        return $block;
      }

    //if user_access()
    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;
      break;
    case 'save':
      variable_set('userpoints_block_up_records_' . $delta, $edit['up_records']);
      break;
  }

  //switch $op
}