Source for file statgraph.php

Documentation is available at statgraph.php

  1. <?php
  2. /**
  3.  *
  4.  * statgraph.php
  5.  *
  6.  * Long description for file:
  7.  * Generate a grphic given a query.
  8.  * This script generates a webpage with no text, and graphics must be generated before text.
  9.  *
  10.  * @package     FreeNAC
  11.  * @author      Core team, Originally T.Dagonnier
  12.  * @copyright   2008 FreeNAC
  13.  * @license     http://www.gnu.org/copyleft/gpl.html   GNU Public License Version 3
  14.  * @version     SVN: $Id: find.php,v 1.1 2008/02/22 13:04:57 root Exp root $
  15.  * @link        http://freenac.net
  16.  *
  17.  *  To test call from a browser with parameters like this:
  18.  *  http://SERVERNAME/nac/statgraph.php?stattype=os&order=DESC&graphtype=bar
  19.  *
  20.  */
  21.  
  22. ## Initialise (standard header for all modules)
  23.   dir(dirname(__FILE__))set_include_path("./:../lib:../");
  24.   require_once('webfuncs.inc');
  25.   $logger=Logger::getInstance();
  26.   $logger->setDebugLevel(0);     // set to 0 to see graphs
  27.  
  28.   ## Loggin in? User identified?
  29.   include 'session.inc.php';
  30.   check_login()// logged in?
  31.   #$logger->debug('Start, uid=' .$_SESSION['uid'], 3);
  32. ## end of standardc header ------
  33.  
  34.  
  35. // 1. Check rights
  36. if ($_SESSION['nac_rights']<1{
  37.   throw new InsufficientRightsException($_SESSION['nac_rights']);
  38. }
  39. else if ($_SESSION['nac_rights']==1{
  40.   $action_menu='';
  41. }
  42. else if ($_SESSION['nac_rights']==2{
  43.   $action_menu='';
  44.   //$action_menu=array('Print','Edit');   // 'buttons' in action column
  45. }
  46. else if ($_SESSION['nac_rights']==99{
  47.   $action_menu='';
  48.   //$action_menu=array('Print', 'Edit', 'Delete');   // 'buttons' in action column
  49. else {
  50.   throw new InsufficientRightsException("Unknown nac_rights: ".$_SESSION['nac_rights']);
  51. }
  52.  
  53.  
  54.  
  55. function cbFmtPercentage($aVal{
  56.         return sprintf("%.0f",$aVal)// Convert to string
  57. };
  58.  
  59.  
  60. // ------------ main () ----------------
  61.  
  62. // Build a query object
  63.   $reportnew WebCommon(false);      // new webpage, no header
  64.   $report->logger->setDebugLevel(0);  // set to 0 to see graphs
  65.   $conn=$report->getConnection();     //  make sure we have a DB connection
  66.  
  67.  
  68. // Clean inputs from the web, (security)
  69.    $_GET=array_map('validate_webinput',$_GET);
  70.    $_POST=array_map('validate_webinput',$_POST);
  71.    $_REQUEST=array_map('validate_webinput',$_REQUEST);
  72.    $_COOKIE=array_map('validate_webinput',$_COOKIE);
  73.  
  74.    if isset($_GET["stattype"]) )
  75.      $stattype $_GET["stattype"];
  76.    else
  77.      $stattype 'os';
  78.  
  79.    if isset($_GET["graphtype"]) )
  80.      $graphtype $_GET["graphtype"];
  81.    else
  82.      $graphtype 'bar';
  83.  
  84.    if isset($_GET["order"]) )
  85.      $order $_GET["order"];
  86.    else
  87.      $order 'DESC';
  88.  
  89.   $report->debug("stattype=$stattype, graphtype=$graphtype, order=$order"2);
  90.  
  91.   include_once('graphdefs.inc');                      // generic queries
  92.   $incs=array($conf->web_jpgraph.'/jpgraph.php'$conf->web_jpgraph.'/jpgraph_'.$graphtype.'.php');
  93.   foreach ($incs as $f{
  94.     if (is_readable($f)) {
  95.       $logger->debug("include $f"3);
  96.       include_once($f);       
  97.     else {
  98.       throw new FileMissingException("ERROR: The file $f cannot be opened");
  99.     
  100.   
  101.  
  102.  
  103.  
  104.   // build and run the query
  105.   switch ($stattype{
  106.    case 'wsus1':
  107.       $q $sel[$stattype]['graph';
  108.       break;
  109.    default:
  110.       $q $sel[$stattype]['graph'." ORDER BY count(*) $order";
  111.       break;
  112.   };
  113.  
  114.   $report->debug($q3);
  115.   $res $conn->query($q);
  116.   if ($res === FALSE)
  117.        throw new DatabaseErrorException($q .'; ' .$conn->error);
  118.  
  119.   while (($row $res->fetch_assoc()) !== NULL{
  120.       $data[$row["count"];
  121.       $data_names[$row["datax"]//." (%.0f%%)";
  122.   }
  123.         
  124.   // create the graph
  125.     if ($graphtype == 'bar' && isset($data) ) {
  126.         $graph new Graph(800,400);
  127.  
  128.         // Create the graph. 
  129.         $bar1 new BarPlot($data);
  130.  
  131.         $graph->SetScale("textlin");
  132.         $graph->xaxis->SetFont(FF_VERDANA,FS_NORMAL,8);
  133.         $graph->yaxis->SetFont(FF_VERDANA,FS_NORMAL,8);
  134.  
  135.         $graph->xaxis->SetTickLabels($data_names);
  136.         $graph->xaxis->SetLabelAngle(45);
  137.  
  138.         $bar1->SetFillGradient("navy","lightsteelblue",GRAD_MIDVER);
  139.         $bar1->value->SetFont(FF_VERDANA,FS_NORMAL,8);
  140.  
  141.         $bar1->value->SetFormatCallback("cbFmtPercentage");
  142.         $bar1->value->Show();
  143.  
  144.         // Add the plot to the graph
  145.         $graph->Add($bar1);
  146.  
  147.     
  148.         else if ($graphtype == 'pie' && isset($data) ) {
  149.              $graph new Graph(500,500);
  150.            $graph new PieGraph(800,400);//,$filename,60);
  151.            $graph->SetShadow();
  152.  
  153.     //       $graph->SetSize(0.4);
  154.         // Set A title for the plot
  155.     //       $graph->title->Set($PIE_TITLE);
  156.     //       $graph->title->SetFont(FF_FONT1,FS_BOLD);
  157.  
  158.         // Create
  159.            $p1 new PiePlot($data);
  160.            $p1->SetCenter(0.35,0.5);
  161.     //       $p1->SetLegends($data_names);
  162.     //       $p1->SetLabelType(PIE_VALUE_PER);
  163.             $p1->SetLabels($data_names);
  164.            $p1->SetTheme("sand");
  165.            $p1->value->SetFont(FF_VERDANA,FS_NORMAL,8);
  166.      
  167.            $graph->Add($p1);
  168.     };
  169.  
  170.         if ( (($graphtype == 'pie'|| ($graphtype == 'bar') )
  171.             && isset($data) ) {
  172.       $graph->Stroke();
  173.         }
  174.  
  175.  
  176.  
  177. ?>

Documentation generated on Mon, 17 Nov 2008 01:10:50 +0100 by phpDocumentor 1.4.0