You are here

function userpoints_list_my_userpoints in User Points 6

1 string reference to 'userpoints_list_my_userpoints'
userpoints_menu in ./userpoints.module
Implementation of hook_menu().

File

./userpoints.module, line 1685

Code

function userpoints_list_my_userpoints($points_user) {
  $overall_total = 0;
  $unapproved_total = 0;
  $approved_total = 0;
  $title = t('!Points for ', userpoints_translation()) . check_plain($points_user->name);
  drupal_set_title($title);

  //Grab all tids for this user
  $sql = "SELECT p.tid, t.name\n    FROM {userpoints_txn} p\n    LEFT JOIN {term_data} t on p.tid = t.tid\n    WHERE uid = %d\n    GROUP BY p.tid, t.name";
  $results = db_query($sql, $points_user->uid);
  $grand_total = 0;
  while ($result = db_fetch_array($results)) {
    if ($result['name'] == NULL) {
      $result['name'] = t('!Uncategorized', userpoints_translation());
    }

    //pull the sum from the caching table for resource reason and b/c the
    $result['total'] = userpoints_get_current_points($points_user->uid, $result['tid']);
    $args['subtotals'][$result['tid']] = $result;

    //maintain a grand total
    $grand_total += $result['total'];
  }
  $args['approved_total'] = $grand_total;

  //Grab the unmoderated point total
  $args['unapproved_total'] = (int) db_result(db_query("SELECT SUM(points) FROM {userpoints_txn} WHERE uid = %d AND status = 1", $points_user->uid));
  $args['overall_total'] = $args['approved_total'] + $args['unapproved_total'];
  $header = array(
    array(
      'data' => t('!Points', userpoints_translation()),
      'field' => 'points',
    ),
    array(
      'data' => t('Approved?'),
      'field' => 'status',
    ),
    array(
      'data' => t('Date'),
      'field' => 'time_stamp',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Operation'),
      'field' => 'operation',
    ),
    array(
      'data' => t('Category'),
      'field' => 'cat',
    ),
    array(
      'data' => t('Description'),
      'field' => 'description',
    ),
  );
  $sql = "SELECT p.points, p.time_stamp, p.operation, p.description, p.status, p.tid, t.name as cat , p.entity_id, p.entity_type\n           FROM {userpoints_txn} p\n           LEFT JOIN {term_data} t on p.tid = t.tid\n           WHERE p.uid = %d";
  $sql .= tablesort_sql($header);
  $pager_limit = variable_get(USERPOINTS_REPORT_LIMIT, 10);
  $result = pager_query($sql, $pager_limit, 0, NULL, $points_user->uid);
  $stati = userpoints_txn_status();
  $num_rows = 0;
  $rows = array();
  while ($row = db_fetch_object($result)) {
    $num_rows++;
    $status = $stati[$row->status];
    if (!$row->cat) {
      $row->cat = t('!Uncategorized', userpoints_translation());
    }
    if ($row->description) {
      $description = filter_xss($row->description);
    }
    else {
      $description = t('None');
    }
    $operations = module_invoke_all('userpoints', 'entity_type', $row);
    if (is_array($operations) && isset($operations[0])) {
      $operation = $operations[0];
    }
    else {
      switch ($row->entity_type) {
        case 'node':
          $node = node_load($row->entity_id);
          if ($node) {
            $operation = l($row->operation, 'node/' . $node->nid, array(
              'attributes' => array(
                'title' => $node->title,
              ),
            ));
          }
          else {
            $operation = check_plain($row->operation);
          }
          break;
        case 'comment':
          if (module_exists('comment')) {

            //We have to load the comment to get the nid for the comment
            $comment = _comment_load($row->entity_id);
            if ($comment) {
              $operation = l($row->operation, 'node/' . $comment->nid, array(
                'attributes' => array(
                  'title' => $comment->subject,
                ),
                'fragment' => 'comment-' . $comment->cid,
              ));
            }
            else {
              $operation = check_plain($row->operation);
            }
          }
          break;
        case 'user':
          $operation = l($row->operation, 'user/' . $row->entity_id);
          break;
        default:
          $operation = check_plain($row->operation);
      }
    }
    $rows[] = array(
      array(
        'data' => $row->points,
        'align' => 'center',
      ),
      array(
        'data' => $status,
        'align' => 'center',
      ),
      array(
        'data' => format_date($row->time_stamp, 'small'),
        'align' => 'center',
      ),
      array(
        'data' => $operation,
      ),
      array(
        'data' => $row->cat,
      ),
      array(
        'data' => $description,
      ),
    );
    if ($num_rows <= 0) {
      $rows[] = array(
        array(
          'data' => t('No !Points earned', userpoints_translation()),
          'colspan' => 5,
          'align' => 'center',
        ),
      );
    }
  }
  return theme('userpoints_list_my_userpoints', $args, $header, $rows);
}