Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
namespace core_table;
18
 
19
use core\exception\coding_exception;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
global $CFG;
24
 
25
require_once("{$CFG->libdir}/tablelib.php");
26
 
27
use core\dataformat;
28
 
29
/**
30
 * Dataformat exporter
31
 *
32
 * @package    core_table
33
 * @subpackage tablelib
34
 * @copyright  2016 Brendan Heywood (brendan@catalyst-au.net)
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class dataformat_export_format extends base_export_format {
38
    /** @var \core\dataformat\base $dataformat */
39
    protected $dataformat;
40
 
41
    /** @var int $rownum */
42
    protected $rownum = 0;
43
 
44
    /** @var array $columns */
45
    protected $columns;
46
 
47
    /**
48
     * Constructor
49
     *
50
     * @param string $table An sql table
51
     * @param string $dataformat type of dataformat for export
52
     */
53
    public function __construct(&$table, $dataformat) {
54
        parent::__construct($table);
55
 
56
        if (ob_get_length()) {
57
            throw new coding_exception("Output can not be buffered before instantiating table_dataformat_export_format");
58
        }
59
 
60
        $this->dataformat = dataformat::get_format_instance($dataformat);
61
 
62
        // The dataformat export time to first byte could take a while to generate...
63
        set_time_limit(0);
64
 
65
        // Close the session so that the users other tabs in the same session are not blocked.
66
        \core\session\manager::write_close();
67
    }
68
 
69
    /**
70
     * Whether the current dataformat supports export of HTML
71
     *
72
     * @return bool
73
     */
74
    public function supports_html(): bool {
75
        return $this->dataformat->supports_html();
76
    }
77
 
78
    /**
79
     * Start document
80
     *
81
     * @param string $filename
82
     * @param string $sheettitle
83
     */
84
    public function start_document($filename, $sheettitle) {
85
        $this->documentstarted = true;
86
        $this->dataformat->set_filename($filename);
87
        $this->dataformat->send_http_headers();
88
        $this->dataformat->set_sheettitle($sheettitle);
89
        $this->dataformat->start_output();
90
    }
91
 
92
    /**
93
     * Start export
94
     *
95
     * @param string $sheettitle optional spreadsheet worksheet title
96
     */
97
    public function start_table($sheettitle) {
98
        $this->dataformat->set_sheettitle($sheettitle);
99
    }
100
 
101
    /**
102
     * Output headers
103
     *
104
     * @param array $headers
105
     */
106
    public function output_headers($headers) {
107
        $this->columns = $this->format_data($headers);
108
        if (method_exists($this->dataformat, 'write_header')) {
109
            error_log('The function write_header() does not support multiple sheets. In order to support multiple sheets you ' .
110
                'must implement start_output() and start_sheet() and remove write_header() in your dataformat.');
111
            $this->dataformat->write_header($this->columns);
112
        } else {
113
            $this->dataformat->start_sheet($this->columns);
114
        }
115
    }
116
 
117
    /**
118
     * Add a row of data
119
     *
120
     * @param array $row One record of data
121
     */
122
    public function add_data($row) {
123
        if (!$this->supports_html()) {
124
            $row = $this->format_data($row);
125
        }
126
 
127
        $this->dataformat->write_record($row, $this->rownum++);
128
        return true;
129
    }
130
 
131
    /**
132
     * Finish export
133
     */
134
    public function finish_table() {
135
        if (method_exists($this->dataformat, 'write_footer')) {
136
            error_log('The function write_footer() does not support multiple sheets. In order to support multiple sheets you ' .
137
                'must implement close_sheet() and close_output() and remove write_footer() in your dataformat.');
138
            $this->dataformat->write_footer($this->columns);
139
        } else {
140
            $this->dataformat->close_sheet($this->columns);
141
        }
142
    }
143
 
144
    /**
145
     * Finish download
146
     */
147
    public function finish_document() {
148
        $this->dataformat->close_output();
149
        exit();
150
    }
151
}
152
 
153
// Alias this class to the old name.
154
// This file will be autoloaded by the legacyclasses autoload system.
155
// In future all uses of this class will be corrected and the legacy references will be removed.
156
class_alias(dataformat_export_format::class, \table_dataformat_export_format::class);