Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 39 additions & 23 deletions classes/form/edit_query_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,6 @@ protected function definition() {
);

$this->add_audience_elements($mform);

// Authors get a plain Save (draft); approvers additionally get a one-click Save & publish so
// they don't have to round-trip through the index page to publish. The capability is also
// re-checked in edit.php before publishing — the button is convenience, not the gate.
$buttonarray = [];
$buttonarray[] = $mform->createElement('submit', 'submitbutton', get_string('savechanges'));
if (!empty($this->_customdata['canpublish'])) {
$buttonarray[] = $mform->createElement(
'submit',
'saveandpublish',
get_string('saveandpublish', 'report_sql')
);
// Publish but stay on the form to iterate (republish, configure the just-unlocked
// chart / filters) without round-tripping the index. edit.php redirects back here.
$buttonarray[] = $mform->createElement(
'submit',
'saveandpublishedit',
get_string('saveandpublishedit', 'report_sql')
);
}
$buttonarray[] = $mform->createElement('cancel');
$mform->addGroup($buttonarray, 'buttonar', '', [' '], false);
$mform->closeHeaderBefore('buttonar');
}

/**
Expand Down Expand Up @@ -254,6 +231,16 @@ private function add_audience_elements(\MoodleQuickForm $mform): void {
* Add chart configuration fields once column metadata is available (published queries only).
*/
public function definition_after_data() {
parent::definition_after_data();
$this->add_late_sections();
// Added last so the buttons sit below the sections that depend on the saved record.
$this->add_action_buttons_group();
}

/**
* Add the chart / filter sections that depend on the saved record.
*/
private function add_late_sections(): void {
global $DB;

$mform = $this->_form;
Expand Down Expand Up @@ -481,4 +468,33 @@ public function validation($data, $files): array {

return $errors;
}

/**
* Add the save / publish / cancel buttons.
*/
private function add_action_buttons_group(): void {
$mform = $this->_form;
// Authors get a plain Save (draft); approvers additionally get a one-click Save & publish so
// they don't have to round-trip through the index page to publish. The capability is also
// re-checked in edit.php before publishing — the button is convenience, not the gate.
$buttonarray = [];
$buttonarray[] = $mform->createElement('submit', 'submitbutton', get_string('savechanges'));
if (!empty($this->_customdata['canpublish'])) {
$buttonarray[] = $mform->createElement(
'submit',
'saveandpublish',
get_string('saveandpublish', 'report_sql')
);
// Publish but stay on the form to iterate (republish, configure the just-unlocked
// chart / filters) without round-tripping the index. edit.php redirects back here.
$buttonarray[] = $mform->createElement(
'submit',
'saveandpublishedit',
get_string('saveandpublishedit', 'report_sql')
);
}
$buttonarray[] = $mform->createElement('cancel');
$mform->addGroup($buttonarray, 'buttonar', '', [' '], false);
$mform->closeHeaderBefore('buttonar');
}
}
11 changes: 10 additions & 1 deletion classes/reportbuilder/local/entities/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,23 @@ private function get_all_columns(): array {
$columns[] = (new column('name', new lang_string('name', 'report_sql'), $this->get_entity_name()))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_fields("{$q}.name, {$q}.chartmeta, {$q}.querysql")
->add_fields("{$q}.name, {$q}.id, {$q}.ownerid, {$q}.chartmeta, {$q}.querysql")
->set_is_sortable(true, ["{$q}.name"])
->add_callback(static function ($value, \stdClass $row): string {
if ($value === null) {
return '';
}
$name = format_string($value);

// Link to the edit page for users who may edit (admin-owned queries are locked to admins).
global $USER;
if (
has_capability('report/sql:author', \context_system::instance())
&& (!is_siteadmin($row->ownerid) || is_siteadmin($USER))
) {
$name = html_writer::link(new \moodle_url('/report/sql/edit.php', ['id' => $row->id]), $name);
}

// Cap the column width (main used a 33% html_table size); long names wrap within it.
$wrap = static fn(string $html): string => \html_writer::span($html, 'rs-query-name');

Expand Down
18 changes: 17 additions & 1 deletion classes/reportbuilder/local/systemreports/queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,24 @@ protected function initialise(): void {
"{$alias}.id, {$alias}.status, {$alias}.reportid, {$alias}.chartreportid, {$alias}.ownerid, {$alias}.chartmeta"
);

$this->add_columns_from_entities(["{$entityname}:name"]);

// Link to the live report, in its own column beside the name. Only published queries have one.
$this->add_column((new column('viewreport', new lang_string('runreport', 'report_sql'), $entityname))
->set_type(column::TYPE_TEXT)
->add_fields("{$alias}.reportid, {$alias}.status")
->set_is_sortable(false)
->add_callback(static function ($value, \stdClass $row): string {
if ($row->status !== query::STATUS_PUBLISHED || empty($row->reportid)) {
return '';
}
return html_writer::link(
new moodle_url('/reportbuilder/view.php', ['id' => $row->reportid]),
get_string('runreport', 'report_sql')
);
}));

$columns = [
"{$entityname}:name",
"{$entityname}:owner",
"{$entityname}:course",
"{$entityname}:status",
Expand Down
56 changes: 30 additions & 26 deletions classes/reportbuilder/local/systemreports/usage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@

namespace report_sql\reportbuilder\local\systemreports;

use core_reportbuilder\local\report\action;
use core_reportbuilder\local\report\column;
use core_reportbuilder\system_report;
use lang_string;
use report_sql\local\query;
use report_sql\reportbuilder\local\entities\query as query_entity;
use moodle_url;
use pix_icon;

/**
* System report showing how often each published report source has been opened.
Expand All @@ -39,7 +38,7 @@
*/
class usage extends system_report {
/**
* Initialise the report: main table, entity, columns, filters, action.
* Initialise the report: main table, entity, columns and filters.
*/
protected function initialise(): void {
$entity = new query_entity();
Expand All @@ -49,11 +48,36 @@ protected function initialise(): void {
$this->set_main_table('report_sql_query', $alias);
$this->add_entity($entity);

// Consumed by the row actions below.
$this->add_base_fields("{$alias}.id, {$alias}.reportid");
$this->add_columns_from_entities(["{$entityname}:name"]);

// Link to the live report, in its own column beside the name.
$this->add_column((new column('viewreport', new lang_string('runreport', 'report_sql'), $entityname))
->set_type(column::TYPE_TEXT)
->add_field("{$alias}.reportid")
->set_is_sortable(false)
->add_callback(static function ($value): string {
if (empty($value)) {
return '';
}
return \html_writer::link(
new moodle_url('/reportbuilder/view.php', ['id' => $value]),
get_string('runreport', 'report_sql')
);
}));

// Link to this source's usage detail (who / trend / recent), in its own column.
$this->add_column((new column('viewdetail', new lang_string('usage:detaillabel', 'report_sql'), $entityname))
->set_type(column::TYPE_TEXT)
->add_field("{$alias}.id")
->set_is_sortable(false)
->add_callback(static function ($value): string {
return \html_writer::link(
new moodle_url('/report/sql/query_usage.php', ['id' => $value]),
get_string('usage:detaillabel', 'report_sql')
);
}));

$this->add_columns_from_entities([
"{$entityname}:name",
"{$entityname}:owner",
"{$entityname}:course",
"{$entityname}:viewcount",
Expand All @@ -73,26 +97,6 @@ protected function initialise(): void {
// Only published queries have a report and can accrue views.
$this->add_base_condition_simple("{$alias}.status", query::STATUS_PUBLISHED);

// Drill into this source's usage detail (who / trend / recent).
$this->add_action((new action(
new moodle_url('/report/sql/query_usage.php', ['id' => ':id']),
new pix_icon('i/stats', ''),
[],
false,
new lang_string('usage:detaillabel', 'report_sql')
)));

// Open the live report.
$this->add_action((new action(
new moodle_url('/reportbuilder/view.php', ['id' => ':reportid']),
new pix_icon('i/report', ''),
[],
false,
new lang_string('runreport', 'report_sql')
))->add_callback(static function (\stdClass $row): bool {
return !empty($row->reportid);
}));

// Default sort: busiest report sources first.
$this->set_initial_sort_column("{$entityname}:viewcount", SORT_DESC);
$this->set_default_per_page(50);
Expand Down
43 changes: 36 additions & 7 deletions docs.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@

require(__DIR__ . '/../../config.php');

require_login();
require_once($CFG->libdir . '/adminlib.php');

$context = context_system::instance();
// Only authors (people who can create report views) get the docs page.
require_capability('report/sql:author', $context);

$PAGE->set_context($context);
$PAGE->set_url(new moodle_url('/report/sql/docs.php'));
$PAGE->set_pagelayout('admin');
// Registered in the admin tree (settings.php) gated by report/sql:author, so only authors get the page
// and the primary/secondary navigation and breadcrumb are set.
// The listing is the active parent, so tabs and the breadcrumb trail run through index.php.
navigation_node::override_active_url(new moodle_url('/report/sql/index.php'));
admin_externalpage_setup('report_sql_docs', '', null, new moodle_url('/report/sql/docs.php'));
$PAGE->navbar->add(get_string('userdocs', 'report_sql'));
$PAGE->add_body_class('limitedwidth');
$PAGE->set_title(get_string('userdocs', 'report_sql'));
$PAGE->set_heading(get_string('reportsources', 'report_sql'));

Expand Down Expand Up @@ -85,5 +88,31 @@ function ($m) use ($slugger) {
$html
);

echo $OUTPUT->box($html, 'generalbox rs-docs');
// Lift the "Contents" list out of the flow. It is output first so on small and medium screens it sits
// at the top of the content, and on large screens (see styles.css) it floats in the left margin outside
// the content box without narrowing it (the list is rendered twice, once per breakpoint).
$toc = '';
if (preg_match('/<h2 id="contents">.*?<\/ol>/s', $html, $m)) {
$toc = $m[0];
$html = str_replace($m[0], '', $html);
// The "---" rules on either side of the list would leave a double divider behind.
$html = preg_replace('/(<hr\s*\/?>\s*){2,}/', '<hr />', $html, 1);
}

// Below xxl the list is plain content at the top of the box. From xxl up it floats in the left page margin
// (as in mod_cms's type edit page): absolutely positioned outside a relative wrapper, sticky within it.
$floating = '';
if ($toc !== '') {
$floating = html_writer::tag(
'nav',
html_writer::div($toc, 'position-sticky overflow-auto', ['style' => 'top: 9rem; max-height: calc(100vh - 10rem);']),
[
'class' => 'rs-docs-toc d-none d-xxl-block position-absolute top-0 bottom-0 end-100 me-3',
'style' => 'width: 16rem;',
'aria-label' => get_string('userdocs', 'report_sql'),
]
);
$toc = html_writer::div($toc, 'd-xxl-none mb-4');
}
echo html_writer::div($floating . $OUTPUT->box($toc . $html, 'generalbox rs-docs'), 'position-relative');
echo $OUTPUT->footer();
Loading
Loading