[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/ -> dataformatlib.php (source)

   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   * dataformatlib.php - Contains core dataformat related functions.
  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  /**
  27   * Sends a formated data file to the browser
  28   *
  29   * @package    core
  30   * @subpackage dataformat
  31   *
  32   * @param string $filename The base filename without an extension
  33   * @param string $dataformat A dataformat name
  34   * @param array $columns An ordered map of column keys and labels
  35   * @param Iterator $iterator An iterator over the records, usually a RecordSet
  36   * @param function $callback An option function applied to each record before writing
  37   * @param mixed $extra An optional value which is passed into the callback function
  38   */
  39  function download_as_dataformat($filename, $dataformat, $columns, $iterator, $callback = null) {
  40  
  41      if (ob_get_length()) {
  42          throw new coding_exception("Output can not be buffered before calling download_as_dataformat");
  43      }
  44  
  45      $classname = 'dataformat_' . $dataformat . '\writer';
  46      if (!class_exists($classname)) {
  47          throw new coding_exception("Unable to locate dataformat/$type/classes/writer.php");
  48      }
  49      $format = new $classname;
  50  
  51      // The data format export could take a while to generate...
  52      set_time_limit(0);
  53  
  54      // Close the session so that the users other tabs in the same session are not blocked.
  55      \core\session\manager::write_close();
  56  
  57      $format->set_filename($filename);
  58      $format->send_http_headers();
  59      $format->write_header($columns);
  60      $c = 0;
  61      foreach ($iterator as $row) {
  62          if ($callback) {
  63              $row = $callback($row);
  64          }
  65          if ($row === null) {
  66              continue;
  67          }
  68          $format->write_record($row, $c++);
  69      }
  70      $format->write_footer($columns);
  71  }
  72  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1