MDL-82183 core_table: Move tablelib classes to core_table
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once("{$CFG->libdir}/tablelib.php");
|
||||
|
||||
use core\dataformat;
|
||||
|
||||
/**
|
||||
* Dataformat exporter
|
||||
*
|
||||
* @package core
|
||||
* @subpackage tablelib
|
||||
* @copyright 2016 Brendan Heywood ([email protected])
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class table_dataformat_export_format extends table_default_export_format_parent {
|
||||
|
||||
/** @var \core\dataformat\base $dataformat */
|
||||
protected $dataformat;
|
||||
|
||||
/** @var int $rownum */
|
||||
protected $rownum = 0;
|
||||
|
||||
/** @var array $columns */
|
||||
protected $columns;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $table An sql table
|
||||
* @param string $dataformat type of dataformat for export
|
||||
*/
|
||||
public function __construct(&$table, $dataformat) {
|
||||
parent::__construct($table);
|
||||
|
||||
if (ob_get_length()) {
|
||||
throw new coding_exception("Output can not be buffered before instantiating table_dataformat_export_format");
|
||||
}
|
||||
|
||||
$this->dataformat = dataformat::get_format_instance($dataformat);
|
||||
|
||||
// The dataformat export time to first byte could take a while to generate...
|
||||
set_time_limit(0);
|
||||
|
||||
// Close the session so that the users other tabs in the same session are not blocked.
|
||||
\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
|
||||
*
|
||||
* @param string $filename
|
||||
* @param string $sheettitle
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start export
|
||||
*
|
||||
* @param string $sheettitle optional spreadsheet worksheet title
|
||||
*/
|
||||
public function start_table($sheettitle) {
|
||||
$this->dataformat->set_sheettitle($sheettitle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output headers
|
||||
*
|
||||
* @param array $headers
|
||||
*/
|
||||
public function output_headers($headers) {
|
||||
$this->columns = $this->format_data($headers);
|
||||
if (method_exists($this->dataformat, 'write_header')) {
|
||||
error_log('The function write_header() does not support multiple sheets. In order to support multiple sheets you ' .
|
||||
'must implement start_output() and start_sheet() and remove write_header() in your dataformat.');
|
||||
$this->dataformat->write_header($this->columns);
|
||||
} else {
|
||||
$this->dataformat->start_sheet($this->columns);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a row of data
|
||||
*
|
||||
* @param array $row One record of data
|
||||
*/
|
||||
public function add_data($row) {
|
||||
if (!$this->supports_html()) {
|
||||
$row = $this->format_data($row);
|
||||
}
|
||||
|
||||
$this->dataformat->write_record($row, $this->rownum++);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finish export
|
||||
*/
|
||||
public function finish_table() {
|
||||
if (method_exists($this->dataformat, 'write_footer')) {
|
||||
error_log('The function write_footer() does not support multiple sheets. In order to support multiple sheets 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() {
|
||||
$this->dataformat->close_output();
|
||||
exit();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user