Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Class containing utility methods for dataformats
19
 *
20
 * @package     core
21
 * @copyright   2020 Paul Holden <paulh@moodle.com>
22
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace core;
26
 
27
use coding_exception;
11 efrain 28
use core\dataformat\base;
1 efrain 29
use core_php_time_limit;
30
use stored_file;
31
 
32
/**
33
 * Dataformat utility class
34
 *
35
 * @package     core
36
 * @copyright   2020 Paul Holden <paulh@moodle.com>
37
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class dataformat {
40
 
41
    /**
42
     * Return an instance of a dataformat writer from given dataformat type
43
     *
44
     * @param string $dataformat
11 efrain 45
     * @return base
46
     *
47
     * @throws coding_exception For unknown dataformat
1 efrain 48
     */
11 efrain 49
    public static function get_format_instance(string $dataformat): base {
1 efrain 50
        $classname = 'dataformat_' . $dataformat . '\writer';
51
        if (!class_exists($classname)) {
52
            throw new coding_exception('Invalid dataformat', $dataformat);
53
        }
54
        return new $classname();
55
    }
56
 
57
    /**
58
     * Sends a formatted data file to the browser
59
     *
60
     * @param string $filename
61
     * @param string $dataformat
62
     * @param array $columns
63
     * @param Iterable $iterator
64
     * @param callable|null $callback Optional callback method to apply to each record prior to writing, which accepts two
65
     *      parameters as such: function($record, bool $supportshtml) returning formatted record
66
     * @throws coding_exception
67
     */
68
    public static function download_data(string $filename, string $dataformat, array $columns, Iterable $iterator,
69
            callable $callback = null): void {
70
 
71
        if (ob_get_length()) {
72
            throw new coding_exception('Output can not be buffered before calling download_data()');
73
        }
74
 
75
        $format = self::get_format_instance($dataformat);
76
 
77
        // The data format export could take a while to generate.
78
        core_php_time_limit::raise();
79
 
80
        // Close the session so that the users other tabs in the same session are not blocked.
81
        \core\session\manager::write_close();
82
 
83
        // If this file was requested from a form, then mark download as complete (before sending headers).
84
        \core_form\util::form_download_complete();
85
 
86
        $format->set_filename($filename);
87
        $format->send_http_headers();
88
 
89
        $format->start_output();
90
        $format->start_sheet($columns);
91
 
92
        $rownum = 0;
93
        foreach ($iterator as $row) {
94
            if (is_callable($callback)) {
95
                $row = $callback($row, $format->supports_html());
96
            }
97
            if ($row === null) {
98
                continue;
99
            }
100
            $format->write_record($row, $rownum++);
101
        }
102
 
103
        $format->close_sheet($columns);
104
        $format->close_output();
105
    }
106
 
107
    /**
108
     * Writes a formatted data file with specified filename
109
     *
110
     * @param string $filename
111
     * @param string $dataformat
112
     * @param array $columns
113
     * @param Iterable $iterator
114
     * @param callable|null $callback
115
     * @return string Complete path to the file on disk
116
     */
117
    public static function write_data(string $filename, string $dataformat, array $columns, Iterable $iterator,
118
            callable $callback = null): string {
119
 
120
        $format = self::get_format_instance($dataformat);
121
 
122
        // The data format export could take a while to generate.
123
        core_php_time_limit::raise();
124
 
125
        // Close the session so that the users other tabs in the same session are not blocked.
126
        \core\session\manager::write_close();
127
 
128
        $filepath = make_request_directory() . '/' . $filename . $format->get_extension();
129
        $format->set_filepath($filepath);
130
 
131
        $format->start_output_to_file();
132
        $format->start_sheet($columns);
133
 
134
        $rownum = 0;
135
        foreach ($iterator as $row) {
136
            if (is_callable($callback)) {
137
                $row = $callback($row, $format->supports_html());
138
            }
139
            if ($row === null) {
140
                continue;
141
            }
142
            $format->write_record($row, $rownum++);
143
        }
144
 
145
        $format->close_sheet($columns);
146
        $format->close_output_to_file();
147
 
148
        return $filepath;
149
    }
150
 
151
    /**
152
     * Writes a formatted data file to file storage
153
     *
154
     * @param array $filerecord File record for storage, 'filename' extension should be omitted as it's added by the dataformat
155
     * @param string $dataformat
156
     * @param array $columns
157
     * @param Iterable $iterator Iterable set of records to write
158
     * @param callable|null $callback Optional callback method to apply to each record prior to writing
159
     * @return stored_file
160
     */
161
    public static function write_data_to_filearea(array $filerecord, string $dataformat, array $columns, Iterable $iterator,
162
            callable $callback = null): stored_file {
163
 
164
        $filepath = self::write_data($filerecord['filename'], $dataformat, $columns, $iterator, $callback);
165
 
166
        // Update filename of returned file record.
167
        $filerecord['filename'] = basename($filepath);
168
 
169
        return get_file_storage()->create_file_from_pathname($filerecord, $filepath);
170
    }
171
}