Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 311 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   * 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  /**
  29   * Common Spout class for dataformat.
  30   *
  31   * @package    core
  32   * @subpackage dataformat
  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  abstract class spout_base extends \core\dataformat\base {
  37  
  38      /** @var $spouttype */
  39      protected $spouttype = '';
  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          $this->writer = \Box\Spout\Writer\Common\Creator\WriterEntityFactory::createWriter($this->spouttype);
  55          if (method_exists($this->writer, 'setTempFolder')) {
  56              $this->writer->setTempFolder(make_request_directory());
  57          }
  58          $filename = $this->filename . $this->get_extension();
  59          if (PHPUNIT_TEST) {
  60              $this->writer->openToFile('php://output');
  61          } else {
  62              $this->writer->openToBrowser($filename);
  63          }
  64  
  65          // By default one sheet is always created, but we want to rename it when we call start_sheet().
  66          $this->renamecurrentsheet = true;
  67      }
  68  
  69      /**
  70       * Set the dataformat to be output to current file
  71       */
  72      public function start_output_to_file(): void {
  73          $this->writer = \Box\Spout\Writer\Common\Creator\WriterEntityFactory::createWriter($this->spouttype);
  74          if (method_exists($this->writer, 'setTempFolder')) {
  75              $this->writer->setTempFolder(make_request_directory());
  76          }
  77  
  78          $this->writer->openToFile($this->filepath);
  79  
  80          // By default one sheet is always created, but we want to rename it when we call start_sheet().
  81          $this->renamecurrentsheet = true;
  82  
  83          $this->start_output();
  84      }
  85  
  86      /**
  87       * Set the title of the worksheet inside a spreadsheet
  88       *
  89       * For some formats this will be ignored.
  90       *
  91       * @param string $title
  92       */
  93      public function set_sheettitle($title) {
  94          $this->sheettitle = $title;
  95      }
  96  
  97      /**
  98       * Write the start of the sheet we will be adding data to.
  99       *
 100       * @param array $columns
 101       */
 102      public function start_sheet($columns) {
 103          if ($this->sheettitle && $this->writer instanceof \Box\Spout\Writer\WriterMultiSheetsAbstract) {
 104              if ($this->renamecurrentsheet) {
 105                  $sheet = $this->writer->getCurrentSheet();
 106                  $this->renamecurrentsheet = false;
 107              } else {
 108                  $sheet = $this->writer->addNewSheetAndMakeItCurrent();
 109              }
 110              $sheet->setName($this->sheettitle);
 111          }
 112          $row = \Box\Spout\Writer\Common\Creator\WriterEntityFactory::createRowFromArray((array)$columns);
 113          $this->writer->addRow($row);
 114      }
 115  
 116      /**
 117       * Write a single record
 118       *
 119       * @param array $record
 120       * @param int $rownum
 121       */
 122      public function write_record($record, $rownum) {
 123          $row = \Box\Spout\Writer\Common\Creator\WriterEntityFactory::createRowFromArray($this->format_record($record));
 124          $this->writer->addRow($row);
 125      }
 126  
 127      /**
 128       * Write the end of the file.
 129       */
 130      public function close_output() {
 131          $this->writer->close();
 132          $this->writer = null;
 133      }
 134  
 135      /**
 136       * Write data to disk
 137       *
 138       * @return bool
 139       */
 140      public function close_output_to_file(): bool {
 141          $this->close_output();
 142  
 143          return true;
 144      }
 145  }