Totara Learn Open Discussions

Dev question : Set default filter value in report builder

 
Easter Island / Rapa Nui
Dev question : Set default filter value in report builder
by Russell England - Friday, 23 November 2018, 9:31 AM
 

Is it possible to set a default value for a filter in report builder?

For example, on a page using an embedded report I'd like to set the date filter to the previous 12 months when landing on the page

I'm currently using this work around:

    $report = reportbuilder_get_embedded_report($shortname, $reportdata, false, $sid)

    // Calculate todays date minus x months.
    $timestart = new DateTime();
    $timestart->setTimezone(core_date::get_user_timezone_object());
    $timestart->sub(new DateInterval('P12M'));

    $SESSION->reportbuilder[$report->get_uniqueid()] =
        array(
            'base-completeddate' =>
            array(
                'after' => $timestart->getTimestamp(),
                'after_applied' => 1,
                'before' => 0,
                'daysafter' => 0,
                'daysbefore' => 0,
                'notset' => 0
        )
    );
Tom Wood
Re: Dev question : Set default filter value in report builder
by Tom Wood - Friday, 30 November 2018, 7:27 AM
Group Totara
Hi Russ,

This should be possible from Totara version 11+ using the following method:

Default value for filters:

Default value for filters is an option when defining embedded report or when defining the default reports through the define_defaultfilters method.

Values are saved when creating the reports.

The only thing that needs to be added is the defaultvalue option as an array with the corresponding filter options. 

Example: 

[
  'type' => 'user',
  'value' => 'deleted',
  'advanced' => 0,
  'defaultvalue' => ['operator'=> 1, 'value' => 0]
]

For a real example check the "rb_system_browse_users_embedded" report which "User Status" filter is now set to "Active users only". Site administration > Users > Accounts > Browse list of users.

An example of setting a date filter for the "User last accessed" date filter would be:

array(
'type' => 'user',
'value' => 'lastlogin',
'advanced' => 0,
'defaultvalue' => ['after' => 0, 'daysbefore' => 0, 'daysafter' => 0, 'before'=> '1534809600'],
),


For more examples check totara/reportbuilder/tests/rb_filters_default_value.php

Trying adding new default values to embedded reports or in the define_defaultfilters method.

I hope this is useful, let me know if you have any other questions

Cheers
Easter Island / Rapa Nui
Re: Dev question : Set default filter value in report builder
by Russell England - Friday, 30 November 2018, 8:12 AM
 
Awesome!


Cheers Tom