diff --git a/reportbuilder/classes/local/helpers/schedule.php b/reportbuilder/classes/local/helpers/schedule.php index 1f237718b84..5ecbc78b9cf 100644 --- a/reportbuilder/classes/local/helpers/schedule.php +++ b/reportbuilder/classes/local/helpers/schedule.php @@ -28,6 +28,7 @@ use core\message\message; use core\plugininfo\dataformat; use core_reportbuilder\local\models\audience as audience_model; use core_reportbuilder\local\models\schedule as model; +use core_reportbuilder\output\dataformat_export_format; use core_reportbuilder\table\custom_report_table_view; /** @@ -153,6 +154,13 @@ class schedule { $table->setup(); $table->query_db(0, false); + // Set up table as if it were being downloaded, retrieve appropriate export class (ensure output buffer is + // cleaned in order to instantiate export class without exception). + ob_start(); + $table->download = $schedule->get('format'); + $exportclass = new dataformat_export_format($table, $table->download); + ob_end_clean(); + // Create our schedule report stored file. $context = context_user::instance($schedule->get('usercreated')); $filerecord = [ @@ -166,11 +174,12 @@ class schedule { $storedfile = \core\dataformat::write_data_to_filearea( $filerecord, - $schedule->get('format'), + $table->download, $table->headers, $table->rawdata, - static function(stdClass $record) use ($table): array { - return $table->format_row($record); + static function(stdClass $record) use ($table, $exportclass): array { + $record = $table->format_row($record); + return $exportclass->format_data($record); } ); diff --git a/reportbuilder/classes/output/dataformat_export_format.php b/reportbuilder/classes/output/dataformat_export_format.php index 5b98215becc..230c8b5488f 100644 --- a/reportbuilder/classes/output/dataformat_export_format.php +++ b/reportbuilder/classes/output/dataformat_export_format.php @@ -34,16 +34,28 @@ require_once("{$CFG->libdir}/tablelib.php"); 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 + * Add a row of data * * @param array $row * @return bool */ public function add_data($row): bool { + $row = $this->format_data($row); + + return parent::add_data($row); + } + + /** + * Format a row of data. If the export format doesn't support HTML, then format cell contents to remove tags + * + * @param array $row + * @return array + */ + public function format_data(array $row): array { if (!$this->dataformat->supports_html()) { $row = array_map([$this, 'format_text'], $row); } - return parent::add_data($row); + return $row; } }