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.
   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   * Content API Export definition.
  19   *
  20   * @package     core
  21   * @copyright   2020 Andrew Nicols <andrew@nicols.co.uk>
  22   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core\content\export\exporters;
  25  
  26  use coding_exception;
  27  use context;
  28  use core\content\export\zipwriter;
  29  use core_component;
  30  use stdClass;
  31  
  32  /**
  33   * A class to help define, describe, and export content in a specific context.
  34   *
  35   * @copyright   2020 Andrew Nicols <andrew@nicols.co.uk>
  36   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  abstract class component_exporter {
  39  
  40      /** @var context The context to be exported */
  41      protected $context = null;
  42  
  43      /** @var string The component that this instance belongs to */
  44      protected $component = null;
  45  
  46      /** @var stdClass The user being exported */
  47      protected $user;
  48  
  49      /** @var zipwriter A reference to the zipwriter */
  50      protected $archive;
  51  
  52      /**
  53       * Constructor for a new exporter.
  54       *
  55       * @param   context $context The context to export
  56       * @param   string $component The component that this instance relates to
  57       * @param   stdClass $user The user to be exported
  58       * @param   zipwriter $archive
  59       */
  60      public function __construct(context $context, string $component, stdClass $user, zipwriter $archive) {
  61          $this->context = $context;
  62          $this->component = $component;
  63          $this->user = $user;
  64          $this->archive = $archive;
  65      }
  66  
  67      /**
  68       * Get the context being exported.
  69       *
  70       * @return  context
  71       */
  72      public function get_context(): context {
  73          return $this->context;
  74      }
  75  
  76      /**
  77       * Get the component name.
  78       *
  79       * @return  string
  80       */
  81      public function get_component(): string {
  82          [$type, $component] = core_component::normalize_component($this->component);
  83          if ($type === 'core') {
  84              return $component;
  85          }
  86  
  87          return core_component::normalize_componentname($this->component);
  88      }
  89  
  90      /**
  91       * Get the archive used for export.
  92       *
  93       * @return  zipwriter
  94       */
  95      public function get_archive(): zipwriter {
  96          if ($this->archive === null) {
  97              throw new coding_exception("Archive has not been set up yet");
  98          }
  99  
 100          return $this->archive;
 101      }
 102  
 103      /**
 104       * Get the name of the exporter for the specified component.
 105       *
 106       * @param   string $component The component to fetch a classname for
 107       * @return  string The classname for the component
 108       */
 109      public static function get_classname_for_component(string $component): string {
 110          return "{$component}\\content\\exporter";
 111      }
 112  }