From 703f0b2a0332255c3e45185a32d57eb60c51814f Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Mon, 25 Jul 2022 12:41:17 +0100 Subject: [PATCH] MDL-72058 dataformat: remove HTML when export format lacks support. --- lib/tablelib.php | 22 ++++++++++++++++++---- lib/upgrade.txt | 1 + 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/tablelib.php b/lib/tablelib.php index 70138797506..daaf4718e6f 100644 --- a/lib/tablelib.php +++ b/lib/tablelib.php @@ -2197,7 +2197,17 @@ class table_default_export_format_parent { function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL) { //use some whitespace to indicate where there was some line spacing. $text = str_replace(array('

', "\n", "\r"), ' ', $text); - return strip_tags($text); + return html_entity_decode(strip_tags($text)); + } + + /** + * Format a row of data, removing HTML tags and entities from each of the cells + * + * @param array $row + * @return array + */ + public function format_data(array $row): array { + return array_map([$this, 'format_text'], $row); } } @@ -2284,13 +2294,13 @@ class table_dataformat_export_format extends table_default_export_format_parent * @param array $headers */ public function output_headers($headers) { - $this->columns = $headers; + $this->columns = $this->format_data($headers); if (method_exists($this->dataformat, 'write_header')) { error_log('The function write_header() does not support multiple sheets. In order to support multiple sheets you ' . 'must implement start_output() and start_sheet() and remove write_header() in your dataformat.'); - $this->dataformat->write_header($headers); + $this->dataformat->write_header($this->columns); } else { - $this->dataformat->start_sheet($headers); + $this->dataformat->start_sheet($this->columns); } } @@ -2300,6 +2310,10 @@ class table_dataformat_export_format extends table_default_export_format_parent * @param array $row One record of data */ public function add_data($row) { + if (!$this->supports_html()) { + $row = $this->format_data($row); + } + $this->dataformat->write_record($row, $this->rownum++); return true; } diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 26ecd6c3abe..f07fcf516f3 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -38,6 +38,7 @@ information provided here is intended especially for developers. * Added $CFG->proxylogunsafe and proxyfixunsafe to detect code which doesn't honor the proxy config * Function admin_externalpage_setup() now has additional option 'nosearch' allowing to remove Site administration search form. * The function print_error has been deprecated. Kindly use moodle_exception. +* When exporting table content, HTML tags/entities will be removed when the selected dataformat does not support HTML * The abstract `get_name` method has been moved from `\core\task\scheduled_task` to the `\core\task\task_base` class and can now be implemented by adhoc tasks. For backwards compatibility, a default implementation has been added to `\core\task\adhoc_task` to return the class name. -- 2.11.4.GIT