Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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   * JSON data format writer
  19   *
  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
  23   */
  24  
  25  namespace dataformat_json;
  26  
  27  use core_text;
  28  
  29  /**
  30   * JSON data format writer
  31   *
  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
  35   */
  36  class writer extends \core\dataformat\base {
  37  
  38      /** @var $mimetype */
  39      public $mimetype = "application/json";
  40  
  41      /** @var $extension */
  42      public $extension = ".json";
  43  
  44      /** @var $sheetstarted */
  45      public $sheetstarted = false;
  46  
  47      /** @var $sheetdatadded */
  48      public $sheetdatadded = false;
  49  
  50      /** @var string[] $columns */
  51      protected $columns = [];
  52  
  53      /**
  54       * Write the start of the file.
  55       */
  56      public function start_output() {
  57          echo "[";
  58      }
  59  
  60      /**
  61       * Write the start of the sheet we will be adding data to.
  62       *
  63       * @param array $columns
  64       */
  65      public function start_sheet($columns) {
  66          $this->columns = array_map(function($column) {
  67              return core_text::strtolower(clean_param($column, PARAM_ALPHANUMEXT));
  68          }, $columns);
  69  
  70          if ($this->sheetstarted) {
  71              echo ",";
  72          } else {
  73              $this->sheetstarted = true;
  74          }
  75          $this->sheetdatadded = false;
  76          echo "[";
  77      }
  78  
  79      /**
  80       * Write a single record
  81       *
  82       * @param array $record
  83       * @param int $rownum
  84       */
  85      public function write_record($record, $rownum) {
  86          if ($this->sheetdatadded) {
  87              echo ",";
  88          }
  89  
  90          // Ensure our record is keyed by column names, rather than numerically.
  91          $record = array_combine($this->columns, (array) $record);
  92          echo json_encode($this->format_record($record), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  93  
  94          $this->sheetdatadded = true;
  95      }
  96  
  97      /**
  98       * Write the end of the sheet containing the data.
  99       *
 100       * @param array $columns
 101       */
 102      public function close_sheet($columns) {
 103          echo "]";
 104      }
 105  
 106      /**
 107       * Write the end of the file.
 108       */
 109      public function close_output() {
 110          echo "]";
 111      }
 112  }