[ Index ]

PHP Cross Reference of MyBB 1.8.21

title

Body

[close]

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

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


2005 - 2019 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref 0.7.1