Files
moodle/reportbuilder/classes/table/system_report_table.php
T
Paul Holden 8ca9e04f9a MDL-70795 reportbuilder: beginnings of the report editor.
Implement UI elements for editing columns of a report, taking their
definitions from the selected datasource.

Co-authored-By: David Matamoros <davidmc@moodle.com>
Co-authored-By: Mikel Martín <mikel@moodle.com>
2021-10-19 13:56:02 +01:00

237 lines
8.5 KiB
PHP

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_reportbuilder\table;
use core_table\local\filter\filterset;
use html_writer;
use moodle_exception;
use stdClass;
use core_reportbuilder\manager;
use core_reportbuilder\local\models\report;
use core_reportbuilder\local\report\action;
use core_reportbuilder\local\report\column;
defined('MOODLE_INTERNAL') || die;
/**
* System report dynamic table class
*
* @package core_reportbuilder
* @copyright 2020 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class system_report_table extends base_report_table {
/** @var string Unique ID prefix for the table */
private const UNIQUEID_PREFIX = 'system-report-table-';
/**
* Table constructor. Note that the passed unique ID value must match the pattern "system-report-table-(\d+)" so that
* dynamic updates continue to load the same report
*
* @param string $uniqueid
* @param array $parameters
* @throws moodle_exception For invalid unique ID
*/
public function __construct(string $uniqueid, array $parameters = []) {
if (!preg_match('/^' . self::UNIQUEID_PREFIX . '(?<id>\d+)$/', $uniqueid, $matches)) {
throw new moodle_exception('invalidsystemreportid', 'core_reportbuilder', '', null, $uniqueid);
}
parent::__construct($uniqueid);
// Load the report persistent, and accompanying system report instance.
$this->persistent = new report($matches['id']);
$this->report = manager::get_report_from_persistent($this->persistent, $parameters);
$fields = $this->report->get_base_fields();
$maintable = $this->report->get_main_table();
$maintablealias = $this->report->get_main_table_alias();
$joins = $this->report->get_joins();
[$where, $params] = $this->report->get_base_condition();
$this->set_attribute('data-region', 'reportbuilder-table');
$this->set_attribute('class', $this->attributes['class'] . ' reportbuilder-table');
// Download options.
$this->showdownloadbuttonsat = [TABLE_P_BOTTOM];
$this->is_downloading($parameters['download'] ?? null, $this->report->get_downloadfilename());
// Retrieve all report columns. If we are downloading the report, remove as required.
$columns = $this->report->get_columns();
if ($this->is_downloading()) {
$columns = array_diff_key($columns,
array_flip($this->report->get_exclude_columns_for_download()));
}
$columnheaders = [];
$columnindex = 1;
foreach ($columns as $identifier => $column) {
$column->set_index($columnindex++);
$columnheaders[$column->get_column_alias()] = $column->get_title();
// Specify whether column should behave as a user fullname column.
if (preg_match('/^user:fullname.*$/', $column->get_unique_identifier())) {
$this->userfullnamecolumns[] = $column->get_column_alias();
}
// Add each columns fields, joins and params to our report.
$fields = array_merge($fields, $column->get_fields());
$joins = array_merge($joins, $column->get_joins());
$params = array_merge($params, $column->get_params());
// Disable sorting for some columns.
if (!$column->get_is_sortable()) {
$this->no_sorting($column->get_column_alias());
}
}
// If the report has any actions then append appropriate column, note that actions are excluded during download.
if ($this->report->has_actions() && !$this->is_downloading()) {
$columnheaders['actions'] = html_writer::tag('span', get_string('actions', 'core_reportbuilder'), [
'class' => 'sr-only',
]);
$this->no_sorting('actions');
}
$this->define_columns(array_keys($columnheaders));
$this->define_headers(array_values($columnheaders));
// Initial table sort column.
if ($sortcolumn = $this->report->get_initial_sort_column()) {
$this->sortable(true, $sortcolumn->get_column_alias(), $this->report->get_initial_sort_direction());
}
// Table configuration.
$this->initialbars(false);
$this->collapsible(false);
$this->pageable(true);
$this->set_default_per_page($this->report->get_default_per_page());
// Initialise table SQL properties.
$fieldsql = implode(', ', $fields);
$this->init_sql($fieldsql, "{{$maintable}} {$maintablealias}", $joins, $where, $params);
}
/**
* Return a new instance of the class for given report ID. We include report parameters here so they are present during
* initialisation
*
* @param int $reportid
* @param array $parameters
* @return static
*/
public static function create(int $reportid, array $parameters): self {
return new static(self::UNIQUEID_PREFIX . $reportid, $parameters);
}
/**
* Set the filterset in the table class. We set the report parameters here so that they are persisted while paging
*
* @param filterset $filterset
*/
public function set_filterset(filterset $filterset): void {
$parameters = $filterset->get_filter('parameters')->current();
$this->report->set_parameters((array) json_decode($parameters, true));
parent::set_filterset($filterset);
}
/**
* Override parent method for retrieving row class with that defined by the system report
*
* @param array|stdClass $row
* @return string
*/
public function get_row_class($row) {
return $this->report->get_row_class((object) $row);
}
/**
* Format each row of returned data, executing defined callbacks for the row and each column
*
* @param array|stdClass $row
* @return array
*/
public function format_row($row) {
$this->report->row_callback((object) $row);
/** @var column[] $columnsbyalias */
$columnsbyalias = [];
// Create a lookup for convenience, indexed by column alias.
$columns = $this->report->get_columns();
foreach ($columns as $column) {
$columnsbyalias[$column->get_column_alias()] = $column;
}
// Walk over the row, and for any key that matches one of our column aliases, call that columns format method.
$row = (array) $row;
array_walk($row, static function(&$value, $key) use ($row, $columnsbyalias): void {
if (array_key_exists($key, $columnsbyalias)) {
$value = $columnsbyalias[$key]->format_value($row);
}
});
if ($this->report->has_actions()) {
$row['actions'] = $this->format_row_actions((object) $row);
}
return $row;
}
/**
* Return formatted actions column for the row
*
* @param stdClass $row
* @return string
*/
private function format_row_actions(stdClass $row): string {
$actions = array_map(static function(action $action) use ($row): string {
return (string) $action->get_action_link($row);
}, $this->report->get_actions());
return implode('', $actions);
}
/**
* Get the html for the download buttons
*
* @return string
*/
public function download_buttons(): string {
global $OUTPUT;
if ($this->report->can_be_downloaded() && !$this->is_downloading()) {
return $OUTPUT->download_dataformat_selector(
get_string('downloadas', 'table'),
new \moodle_url('/reportbuilder/download.php'),
'download',
[
'id' => $this->persistent->get('id'),
'parameters' => json_encode($this->report->get_parameters()),
]
);
}
return '';
}
}