Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402]

   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   * Forum Exporter factory.
  19   *
  20   * @package    mod_forum
  21   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_forum\local\factories;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use mod_forum\local\entities\discussion as discussion_entity;
  30  use mod_forum\local\entities\forum as forum_entity;
  31  use mod_forum\local\entities\post as post_entity;
  32  use mod_forum\local\entities\post_read_receipt_collection as post_read_receipt_collection_entity;
  33  use mod_forum\local\factories\legacy_data_mapper as legacy_data_mapper_factory;
  34  use mod_forum\local\factories\manager as manager_factory;
  35  use mod_forum\local\factories\url as url_factory;
  36  use mod_forum\local\factories\vault as vault_factory;
  37  use mod_forum\local\exporters\forum as forum_exporter;
  38  use mod_forum\local\exporters\discussion as discussion_exporter;
  39  use mod_forum\local\exporters\discussion_summaries as discussion_summaries_exporter;
  40  use mod_forum\local\exporters\post as post_exporter;
  41  use mod_forum\local\exporters\posts as posts_exporter;
  42  use context;
  43  use rating;
  44  use stdClass;
  45  
  46  /**
  47   * The exporter factory class used to fetch an instance of the different exporter types.
  48   *
  49   * See:
  50   * https://designpatternsphp.readthedocs.io/en/latest/Creational/SimpleFactory/README.html
  51   *
  52   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  53   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  54   */
  55  class exporter {
  56      /** @var legacy_data_mapper_factory The factory to fetch a legacy data mapper */
  57      private $legacydatamapperfactory;
  58  
  59      /** @var manager_factory The factory to fetch a new manager */
  60      private $managerfactory;
  61  
  62      /** @var url_factory The factory to create urls */
  63      private $urlfactory;
  64  
  65      /** @var vault_factory The vault factory */
  66      private $vaultfactory;
  67  
  68      /**
  69       * Constructor for the exporter factory.
  70       *
  71       * @param legacy_data_mapper_factory $legacydatamapperfactory The factory to fetch a legacy data mapper instance
  72       * @param manager_factory $managerfactory The factory fo fetch a manager instance
  73       * @param url_factory $urlfactory The factory to create urls
  74       * @param vault_factory $vaultfactory The vault factory
  75       */
  76      public function __construct(legacy_data_mapper_factory $legacydatamapperfactory, manager_factory $managerfactory,
  77              url_factory $urlfactory, vault_factory $vaultfactory) {
  78          $this->legacydatamapperfactory = $legacydatamapperfactory;
  79          $this->managerfactory = $managerfactory;
  80          $this->urlfactory = $urlfactory;
  81          $this->vaultfactory = $vaultfactory;
  82      }
  83  
  84      /**
  85       * Construct a new forum exporter for the specified user and forum.
  86       *
  87       * @param   stdClass        $user The user viewing the forum
  88       * @param   forum_entity    $forum The forum being viewed
  89       * @param   int             $currentgroup The group currently being viewed
  90       * @return  forum_exporter
  91       */
  92      public function get_forum_exporter(
  93          stdClass $user,
  94          forum_entity $forum,
  95          ?int $currentgroup
  96      ) : forum_exporter {
  97          return new forum_exporter($forum, [
  98              'legacydatamapperfactory' => $this->legacydatamapperfactory,
  99              'capabilitymanager' => $this->managerfactory->get_capability_manager($forum),
 100              'urlfactory' => $this->urlfactory,
 101              'user' => $user,
 102              'currentgroup' => $currentgroup,
 103              'vaultfactory' => $this->vaultfactory,
 104          ]);
 105      }
 106  
 107      /**
 108       * Fetch the structure of the forum exporter.
 109       *
 110       * @return  array
 111       */
 112      public static function get_forum_export_structure() : array {
 113          return forum_exporter::read_properties_definition();
 114      }
 115  
 116      /**
 117       * Construct a new discussion exporter for the specified user and forum discussion.
 118       *
 119       * @param   stdClass          $user The user viewing the forum
 120       * @param   forum_entity      $forum The forum being viewed
 121       * @param   discussion_entity $discussion The discussion being viewed
 122       * @param   stdClass[]        $groupsbyid The list of groups in the forum
 123       * @return  discussion_exporter
 124       */
 125      public function get_discussion_exporter(
 126          stdClass $user,
 127          forum_entity $forum,
 128          discussion_entity $discussion,
 129          array $groupsbyid = [],
 130          array $favouriteids = []
 131      ) : discussion_exporter {
 132          return new discussion_exporter($discussion, [
 133              'context' => $forum->get_context(),
 134              'forum' => $forum,
 135              'capabilitymanager' => $this->managerfactory->get_capability_manager($forum),
 136              'urlfactory' => $this->urlfactory,
 137              'user' => $user,
 138              'legacydatamapperfactory' => $this->legacydatamapperfactory,
 139              'latestpostid' => null,
 140              'groupsbyid' => $groupsbyid,
 141              'favouriteids' => $favouriteids
 142          ]);
 143      }
 144  
 145      /**
 146       * Fetch the structure of the discussion exporter.
 147       *
 148       * @return  array
 149       */
 150      public static function get_discussion_export_structure() {
 151          return discussion_exporter::read_properties_definition();
 152      }
 153  
 154      /**
 155       * Construct a new discussion summaries exporter for the specified user and set of discussions.
 156       *
 157       * @param   stdClass        $user The user viewing the forum
 158       * @param   forum_entity    $forum The forum being viewed
 159       * @param   discussion_summary_entity[] $discussions The set of discussion summaries to export
 160       * @param   stdClass[]      $groupsbyauthorid The set of groups in an associative array for each author
 161       * @param   stdClass[]      $groupsbyid The set of groups in the forum in an associative array for each group
 162       * @param   int[]           $discussionreplycount The number of replies for each discussion
 163       * @param   int[]           $discussionunreadcount The number of unread posts for each discussion
 164       * @param   int[]           $latestpostids The latest post id for each discussion
 165       * @param   int[]           $postauthorcontextids The context ids for the first and last post authors (indexed by author id)
 166       * @param   int[]           $favourites The list of discussion ids that have been favourited
 167       * @return  discussion_summaries_exporter
 168       */
 169      public function get_discussion_summaries_exporter(
 170          stdClass $user,
 171          forum_entity $forum,
 172          array $discussions,
 173          array $groupsbyid = [],
 174          array $groupsbyauthorid = [],
 175          array $discussionreplycount = [],
 176          array $discussionunreadcount = [],
 177          array $latestpostids = [],
 178          array $postauthorcontextids = [],
 179          array $favourites = [],
 180          array $latestauthors = []
 181      ) : discussion_summaries_exporter {
 182          return new discussion_summaries_exporter(
 183              $discussions,
 184              $groupsbyid,
 185              $groupsbyauthorid,
 186              $discussionreplycount,
 187              $discussionunreadcount,
 188              $latestpostids,
 189              $postauthorcontextids,
 190              [
 191                  'legacydatamapperfactory' => $this->legacydatamapperfactory,
 192                  'context' => $forum->get_context(),
 193                  'forum' => $forum,
 194                  'capabilitymanager' => $this->managerfactory->get_capability_manager($forum),
 195                  'urlfactory' => $this->urlfactory,
 196                  'user' => $user,
 197                  'favouriteids' => $favourites,
 198                  'latestauthors' => $latestauthors
 199              ]
 200          );
 201      }
 202  
 203      /**
 204       * Fetch the structure of the discussion summaries exporter.
 205       *
 206       * @return  array
 207       */
 208      public static function get_discussion_summaries_export_structure() {
 209          return discussion_summaries_exporter::read_properties_definition();
 210      }
 211  
 212      /**
 213       * Construct a new post exporter for the specified user and set of post.
 214       *
 215       * @param   stdClass        $user The user viewing the forum
 216       * @param   forum_entity    $forum The forum being viewed
 217       * @param   discussion_entity $discussion The discussion that the post is in
 218       * @param   post_entity[]   $posts The set of posts to be exported
 219       * @param   author_entity[] $authorsbyid List of authors indexed by author id
 220       * @param   int[]           $authorcontextids List of authors context ids indexed by author id
 221       * @param   array           $attachmentsbypostid List of attachments for each post indexed by post id
 222       * @param   array           $groupsbyauthorid List of groups for the post authors indexed by author id
 223       * @param   post_read_receipt_collection_entity|null $readreceiptcollection Details of read receipts for each post
 224       * @param   array           $tagsbypostid List of tags for each post indexed by post id
 225       * @param   rating[]        $ratingbypostid List of ratings for each post indexed by post id
 226       * @param   bool            $includehtml Include some pre-constructed HTML in the export
 227       * @param   array           $inlineattachmentsbypostid List of attachments for each post indexed by post id
 228       * @return  posts_exporter
 229       */
 230      public function get_posts_exporter(
 231          stdClass $user,
 232          forum_entity $forum,
 233          discussion_entity $discussion,
 234          array $posts,
 235          array $authorsbyid = [],
 236          array $authorcontextids = [],
 237          array $attachmentsbypostid = [],
 238          array $groupsbyauthorid = [],
 239          post_read_receipt_collection_entity $readreceiptcollection = null,
 240          array $tagsbypostid = [],
 241          array $ratingbypostid = [],
 242          bool $includehtml = false,
 243          array $inlineattachmentsbypostid = []
 244      ) : posts_exporter {
 245          return new posts_exporter(
 246              $posts,
 247              $authorsbyid,
 248              $authorcontextids,
 249              $attachmentsbypostid,
 250              $groupsbyauthorid,
 251              $tagsbypostid,
 252              $ratingbypostid,
 253              [
 254                  'capabilitymanager' => $this->managerfactory->get_capability_manager($forum),
 255                  'urlfactory' => $this->urlfactory,
 256                  'forum' => $forum,
 257                  'discussion' => $discussion,
 258                  'user' => $user,
 259                  'context' => $forum->get_context(),
 260                  'readreceiptcollection' => $readreceiptcollection,
 261                  'includehtml' => $includehtml
 262              ],
 263              $inlineattachmentsbypostid
 264          );
 265      }
 266  
 267      /**
 268       * Fetch the structure of the posts exporter.
 269       *
 270       * @return  array
 271       */
 272      public static function get_posts_export_structure() {
 273          return posts_exporter::read_properties_definition();
 274      }
 275  }