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]

   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   * Posts exporter class.
  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\exporters;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use mod_forum\local\entities\author as author_entity;
  30  use mod_forum\local\entities\post as post_entity;
  31  use mod_forum\local\exporters\post as post_exporter;
  32  use core\external\exporter;
  33  use renderer_base;
  34  
  35  require_once($CFG->dirroot . '/mod/forum/lib.php');
  36  
  37  /**
  38   * Posts exporter class.
  39   *
  40   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class posts extends exporter {
  44      /** @var post_entity[] $posts List of posts to export */
  45      private $posts;
  46      /** @var author_entity[] $authorsbyid List of authors for the posts indexed by author id */
  47      private $authorsbyid;
  48      /** @var int[] $authorcontextids List of authors context ids indexed by author id */
  49      private $authorcontextids;
  50      /** @var array $attachmentsbypostid List of attachments indexed by post id */
  51      private $attachmentsbypostid;
  52      /** @var array $inlineattachmentsbypostid List of inline attachments indexed by post id */
  53      private $inlineattachmentsbypostid;
  54      /** @var array $groupsbyauthorid List of author's groups indexed by author id */
  55      private $groupsbyauthorid;
  56      /** @var array $tagsbypostid List of tags indexed by post id */
  57      private $tagsbypostid;
  58      /** @var array $ratingbypostid List of ratings indexed by post id */
  59      private $ratingbypostid;
  60  
  61      /**
  62       * Constructor.
  63       *
  64       * @param post_entity[] $posts List of posts to export
  65       * @param author_entity[] $authorsbyid List of authors for the posts indexed by author id
  66       * @param int[] $authorcontextids List of authors context ids indexed by author id
  67       * @param array $attachmentsbypostid List of attachments indexed by post id
  68       * @param array $groupsbyauthorid List of author's groups indexed by author id
  69       * @param array $tagsbypostid List of tags indexed by post id
  70       * @param array $ratingbypostid List of ratings indexed by post id
  71       * @param array $related The related objects for exporting
  72       * @param array $inlineattachmentsbypostid List of inline attachments indexed by post id
  73       */
  74      public function __construct(
  75          array $posts,
  76          array $authorsbyid = [],
  77          array $authorcontextids = [],
  78          array $attachmentsbypostid = [],
  79          array $groupsbyauthorid = [],
  80          array $tagsbypostid = [],
  81          array $ratingbypostid = [],
  82          array $related = [],
  83          array $inlineattachmentsbypostid = []
  84      ) {
  85          $this->posts = $posts;
  86          $this->authorsbyid = $authorsbyid;
  87          $this->authorcontextids = $authorcontextids;
  88          $this->attachmentsbypostid = $attachmentsbypostid;
  89          $this->inlineattachmentsbypostid = $inlineattachmentsbypostid;
  90          $this->groupsbyauthorid = $groupsbyauthorid;
  91          $this->tagsbypostid = $tagsbypostid;
  92          $this->ratingbypostid = $ratingbypostid;
  93          return parent::__construct([], $related);
  94      }
  95  
  96      /**
  97       * Return the list of additional properties.
  98       *
  99       * @return array
 100       */
 101      protected static function define_other_properties() {
 102          return [
 103              'posts' => [
 104                  'type' => post_exporter::read_properties_definition(),
 105                  'multiple' => true
 106              ]
 107          ];
 108      }
 109  
 110      /**
 111       * Get the additional values to inject while exporting.
 112       *
 113       * @param renderer_base $output The renderer.
 114       * @return array Keys are the property names, values are their values.
 115       */
 116      protected function get_other_values(renderer_base $output) {
 117          $related = $this->related;
 118          $authorsbyid = $this->authorsbyid;
 119          $authorcontextids = $this->authorcontextids;
 120          $attachmentsbypostid = $this->attachmentsbypostid;
 121          $inlineattachmentsbypostid = $this->inlineattachmentsbypostid;
 122          $groupsbyauthorid = $this->groupsbyauthorid;
 123          $tagsbypostid = $this->tagsbypostid;
 124          $ratingbypostid = $this->ratingbypostid;
 125          $exportedposts = array_map(
 126              function($post) use (
 127                  $related,
 128                  $authorsbyid,
 129                  $authorcontextids,
 130                  $attachmentsbypostid,
 131                  $groupsbyauthorid,
 132                  $tagsbypostid,
 133                  $ratingbypostid,
 134                  $output,
 135                  $inlineattachmentsbypostid
 136              ) {
 137                  $authorid = $post->get_author_id();
 138                  $postid = $post->get_id();
 139                  $author = isset($authorsbyid[$authorid]) ? $authorsbyid[$authorid] : [];
 140                  $authorcontextid = isset($authorcontextids[$authorid]) ? $authorcontextids[$authorid] : null;
 141                  $attachments = isset($attachmentsbypostid[$postid]) ? $attachmentsbypostid[$postid] : [];
 142                  $inlineattachments = isset($inlineattachmentsbypostid[$postid]) ? $inlineattachmentsbypostid[$postid] : [];
 143                  $authorgroups = isset($groupsbyauthorid[$authorid]) ? $groupsbyauthorid[$authorid] : [];
 144                  $tags = isset($tagsbypostid[$postid]) ? $tagsbypostid[$postid] : [];
 145                  $rating = isset($ratingbypostid[$postid]) ? $ratingbypostid[$postid] : null;
 146                  $exporter = new post_exporter($post, array_merge($related, [
 147                      'author' => $author,
 148                      'authorcontextid' => $authorcontextid,
 149                      'attachments' => $attachments,
 150                      'messageinlinefiles' => $inlineattachments,
 151                      'authorgroups' => $authorgroups,
 152                      'tags' => $tags,
 153                      'rating' => $rating
 154                  ]));
 155                  return $exporter->export($output);
 156              },
 157              $this->posts
 158          );
 159  
 160          return [
 161              'posts' => $exportedposts
 162          ];
 163      }
 164  
 165      /**
 166       * Returns a list of objects that are related.
 167       *
 168       * @return array
 169       */
 170      protected static function define_related() {
 171          return [
 172              'capabilitymanager' => 'mod_forum\local\managers\capability',
 173              'urlfactory' => 'mod_forum\local\factories\url',
 174              'forum' => 'mod_forum\local\entities\forum',
 175              'discussion' => 'mod_forum\local\entities\discussion',
 176              'readreceiptcollection' => 'mod_forum\local\entities\post_read_receipt_collection?',
 177              'user' => 'stdClass',
 178              'context' => 'context',
 179              'includehtml' => 'bool'
 180          ];
 181      }
 182  }