MDL-62388 tool_dataprivacy: Fixing bs4stable table layout
[moodle.git] / lib / dataformatlib.php
blob4412e962f2273ea56362f5e54c1a967e084e2557
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 * dataformatlib.php - Contains core dataformat related functions.
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 /**
27 * Sends a formated data file to the browser
29 * @package core
30 * @subpackage dataformat
32 * @param string $filename The base filename without an extension
33 * @param string $dataformat A dataformat name
34 * @param array $columns An ordered map of column keys and labels
35 * @param Iterator $iterator An iterator over the records, usually a RecordSet
36 * @param function $callback An option function applied to each record before writing
37 * @param mixed $extra An optional value which is passed into the callback function
39 function download_as_dataformat($filename, $dataformat, $columns, $iterator, $callback = null) {
41 if (ob_get_length()) {
42 throw new coding_exception("Output can not be buffered before calling download_as_dataformat");
45 $classname = 'dataformat_' . $dataformat . '\writer';
46 if (!class_exists($classname)) {
47 throw new coding_exception("Unable to locate dataformat/$dataformat/classes/writer.php");
49 $format = new $classname;
51 // The data format export could take a while to generate...
52 set_time_limit(0);
54 // Close the session so that the users other tabs in the same session are not blocked.
55 \core\session\manager::write_close();
57 $format->set_filename($filename);
58 $format->send_http_headers();
59 // This exists to support all dataformats - see MDL-56046.
60 if (method_exists($format, 'write_header')) {
61 error_log('The function write_header() does not support multiple sheets. In order to support multiple sheets you ' .
62 'must implement start_output() and start_sheet() and remove write_header() in your dataformat.');
63 $format->write_header($columns);
64 } else {
65 $format->start_output();
66 $format->start_sheet($columns);
68 $c = 0;
69 foreach ($iterator as $row) {
70 if ($callback) {
71 $row = $callback($row);
73 if ($row === null) {
74 continue;
76 $format->write_record($row, $c++);
78 // This exists to support all dataformats - see MDL-56046.
79 if (method_exists($format, 'write_footer')) {
80 error_log('The function write_footer() does not support multiple sheets. In order to support multiple sheets you ' .
81 'must implement close_sheet() and close_output() and remove write_footer() in your dataformat.');
82 $format->write_footer($columns);
83 } else {
84 $format->close_sheet($columns);
85 $format->close_output();