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 |
* 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 |
|
|
|
26 |
namespace core\dataformat;
|
|
|
27 |
|
|
|
28 |
use OpenSpout\Common\Entity\Row;
|
|
|
29 |
use OpenSpout\Writer\Common\Creator\WriterFactory;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Common Spout class for dataformat.
|
|
|
33 |
*
|
|
|
34 |
* @package core
|
|
|
35 |
* @subpackage dataformat
|
|
|
36 |
* @copyright 2016 Brendan Heywood (brendan@catalyst-au.net)
|
|
|
37 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
38 |
*/
|
|
|
39 |
abstract class spout_base extends \core\dataformat\base {
|
|
|
40 |
|
|
|
41 |
/** @var $writer */
|
|
|
42 |
protected $writer;
|
|
|
43 |
|
|
|
44 |
/** @var $sheettitle */
|
|
|
45 |
protected $sheettitle;
|
|
|
46 |
|
|
|
47 |
/** @var $renamecurrentsheet */
|
|
|
48 |
protected $renamecurrentsheet = false;
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Output file headers to initialise the download of the file.
|
|
|
52 |
*/
|
|
|
53 |
public function send_http_headers() {
|
|
|
54 |
$filename = $this->filename . $this->get_extension();
|
|
|
55 |
|
|
|
56 |
$this->writer = WriterFactory::createFromFile($filename);
|
|
|
57 |
if (method_exists($this->writer->getOptions(), 'setTempFolder')) {
|
|
|
58 |
$this->writer->getOptions()->setTempFolder(make_request_directory());
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
if (PHPUNIT_TEST) {
|
|
|
62 |
$this->writer->openToFile('php://output');
|
|
|
63 |
} else {
|
|
|
64 |
$this->writer->openToBrowser($filename);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
// By default one sheet is always created, but we want to rename it when we call start_sheet().
|
|
|
68 |
$this->renamecurrentsheet = true;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Set the dataformat to be output to current file
|
|
|
73 |
*/
|
|
|
74 |
public function start_output_to_file(): void {
|
|
|
75 |
$this->writer = WriterFactory::createFromFile($this->filepath);
|
|
|
76 |
if (method_exists($this->writer->getOptions(), 'setTempFolder')) {
|
|
|
77 |
$this->writer->getOptions()->setTempFolder(make_request_directory());
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$this->writer->openToFile($this->filepath);
|
|
|
81 |
|
|
|
82 |
// By default one sheet is always created, but we want to rename it when we call start_sheet().
|
|
|
83 |
$this->renamecurrentsheet = true;
|
|
|
84 |
|
|
|
85 |
$this->start_output();
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Set the title of the worksheet inside a spreadsheet
|
|
|
90 |
*
|
|
|
91 |
* For some formats this will be ignored.
|
|
|
92 |
*
|
|
|
93 |
* @param string $title
|
|
|
94 |
*/
|
|
|
95 |
public function set_sheettitle($title) {
|
|
|
96 |
$this->sheettitle = $title;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Write the start of the sheet we will be adding data to.
|
|
|
101 |
*
|
|
|
102 |
* @param array $columns
|
|
|
103 |
*/
|
|
|
104 |
public function start_sheet($columns) {
|
|
|
105 |
if ($this->sheettitle && $this->writer instanceof \OpenSpout\Writer\AbstractWriterMultiSheets) {
|
|
|
106 |
if ($this->renamecurrentsheet) {
|
|
|
107 |
$sheet = $this->writer->getCurrentSheet();
|
|
|
108 |
$this->renamecurrentsheet = false;
|
|
|
109 |
} else {
|
|
|
110 |
$sheet = $this->writer->addNewSheetAndMakeItCurrent();
|
|
|
111 |
}
|
|
|
112 |
$sheet->setName($this->sheettitle);
|
|
|
113 |
}
|
|
|
114 |
// Create a row with cells and apply the style to all cells.
|
|
|
115 |
$row = Row::fromValues((array)$columns);
|
|
|
116 |
$this->writer->addRow($row);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Write a single record
|
|
|
121 |
*
|
|
|
122 |
* @param array $record
|
|
|
123 |
* @param int $rownum
|
|
|
124 |
*/
|
|
|
125 |
public function write_record($record, $rownum) {
|
|
|
126 |
$row = Row::fromValues($this->format_record($record));
|
|
|
127 |
$this->writer->addRow($row);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Write the end of the file.
|
|
|
132 |
*/
|
|
|
133 |
public function close_output() {
|
|
|
134 |
$this->writer->close();
|
|
|
135 |
$this->writer = null;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
/**
|
|
|
139 |
* Write data to disk
|
|
|
140 |
*
|
|
|
141 |
* @return bool
|
|
|
142 |
*/
|
|
|
143 |
public function close_output_to_file(): bool {
|
|
|
144 |
$this->close_output();
|
|
|
145 |
|
|
|
146 |
return true;
|
|
|
147 |
}
|
|
|
148 |
}
|