Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

SQL report block (block_sqlreports)

A Moodle block that surfaces a published report_sql report — as a table or a chart — inline on course pages, the site front page, or the user Dashboard.

It is the display companion to the report_sql plugin. That plugin is where reports are authored (write SQL, publish, get a Report Builder report); this block is where a single report is pinned so people see it without navigating to the full report viewer.

Requirements

  • Moodle 4.5 – 5.0 ($plugin->supported = [405, 502])
  • report_sql must be installed first — it is a hard dependency (version.php → $plugin->dependencies).

Installation

  1. Install report_sql into <moodleroot>/report/sql.
  2. Copy this folder into <moodleroot>/blocks/sqlreports.
  3. Go to Site admin → Notifications to complete the install.

What it does

  • Picks one published report. The instance config form lists only reports that report_sql has published (query::published_menu()).

  • Renders table or chart. Display mode is auto (chart if the report has chart metadata, else table), or forced to table / chart.

  • Lists reports. Display mode list ("List of reports") shows links to published reports instead of rendering one. Two toggles choose the sources: Site reports (reports not tied to a course) and Course reports (reports for the course the block is on; empty on the front page and Dashboard). Each viewer sees only the reports report_sql lets them open (current_user_can_view_report()); the block adds no rules of its own. A chart icon links to the report's chart when it has one. Authors also get a "Manage reports" link, and the block shows "No reports available" when there is nothing to list.

    Difference from Configurable Reports: Configurable Reports decides who can see a report from the page the block is on, so a teachers-only report shows on that teacher's course page but not on the front page. List mode uses the report's audience instead, which is the same on every page. So a report can show on more pages than it did in Configurable Reports. To limit it, narrow the report's audience in report_sql.

  • Respects per-viewer access. The block calls the same access path as the Report Builder viewer (current_user_can_view_report()) and fetches rows through the shared fetch_rows_for_viewer(). So one shared block instance shows each viewer only their own rows (e.g. a teacher sees only their courses). No access → empty content → the block hides itself.

  • Expandable charts. A narrow block region is cramped for a chart, so chart mode adds an Expand button. It opens a large modal that re-renders the same chart client-side (core/chart_builder), with a fullscreen toggle (amd/src/expand.js).

  • Shows a row count. The footer states how many rows the viewer is seeing — Showing N of M rows while limited, or M rows when everything is shown. The total is an accurate, viewer-scoped count (query::count_rows_for_viewer()), not just the number rendered.

  • "Show all" without leaving the page. When a numeric row limit is set and the report has more rows, the footer offers a Show all / Show fewer toggle. It re-renders the same page (?sqlreportsall=<blockid>), so the block's course scope is preserved — unlike "View full report" below. Each block instance on a page expands independently. Charts have no toggle (a chart aggregates every fetched row, so paging it is meaningless).

  • Links to the full report. When enabled, the footer links to /reportbuilder/view.php for the underlying report. Note this opens the full Report Builder report, which is not limited to the current course — on a course page the block itself is course-scoped, but the full report is not. A tooltip on the link says so, and the link can be hidden per instance.

Configuration (per block instance)

Set via the block's edit form (edit_form.php):

Setting String key Notes
Report config_queryid Which published report to show (table / chart modes)
Display as config_displaymode auto / table / chart / list (list of reports)
Show site reports config_showsitereports List mode only. Default on. Lists reports with no course, under "Site reports".
Show course reports config_showcoursereports List mode only. Default on. Lists reports for the page's course, under "Course reports". Empty on the front page and Dashboard.
Rows to show config_rowlimit 5 / 10 / 25 / 50 / 100 / All; default All (0). A numeric limit shows a "Show all" toggle in the block; All caps internally at 5000.
Show "View full report" link config_showfull Off by default (keeps viewers in the block's course-scoped view). Turn on to add the full-report link, which is not course-scoped.
Custom block title config_title Overrides the report name

Multiple instances are allowed per page (instance_allow_multiple()), so you can pin several reports side by side.

Migrating from Configurable Reports

Each block_configurable_reports block can be replaced with a "List of reports" block in one step. It works from database rows only, so it still runs after Configurable Reports has been removed from disk.

  1. Import the reports with report_sql (Import from Configurable Reports), then publish them and set each report's audience there. This plugin does not import or publish reports, and Configurable Reports permissions are not migrated.
  2. Open Site administration > Plugins > Blocks > SQL report and follow the migration link (/blocks/sqlreports/migrate_bcr.php). It lists the blocks it will replace; confirm to run it. Or from the command line: php blocks/sqlreports/cli/migrate_bcr.php --dry-run, then without --dry-run.

Each new block keeps the old block's page placement (context, pages, region, weight), its per-page hidden/shown state, per-user collapsed state, the moodle/block:view and moodle/block:edit permission overrides, its title, and its "global" / "course" report settings (as "Show site reports" and "Show course reports"). Configurable Reports' default title is not carried over. Each block is migrated in its own transaction, so one failure leaves that block unchanged and the rest go ahead. Blocks already replaced are gone, so running it again is safe.

Capabilities

Defined in db/access.php:

  • block/sqlreports:addinstance — add the block to a course/page. Granted to editingteacher and manager by default; carries RISK_SPAM | RISK_XSS.
  • block/sqlreports:myaddinstance — add the block to a personal Dashboard. Granted to all users so anyone can pin a report to their own /my page.

The block grants placement; the report's own Report Builder access still gates the data.

Security notes

  • Cell HTML is passed through format_text(..., FORMAT_HTML, ['filter' => false]), matching how the Report Builder report itself renders — anchors survive, scripts are stripped.
  • A misconfigured filter column fails closed: rows are never shown raw; a neutral errdata message is displayed instead.
  • Links in cells are rewritten to target="_blank" rel="noopener noreferrer" so opening a link does not lose the Dashboard and does not expose the reverse-tabnabbing hole.

File map

block_sqlreports.php   Block class: init, formats, get_content, table/chart/list rendering
classes/display_mode.php  Display mode enum (auto / table / chart / list)
classes/local/report_list.php  List-of-reports scoping and access (web + mobile)
classes/local/bcr_migrate.php, bcr_migration_item.php  Configurable Reports block migration
migrate_bcr.php, cli/migrate_bcr.php  Migration admin page and CLI
settings.php              Admin settings page (links to the migration)
classes/local/report_list_section.php, report_list_item.php, section_type.php  List data types
edit_form.php             Per-instance config form
db/access.php             Capabilities
lang/en/...               English strings
templates/report_list.mustache  List-of-reports template (web)
amd/src/expand.js         Client-side chart expand/fullscreen modal (ES6 source)
amd/build/...             Built AMD (grunt output)
styles.css                Chart modal / fullscreen layout, list styling
tests/block_test.php      Block PHPUnit tests
tests/local/report_list_test.php  report_list PHPUnit tests
tests/output/mobile_test.php      Mobile output PHPUnit tests
tests/local/bcr_migrate_test.php  Configurable Reports migration PHPUnit tests
tests/classes/helper.php  Shared test helper trait
version.php               Version, requires, dependency on report_sql

Tests

PHPUnit tests in tests/block_test.php cover: class load + applicable formats, the editing-teacher capability default, the published-report picker feed, table rendering + footer link, chart mode emitting the Expand trigger, the row-count + "Show all"/"Show fewer" toggle, and empty content when unconfigured. tests/local/report_list_test.php covers the list mode's scoping, toggles, audiences, unpublished reports, and chart links. tests/output/mobile_test.php covers the mobile list output. Shared test plumbing lives in tests/classes/helper.php.

vendor/bin/phpunit --testsuite block_sqlreports_testsuite

How this plugin was created

This block was built as a companion to report_sql, following the same workflow as the parent plugin:

  1. Started from the dependency. report_sql already owned report authoring, publishing, the published-report menu (query::published_menu()), and the per-viewer fetch path (fetch_rows_for_viewer(), current_user_can_view_report()). The block was designed to reuse that API rather than re-implement any SQL or access logic.
  2. Scaffolded a standard Moodle block — block_sqlreports.php, edit_form.php, db/access.php, version.php, lang strings — with correct frankenstyle component naming (block_sqlreports), GPL headers, and a hard dependencies entry on report_sql.
  3. Added rendering paths for table and chart, deferring to core (html_table, \core\chart_*, $OUTPUT->render_chart()) and to the report's saved chart metadata.
  4. Added the client-side Expand modal as an AMD module (amd/src/expand.js), built with grunt into amd/build/, using core modules (core/modal, core/chart_builder, core/chart_output_chartjs, core/str, core/notification).
  5. Hardened the output — fail-closed on filter errors, format_text cleaning, and safe new-tab link rewriting.
  6. Wrote PHPUnit smoke tests against the real report_sql API (publish a view, render the block, assert table/chart output), with a tearDown() that drops the database VIEWs publish() leaves behind so the test reset does not error.

License

GNU GPL v3 or later. © 2026 Marcus Green.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages