You are here

mostpopular_drupal.module in Drupal Most Popular 7

Same filename and directory in other branches
  1. 6 modules/mostpopular_drupal/mostpopular_drupal.module

This module uses the Drupal statistics module to provide Most Popular data.

File

modules/mostpopular_drupal/mostpopular_drupal.module
View source
<?php

// $Id$

/*
 * Drupal Most Popular - Showcase the most popular content across your Drupal website and engage your audience.
 * Copyright © 2009-2012 New Signature
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * You can contact New Signature by electronic mail at labs@newsignature.com -or- by U.S. Postal Service at 1100 H St. NW, Suite 940, Washington, DC 20005.
 */

/**
 * @file
 * This module uses the Drupal statistics module to provide Most Popular data.
 */

/**
 * Implements hook_mostpopular_service_info().
 *
 * @see hook_mostpopular_service_info()
 */
function mostpopular_drupal_mostpopular_service_info() {
  $info = array();
  if (module_exists('statistics')) {
    $info['viewed'] = array(
      'name' => t('Drupal Most Viewed Nodes'),
      'title' => t('Viewed'),
      'entity_types' => array(
        'node',
      ),
    );
  }
  if (module_exists('comment')) {
    $info['commented'] = array(
      'name' => t('Drupal Most Commented Nodes'),
      'title' => t('Commented'),
      'entity_types' => TRUE,
    );
  }
  return $info;
}

/**
 * Implements the 'refresh_delta' callback for the Drupal Viewed service.
 * 
 * @param object $service The service definition.
 * @param object $block The block definition. 
 * @param integer $span The number of seconds over which to search.
 * @param integer $last_run the timestamp of the last time this service was run.
 */
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;
}

/**
 * Implements the 'refresh_delta' callback for the Drupal Commented service.
 * 
 * @param object $service The service definition.
 * @param object $block The block definition. 
 * @param integer $span The number of seconds over which to search.
 * @param integer $last_run the timestamp of the last time this service was run.
 */
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;
}

/**
 * Implements the 'next_run' callback for the Drupal service.
 * 
 * Always refresh this service immediately, since we don't need to worry about quotas.
 * 
 * @param array $service The service definition.
 * @param integer $span The number of seconds representing the current interval.
 * @param integer $last_run The timestamp at which this service was last run for this interval.
 */
function mostpopular_drupal_next_run($service, $span, $last_run) {
  return time();
}

Functions

Namesort descending Description
mostpopular_drupal_mostpopular_service_info Implements hook_mostpopular_service_info().
mostpopular_drupal_next_run Implements the 'next_run' callback for the Drupal service.
mostpopular_drupal_refresh_commented Implements the 'refresh_delta' callback for the Drupal Commented service.
mostpopular_drupal_refresh_viewed Implements the 'refresh_delta' callback for the Drupal Viewed service.