2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Common Spout class for dataformat.
21 * @subpackage dataformat
22 * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 namespace core\dataformat
;
29 * Common Spout class for dataformat.
32 * @subpackage dataformat
33 * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net)
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 abstract class spout_base
extends \core\dataformat\base
{
38 /** @var $spouttype */
39 protected $spouttype = '';
44 /** @var $sheettitle */
45 protected $sheettitle;
48 * Output file headers to initialise the download of the file.
50 public function send_http_headers() {
51 $this->writer
= \Box\Spout\Writer\WriterFactory
::create($this->spouttype
);
52 if (method_exists($this->writer
, 'setTempFolder')) {
53 $this->writer
->setTempFolder(make_request_directory());
55 $filename = $this->filename
. $this->get_extension();
56 $this->writer
->openToBrowser($filename);
57 if ($this->sheettitle
&& $this->writer
instanceof \Box\Spout\Writer\AbstractMultiSheetsWriter
) {
58 $sheet = $this->writer
->getCurrentSheet();
59 $sheet->setName($this->sheettitle
);
64 * Set the title of the worksheet inside a spreadsheet
66 * For some formats this will be ignored.
68 * @param string $title
70 public function set_sheettitle($title) {
74 $this->sheettitle
= $title;
78 * Write the start of the format
80 * @param array $columns
82 public function write_header($columns) {
83 $this->writer
->addRow(array_values((array)$columns));
87 * Write a single record
89 * @param object $record
92 public function write_record($record, $rownum) {
93 $this->writer
->addRow(array_values((array)$record));
97 * Write the end of the format
99 * @param array $columns
101 public function write_footer($columns) {
102 $this->writer
->close();
103 $this->writer
= null;