Merge branch 'MDL-56046_master' of git://github.com/markn86/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2017-06-27 02:42:31 +02:00
commit 7ad2822269
7 changed files with 145 additions and 47 deletions

View File

@ -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 "<!DOCTYPE html><html>";
echo \html_writer::tag('title', $this->filename);
echo "<style>
@ -75,9 +73,16 @@ table {
margin: auto;
}
</style>
<body>
<table border=1 cellspacing=0 cellpadding=3>
";
<body>";
}
/**
* Write the start of the sheet we will be adding data to.
*
* @param array $columns
*/
public function start_sheet($columns) {
echo "<table border=1 cellspacing=0 cellpadding=3>";
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 "</table></body></html>";
public function close_sheet($columns) {
echo "</table>";
}
/**
* Write the end of the sheet containing the data.
*/
public function close_output() {
echo "</body></html>";
}
}

View File

@ -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 "]";
}
}

View File

@ -1,7 +1,14 @@
This files describes API changes in /dataformat/ download system,
information provided here is intended especially for developers.
=== 3.4 ===
* In order to allow multiple sheets in an exported file the functions write_header() and write_footer() have
been removed from core dataformats plugins and have been replaced.
- write_header() has been split into the two functions start_output() and start_sheet().
- write_footer() has been split into the two functions close_output() and close_sheet().
For backwards compatibility write_header() and write_footer() will continue to work but if used will
trigger the function error_log().
=== 3.1 ===
* Added new plugin system with low memory support for csv, ods, xls and json

View File

@ -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.
}
}

View File

@ -44,6 +44,9 @@ abstract class spout_base extends \core\dataformat\base {
/** @var $sheettitle */
protected $sheettitle;
/** @var $renamecurrentsheet */
protected $renamecurrentsheet = false;
/**
* Output file headers to initialise the download of the file.
*/
@ -54,10 +57,9 @@ abstract class spout_base extends \core\dataformat\base {
}
$filename = $this->filename . $this->get_extension();
$this->writer->openToBrowser($filename);
if ($this->sheettitle && $this->writer instanceof \Box\Spout\Writer\AbstractMultiSheetsWriter) {
$sheet = $this->writer->getCurrentSheet();
$sheet->setName($this->sheettitle);
}
// By default one sheet is always created, but we want to rename it when we call start_sheet().
$this->renamecurrentsheet = true;
}
/**
@ -68,18 +70,24 @@ abstract class spout_base extends \core\dataformat\base {
* @param string $title
*/
public function set_sheettitle($title) {
if (!$title) {
return;
}
$this->sheettitle = $title;
}
/**
* 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) {
if ($this->sheettitle && $this->writer instanceof \Box\Spout\Writer\AbstractMultiSheetsWriter) {
if ($this->renamecurrentsheet) {
$sheet = $this->writer->getCurrentSheet();
$this->renamecurrentsheet = false;
} else {
$sheet = $this->writer->addNewSheetAndMakeItCurrent();
}
$sheet->setName($this->sheettitle);
}
$this->writer->addRow(array_values((array)$columns));
}
@ -94,13 +102,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;
}
}

View File

@ -56,7 +56,15 @@ function download_as_dataformat($filename, $dataformat, $columns, $iterator, $ca
$format->set_filename($filename);
$format->send_http_headers();
$format->write_header($columns);
// This exists to support all dataformats - see MDL-56046.
if (method_exists($format, 'write_header')) {
error_log('The function write_header() does not support multiple tables. In order to support multiple tables you ' .
'must implement start_output() and start_sheet() and remove write_header() in your dataformat.');
$format->write_header($columns);
} else {
$format->start_output();
$format->start_sheet($columns);
}
$c = 0;
foreach ($iterator as $row) {
if ($callback) {
@ -67,6 +75,14 @@ function download_as_dataformat($filename, $dataformat, $columns, $iterator, $ca
}
$format->write_record($row, $c++);
}
$format->write_footer($columns);
// This exists to support all dataformats - see MDL-56046.
if (method_exists($format, 'write_footer')) {
error_log('The function write_footer() does not support multiple tables. In order to support multiple tables you ' .
'must implement close_sheet() and close_output() and remove write_footer() in your dataformat.');
$format->write_footer($columns);
} else {
$format->close_sheet($columns);
$format->close_output();
}
}

View File

@ -125,6 +125,12 @@ class flexible_table {
*/
private $prefs = array();
/** @var $sheettitle */
protected $sheettitle;
/** @var $filename */
protected $filename;
/**
* Constructor
* @param string $uniqueid all tables have to have a unique id, this is used
@ -180,7 +186,7 @@ class flexible_table {
} else if (is_null($this->exportclass) && !empty($this->download)) {
$this->exportclass = new table_dataformat_export_format($this, $this->download);
if (!$this->exportclass->document_started()) {
$this->exportclass->start_document($this->filename);
$this->exportclass->start_document($this->filename, $this->sheettitle);
}
}
return $this->exportclass;
@ -1741,11 +1747,14 @@ class table_dataformat_export_format extends table_default_export_format_parent
* Start document
*
* @param string $filename
* @param string $sheettitle
*/
public function start_document($filename) {
$this->filename = $filename;
public function start_document($filename, $sheettitle) {
$this->documentstarted = true;
$this->dataformat->set_filename($filename);
$this->dataformat->send_http_headers();
$this->dataformat->set_sheettitle($sheettitle);
$this->dataformat->start_output();
}
/**
@ -1755,7 +1764,6 @@ class table_dataformat_export_format extends table_default_export_format_parent
*/
public function start_table($sheettitle) {
$this->dataformat->set_sheettitle($sheettitle);
$this->dataformat->send_http_headers();
}
/**
@ -1765,7 +1773,13 @@ class table_dataformat_export_format extends table_default_export_format_parent
*/
public function output_headers($headers) {
$this->columns = $headers;
$this->dataformat->write_header($headers);
if (method_exists($this->dataformat, 'write_header')) {
error_log('The function write_header() does not support multiple tables. In order to support multiple tables you ' .
'must implement start_output() and start_sheet() and remove write_header() in your dataformat.');
$this->dataformat->write_header($headers);
} else {
$this->dataformat->start_sheet($headers);
}
}
/**
@ -1782,15 +1796,21 @@ class table_dataformat_export_format extends table_default_export_format_parent
* Finish export
*/
public function finish_table() {
$this->dataformat->write_footer($this->columns);
if (method_exists($this->dataformat, 'write_footer')) {
error_log('The function write_footer() does not support multiple tables. In order to support multiple tables you ' .
'must implement close_sheet() and close_output() and remove write_footer() in your dataformat.');
$this->dataformat->write_footer($this->columns);
} else {
$this->dataformat->close_sheet($this->columns);
}
}
/**
* Finish download
*/
public function finish_document() {
exit;
$this->dataformat->close_output();
exit();
}
}