MDL-73467 reportbuilder: consistent scheduled report export class.

Use the same dataformat exporter in scheduled reports as that used
when downloading reports, from 7d753bed.
This commit is contained in:
Paul Holden
2022-01-12 17:41:41 +00:00
parent e78184d827
commit b625b9ffed
2 changed files with 26 additions and 5 deletions
@@ -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);
}
);
@@ -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;
}
}