[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/admin/modules/tools/ -> statistics.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.6
   4   * Copyright 2010 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://mybb.com
   7   * License: http://mybb.com/about/license
   8   *
   9   * $Id: statistics.php 5304 2011-01-12 18:33:54Z Tomm $
  10   */
  11  
  12  // Disallow direct access to this file for security reasons
  13  if(!defined("IN_MYBB"))
  14  {
  15      die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
  16  }
  17  
  18  if($mybb->input['action'] == "do_graph")
  19  {
  20      $range = array(
  21          'start' => intval($mybb->input['start']),
  22          'end' => intval($mybb->input['end'])
  23      );
  24      create_graph($mybb->input['type'], $range);
  25      die;
  26  }
  27  
  28  $page->add_breadcrumb_item($lang->statistics, "index.php?module=tools-statistics");
  29  
  30  $sub_tabs['overall_statistics'] = array(
  31      'title' => $lang->overall_statistics,
  32      'link' => "index.php?module=tools-statistics",
  33      'description' => $lang->overall_statistics_desc
  34  );
  35  
  36  $plugins->run_hooks("admin_tools_statistics_begin");
  37  
  38  if(!$mybb->input['action'])
  39  {
  40      $plugins->run_hooks("admin_tools_statistics_overall_begin");
  41      
  42      $query = $db->simple_select("stats", "COUNT(*) as total");
  43      if($db->fetch_field($query, "total") == 0)
  44      {
  45          flash_message($lang->error_no_statistics_available_yet, 'error');
  46          admin_redirect("index.php?module=tools");
  47      }
  48      
  49      $per_page = 20;
  50      
  51      // Do we have date range criteria?
  52      if($mybb->input['from_year'])
  53      {
  54          $start_dateline = mktime(0, 0, 0, intval($mybb->input['from_month']), intval($mybb->input['from_day']), intval($mybb->input['from_year']));
  55          $end_dateline = mktime(23, 59, 59, intval($mybb->input['to_month']), intval($mybb->input['to_day']), intval($mybb->input['to_year']));
  56          $range = "&amp;start={$start_dateline}&amp;end={$end_dateline}";
  57      }
  58      
  59      // Otherwise default to the last 30 days
  60      if(!$mybb->input['from_year'] || $start_dateline > TIME_NOW || $end_dateline > mktime(23, 59, 59))
  61      {
  62          $start_dateline = TIME_NOW-(60*60*24*30);
  63          $end_dateline = TIME_NOW;
  64          
  65          list($mybb->input['from_day'], $mybb->input['from_month'], $mybb->input['from_year']) = explode('-', date('j-n-Y', $start_dateline));
  66          list($mybb->input['to_day'], $mybb->input['to_month'], $mybb->input['to_year']) = explode('-', date('j-n-Y', $end_dateline));
  67          
  68          $range = "&amp;start={$start_dateline}&amp;end={$end_dateline}";
  69      }
  70      
  71      $last_dateline = 0;
  72      
  73      if($mybb->input['page'] && $mybb->input['page'] > 1)
  74      {
  75          $mybb->input['page'] = intval($mybb->input['page']);
  76          $start = ($mybb->input['page']*$per_page)-$per_page;
  77      }
  78      else
  79      {
  80          $mybb->input['page'] = 1;
  81          $start = 0;
  82      }
  83      
  84      $query = $db->simple_select("stats", "*", "dateline >= '".intval($start_dateline)."' AND dateline <= '".intval($end_dateline)."'", array('order_by' => 'dateline', 'order_dir' => 'asc'));
  85      while($stat = $db->fetch_array($query))
  86      {
  87          if($last_dateline)
  88          {
  89              $stat['change_users'] = ($stat['numusers'] - $stats[$last_dateline]['numusers']);
  90              $stat['change_threads'] = ($stat['numthreads'] - $stats[$last_dateline]['numthreads']);
  91              $stat['change_posts'] = ($stat['numposts'] - $stats[$last_dateline]['numposts']);
  92          }
  93          
  94          $stats[$stat['dateline']] = $stat;
  95          
  96          $last_dateline = $stat['dateline'];
  97      }
  98      
  99      if(empty($stats))
 100      {
 101          flash_message($lang->error_no_results_found_for_criteria, 'error');
 102          admin_redirect("index.php?module=tools");
 103      }
 104      
 105      krsort($stats, SORT_NUMERIC);
 106      
 107      $page->add_breadcrumb_item($lang->overall_statistics, "index.php?module=tools-statistics");
 108      
 109      $page->output_header($lang->statistics." - ".$lang->overall_statistics);
 110      
 111      $page->output_nav_tabs($sub_tabs, 'overall_statistics');
 112      
 113      // Date range fields        
 114      $form = new Form("index.php?module=tools-statistics", "post", "overall");
 115      echo "<fieldset><legend>{$lang->date_range}</legend>\n";
 116      echo "{$lang->from} ".$form->generate_date_select('from', $mybb->input['from_day'], $mybb->input['from_month'], $mybb->input['from_year']);
 117      echo " {$lang->to} ".$form->generate_date_select('to', $mybb->input['to_day'], $mybb->input['to_month'], $mybb->input['to_year']);
 118      echo " ".$form->generate_submit_button($lang->view);
 119      echo "</fieldset>\n";
 120      $form->end();
 121      
 122      echo "<fieldset><legend>{$lang->users}</legend>\n";
 123      echo "<img src=\"index.php?module=tools-statistics&amp;action=do_graph&amp;type=users{$range}\" />\n";
 124      echo "</fieldset>\n";
 125      
 126      echo "<fieldset><legend>{$lang->threads}</legend>\n";
 127      echo "<img src=\"index.php?module=tools-statistics&amp;action=do_graph&amp;type=threads{$range}\" />\n";
 128      echo "</fieldset>\n";
 129      
 130      echo "<fieldset><legend>{$lang->posts}</legend>\n";
 131      echo "<img src=\"index.php?module=tools-statistics&amp;action=do_graph&amp;type=posts{$range}\" />\n";
 132      echo "</fieldset>\n";
 133      
 134      $total_rows = count($stats);
 135      
 136      $table = new Table;
 137      $table->construct_header($lang->date);
 138      $table->construct_header($lang->users);
 139      $table->construct_header($lang->threads);
 140      $table->construct_header($lang->posts);    
 141      $query = $db->simple_select("stats", "*", "dateline >= '".intval($start_dateline)."' AND dateline <= '".intval($end_dateline)."'", array('order_by' => 'dateline', 'order_dir' => 'desc', 'limit_start' => $start, 'limit' => $per_page));
 142      while($stat = $db->fetch_array($query))
 143      {
 144          $table->construct_cell("<strong>".date($mybb->settings['dateformat'], $stat['dateline'])."</strong>");
 145          $table->construct_cell(my_number_format($stat['numusers'])." <small>".generate_growth_string($stats[$stat['dateline']]['change_users'])."</small>");
 146          $table->construct_cell(my_number_format($stat['numthreads'])." <small>".generate_growth_string($stats[$stat['dateline']]['change_threads'])."</small>");
 147          $table->construct_cell(my_number_format($stat['numposts'])." <small>".generate_growth_string($stats[$stat['dateline']]['change_posts'])."</small>");
 148          $table->construct_row();
 149      }
 150      $table->output($lang->overall_statistics);
 151      
 152      $url_range = "&amp;from_month=".intval($mybb->input['from_month'])."&amp;from_day=".intval($mybb->input['from_day'])."&amp;from_year=".intval($mybb->input['from_year']);
 153      $url_range .= "&amp;to_month=".intval($mybb->input['to_month'])."&amp;to_day=".intval($mybb->input['to_day'])."&amp;to_year=".intval($mybb->input['to_year']);
 154      
 155      echo draw_admin_pagination($mybb->input['page'], $per_page, $total_rows, "index.php?module=tools-statistics{$url_range}&amp;page={page}");
 156      
 157      $page->output_footer();
 158  }
 159  
 160  function generate_growth_string($number)
 161  {
 162      global $lang, $cp_style;
 163      
 164      if($number === null)
 165      {
 166          return "";
 167      }
 168      
 169      $number = intval($number);
 170      $friendly_number = my_number_format(abs($number));
 171      
 172      if($number > 0)
 173      {
 174          $growth_string = "(<img src=\"./styles/{$cp_style}/images/icons/increase.gif\" alt=\"{$lang->increase}\" title=\"{$lang->increase}\" style=\"vertical-align: middle; margin-top: -2px;\" /> {$friendly_number})";
 175      }
 176      elseif($number == 0)
 177      {
 178          $growth_string = "(<img src=\"./styles/{$cp_style}/images/icons/no_change.gif\" alt=\"{$lang->no_change}\" title=\"{$lang->no_change}\" style=\"vertical-align: middle; margin-top: -2px;\" /> {$friendly_number})";
 179      }
 180      else
 181      {
 182          $growth_string = "(<img src=\"./styles/{$cp_style}/images/icons/decrease.gif\" alt=\"{$lang->decrease}\" title=\"{$lang->decrease}\" style=\"vertical-align: middle; margin-top: -2px;\" /> {$friendly_number})";
 183      }
 184      
 185      return $growth_string;
 186  }
 187  
 188  function create_graph($type, $range=null)
 189  {
 190      global $db;
 191      
 192      // Do we have date range criteria?
 193      if($range['end'] || $range['start'])
 194      {
 195          $start = intval($range['start']);
 196          $end = intval($range['end']);
 197      }
 198      // Otherwise default to the last 30 days
 199      else
 200      {
 201          $start = TIME_NOW-(60*60*24*30);
 202          $end = TIME_NOW;
 203      }
 204      
 205      $allowed_types = array('users', 'threads', 'posts');
 206      if(!in_array($type, $allowed_types))
 207      {
 208          die;
 209      }
 210      
 211      require_once  MYBB_ROOT.'inc/class_graph.php';
 212      
 213      $points = $stats = $datelines = array();
 214      if($start == 0)
 215      {
 216          $query = $db->simple_select("stats", "dateline,num{$type}", "dateline <= '".intval($end)."'", array('order_by' => 'dateline', 'order_dir' => 'desc', 'limit' => 2));
 217          while($stat = $db->fetch_array($query))
 218          {
 219              $stats[] = $stat['num'.$type];
 220              $datelines[] = $stat['dateline'];
 221              $x_labels[] = date("j/m", $stat['dateline']);
 222          }
 223          $points[$datelines[0]] = 0;
 224          $points[$datelines[1]] = $stats[0]-$stats[1];
 225          ksort($points, SORT_NUMERIC);
 226      }
 227      elseif($end == 0)
 228      {
 229          $query = $db->simple_select("stats", "dateline,num{$type}", "dateline >= '".intval($start)."'", array('order_by' => 'dateline', 'order_dir' => 'asc', 'limit' => 2));
 230          while($stat = $db->fetch_array($query))
 231          {
 232              $stats[] = $stat['num'.$type];
 233              $datelines[] = $stat['dateline'];
 234              $x_labels[] = date("j/m", $stat['dateline']);
 235          }
 236          $points[$datelines[0]] = 0;
 237          $points[$datelines[1]] = $stats[1]-$stats[0];
 238          ksort($points, SORT_NUMERIC);
 239      }
 240      else
 241      {
 242          $query = $db->simple_select("stats", "dateline,num{$type}", "dateline >= '".intval($start)."' AND dateline <= '".intval($end)."'", array('order_by' => 'dateline', 'order_dir' => 'asc'));
 243          while($stat = $db->fetch_array($query))
 244          {
 245              $points[$stat['dateline']] = $stat['num'.$type];
 246              $datelines[] = $stat['dateline'];
 247              $x_labels[] = date("j/m", $stat['dateline']);
 248          }
 249      }
 250      
 251      sort($datelines, SORT_NUMERIC);
 252  
 253      // Find our year(s) label
 254      $start_year = date('Y', $datelines[0]);
 255      $last_year = date('Y', $datelines[count($datelines)-1]);
 256      if(($last_year - $start_year) == 0)
 257      {
 258          $bottom_label = $start_year;
 259      }
 260      else
 261      {
 262          $bottom_label = $start_year." - ".$last_year;
 263      }
 264      
 265      // Create the graph outline
 266      $graph = new Graph();
 267      $graph->add_points(array_values($points));
 268      $graph->add_x_labels($x_labels);
 269      $graph->set_bottom_label($bottom_label);
 270      $graph->render();
 271      $graph->output();
 272  }
 273  ?>


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1