You are here

function popup_onload_statistics_get_stats in Popup On Load 8

Same name and namespace in other branches
  1. 7 popup_onload_statistics/popup_onload_statistics.module \popup_onload_statistics_get_stats()

Fetch statistics from the database.

1 call to popup_onload_statistics_get_stats()
popup_onload_statistics_admin_form in modules/popup_onload_statistics/popup_onload_statistics.admin.inc
Callback for the admin report table.

File

modules/popup_onload_statistics/popup_onload_statistics.module, line 100
Main popup on loadvertisement statistics functions.

Code

function popup_onload_statistics_get_stats($date_from = NULL, $date_to = NULL) {
  $params = array();
  $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;
  }

  // TODO: Drupal Rector Notice: Please delete the following comment after you've made any necessary changes.
  // You will need to use `\Drupal\core\Database\Database::getConnection()` if you do not yet have access to the container here.
  $query = \Drupal::database()
    ->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', array(
    'name',
    'popup_id',
  ))
    ->fields('sps', array(
    'atime',
    'type',
    'aid',
  ))
    ->groupBy('popup_id')
    ->groupBy('type')
    ->orderBy('name');
  $result = $query
    ->execute()
    ->fetchAll();
  $stats = array();
  $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;
}