[ Index ]

PHP Cross Reference of MyBB 1.8.38

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->get_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->get_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      $mybb->input['page'] = $mybb->get_input('page', MyBB::INPUT_INT);
  73      if($mybb->input['page'] > 1)
  74      {
  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          else
  95          {
  96              $stat['change_users'] = 0;
  97              $stat['change_threads'] = 0;
  98              $stat['change_posts'] = 0;
  99          }
 100  
 101          $stats[$stat['dateline']] = $stat;
 102  
 103          $last_dateline = $stat['dateline'];
 104      }
 105  
 106      if(empty($stats))
 107      {
 108          flash_message($lang->error_no_results_found_for_criteria, 'error');
 109      }
 110  
 111      krsort($stats, SORT_NUMERIC);
 112  
 113      $page->add_breadcrumb_item($lang->overall_statistics, "index.php?module=tools-statistics");
 114  
 115      $page->output_header($lang->statistics." - ".$lang->overall_statistics);
 116  
 117      $page->output_nav_tabs($sub_tabs, 'overall_statistics');
 118  
 119      // Date range fields
 120      $form = new Form("index.php?module=tools-statistics", "post", "overall");
 121      echo "<fieldset><legend>{$lang->date_range}</legend>\n";
 122      echo "{$lang->from} ".$form->generate_date_select('from', $mybb->input['from_day'], $mybb->input['from_month'], $mybb->input['from_year']);
 123      echo " {$lang->to} ".$form->generate_date_select('to', $mybb->input['to_day'], $mybb->input['to_month'], $mybb->input['to_year']);
 124      echo " ".$form->generate_submit_button($lang->view);
 125      echo "</fieldset>\n";
 126      $form->end();
 127  
 128      if(!empty($stats))
 129      {
 130          echo "<fieldset><legend>{$lang->users}</legend>\n";
 131          echo "<img src=\"index.php?module=tools-statistics&amp;action=do_graph&amp;type=users{$range}\" />\n";
 132          echo "</fieldset>\n";
 133  
 134          echo "<fieldset><legend>{$lang->threads}</legend>\n";
 135          echo "<img src=\"index.php?module=tools-statistics&amp;action=do_graph&amp;type=threads{$range}\" />\n";
 136          echo "</fieldset>\n";
 137  
 138          echo "<fieldset><legend>{$lang->posts}</legend>\n";
 139          echo "<img src=\"index.php?module=tools-statistics&amp;action=do_graph&amp;type=posts{$range}\" />\n";
 140          echo "</fieldset>\n";
 141  
 142          $total_rows = count($stats);
 143          $pages = ceil($total_rows / $per_page);
 144          if($mybb->input['page'] > $pages)
 145          {
 146              $mybb->input['page'] = 1;
 147              $start = 0;
 148          }
 149  
 150          $table = new Table;
 151          $table->construct_header($lang->date);
 152          $table->construct_header($lang->users);
 153          $table->construct_header($lang->threads);
 154          $table->construct_header($lang->posts);
 155          $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));
 156          while($stat = $db->fetch_array($query))
 157          {
 158              $table->construct_cell("<strong>".date($mybb->settings['dateformat'], $stat['dateline'])."</strong>");
 159              $table->construct_cell(my_number_format($stat['numusers'])." <small>".generate_growth_string($stats[$stat['dateline']]['change_users'])."</small>");
 160              $table->construct_cell(my_number_format($stat['numthreads'])." <small>".generate_growth_string($stats[$stat['dateline']]['change_threads'])."</small>");
 161              $table->construct_cell(my_number_format($stat['numposts'])." <small>".generate_growth_string($stats[$stat['dateline']]['change_posts'])."</small>");
 162              $table->construct_row();
 163          }
 164          $table->output($lang->overall_statistics);
 165  
 166          $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);
 167          $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);
 168  
 169          echo draw_admin_pagination($mybb->input['page'], $per_page, $total_rows, "index.php?module=tools-statistics{$url_range}&amp;page={page}");
 170      }
 171  
 172      $page->output_footer();
 173  }
 174  
 175  /**
 176   * @param int $number
 177   *
 178   * @return string
 179   */
 180  function generate_growth_string($number)
 181  {
 182      global $lang, $cp_style;
 183  
 184      if($number === null)
 185      {
 186          return "";
 187      }
 188  
 189      $number = (int)$number;
 190      $friendly_number = my_number_format(abs($number));
 191  
 192      if($number > 0)
 193      {
 194          $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})";
 195      }
 196      elseif($number == 0)
 197      {
 198          $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})";
 199      }
 200      else
 201      {
 202          $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})";
 203      }
 204  
 205      return $growth_string;
 206  }
 207  
 208  /**
 209   * @param string $type users, threads, posts
 210   * @param array $range
 211   */
 212  function create_graph($type, $range=null)
 213  {
 214      global $db;
 215  
 216      // Do we have date range criteria?
 217      if($range['end'] || $range['start'])
 218      {
 219          $start = (int)$range['start'];
 220          $end = (int)$range['end'];
 221      }
 222      // Otherwise default to the last 30 days
 223      else
 224      {
 225          $start = TIME_NOW-(60*60*24*30);
 226          $end = TIME_NOW;
 227      }
 228  
 229      $allowed_types = array('users', 'threads', 'posts');
 230      if(!in_array($type, $allowed_types))
 231      {
 232          die;
 233      }
 234  
 235      require_once  MYBB_ROOT.'inc/class_graph.php';
 236  
 237      if(!Graph::can_use())
 238      {
 239          die;
 240      }
 241  
 242      $points = $stats = $datelines = array();
 243      if($start == 0)
 244      {
 245          $query = $db->simple_select("stats", "dateline,num{$type}", "dateline <= '".(int)$end."'", array('order_by' => 'dateline', 'order_dir' => 'desc', 'limit' => 2));
 246          while($stat = $db->fetch_array($query))
 247          {
 248              $stats[] = $stat['num'.$type];
 249              $datelines[] = $stat['dateline'];
 250              $x_labels[] = date("m/j", $stat['dateline']);
 251          }
 252          $points[$datelines[0]] = 0;
 253          $points[$datelines[1]] = $stats[0]-$stats[1];
 254          ksort($points, SORT_NUMERIC);
 255      }
 256      elseif($end == 0)
 257      {
 258          $query = $db->simple_select("stats", "dateline,num{$type}", "dateline >= '".(int)$start."'", array('order_by' => 'dateline', 'order_dir' => 'asc', 'limit' => 2));
 259          while($stat = $db->fetch_array($query))
 260          {
 261              $stats[] = $stat['num'.$type];
 262              $datelines[] = $stat['dateline'];
 263              $x_labels[] = date("m/j", $stat['dateline']);
 264          }
 265          $points[$datelines[0]] = 0;
 266          $points[$datelines[1]] = $stats[1]-$stats[0];
 267          ksort($points, SORT_NUMERIC);
 268      }
 269      else
 270      {
 271          $query = $db->simple_select("stats", "dateline,num{$type}", "dateline >= '".(int)$start."' AND dateline <= '".(int)$end."'", array('order_by' => 'dateline', 'order_dir' => 'asc'));
 272          while($stat = $db->fetch_array($query))
 273          {
 274              $points[$stat['dateline']] = $stat['num'.$type];
 275              $datelines[] = $stat['dateline'];
 276              $x_labels[] = date("m/j", $stat['dateline']);
 277          }
 278      }
 279  
 280      sort($datelines, SORT_NUMERIC);
 281  
 282      // Find our year(s) label
 283      $start_year = date('Y', $datelines[0]);
 284      $last_year = date('Y', $datelines[count($datelines)-1]);
 285      if(($last_year - $start_year) == 0)
 286      {
 287          $bottom_label = $start_year;
 288      }
 289      else
 290      {
 291          $bottom_label = $start_year." - ".$last_year;
 292      }
 293  
 294      // Create the graph outline
 295      $graph = new Graph();
 296      $graph->add_points(array_values($points));
 297      $graph->add_x_labels($x_labels);
 298      $graph->set_bottom_label($bottom_label);
 299      $graph->render();
 300      $graph->output();
 301  }


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