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.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 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   * This file contains the interface required to implmeent a content writer.
  19   *
  20   * @package core_privacy
  21   * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core_privacy\local\request;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * The writer factory class used to fetch and work with the content_writer.
  30   *
  31   * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class writer {
  35      /**
  36       * @var writer The singleton instance of this writer.
  37       */
  38      protected static $instance = null;
  39  
  40      /**
  41       * @var content_writer The current content_writer instance.
  42       */
  43      protected $realwriter = null;
  44  
  45      /**
  46       * Constructor for the content writer.
  47       *
  48       * Protected to prevent direct instantiation.
  49       */
  50      protected function __construct() {
  51      }
  52  
  53      /**
  54       * Singleton to return or create and return a copy of a content_writer.
  55       *
  56       * @return  content_writer
  57       */
  58      protected function get_writer_instance() : content_writer {
  59          if (null === $this->realwriter) {
  60              if (PHPUNIT_TEST) {
  61                  $this->realwriter = new \core_privacy\tests\request\content_writer(static::instance());
  62              } else {
  63                  $this->realwriter = new moodle_content_writer(static::instance());
  64              }
  65          }
  66  
  67          return $this->realwriter;
  68      }
  69  
  70      /**
  71       * Create a real content_writer for use by PHPUnit tests,
  72       * where a mock writer will not suffice.
  73       *
  74       * @return  content_writer
  75       */
  76      public static function setup_real_writer_instance() {
  77          if (!PHPUNIT_TEST) {
  78              throw new \coding_exception('setup_real_writer_instance() is only for use with PHPUnit tests.');
  79          }
  80  
  81          $instance = static::instance();
  82  
  83          if (null === $instance->realwriter) {
  84              $instance->realwriter = new moodle_content_writer(static::instance());
  85          }
  86      }
  87  
  88      /**
  89       * Return an instance of
  90       */
  91      protected static final function instance() {
  92          if (null === self::$instance) {
  93              self::$instance = new static();
  94          }
  95  
  96          return self::$instance;
  97      }
  98  
  99      /**
 100       * Reset the writer and content_writer.
 101       */
 102      public static final function reset() {
 103          static::$instance = null;
 104      }
 105  
 106      /**
 107       * Provide an instance of the writer with the specified context applied.
 108       *
 109       * @param   \context        $context    The context to apply
 110       * @return  content_writer              The content_writer
 111       */
 112      public static function with_context(\context $context) : content_writer {
 113          return static::instance()
 114              ->get_writer_instance()
 115              ->set_context($context);
 116      }
 117  
 118      /**
 119       * Export the specified user preference.
 120       *
 121       * @param   string          $component  The name of the component.
 122       * @param   string          $key        The name of th key to be exported.
 123       * @param   string          $value      The value of the preference
 124       * @param   string          $description    A description of the value
 125       * @return  content_writer
 126       */
 127      public static function export_user_preference(
 128          string $component,
 129          string $key,
 130          string $value,
 131          string $description
 132      ) : content_writer {
 133          return static::with_context(\context_system::instance())
 134              ->export_user_preference($component, $key, $value, $description);
 135      }
 136  }