Developer Guide > Create a report

Creating the report files

A report is a set of files, stored in his own directory inside the report/ directory (of any plugin).
The report has a short name, which is used internally, and must be the same as his directory : for example myreport
It's composed of :
  • a file which contains the report (called myreport.php)
  • several language files, called (myreport.en_GB.php, myreport.fr_FR.php, etc.)
report/myreport/
report/myreport/myreport.php
report/myreport/myreport.en_GB.php
report/myreport/myreport.fr_FR.php
report/myreport/myreport/...

All reports are available throught the Tools > Reports menu by default. It is possible to add it into the Helpdesk > Statistics menu by added the word stat into the name (for example : stattickets).

When the directory myreport is created, it adds an new entry into the Administration > Profile > the profile > Additional Reports menu. You can configure access to this report independently for each profile.

h2 . Implementing the report

Database connection

The 2 first lines (which are optionnal) indicates the behavior regarding the database connection :
$USEDBREPLICATE= 1;
$DBCONNECION_REQUIRED= 0;
  • $USEDBREPLICATE= 1; //If replicate is configured use it by default (1 use it, 0 : do not use it)
  • $DBCONNECION_REQUIRED= 0; //If replicate is configured but not available fallback onto the master database or display

Includes

Theses 2 line should always be present in a report :

define('GLPI_ROOT', '../../../..');
include(GLPI_ROOT."/inc/includes.php");

Report & criterias definition

Create an instance of the AutoReport class : it'll manage criterias, request execution and display

$report= new PluginReportsAutoReport();

Then add (if necessary) criterias to your report (this example adds a date interval, which is linked to the date_mod field of glpi's history table :

new PluginReportsDateIntervalCriteria($report, "`glpi_logs`.`date_mod`");

Note : please check of available criterias here.

Now display the search criterias form :

$report->displayCriteriasForm();

Now defined the report's columns :
$report->setColumns(array(new PluginReportsColumn('completename', $LANG["entity"][0]),
                          new PluginReportsColumnLink('groupid', $LANG["common"][35], 'Group'),
                          new PluginReportsColumnLink('userid', $LANG["setup"][18], 'User'),
                          new PluginReportsColumn('firstname', $LANG["common"][43]),
                          new PluginReportsColumn('realname', $LANG["common"][48]),
                          new PluginReportsColumnDateTime('last_login', $LANG['login'][0])));

Each column is an object which contains :
  • the sql field to display
  • the column label (to be display in the headers)
  • and optional parameter (depends on the column type)
    The list of columns is available here

Now defind the query to execute :

$query = "SELECT `glpi_entities`.`completename`,
                 `glpi_groups`.`id` AS groupid,
                 `glpi_users`.`id` AS userid,
                 `glpi_users`.`firstname`,
                 `glpi_users`.`realname`,
                 `glpi_users`.`last_login`
          FROM `glpi_groups`
          LEFT JOIN `glpi_groups_users` ON (`glpi_groups_users`.`groups_id` = `glpi_groups`.`id`)
          LEFT JOIN `glpi_users` ON (`glpi_groups_users`.`users_id` = `glpi_users`.`id`
                                     AND `glpi_users`.`is_deleted` = '0' )
          LEFT JOIN `glpi_entities` ON (`glpi_groups`.`entities_id` = `glpi_entities`.`id`)".
          getEntitiesRestrictRequest(" WHERE ", "glpi_groups") ." 
          ORDER BY `completename`, `glpi_groups`.`name`, `glpi_users`.`name`";

$report->setGroupBy(array('completename',
                          'groupid'));

$report->setSqlRequest($query);

Execute the query (and display results) :

$report->execute();

All the options and methods provided by the AutoReport class are available here