From 118a1094998852f26d6925763af3b8b5820a1fd3 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Wed, 18 Dec 2019 21:38:40 +0000 Subject: [PATCH] MDL-67547 dataformat: allow plugins to declare support for HTML. --- dataformat/html/classes/writer.php | 11 +++++++++++ dataformat/json/classes/writer.php | 2 +- dataformat/pdf/classes/writer.php | 23 +++++++++++++++++------ lib/classes/dataformat/base.php | 21 +++++++++++++++++++++ lib/classes/dataformat/spout_base.php | 4 ++-- lib/tablelib.php | 11 ++++++++++- 6 files changed, 62 insertions(+), 10 deletions(-) diff --git a/dataformat/html/classes/writer.php b/dataformat/html/classes/writer.php index a7b60d4ea0e35..49ed857d0fe0f 100644 --- a/dataformat/html/classes/writer.php +++ b/dataformat/html/classes/writer.php @@ -92,6 +92,15 @@ public function start_sheet($columns) { echo \html_writer::end_tag('tr'); } + /** + * Method to define whether the dataformat supports export of HTML + * + * @return bool + */ + public function supports_html(): bool { + return true; + } + /** * Write a single record * @@ -99,6 +108,8 @@ public function start_sheet($columns) { * @param int $rownum */ public function write_record($record, $rownum) { + $record = $this->format_record($record); + echo \html_writer::start_tag('tr'); foreach ($record as $cell) { echo \html_writer::tag('td', $cell); diff --git a/dataformat/json/classes/writer.php b/dataformat/json/classes/writer.php index 599f3fee80907..effd83ecaa9fc 100644 --- a/dataformat/json/classes/writer.php +++ b/dataformat/json/classes/writer.php @@ -80,7 +80,7 @@ public function write_record($record, $rownum) { echo ","; } - echo json_encode($record, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); + echo json_encode($this->format_record($record), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); $this->sheetdatadded = true; } diff --git a/dataformat/pdf/classes/writer.php b/dataformat/pdf/classes/writer.php index 1fce4399d16b0..9768ffd0c4ac5 100644 --- a/dataformat/pdf/classes/writer.php +++ b/dataformat/pdf/classes/writer.php @@ -93,14 +93,25 @@ public function start_sheet($columns) { $this->print_heading(); } + /** + * Method to define whether the dataformat supports export of HTML + * + * @return bool + */ + public function supports_html(): bool { + return true; + } + + /** + * Write a single record + * + * @param array $record + * @param int $rownum + */ public function write_record($record, $rownum) { $rowheight = 0; - // If $record is an object convert it to an array. - if (is_object($record)) { - $record = (array)$record; - } - + $record = $this->format_record($record); foreach ($record as $cell) { $rowheight = max($rowheight, $this->pdf->getStringHeight($this->colwidth, $cell, false, true, '', 1)); } @@ -123,7 +134,7 @@ public function write_record($record, $rownum) { // Determine whether we're at the last element of the record. $nextposition = ($lastkey === $key) ? 1 : 0; // Write the element. - $this->pdf->Multicell($this->colwidth, $rowheight, $cell, 1, 'L', false, $nextposition); + $this->pdf->writeHTMLCell($this->colwidth, $rowheight, '', '', $cell, 1, $nextposition, false, true, 'L'); } } diff --git a/lib/classes/dataformat/base.php b/lib/classes/dataformat/base.php index a0dc3686fa67e..9fb443b320061 100644 --- a/lib/classes/dataformat/base.php +++ b/lib/classes/dataformat/base.php @@ -145,6 +145,27 @@ public function start_sheet($columns) { // Override me if needed. } + /** + * Method to define whether the dataformat supports export of HTML + * + * @return bool + */ + public function supports_html(): bool { + return false; + } + + /** + * Apply formatting to the cells of a given record + * + * @param array|\stdClass $record + * @return array + */ + protected function format_record($record): array { + $record = (array)$record; + + return $record; + } + /** * Write a single record * diff --git a/lib/classes/dataformat/spout_base.php b/lib/classes/dataformat/spout_base.php index 02a827f60f911..4c582707ec301 100644 --- a/lib/classes/dataformat/spout_base.php +++ b/lib/classes/dataformat/spout_base.php @@ -116,11 +116,11 @@ public function start_sheet($columns) { /** * Write a single record * - * @param object $record + * @param array $record * @param int $rownum */ public function write_record($record, $rownum) { - $row = \Box\Spout\Writer\Common\Creator\WriterEntityFactory::createRowFromArray((array)$record); + $row = \Box\Spout\Writer\Common\Creator\WriterEntityFactory::createRowFromArray($this->format_record($record)); $this->writer->addRow($row); } diff --git a/lib/tablelib.php b/lib/tablelib.php index 55a83c2c71913..ff6a1ac018ab3 100644 --- a/lib/tablelib.php +++ b/lib/tablelib.php @@ -2012,7 +2012,7 @@ function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL */ class table_dataformat_export_format extends table_default_export_format_parent { - /** @var $dataformat */ + /** @var \core\dataformat\base $dataformat */ protected $dataformat; /** @var $rownum */ @@ -2047,6 +2047,15 @@ public function __construct(&$table, $dataformat) { \core\session\manager::write_close(); } + /** + * Whether the current dataformat supports export of HTML + * + * @return bool + */ + public function supports_html(): bool { + return $this->dataformat->supports_html(); + } + /** * Start document *