2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * JSON data format writer
20 * @package dataformat_json
21 * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace dataformat_json
;
27 defined('MOODLE_INTERNAL') ||
die();
30 * JSON data format writer
32 * @package dataformat_json
33 * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net)
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class writer
extends \core\dataformat\base
{
39 public $mimetype = "application/json";
41 /** @var $extension */
42 public $extension = ".json";
44 /** @var $sheetstarted */
45 public $sheetstarted = false;
47 /** @var $sheetdatadded */
48 public $sheetdatadded = false;
51 * Write the start of the file.
53 public function start_output() {
58 * Write the start of the sheet we will be adding data to.
60 * @param array $columns
62 public function start_sheet($columns) {
63 if ($this->sheetstarted
) {
66 $this->sheetstarted
= true;
68 $this->sheetdatadded
= false;
73 * Write a single record
75 * @param array $record
78 public function write_record($record, $rownum) {
79 if ($this->sheetdatadded
) {
83 echo json_encode($record);
85 $this->sheetdatadded
= true;
89 * Write the end of the sheet containing the data.
91 * @param array $columns
93 public function close_sheet($columns) {
98 * Write the end of the file.
100 public function close_output() {