From b62b5879af34da8f02a4a0522ec3d8576ea2c163 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Wed, 24 May 2017 23:11:38 +0800 Subject: [PATCH] MDL-56046 core_dataformat: added functions to support multiple sheets Also removed write_header() and write_footer(). The reason for this is because in core we want to know if the format being used supports multiple sheets. To do this, and ensure we do not break third-party dataformat plugins, we are using method_exist(). If write_header() and write_footer() remain in the base class then this will always be true. --- lib/classes/dataformat/base.php | 23 +++++++++++++++++------ lib/classes/dataformat/spout_base.php | 11 ++++------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/classes/dataformat/base.php b/lib/classes/dataformat/base.php index d9fe32da04d..d00e46f537c 100644 --- a/lib/classes/dataformat/base.php +++ b/lib/classes/dataformat/base.php @@ -76,8 +76,6 @@ abstract class base { * Output file headers to initialise the download of the file. */ public function send_http_headers() { - global $CFG; - if (defined('BEHAT_SITE_RUNNING')) { // For text based formats - we cannot test the output with behat if we force a file download. return; @@ -98,11 +96,18 @@ abstract class base { } /** - * Write the start of the format + * Write the start of the file. + */ + public function start_output() { + // Override me if needed. + } + + /** + * Write the start of the sheet we will be adding data to. * * @param array $columns */ - public function write_header($columns) { + public function start_sheet($columns) { // Override me if needed. } @@ -115,12 +120,18 @@ abstract class base { abstract public function write_record($record, $rownum); /** - * Write the end of the format + * Write the end of the sheet containing the data. * * @param array $columns */ - public function write_footer($columns) { + public function close_sheet($columns) { // Override me if needed. } + /** + * Write the end of the file. + */ + public function close_output() { + // Override me if needed. + } } diff --git a/lib/classes/dataformat/spout_base.php b/lib/classes/dataformat/spout_base.php index 9208e605b3f..6e622ea8beb 100644 --- a/lib/classes/dataformat/spout_base.php +++ b/lib/classes/dataformat/spout_base.php @@ -75,11 +75,11 @@ abstract class spout_base extends \core\dataformat\base { } /** - * Write the start of the format + * Write the start of the sheet we will be adding data to. * * @param array $columns */ - public function write_header($columns) { + public function start_sheet($columns) { $this->writer->addRow(array_values((array)$columns)); } @@ -94,13 +94,10 @@ abstract class spout_base extends \core\dataformat\base { } /** - * Write the end of the format - * - * @param array $columns + * Write the end of the file. */ - public function write_footer($columns) { + public function close_output() { $this->writer->close(); $this->writer = null; } - }