You are here

function user_badges_badgelist_form in User Badges 7

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

Form builder; list of badges

1 string reference to 'user_badges_badgelist_form'
user_badges_menu in ./user_badges.module
Implements hook_menu().

File

./user_badges.admin.inc, line 21
@brief User Badges admin functions

Code

function user_badges_badgelist_form($form, &$form_state) {
  $show_users = (bool) variable_get('user_badges_list_show_users', 0);

  // Load the badges that we want to display.
  $header = array(
    'name' => array(
      'data' => t('Name'),
      'field' => 'name',
    ),
    'badge' => array(
      'data' => t('Badge'),
    ),
    'weight' => array(
      'data' => t('Weight'),
      'field' => 'weight',
    ),
    'options' => array(
      'data' => t('Options'),
    ),
  );
  if ($show_users) {
    $header['users'] = array(
      'data' => t('Assigned to'),
    );
  }
  $header['edit'] = array(
    'data' => t('Edit'),
  );
  $header['delete'] = array(
    'data' => t('Delete'),
  );

  // Which will be the default sort order?
  if (variable_get('user_badges_userweight', 0)) {
    $header['weight']['sort'] = 'asc';
  }
  else {
    $header['name']['sort'] = 'asc';
  }
  $form['#header'] = $header;
  $result = db_select('user_badges_badges', 'ubb')
    ->fields('ubb')
    ->extend('TableSort')
    ->orderByHeader($header)
    ->extend('PagerDefault')
    ->limit(50)
    ->execute();

  // Build a table listing the appropriate badges.
  foreach ($result as $badge) {
    $badge->class = 'badge badge-$badge->bid' . drupal_html_class($badge->name);
    $form['name'][$badge->bid] = array(
      '#value' => check_plain($badge->name),
    );
    $form['badge'][$badge->bid] = array(
      '#value' => theme('user_badge', array(
        'badge' => $badge,
      )),
    );
    $form['weight'][$badge->bid] = array(
      '#type' => 'textfield',
      '#size' => 4,
      '#maxlength' => 255,
      '#default_value' => $badge->weight,
    );
    $options = array();
    if ($badge->unhideable) {
      $options[] = t('Cannot be hidden');
    }
    if ($badge->doesnotcounttolimit) {
      $options[] = t('Does not count to limit');
    }
    $form['options'][$badge->bid] = array(
      '#type' => 'value',
      '#value' => implode("<br />", $options),
    );
    if ($show_users) {
      $query = db_select('user_badges_user', 'ubu');
      $query
        ->join('users', 'u', 'u.uid = ubu.uid');
      $query
        ->fields('u', array(
        'uid',
        'name',
      ))
        ->condition('ubu.bid', $badge->bid);
      $users = $query
        ->execute();
      $u_list = array();
      foreach ($users as $acct) {
        $u_list[$acct->uid] = l(format_username($acct), "user/{$acct->uid}/badges");
      }
      if (!$u_list) {
        $u_list[] = t('None');
      }
      $form['users'][$badge->bid] = array(
        '#value' => theme('item_list', array(
          'items' => $u_list,
        )),
      );
    }
    $form['edit'][$badge->bid] = array(
      '#value' => l(t('edit'), BASE_ADMIN_PATH . '/edit/' . $badge->bid),
    );
    $form['delete'][$badge->bid] = array(
      '#value' => l(t('delete'), BASE_ADMIN_PATH . '/delete/' . $badge->bid),
    );
  }
  $form['submit'] = array(
    '#submit' => t('Update'),
  );
  $form['pager'] = array(
    '#value' => theme('pager', array(
      'tags' => NULL,
      'element' => 0,
    )),
  );
  return $form;
}