You are here

popup_onload_statistics.module in Popup On Load 7

Main popup on loadvertisement statistics functions.

File

popup_onload_statistics/popup_onload_statistics.module
View source
<?php

/**
 * @file
 * Main popup on loadvertisement statistics functions.
 */

/**
 * Implements hook_theme().
 */
function popup_onload_statistics_theme($existing, $type, $theme, $path) {
  $items = [
    'popup_onload_statistics_table' => [
      'variables' => [
        'stats' => NULL,
      ],
      'file' => 'popup_onload_statistics.theme.inc',
    ],
  ];
  return $items;
}

/**
 * Implements hook_entity_delete().
 */
function popup_onload_statistics_entity_delete($entity, $type) {
  if ($type == 'popup_onload') {
    db_delete('popup_onload_statistics')
      ->condition('popup_id', $entity->popup_id)
      ->execute();
  }
}

/**
 * Implements hook_init().
 */
function popup_onload_statistics_init() {

  // Check if popup is added, and thus we need to add statistics JS.
  if (drupal_static(POPUP_ONLOAD_IS_POPUP_ADDED)) {
    popup_onload_statistics_add_js();
  }
}

/**
 * Add popup statistics JS to the page.
 */
function popup_onload_statistics_add_js() {
  drupal_add_js(drupal_get_path('module', 'popup_onload_statistics') . '/popup_onload_statistics.js');
}

/**
 * Implements hook_menu().
 */
function popup_onload_statistics_menu() {
  $items = [];
  $items['popup_onload_stats_log'] = [
    'title' => 'Popup statistics AJAX gate',
    'page callback' => 'popup_onload_statistics_log',
    'access arguments' => [
      'access content',
    ],
    'type' => MENU_CALLBACK,
  ];
  $items['admin/reports/popup-statistics'] = [
    'title' => 'Popup statistics',
    'page callback' => 'drupal_get_form',
    'page arguments' => [
      'popup_onload_statistics_admin_form',
    ],
    'file' => 'popup_onload_statistics.admin.inc',
    'access arguments' => [
      'access site reports',
    ],
    'type' => MENU_NORMAL_ITEM,
  ];
  return $items;
}

/**
 * AJAX gate for popup statistics logging.
 */
function popup_onload_statistics_log() {
  $post = drupal_get_query_parameters($_POST);
  array_map('check_plain', $post);
  $action_type = $post['atype'];
  $popup_id = $post['popup_id'];
  $record = new stdClass();
  $record->type = $action_type;
  $record->popup_id = $popup_id;
  $record->atime = time();
  drupal_write_record('popup_onload_statistics', $record);
  drupal_json_output([
    'status' => 1,
  ]);
}

/**
 * Fetch statistics from the database.
 */
function popup_onload_statistics_get_stats($date_from = NULL, $date_to = NULL) {
  $params = [];
  $date_sql = '';
  if (!is_null($date_from)) {
    $date_sql .= "AND sps.atime >= :date_from ";
    $params[':date_from'] = $date_from;
  }
  if (!is_null($date_to)) {
    $date_sql .= "AND sps.atime <= :date_to ";
    $params[':date_to'] = $date_to;
  }
  $query = db_select('popup_onload', 'p');
  $query
    ->leftJoin('popup_onload_statistics', 'sps', 'sps.popup_id = p.popup_id ' . $date_sql, $params);
  $query
    ->addExpression('COUNT(sps.type)', 'action_count');
  $query
    ->fields('p', [
    'name',
    'popup_id',
  ])
    ->fields('sps', [
    'atime',
    'type',
    'aid',
  ])
    ->groupBy('popup_id')
    ->groupBy('type')
    ->orderBy('name');
  $result = $query
    ->execute()
    ->fetchAll();
  $stats = [];
  $group_id = 0;
  foreach ($result as $row) {
    if ($row->popup_id != $group_id) {
      $group_id = $row->popup_id;
    }
    $stats[$group_id]['popup_id'] = $row->popup_id;
    $stats[$group_id]['name'] = $row->name;
    if ($row->type) {
      $stats[$group_id][$row->type] = $row->action_count;
    }
    else {
      $stats[$group_id]['view'] = 0;
      $stats[$group_id]['click'] = 0;
    }
  }
  return $stats;
}

Functions

Namesort descending Description
popup_onload_statistics_add_js Add popup statistics JS to the page.
popup_onload_statistics_entity_delete Implements hook_entity_delete().
popup_onload_statistics_get_stats Fetch statistics from the database.
popup_onload_statistics_init Implements hook_init().
popup_onload_statistics_log AJAX gate for popup statistics logging.
popup_onload_statistics_menu Implements hook_menu().
popup_onload_statistics_theme Implements hook_theme().