From 4ba0709401303f77348eb04e962f38b03e04adeb Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Wed, 24 May 2017 22:08:13 +0800 Subject: [PATCH] MDL-56046 dataformat_*: convert core plugins --- dataformat/html/classes/writer.php | 31 ++++++++++++++++-------- dataformat/json/classes/writer.php | 38 ++++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/dataformat/html/classes/writer.php b/dataformat/html/classes/writer.php index 90ca3688a77..0eaa5559725 100644 --- a/dataformat/html/classes/writer.php +++ b/dataformat/html/classes/writer.php @@ -42,11 +42,9 @@ class writer extends \core\dataformat\base { public $extension = ".html"; /** - * Write the start of the format - * - * @param array $columns + * Write the start of the output */ - public function write_header($columns) { + public function start_output() { echo ""; echo \html_writer::tag('title', $this->filename); echo " - - -"; +"; + } + + /** + * Write the start of the sheet we will be adding data to. + * + * @param array $columns + */ + public function start_sheet($columns) { + echo "
"; echo \html_writer::start_tag('tr'); foreach ($columns as $k => $v) { echo \html_writer::tag('th', $v); @@ -100,12 +105,18 @@ table { } /** - * Write the end of the format + * Write the end of the sheet containing the data. * * @param array $columns */ - public function write_footer($columns) { - echo "
"; + public function close_sheet($columns) { + echo ""; } + /** + * Write the end of the sheet containing the data. + */ + public function close_output() { + echo ""; + } } diff --git a/dataformat/json/classes/writer.php b/dataformat/json/classes/writer.php index 84f3cc67ccc..fe741a6ae93 100644 --- a/dataformat/json/classes/writer.php +++ b/dataformat/json/classes/writer.php @@ -41,12 +41,31 @@ class writer extends \core\dataformat\base { /** @var $extension */ public $extension = ".json"; + /** @var $hasstarted */ + public $sheetstarted = false; + + /** @var $sheetdatadded */ + public $sheetdatadded = false; + /** - * Write the start of the format + * Write the start of the file. + */ + public function start_output() { + echo "["; + } + + /** + * 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) { + if ($this->sheetstarted) { + echo ","; + } else { + $this->sheetstarted = true; + } + $this->sheetdatadded = false; echo "["; } @@ -57,19 +76,28 @@ class writer extends \core\dataformat\base { * @param int $rownum */ public function write_record($record, $rownum) { - if ($rownum) { + if ($this->sheetdatadded) { echo ","; } + echo json_encode($record); + + $this->sheetdatadded = true; } /** - * 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) { echo "]"; } + /** + * Write the end of the file. + */ + public function close_output() { + echo "]"; + } }