You are here

function userpoints_list_transactions in User Points 7

Same name and namespace in other branches
  1. 7.2 userpoints.pages.inc \userpoints_list_transactions()

Displays a detailed transaction report for an individual user.

Parameters

$account: For which account to display. Defaults to the current user.

1 string reference to 'userpoints_list_transactions'
userpoints_menu in ./userpoints.module
Implements hook_menu().

File

./userpoints.pages.inc, line 15
Menu callbacks for userpoints.module.

Code

function userpoints_list_transactions($form, &$form_state, $account = NULL, $tid = NULL) {

  // If this is an AJAX request, update $_GET['q'] so that table sorting and
  // similar links are using the correct base path.
  if ($_GET['q'] == 'system/ajax') {
    $q = 'myuserpoints';
    if (!empty($account)) {
      $q = 'user/' . $account->uid . '/points';
    }
    $_GET['q'] = $q;
  }
  if (empty($account)) {
    global $user;
    $account = $user;
  }
  $settings = array(
    'show_user' => FALSE,
  );
  $header = userpoints_get_transaction_header($settings);
  $query = db_select('userpoints_txn', 'p')
    ->extend('PagerDefault')
    ->extend('TableSort')
    ->fields('p')
    ->condition('p.uid', $account->uid)
    ->orderByHeader($header)
    ->orderBy('p.txn_id', 'DESC')
    ->limit(variable_get(USERPOINTS_REPORT_LIMIT, 10));
  if (module_exists('taxonomy')) {
    $query
      ->leftJoin('taxonomy_term_data', 't', 'p.tid = t.tid');
  }
  $unapproved_query = db_select('userpoints_txn', 'p')
    ->condition('uid', $account->uid)
    ->condition('status', USERPOINTS_TXN_STATUS_PENDING);
  $unapproved_query
    ->addExpression('SUM(points)');
  $values = userpoints_filter_parse_input($form_state, $tid);
  $active_category = userpoints_filter_query($query, $values);
  userpoints_filter_query($unapproved_query, $values);
  if (isset($active_category)) {
    drupal_set_title(t('!Points for @username (%category category)', userpoints_translation() + array(
      '%category' => $active_category,
      '@username' => format_username($account),
    )), PASS_THROUGH);
    $total_title = t('Total !points (%category category)', userpoints_translation() + array(
      '%category' => $active_category,
    ));
  }
  else {
    drupal_set_title(t('!Points for @username', userpoints_translation() + array(
      '@username' => format_username($account),
    )));
    $total_title = t('Total !points', userpoints_translation());
  }
  $rows = array();
  foreach ($query
    ->execute() as $transaction) {
    $rows[] = userpoints_get_transaction_row($transaction, $settings);
  }

  // Store context in the output array so that modules have access to it.
  $output = array(
    '#account' => $account,
    '#attached' => array(
      'css' => array(
        drupal_get_path('module', 'userpoints') . '/userpoints.css',
      ),
    ),
  );
  $output['form'] = userpoints_filter_form($account, $values);
  $output['list'] = array(
    '#type' => 'container',
    '#id' => 'userpoints_list_wrapper',
  );
  $output['list']['table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('No !Points earned', userpoints_translation()),
    '#weight' => -5,
    '#attributes' => array(
      'class' => array(
        'userpoints-myuserpoints-list',
      ),
    ),
  );
  $output['list']['pager'] = array(
    '#markup' => theme('pager'),
    '#weight' => 0,
  );

  // Fetch pending (not yet approved) points according to the category filter.
  $pending = (int) $unapproved_query
    ->execute()
    ->fetchField();

  // Display both pending and approved points in a simple table.
  $output['list']['summary_table'] = array(
    '#theme' => 'table',
    '#header' => array(
      array(
        'data' => $total_title,
        'colspan' => 2,
      ),
    ),
    '#rows' => array(
      array(
        'data' => array(
          t('Approved !points', userpoints_translation()),
          userpoints_get_current_points($account->uid, isset($values['tid']) ? $values['tid'] : 'all'),
        ),
        'class' => array(
          'userpoints-myuserpoints-total-approved',
        ),
      ),
      array(
        'data' => array(
          t('Pending !points', userpoints_translation()),
          $pending,
        ),
        'class' => array(
          'userpoints-myuserpoints-total-pending',
        ),
      ),
    ),
    '#weight' => 10,
    '#attributes' => array(
      'class' => array(
        'userpoints-myuserpoints-total',
      ),
    ),
  );

  // For simplicity, the generated output is passed to a custom alter function.
  // This would also be possible through hook_page_alter(), but that hook is
  // hard to use.
  drupal_alter('userpoints_list_transactions', $output);
  return $output;
}