You are here

function simplenews_statistics_admin_form in Simplenews Statistics 6.2

Same name and namespace in other branches
  1. 6 simplenews_statistics.module \simplenews_statistics_admin_form()

Statistics overview

1 string reference to 'simplenews_statistics_admin_form'
simplenews_statistics_menu in ./simplenews_statistics.module
Implementation of hook_menu().

File

./simplenews_statistics.module, line 167
Gathers newsletter statistics.

Code

function simplenews_statistics_admin_form(&$form_state, $action = 'sent') {
  drupal_set_title('Newsletter Statistics');
  $vid = variable_get('simplenews_vid', '');
  $form = array();
  $header = array(
    t('Title'),
    t('Newsletter'),
    t('Date created'),
    t('Send'),
    t('Open rate'),
    t('CTR'),
    t('Details'),
  );
  $sql = "SELECT n.*, s.s_status, s.s_format FROM {node} n INNER JOIN {simplenews_newsletters} s ON n.nid = s.nid WHERE s.s_status > 1 AND s.s_format = 'html' ORDER BY n.created DESC";
  $result = db_query(db_rewrite_sql($sql));
  $rows = array();
  while ($row = db_fetch_object($result)) {
    $node = node_load($row->nid);
    $terms = array_keys(taxonomy_node_get_terms_by_vocabulary($node, $vid, 'name'));
    $send = db_fetch_object(db_query("SELECT * FROM {simplenews_statistics} WHERE nid = %d", $node->nid));
    $clicks = db_fetch_object(db_query("SELECT COUNT(a.email) as total FROM ( SELECT email FROM {simplenews_statistics_clicks} WHERE nid = %d GROUP BY email ) a", $node->nid));
    $opens = db_fetch_object(db_query("SELECT COUNT(a.email) as total FROM ( SELECT email FROM {simplenews_statistics_opens} WHERE nid = %d GROUP BY email ) a", $node->nid));
    $rows[] = array(
      l($node->title, 'node/' . $node->nid),
      isset($terms[0]) ? $terms[0] : t('n/a'),
      format_date($node->created, 'small'),
      $send ? $send->send : t('No data available.'),
      $send ? $opens->total . ' (' . round($opens->total / $send->send * 100) . '%)' : '',
      $send ? $clicks->total . ' (' . round($clicks->total / $send->send * 100) . '%)' : '',
      $send ? l('Details', 'admin/content/simplenews/statistics/' . $node->nid) : '',
    );
  }
  if (!$rows) {
    $rows[] = array(
      array(
        'data' => t('No newsletters available.'),
        'colspan' => '7',
      ),
    );
  }
  $table = theme('table', $header, $rows);
  $form['table'] = array(
    '#value' => $table,
  );
  return $form;
}