MDL-65919 core: Skip send headers during phpunit exec in dataformats
[moodle.git] / lib / classes / dataformat / 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 * Base 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 * Base 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 base {
37
38 /** @var $mimetype */
39 protected $mimetype = "text/plain";
40
41 /** @var $extension */
42 protected $extension = ".txt";
43
44 /** @var $filename */
45 protected $filename = '';
46
47 /**
48 * Get the file extension
49 *
50 * @return string file extension
51 */
52 public function get_extension() {
53 return $this->extension;
54 }
55
56 /**
57 * Set download filename base
58 *
59 * @param string $filename
60 */
61 public function set_filename($filename) {
62 $this->filename = $filename;
63 }
64
65 /**
66 * Set the title of the worksheet inside a spreadsheet
67 *
68 * For some formats this will be ignored.
69 *
70 * @param string $title
71 */
72 public function set_sheettitle($title) {
73 }
74
75 /**
76 * Output file headers to initialise the download of the file.
77 */
78 public function send_http_headers() {
40a6b502 79 if (defined('BEHAT_SITE_RUNNING') || PHPUNIT_TEST) {
bff1edbe
BH
80 // For text based formats - we cannot test the output with behat if we force a file download.
81 return;
82 }
83 if (is_https()) {
84 // HTTPS sites - watch out for IE! KB812935 and KB316431.
85 header('Cache-Control: max-age=10');
86 header('Pragma: ');
87 } else {
88 // Normal http - prevent caching at all cost.
89 header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
90 header('Pragma: no-cache');
91 }
92 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
93 header("Content-Type: $this->mimetype\n");
94 $filename = $this->filename . $this->get_extension();
95 header("Content-Disposition: attachment; filename=\"$filename\"");
96 }
97
98 /**
b62b5879
MN
99 * Write the start of the file.
100 */
101 public function start_output() {
102 // Override me if needed.
103 }
104
105 /**
106 * Write the start of the sheet we will be adding data to.
bff1edbe
BH
107 *
108 * @param array $columns
109 */
b62b5879 110 public function start_sheet($columns) {
bff1edbe
BH
111 // Override me if needed.
112 }
113
114 /**
115 * Write a single record
116 *
117 * @param array $record
118 * @param int $rownum
119 */
120 abstract public function write_record($record, $rownum);
121
122 /**
b62b5879 123 * Write the end of the sheet containing the data.
bff1edbe
BH
124 *
125 * @param array $columns
126 */
b62b5879 127 public function close_sheet($columns) {
bff1edbe
BH
128 // Override me if needed.
129 }
130
b62b5879
MN
131 /**
132 * Write the end of the file.
133 */
134 public function close_output() {
135 // Override me if needed.
136 }
bff1edbe 137}