Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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  namespace mod_data\local\exporter;
  18  
  19  use coding_exception;
  20  use csv_export_writer;
  21  
  22  /**
  23   * CSV entries exporter for mod_data.
  24   *
  25   * @package    mod_data
  26   * @copyright  2023 ISB Bayern
  27   * @author     Philipp Memmel
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class csv_entries_exporter extends entries_exporter {
  31  
  32      /** @var string[] Possible delimiter names. Only used internally to check if a valid delimiter name
  33       *   has been specified.
  34       */
  35      private const POSSIBLE_DELIMITER_NAMES = ['comma', 'tab', 'semicolon', 'colon', 'cfg'];
  36  
  37      /**
  38       * @var string name of the delimiter to use for the csv export. Possible values:
  39       *  'comma', 'tab', 'semicolon', 'colon' or 'cfg'.
  40       */
  41      private string $delimitername = 'comma';
  42  
  43      /**
  44       * Returns the csv data exported by the csv_export_writer for further handling.
  45       *
  46       * @see \mod_data\local\exporter\entries_exporter::get_data_file_content()
  47       */
  48      public function get_data_file_content(): string {
  49          global $CFG;
  50          require_once($CFG->libdir . '/csvlib.class.php');
  51  
  52          return csv_export_writer::print_array($this->exportdata, $this->delimitername, '"', true);
  53      }
  54  
  55      /**
  56       * Returns the file extension of this entries exporter.
  57       *
  58       * @see \mod_data\local\exporter\entries_exporter::get_export_data_file_extension()
  59       */
  60      public function get_export_data_file_extension(): string {
  61          return 'csv';
  62      }
  63  
  64      /**
  65       * Setter for the delimiter name which should be used in this csv_entries_exporter object.
  66       *
  67       * Calling this setter is optional, the delimiter name defaults to 'comma'.
  68       *
  69       * @param string $delimitername one of 'comma', 'tab', 'semicolon', 'colon' or 'cfg'
  70       * @return void
  71       * @throws coding_exception if a wrong delimiter name has been specified
  72       */
  73      public function set_delimiter_name(string $delimitername): void {
  74          if (!in_array($delimitername, self::POSSIBLE_DELIMITER_NAMES)) {
  75              throw new coding_exception('Wrong delimiter type',
  76                  'Please choose on of the following delimiters: '
  77                  . '\"comma\", \"tab\", \"semicolon\", \"colon\", \"cfg\"');
  78          }
  79          $this->delimitername = $delimitername;
  80      }
  81  }