Making a report source tenant aware

To make a report source tenant aware, in most cases, we need to override two methods in the report_source. However, some report sources define the base as a subquery. We’ve also described our approach to making these kinds of report sources tenant aware.

Report sources that are hidden and only used in embedded reports do not need to be addressed.

Override the is_source_tenant_compatible method

By default, the rb_base_source class returns false. We need to override this method to return true to make it an available report source when creating tenant reports.

public static function is_source_tenant_compatible() { return true; }

Override the limit_to_tenant method

We override the limit_to_tenant method in the report_source. In here, we return joins required to limit the data to the tenant content. We also need to consider how tenant isolation affects the report generated.

For example, for a report on course membership with isolation enabled, we limit to tenant courses only. We’ve provided a bool parameter to be used to check if isolation should be considered.

Please note that tenant isolation applies only to tenant members. When tenant isolation is on, tenant members should only see tenant content. Tenant participants should still see site-level content.

public function limit_to_tenant(reportbuilder $report, bool $tenantsisolated): array { return $this->get_tenant_users_joins($report->tenantid, 'base.id'); }

We’ve also introduced two properties to the report source [ tenantwhere and tenantparams ]. These can be used to add extra where clauses to the base query. For example:

public function limit_to_tenant(reportbuilder $report, bool $tenantsisolated): array { $this->tenantwhere = "base.parentid = :tparam"; $this->tenantparams = ['tparam' => $tenantid]; return $this->get_tenant_users_joins($report->tenantid, 'base.id'); }

Handling report sources with subqueries as base

Some report sources do not use a table as the $report_source->base, instead the base is defined as a subquery. To make such report sources tenant aware, we use the post_param method to modify the base subquery. As a general approach, we add a placeholder into the base subquery and replace the placeholder with required SQL in the post_param method.

For example, say we have a report source with the base defined as below:

We modify the base as:

And in the post_param method we do: