You are here

function mostpopular_drupal_refresh_commented in Drupal Most Popular 7

Implements the 'refresh_delta' callback for the Drupal Commented 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 107
This module uses the Drupal statistics module to provide Most Popular data.

Code

function mostpopular_drupal_refresh_commented($service, $block, $span, $last_run) {
  $ts = time() - $span;
  $query = db_select('node', 'n');
  $query
    ->innerJoin('comments', 'c', 'n.nid = c.nid');
  $query
    ->addExpression('COUNT(c.cid)', 'count');
  $query
    ->distinct()
    ->fields('n', array(
    'nid',
    'title',
  ))
    ->condition('n.status', 1)
    ->condition('c.timestamp', $ts, '>=')
    ->groupBy('n.nid')
    ->orderBy('count', 'DESC')
    ->orderBy('n.created', 'DESC');
  $out = array();
  $rows = $query
    ->execute();
  foreach ($rows as $row) {
    $out[] = array(
      'entity_type' => 'node',
      'entity_id' => $row->nid,
      'count' => $row->count,
    );
  }
  return $out;
}