Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

reportbuilder/API

From MoodleDocs
Revision as of 02:06, 5 June 2014 by John Okely (talk | contribs)

Back to Index

Overview

Report builder provides an interface to allow administrators to generate reports, customise their appearance, then make them available to groups of users. It is designed to be easily extensible, so developers can add to existing report sources and write their own to allow administrators to generate the kind of reports they are interested in.

The report builder API enables plugin contributors and moodle core developers to create new sources that can be used by site administrators and teachers to generate and customise reports.

How to create a source

To create a source, create a file called rb_source_SOURCE_NAME.php in a subfolder of your plug-in or the relevant core class named rb_sources. The class name must start with 'rb_source_', and the class name and filename must match. The class must be a child of rb_base_source.

The following is an example of a Report Builder source:

<?php

class rb_source_example1 extends rb_base_source {
    public $base, $joinlist, $columnoptions, $filteroptions;

    function __construct() {
        $this->base = '{course}';
        $this->joinlist = array(
            new rb_join(
                'course_category',
                'LEFT',
                '{course_categories}',
                'course_category.id = base.category'
            )
        );
        $this->columnoptions = array(
            new rb_column_option(
                'course',
                'fullname',
                'Course Fullname',
                'base.fullname'
            )
        );
        $this->filteroptions = array();
        $this->sourcetitle   = "Example1";

        parent::__construct();
    }
}

A source consists of:

  • A base table (Defines what table all reports from this source will be based off)
  • A join list (Defines what tables should be joined to the base table)
  • Column options (Defines which columns from each table should be available in the report)
  • Filter options (Defines what fields can be used to filter the results in the report)

When using collumns from a joined table, the rb_column_option should include "joins" in the fifth parameter.

new rb_column_option(                                                                                                                                         
    'assignment',                                                                                                                                             
    'assignment_name',                                                                                                                                        
    'Assignment Name',                                                                                                                                        
    'assign.name',                                                                                                                                            
    array('joins' => 'assign')                                                                                                                                
)

Helper Functions

A number of helper functions exist that can be used to aid the creation of a source. For instance, 'add_user_table_to_joinlist' can be used to join the user table, 'add_user_fields_to_columns' can be used to add the columns, and 'add_user_fields_to_filters' can be used to add the filters.

Example:

$this->add_user_table_to_joinlist($joinlist_array, 'base', 'userid'); // Where 'base' is the name of the joined table with the users' ids, and 'userid' is the name of the column.
$this->add_user_fields_to_columns($columnoptions_array);
$this->add_user_fields_to_filters($filteroptions_array)