MDL-56642 dataformat: Fixed method_exits parameters
[moodle.git] / lib / classes / dataformat / spout_base.php
blob9208e605b3f347c590509dead064b013dfe8a319
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Common Spout class for dataformat.
20 * @package core
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;
28 /**
29 * Common Spout class for dataformat.
31 * @package core
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 = '';
41 /** @var $writer */
42 protected $writer;
44 /** @var $sheettitle */
45 protected $sheettitle;
47 /**
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);
63 /**
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) {
71 if (!$title) {
72 return;
74 $this->sheettitle = $title;
77 /**
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));
86 /**
87 * Write a single record
89 * @param object $record
90 * @param int $rownum
92 public function write_record($record, $rownum) {
93 $this->writer->addRow(array_values((array)$record));
96 /**
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;