You are here

function newsletter_stats in Newsletter 7

Menu callback; present newsletter statistics.

1 string reference to 'newsletter_stats'
newsletter_menu in ./newsletter.module
Implements hook_menu().

File

includes/newsletter.admin.inc, line 11
Admin page callbacks for the newsletter module.

Code

function newsletter_stats($form, &$form_state) {
  $form['options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Options'),
    '#attributes' => array(
      'class' => array(
        'container-inline',
      ),
    ),
  );
  $options['compare'] = t('Compare the selected newsletters');
  $form['options']['operation'] = array(
    '#type' => 'select',
    '#title' => t('Operation'),
    '#title_display' => 'invisible',
    '#options' => $options,
    '#default_value' => 'compare',
  );
  $form['options']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update'),
  );
  $header = array(
    'id' => array(
      'data' => t('ID'),
      'field' => 'nnid',
    ),
    'title' => array(
      'data' => t('Newsletter/List Title'),
      'field' => 'title',
    ),
    'last_sent' => array(
      'data' => t('Last Sent'),
      'field' => 'last_sent',
      'sort' => 'desc',
    ),
    'subscribers_sent' => array(
      'data' => t('E-mails Sent'),
      'field' => 'subscribers_sent',
    ),
    'clicks' => array(
      'data' => t('Total Clicks'),
      'field' => 'clicks',
    ),
    'ctr' => array(
      'data' => t('CTR'),
    ),
    'opened' => array(
      'data' => t('Opened(times)'),
      'field' => 'opens',
    ),
    'open_rate' => array(
      'data' => t('Open Rate'),
    ),
    'send_again' => array(
      'data' => t('To be sent again'),
      'field' => 'send_again',
    ),
  );
  $subquery = db_select('newsletter_newsletter', 's');
  $subquery
    ->fields('s', array(
    'title',
  ));
  $subquery
    ->addExpression('MAX(last_sent)', 'maxts');
  $subquery
    ->groupBy('title');
  $query = db_select('newsletter_newsletter', 't')
    ->extend('PagerDefault')
    ->extend('TableSort');
  $query
    ->fields('t');
  $query
    ->join($subquery, 'grp', 'grp.title = t.title AND grp.maxts = t.last_sent');
  $query
    ->leftjoin('newsletter_list', 'list', 't.title = list.title');
  $query
    ->addField('list', 'send_again', 'send_again');
  $query
    ->condition('t.last_sent', 0, '<>');
  $query
    ->limit(50);
  $query
    ->orderByHeader($header);
  $result = $query
    ->execute();
  $options = array();
  foreach ($result as $row) {
    $send_again_timestamp = strtotime($row->send_again);
    $options[$row->nnid] = array(
      'id' => (int) $row->nnid,
      'title' => check_plain($row->title),
      'last_sent' => format_date($row->last_sent, 'short'),
      'subscribers_sent' => (int) $row->subscribers_sent,
      'clicks' => (int) $row->clicks,
      'ctr' => number_format(@($row->clicks / $row->subscribers_sent) * 100, 2) . '%',
      'opened' => (int) $row->opens,
      'open_rate' => number_format(@($row->opens / $row->subscribers_sent) * 100, 2) . '%',
      'send_again' => is_numeric($send_again_timestamp) ? format_date($send_again_timestamp, 'short') : '-',
    );
  }
  $form['newsletter'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('No newsletters sent yet.'),
  );
  $form['pager'] = array(
    '#theme' => 'pager',
  );
  return $form;
}