You are here

function newsletter_stats_graphic in Newsletter 7

Menu callback; present newsletter statistics graphs.

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

File

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

Code

function newsletter_stats_graphic() {
  if (!module_exists('libraries')) {
    return drupal_set_message(t('For graphical statistics
      please install the <a href=@url-lib>Libraries API</a>
      and <a href=@url-jqplot>download the jQplot library</a>.', array(
      '@url-lib' => 'http://drupal.org/project/libraries',
      '@url-jqplot' => 'https://bitbucket.org/cleonello/jqplot/downloads/',
    )), 'warning');
  }
  $path = libraries_get_path('jqplot');
  if (!file_exists($path . '/jquery.jqplot.min.js')) {
    return drupal_set_message(t('jQplot library is not present in your filesystem.
      You need to <a href=@url>download</a> it
      and extract all its files to @path
      folder, so the library is located to
      @path/jquery.jqplot.min.js', array(
      '@url' => 'https://bitbucket.org/cleonello/jqplot/downloads/',
      '@path' => $path,
    )), 'warning');
  }

  // Add css and js files for our charts.
  $iesupport = array(
    '#type' => 'markup',
    '#markup' => '<!--[if lt IE 9]>
      <script language="javascript"
      type="text/javascript" src="' . $path . '/excanvas.js">
      </script><![endif]--> ',
  );
  drupal_add_html_head($iesupport, 'newsletter');
  drupal_add_js($path . '/jquery.jqplot.min.js');
  drupal_add_js($path . '/plugins/jqplot.highlighter.min.js');
  drupal_add_js($path . '/plugins/jqplot.enhancedLegendRenderer.min.js');
  drupal_add_js($path . '/plugins/jqplot.canvasAxisLabelRenderer.min.js');
  drupal_add_js($path . '/plugins/jqplot.dateAxisRenderer.min.js');
  drupal_add_css($path . '/jquery.jqplot.min.css');
  drupal_add_css(drupal_get_path('module', 'newsletter') . '/css/newsletter.admin.css');
  if (isset($_GET['compare'])) {
    $ids = urldecode($_GET['compare']);
    $ids = explode(',', $ids);
    foreach ($ids as $id) {
      $newsletters[] = db_query('SELECT * FROM {newsletter_newsletter}
        WHERE nnid = :id
        AND last_sent <> 0', array(
        ':id' => $id,
      ))
        ->fetchObject();
    }
  }
  else {

    // Compare all the existing newsletters; might be slow
    // @TODO check performance
    $newsletters = db_query('SELECT *
      FROM {newsletter_newsletter}
      WHERE last_sent <> 0')
      ->fetchAll();
  }
  $ctr_vars = array();
  $or_vars = array();
  $smaller_timestamp = REQUEST_TIME;
  foreach ($newsletters as $newsletter) {
    $ctr = @($newsletter->clicks / $newsletter->subscribers_sent) * 100;
    $timestamp_ms = $newsletter->last_sent * 1000;
    @($ctr_vars[$newsletter->title] .= "[{$timestamp_ms}, {$ctr}], ");
    $open_rate = @($newsletter->opens / $newsletter->subscribers_sent) * 100;
    @($or_vars[$newsletter->title] .= "[{$timestamp_ms}, {$open_rate}], ");
    $smaller_timestamp = $newsletter->last_sent < $smaller_timestamp ? $newsletter->last_sent : $smaller_timestamp;
  }
  $first_day = format_date($smaller_timestamp, 'custom', 'Y-m-d');
  if (empty($ctr_vars) && empty($or_vars)) {
    return drupal_set_message(t('No statistics have been gathered yet.'), 'warning');
  }
  $jqplot_js = '(function ($) {$(document).ready(function(){
    $.jqplot.config.enablePlugins = true;';
  $i = 1;
  $cvars = '';
  $ovars = '';
  $labels = '';
  foreach ($ctr_vars as $title => $string) {
    $jqplot_js .= "var l{$i} = [{$string}];\n";
    $cvars .= "l{$i}, ";
    $labels .= "{label:'{$title}'}, ";
    $i++;
  }
  $k = $i;
  foreach ($or_vars as $title => $string) {
    $jqplot_js .= "var l{$k} = [{$string}];\n";
    $ovars .= "l{$k}, ";
    $k++;
  }
  $jqplot_js .= "var first_day = '{$first_day}';";
  $jqplot_js .= "plot1 = \$.jqplot('chart1', [{$cvars}], {";
  $jqplot_js .= 'legend:{show:true, renderer:$.jqplot.EnhancedLegendRenderer},
                  seriesDefaults: {lineWidth:4},';
  $jqplot_js .= "series:[{$labels}],";
  $jqplot_js .= 'seriesColors:[ "#4bb2c5", "#c5b47f", "#EAA228", "#579575",
                    "#839557", "#958c12", "#953579", "#4b5de4",
                    "#d8b83f", "#ff5800", "#0085cc"],
                  highlighter: {bringSeriesToFront: true},
                  axes:{
                    xaxis:{
                      renderer: $.jqplot.DateAxisRenderer,
                      tickOptions:{formatString:"%d %b %y"},
                      min: first_day,
                      label:"Date Sent",
                    },
                    yaxis:{
                      pad:1.0,
                      numberTicks:5,
                      autoscale:false,
                      label:"CTR (%)",
                    }
                  }
                });';
  $jqplot_js .= "plot2 = \$.jqplot('chart2', [{$ovars}], {";
  $jqplot_js .= 'legend:{show:true, renderer:$.jqplot.EnhancedLegendRenderer},
                  seriesDefaults: {lineWidth:4},';
  $jqplot_js .= "series:[{$labels}],";
  $jqplot_js .= 'seriesColors:[ "#4bb2c5", "#c5b47f", "#EAA228", "#579575",
                    "#839557", "#958c12", "#953579", "#4b5de4",
                    "#d8b83f", "#ff5800", "#0085cc"],
                  highlighter: {bringSeriesToFront: true},
                  axes:{
                    xaxis:{
                      renderer: $.jqplot.DateAxisRenderer,
                      tickOptions:{formatString:"%d %b %y"},
                      min: first_day,
                      label:"Date Sent",
                    },
                    yaxis:{
                      pad:1.0,
                      numberTicks:10,
                      autoscale:false,
                      label:"Open Rate (%)"
                    }
                  }
              });
          });})(jQuery);';
  drupal_add_js($jqplot_js, 'inline');
  return '<div id="chart1"></div><div id="chart2"></div>';
}