Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

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