All pastes #448814 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-04-19 21:50 UTC
rendered paste body
function herfablife_base_posts_page($uid = NULL) {
  $args = array();
  $current_time = time();

  // Handle single user stats.
  if (is_numeric($uid)) {
    $account = user_load(array('uid' => $uid));

    if ($account->uid) {
      $args[] = $uid;
      $uid_where = " WHERE n.uid = %d";
      $output .= '<p><strong>Member:</strong> '. theme('username', $account) .'</p>';
      $cid = 'herfablife_post_stats_for_'. $account->uid;
      $cache = cache_get($cid);
    }
  } else {
    $cid = 'herfablife_post_stats';
    $cache = cache_get($cid);
  }
  
    $output .= $cache->data .'<br />';
    $output .= 'expires on: '. $cache->expire .'<br />';
    $output .= 'current time:'. time() .'<br /><br />';

  // If we find a cached version that has not yet expired.
  if ($cache->data) {
    $output .= 'cached content <br />';
    $cache = $cache->data;
    $stats = unserialize($cache) .'<br />';
  } else {
    cache_set($cid, 'cache', '');
  }

  // If no cache version available.
  if (!$stats) {
    $sql = "SELECT n.nid, n.uid, n.type, n.status FROM {node} n" . $uid_where;
    $result = db_query($sql, $args);
    $stats = array();
    $stats['total'] = db_num_rows($result);

    // Loop through the nodes and add counts where needed.
    while ($row = db_fetch_object($result)) {
      // Create stats array for node type.
      if (empty($stats[$row->type])) {
        $stats[$row->type] = array('published' => 0, 'unpublished' => 0);
      }
      // Add one for this node.
      if ($row->status) {
        $stats[$row->type]['published']++;
        $stats['published']++;
      } else {
        $stats[$row->type]['unpublished']++;
        $stats['unpublished']++;
      }

      // If not a user stats page, collect invidiual user totals.
      if (!$account->uid) {
        if (!$stats['users'][$row->uid]) {
          $stats['users'][$row->uid] = 0;
        }
        $stats['users'][$row->uid]++;
      }
    }

    // Cache results for 5 minutes.
    cache_set($cid, 'cache', serialize($stats), time() + (60 * 5));
  }

  // Total overall.
  $output .= '<div class="item">';
  $output .= '<p>'. t('Total Posts: !t', array('!t' => (int) $stats['total'])) .'</p>';
  $output .= '<div>'. t('Published: !t', array('!t' => (int) $stats['published'])) .'</div>';
  $output .= '<div>'. t('Unpublished: !t', array('!t' => (int) $stats['unpublished'])) .'</div>';
  $output .= '</div>';

  $output .= '<h3>'. t('Post By Type') .'</h3>';

  // Total for each node type.
  foreach ($stats as $type => $row) {
    if (is_array($row)) {
      $output .= '<div class="item">';
      $output .= '  <h3>'. ucwords(str_replace('_', ' ', $type)) .'s</h3>';
      $output .= '  <div>'. t('Published: !t', array('!t' => $row['published'])) .'</div>';
      $output .= '  <div>'. t('Unpublished: !t', array('!t' => $row['unpublished'])) .'</div>';
      $output .= '</div>';
    }
  }

  // Total for each user.
  if (!empty($stats['users'])) {
    $users = array();
    // Sort users by posts DESC.
    //arsort($stats['users']);
    foreach ($stats['users'] as $uid_stat => $uid_total) {
      $stat_account = user_load(array('uid' => $uid_stat));
      $uid_link = l($stat_account->name, 'user/'. $stat_account->uid .'/stats/posts');
      $users[] = t('!u: !t', array('!u' => $uid_link, '!t' => $uid_total));
    }
  }
  if (!empty($users)) {
    $output .= theme('item_list', $users, t('By User'));
  }
  
  $output .= '<p><small>'. t('Please note that this page caches its results for 5 minutes at a time to save load on the server.') .'</small></p>';

  print theme('page', $output);
}