From 7d753bed0b1b4739f6d9c1e36d15b701d03807c3 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Mon, 6 Dec 2021 09:42:43 +0000 Subject: [PATCH] MDL-73275 reportbuilder: dataformat exporter for downloading. If the current dataformat doesn't support HTML then clean the text and remove all tags. --- .../output/dataformat_export_format.php | 49 +++++++++++++++++++ .../classes/table/base_report_table.php | 19 +++++++ 2 files changed, 68 insertions(+) create mode 100644 reportbuilder/classes/output/dataformat_export_format.php diff --git a/reportbuilder/classes/output/dataformat_export_format.php b/reportbuilder/classes/output/dataformat_export_format.php new file mode 100644 index 00000000000..5b98215becc --- /dev/null +++ b/reportbuilder/classes/output/dataformat_export_format.php @@ -0,0 +1,49 @@ +. + +declare(strict_types=1); + +namespace core_reportbuilder\output; + +use table_dataformat_export_format; + +defined('MOODLE_INTERNAL') || die(); + +require_once("{$CFG->libdir}/tablelib.php"); + +/** + * Dataformat export class for reports + * + * @package core_reportbuilder + * @copyright 2021 Paul Holden + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class dataformat_export_format extends table_dataformat_export_format { + + /** + * Add a row of data. If the export format doesn't support HTML, then format cell contents to remove tags + * + * @param array $row + * @return bool + */ + public function add_data($row): bool { + if (!$this->dataformat->supports_html()) { + $row = array_map([$this, 'format_text'], $row); + } + + return parent::add_data($row); + } +} diff --git a/reportbuilder/classes/table/base_report_table.php b/reportbuilder/classes/table/base_report_table.php index f055c83bb95..ba3ab5e0c4c 100644 --- a/reportbuilder/classes/table/base_report_table.php +++ b/reportbuilder/classes/table/base_report_table.php @@ -21,6 +21,7 @@ namespace core_reportbuilder\table; use context; use moodle_url; use renderable; +use table_default_export_format_parent; use table_sql; use html_writer; use core_table\dynamic; @@ -29,6 +30,7 @@ use core_reportbuilder\local\filters\base; use core_reportbuilder\local\models\report; use core_reportbuilder\local\report\base as base_report; use core_reportbuilder\local\report\filter; +use core_reportbuilder\output\dataformat_export_format; use core\output\notification; defined('MOODLE_INTERNAL') || die; @@ -173,6 +175,23 @@ abstract class base_report_table extends table_sql implements dynamic, renderabl } } + /** + * Set the export class to use when downloading reports (TODO: consider applying to all tables, MDL-72058) + * + * @param table_default_export_format_parent|null $exportclass + * @return table_default_export_format_parent|null + */ + public function export_class_instance($exportclass = null) { + if (is_null($this->exportclass) && $this->is_downloading()) { + $this->exportclass = new dataformat_export_format($this, $this->download); + if (!$this->exportclass->document_started()) { + $this->exportclass->start_document($this->filename, $this->sheettitle); + } + } + + return $this->exportclass; + } + /** * Get the context for the table (that of the report persistent) *