Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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   * The definition of a text area which can be exported.
  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  
  25  declare(strict_types=1);
  26  
  27  namespace core\content\export\exportable_items;
  28  
  29  use context;
  30  use core\content\export\exportable_item;
  31  use core\content\export\exported_item;
  32  use core\content\export\zipwriter;
  33  
  34  /**
  35   * The definition of a text area which can be exported.
  36   *
  37   * @copyright   2020 Andrew Nicols <andrew@nicols.co.uk>
  38   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class exportable_textarea extends exportable_item {
  41  
  42      /** @var string The name of the table that ha the textarea within it */
  43      protected $tablename;
  44  
  45      /** @var int The id in the table */
  46      protected $id;
  47  
  48      /** @var string The name of the text field within the table */
  49      protected $textfield;
  50  
  51      /** @var null|string The name of the format field relating to the text field */
  52      protected $textformatfield;
  53  
  54      /** @var null|string The name of a file area for this content */
  55      protected $filearea;
  56  
  57      /** @var null|int The itemid for files in this text field */
  58      protected $itemid;
  59  
  60      /** @var null|int The itemid used for constructing pluginfiles */
  61      protected $pluginfileitemid;
  62  
  63      /**
  64       * Create a new exportable_item instance.
  65       *
  66       * If no filearea or itemid  is specified the no attempt will be made to export files.
  67       *
  68       * @param   context $context The context that this content belongs to
  69       * @param   string $component The component that this textarea belongs to
  70       * @param   string $uservisiblename The name displayed to the user when filtering
  71       * @param   string $tablename The name of the table that this textarea is in
  72       * @param   string $textfield The field within the tbale
  73       * @param   int $id The id in the database
  74       * @param   null|string $textformatfield The field in the database relating to the format field if one is present
  75       * @param   null|string $filearea The name of the file area for files associated with this text area
  76       * @param   null|int $itemid The itemid for files associated with this text area
  77       * @param   null|int $pluginfileitemid The itemid to use when constructing the pluginfile URL
  78       *          Some fileareas do not use any itemid in the URL and should therefore provide a `null` value here.
  79       */
  80      public function __construct(
  81          context $context,
  82          string $component,
  83          string $uservisiblename,
  84          string $tablename,
  85          string $textfield,
  86          int $id,
  87          ?string $textformatfield = null,
  88          ?string $filearea = null,
  89          ?int $itemid = null,
  90          ?int $pluginfileitemid = null
  91      ) {
  92          parent::__construct($context, $component, $uservisiblename);
  93  
  94          $this->tablename = $tablename;
  95          $this->textfield = $textfield;
  96          $this->textformatfield = $textformatfield;
  97          $this->id = $id;
  98          $this->filearea = $filearea;
  99          $this->itemid = $itemid;
 100          $this->pluginfileitemid = $pluginfileitemid;
 101      }
 102  
 103      /**
 104       * Add the content to the archive.
 105       *
 106       * @param   zipwriter $archive
 107       */
 108      public function add_to_archive(zipwriter $archive): ?exported_item {
 109          global $DB;
 110  
 111          // Fetch the field.
 112          $fields = [$this->textfield];
 113          if (!empty($this->textformatfield)) {
 114              $fields[] = $this->textformatfield;
 115          }
 116          $record = $DB->get_record($this->tablename, ['id' => $this->id], implode(', ', $fields));
 117  
 118          if (empty($record)) {
 119              return null;
 120          }
 121  
 122          // Export all of the files for this text area.
 123          $text = $record->{$this->textfield};
 124          if (empty($text)) {
 125              $text = '';
 126          }
 127  
 128          if ($this->may_include_files()) {
 129              // This content may include inline files.
 130              $exporteditem = $archive->add_pluginfiles_for_content(
 131                  $this->get_context(),
 132                  "",
 133                  $text,
 134                  $this->component,
 135                  $this->filearea,
 136                  $this->itemid,
 137                  $this->pluginfileitemid
 138              );
 139          } else {
 140              $exporteditem = new exported_item();
 141              $exporteditem->set_content($text);
 142          }
 143  
 144          if (!empty($this->textformatfield)) {
 145              $formattedcontent = format_text($exporteditem->get_content(), $record->{$this->textformatfield},
 146                  ['context' => $this->get_context()]);
 147              $exporteditem->set_content($formattedcontent);
 148          }
 149  
 150          $exporteditem->set_title($this->get_user_visible_name());
 151          return $exporteditem;
 152      }
 153  
 154      /**
 155       * Whether files may be included in this textarea.
 156       *
 157       * Both a filearea, and itemid are required for files to be exportable.
 158       *
 159       * @return  bool
 160       */
 161      protected function may_include_files(): bool {
 162          if ($this->filearea === null) {
 163              return false;
 164          }
 165  
 166          if ($this->itemid === null) {
 167              return false;
 168          }
 169  
 170          return true;
 171      }
 172  }