MDL-65919 core: Skip send headers during phpunit exec in dataformats
[moodle.git] / lib / classes / dataformat / spout_base.php
CommitLineData
bff1edbe
BH
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.
13//
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/>.
16
17/**
18 * Common Spout class for dataformat.
19 *
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
24 */
25
26namespace core\dataformat;
27
28/**
29 * Common Spout class for dataformat.
30 *
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
35 */
36abstract class spout_base extends \core\dataformat\base {
37
38 /** @var $spouttype */
39 protected $spouttype = '';
40
41 /** @var $writer */
42 protected $writer;
43
44 /** @var $sheettitle */
45 protected $sheettitle;
46
faf45f9c
MN
47 /** @var $renamecurrentsheet */
48 protected $renamecurrentsheet = false;
49
bff1edbe
BH
50 /**
51 * Output file headers to initialise the download of the file.
52 */
53 public function send_http_headers() {
54968b15 54 $this->writer = \Box\Spout\Writer\Common\Creator\WriterEntityFactory::createWriter($this->spouttype);
79cb2717 55 if (method_exists($this->writer, 'setTempFolder')) {
1491f215
MG
56 $this->writer->setTempFolder(make_request_directory());
57 }
bff1edbe 58 $filename = $this->filename . $this->get_extension();
40a6b502
ALH
59 if (PHPUNIT_TEST) {
60 $this->writer->openToFile('php://output');
61 } else {
62 $this->writer->openToBrowser($filename);
63 }
faf45f9c
MN
64
65 // By default one sheet is always created, but we want to rename it when we call start_sheet().
66 $this->renamecurrentsheet = true;
bff1edbe
BH
67 }
68
69 /**
70 * Set the title of the worksheet inside a spreadsheet
71 *
72 * For some formats this will be ignored.
73 *
74 * @param string $title
75 */
76 public function set_sheettitle($title) {
bff1edbe 77 $this->sheettitle = $title;
bff1edbe
BH
78 }
79
80 /**
b62b5879 81 * Write the start of the sheet we will be adding data to.
bff1edbe
BH
82 *
83 * @param array $columns
84 */
b62b5879 85 public function start_sheet($columns) {
54968b15 86 if ($this->sheettitle && $this->writer instanceof \Box\Spout\Writer\WriterMultiSheetsAbstract) {
faf45f9c
MN
87 if ($this->renamecurrentsheet) {
88 $sheet = $this->writer->getCurrentSheet();
89 $this->renamecurrentsheet = false;
90 } else {
91 $sheet = $this->writer->addNewSheetAndMakeItCurrent();
92 }
93 $sheet->setName($this->sheettitle);
94 }
54968b15
AG
95 $row = \Box\Spout\Writer\Common\Creator\WriterEntityFactory::createRowFromArray((array)$columns);
96 $this->writer->addRow($row);
bff1edbe
BH
97 }
98
99 /**
100 * Write a single record
101 *
102 * @param object $record
103 * @param int $rownum
104 */
105 public function write_record($record, $rownum) {
54968b15
AG
106 $row = \Box\Spout\Writer\Common\Creator\WriterEntityFactory::createRowFromArray((array)$record);
107 $this->writer->addRow($row);
bff1edbe
BH
108 }
109
110 /**
b62b5879 111 * Write the end of the file.
bff1edbe 112 */
b62b5879 113 public function close_output() {
bff1edbe
BH
114 $this->writer->close();
115 $this->writer = null;
116 }
bff1edbe 117}