You are here

file_download_counter.module in File Download 8

Logs and displays content file_download_counter for a site.

File

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

/**
 * @file
 * Logs and displays content file_download_counter for a site.
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function file_download_counter_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.file_download_counter':
      $output = file_get_contents(drupal_get_path('module', 'file_download_counter') . '/README.md');
      return \Drupal::moduleHandler()
        ->moduleExists('markdown') ? \Drupal\Component\Utility\Xss::filterAdmin(\Drupal::moduleHandler()
        ->invoke('markdown', 'filter', [
        'process',
        0,
        -1,
        $output,
      ])) : '<h3>file_download README</h3><pre>' . \Drupal\Component\Utility\Html::escape($output) . '</pre>';
  }
}

/**
 * Implements hook_cron().
 */
function file_download_counter_cron() {
  $file_download_counter_timestamp = \Drupal::state()
    ->get('file_download_counter.day_timestamp') ?: 0;
  if (\Drupal::time()
    ->getRequestTime() - $file_download_counter_timestamp >= 86400) {

    // Reset day counts.
    \Drupal::database()
      ->update('file_download_counter')
      ->fields([
      'daycount' => 0,
    ])
      ->execute();
    \Drupal::state()
      ->set('file_download_counter.day_timestamp', \Drupal::time()
      ->getRequestTime());
  }

  // Calculate the maximum of node views, for node search ranking.
  \Drupal::state()
    ->set('file_download_counter.node_counter_scale', 1.0 / max(1.0, \Drupal::database()
    ->query('SELECT MAX(totalcount) FROM {file_download_counter}')
    ->fetchField()));
}

/**
 * Returns the most viewed content of all time, today, or the last-viewed node.
 *
 * @param string $dbfield
 *   The database field to use, one of:
 *   - 'totalcount': Integer that shows the top viewed content of all time.
 *   - 'daycount': Integer that shows the top viewed content for today.
 *   - 'timestamp': Integer that shows only the last viewed node.
 * @param int $dbrows
 *   The number of rows to be returned.
 *
 * @return SelectQuery|false
 *   A query result containing the node ID, title, user ID that owns the node,
 *   and the username for the selected node(s), or FALSE if the query could not
 *   be executed correctly.
 */
function file_download_counter_title_list($dbfield, $dbrows) {
  if (in_array($dbfield, [
    'totalcount',
    'daycount',
    'timestamp',
  ])) {
    $query = \Drupal::database()
      ->select('file_managed', 'f');
    $query
      ->join('file_usage', 'us', 'us.fid = f.fid');
    $query
      ->join('file_download_counter', 'c', 'f.fid = c.fid');
    $query
      ->join('node_field_revision', 'nr', 'us.id = nr.nid');
    $query
      ->join('users_field_data', 'u', 'nr.uid = u.uid');
    $query
      ->addField('nr', 'nid', 'nid');
    $query
      ->addField('nr', 'title', 'title');
    $query
      ->addField('u', 'uid', 'uid');
    $query
      ->addField('u', 'name', 'name');
    $query
      ->condition($dbfield, 0, '<>');
    $query
      ->condition('nr.status', 1);
    $query
      ->condition('nr.default_langcode', 1);
    return $query
      ->condition('u.default_langcode', 1)
      ->orderBy($dbfield, 'DESC')
      ->range(0, $dbrows)
      ->execute();
  }
  return FALSE;
}

/**
 * Retrieves a node's "view file_download_counter".
 *
 * @param int $nid
 *   The node ID.
 *
 * @return array
 *   An associative array containing:
 *   - totalcount: Integer for the total number of times the node has been
 *     viewed.
 *   - daycount: Integer for the total number of times the node has been viewed
 *     "today". For the daycount to be reset, cron must be enabled.
 *   - timestamp: Integer for the timestamp of when the node was last viewed.
 */
function file_download_counter_get($fid) {
  if ($fid > 0) {

    // Retrieve an array with both totalcount and daycount.
    return \Drupal\core\Database\Database::getConnection('replica')
      ->query('SELECT totalcount, daycount, timestamp FROM {file_download_counter} WHERE fid = :fid', [
      ':fid' => $fid,
    ], [])
      ->fetchAssoc();
  }
}

/**
 * Implements hook_ENTITY_TYPE_predelete() for node entities.
 */
function file_download_counter_file_predelete(EntityInterface $file) {

  // Clean up file_download_counter table when node is deleted.
  \Drupal::database()
    ->delete('file_download_counter')
    ->condition('fid', $file
    ->id())
    ->execute();
}

/**
 * Implements hook_ranking().
 */
function file_download_counter_ranking() {
  if (\Drupal::config('file_download_counter.settings')
    ->get('count_content_views')) {
    return [
      'views' => [
        'title' => t('Number of downloads'),
        'join' => [
          'type' => 'LEFT',
          'table' => 'file_download_counter',
          'alias' => 'file_download_counter',
          'on' => 'file_download_counter.fid = i.fid',
        ],
        // Inverse law that maps the highest view count on the site to 1 and 0
        // to 0. Note that the ROUND here is necessary for PostgreSQL and SQLite
        // in order to ensure that the :file_download_counter_scale argument is treated as
        // a numeric type, because the PostgreSQL PDO driver sometimes puts
        // values in as strings instead of numbers in complex expressions like
        // this.
        'score' => '2.0 - 2.0 / (1.0 + node_counter.totalcount * (ROUND(:file_download_counter_scale, 4)))',
        'arguments' => [
          ':file_download_counter_scale' => \Drupal::state()
            ->get('file_download_counter.node_counter_scale') ?: 0,
        ],
      ],
    ];
  }
}

/**
 * Implements hook_preprocess_HOOK() for block templates.
 */
function file_download_counter_preprocess_block(&$variables) {
  if ($variables['configuration']['provider'] == 'file_download_counter') {
    $variables['attributes']['role'] = 'navigation';
  }
}

/**
 * Implements hook_block_alter().
 *
 * Removes the "popular" block from display if the module is not configured
 * to count content views.
 */
function file_download_counter_block_alter(&$definitions) {
  $file_download_counter_count_content_views = \Drupal::config('file_download_counter.settings')
    ->get('count_content_views');
  if (empty($file_download_counter_count_content_views)) {
    unset($definitions['file_download_counter_popular_block']);
  }
}

/**
 * Public function to increment file counter against fid
 *
 * @param $fid
 */
function file_download_counter_increment_file($fid) {
  \Drupal::database()
    ->merge('file_download_counter')
    ->key('fid', $fid)
    ->fields([
    'daycount' => 1,
    'totalcount' => 1,
    'timestamp' => \Drupal::time()
      ->getRequestTime(),
  ])
    ->expression('daycount', 'daycount + 1')
    ->expression('totalcount', 'totalcount + 1')
    ->execute();
}

Functions

Namesort descending Description
file_download_counter_block_alter Implements hook_block_alter().
file_download_counter_cron Implements hook_cron().
file_download_counter_file_predelete Implements hook_ENTITY_TYPE_predelete() for node entities.
file_download_counter_get Retrieves a node's "view file_download_counter".
file_download_counter_help Implements hook_help().
file_download_counter_increment_file Public function to increment file counter against fid
file_download_counter_preprocess_block Implements hook_preprocess_HOOK() for block templates.
file_download_counter_ranking Implements hook_ranking().
file_download_counter_title_list Returns the most viewed content of all time, today, or the last-viewed node.