You are here

function mostpopular_drupal_refresh_viewed in Drupal Most Popular 7

Implements the 'refresh_delta' callback for the Drupal Viewed service.

Parameters

object $service The service definition.:

object $block The block definition. :

integer $span The number of seconds over which to search.:

integer $last_run the timestamp of the last time this service was run.:

File

modules/mostpopular_drupal/mostpopular_drupal.module, line 50
This module uses the Drupal statistics module to provide Most Popular data.

Code

function mostpopular_drupal_refresh_viewed($service, $block, $span, $last_run) {
  $ts = time() - $span;

  // This query is borrowed from the Hall of Fame module. However, it does not fetch
  // any nodes which were published a while ago and are only now becoming popular.
  // Use the Google Analytics mostpopular module instead for better results.
  $query = db_select('node', 'n');
  $query
    ->innerJoin('node_counter', 'c', 'n.nid = c.nid');
  $query
    ->distinct()
    ->fields('n', array(
    'nid',
    'title',
  ))
    ->condition('n.status', 1)
    ->condition('n.title', '%page not found%', 'not like')
    ->range(0, $block->count);

  // If we are looking at a single day, get the day stats
  if ($span <= 60 * 60 * 24) {
    $query
      ->addField('c', 'daycount', 'count');
    $query
      ->condition('c.daycount', 0, '>')
      ->orderBy('c.daycount', 'DESC');
  }
  else {
    $query
      ->addField('c', 'totalcount', 'count');
    $query
      ->condition('n.created', $ts, '>=')
      ->condition('c.totalcount', 0, '>')
      ->orderBy('c.totalcount', 'DESC')
      ->orderBy('n.created', 'DESC');
  }

  // If the service has restricted content types, only include those.
  if (!empty($service->data['entity_types']['node'])) {
    $bundles = $service->data['entity_types']['node'];
    $query
      ->condition('n.type', $bundles, 'IN');
  }
  $out = array();
  $rows = $query
    ->execute();
  foreach ($rows as $row) {
    $node_stats = new stdClass();
    $node_stats->entity_type = 'node';
    $node_stats->entity_id = $row->nid;
    $node_stats->count = $row->count;
    $out[] = $node_stats;
  }
  return $out;
}