Skip to content

Develop reports

orthagh edited this page Dec 19, 2014 · 6 revisions

Mreporting plugin allows you to display graphical reports from a single MySQL transaction

Create a new report set

You must declare it.

You need to create a class in inc/ folder
The file and class names must repect GLPI Standards :

  • filename : name.class.php
  • classname : class PluginMreportingName Extends PluginMreportingBaseclass

Replace name label by your choice.
Please note class inheritance, This is required. The plugin detects your class with this inheritance.

You also must create a new locale file : locales/reports_locales/myreport_en_GB.php
And add the class declaration to it :

$LANG['plugin_mreporting']['Classname']['title'] = "Reportings (Wiki Example) - Title";
$LANG['plugin_mreporting']['Classname']['desc'] = "Reportings (Wiki Example) - Description";

'Classname' key is you class name in short format.
Ex :
PluginMreportingMyreport -> Myreport

Functions declaration

For generate graphical reports, you must add functions to the previous class.

Plugin framework provides 2 types of graphs

  • Simple data (Pie, Horizontal bars, Line or Area)
  • Multiple datas (Horizontal grouped bars et Multi lines/areas)

Your functions need to be named in a certain way :

function reportTypeNomDeLaFonction($config = array()) {
...
}
  • they must always be prefixed by 'report'
  • next comes the type of graph (Hbar, Pie, Hgbar, Area, Line, Garea, Gline, ...)
  • And finally a suffix as desired

We must, as for the class, assign titles (descriptions optionally) for each function. Always in your local file (locales/reports_locales/myreport_en_GB.php) :

$LANG['plugin_mreporting']['Classname']['NomDeLaFonction']['title']     = "my function title";
$LANG['plugin_mreporting']['Classname']['NomDeLaFonction']['desc']      = "my function description";
$LANG['plugin_mreporting']['Classname']['NomDeLaFonction']['category']  = "my section";

They should return an array() whose structure is described in the following paragraph.

Returns

Simple datas :

return array(
   'datas' => array(
      "Apples" => 25,
      "Pears" => 52,
      "Strawberries" => 23,
      "Peaches" => 10`
   ),
   'unit' => 'Kg/Lb' //optionnal
);

Multiple datas:

return array(
   "datas" => array(
      "Paris" => array(12, 84, 65, 31),
      "Bordeaux" => array(84, 72, 18, 23),
      "Lille" => array(54, 81, 25, 26)
   ),
   "labels2" => array("Apples", "Pears", "Strawberries", "Peaches")
   'unit' => 'Kg/Lb' //optionnal
);

Note : For type type area/garea/line/gline, an additional parameter is available.
spline : curves the lines

return array(
   'datas' => array(
      ...
   ),
   'unit' => 'Kg/Lb' //optionnal
   'spline' => true' //optionnal
);

Selectors

Framework provides a bunch of selectors for filter your datas :

  • dateinterval (date1 and date2)
  • period (day/week/...)
  • multiplegrouprequest
  • multiplegroupassign
  • userassign
  • category (with type)
  • limit

You can also declare a selector in your class :

static function selectorMyselectorname() {
  // display the selector html 
}

You must declare in you function a session var to define the selectors to display :

$_SESSION['mreporting_selector']['myfunctioname'] = array('dateinterval', 'period', 'myselectorname');

In your function, you can retrieve selector values with the session var :

$_SESSION['mreporting_values'] : {
     period : 'week',
     myselectorname : '...',
     ...
}

'dateinterval' is a special selector. You also can access value by "$_SESSION['mreporting_values'] ('date1'.$randname])", but the framework provides a special function to format you SQL WHERE parts :

$wheredate = PluginMreportingCommon::getSQLDate($field = "`glpi_tickets`.`date`", $delay=365, $config['randname']) 

// return "($field >= '$date1' AND $field <= ADDDATE('$date2' , INTERVAL 1 DAY) )";

$randname is the id of the function. It contains $classname.$functionname. In your report function, you get this var in $config array submitted in parameter of your function.

Clone this wiki locally