Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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 $groupsbyauthorid List of author's groups indexed by author id */
  53      private $groupsbyauthorid;
  54      /** @var array $tagsbypostid List of tags indexed by post id */
  55      private $tagsbypostid;
  56      /** @var array $ratingbypostid List of ratings indexed by post id */
  57      private $ratingbypostid;
  58  
  59      /**
  60       * Constructor.
  61       *
  62       * @param post_entity[] $posts List of posts to export
  63       * @param author_entity[] $authorsbyid List of authors for the posts indexed by author id
  64       * @param int[] $authorcontextids List of authors context ids indexed by author id
  65       * @param array $attachmentsbypostid List of attachments indexed by post id
  66       * @param array $groupsbyauthorid List of author's groups indexed by author id
  67       * @param array $tagsbypostid List of tags indexed by post id
  68       * @param array $ratingbypostid List of ratings indexed by post id
  69       * @param array $related The related objects for exporting
  70       */
  71      public function __construct(
  72          array $posts,
  73          array $authorsbyid = [],
  74          array $authorcontextids = [],
  75          array $attachmentsbypostid = [],
  76          array $groupsbyauthorid = [],
  77          array $tagsbypostid = [],
  78          array $ratingbypostid = [],
  79          array $related = []
  80      ) {
  81          $this->posts = $posts;
  82          $this->authorsbyid = $authorsbyid;
  83          $this->authorcontextids = $authorcontextids;
  84          $this->attachmentsbypostid = $attachmentsbypostid;
  85          $this->groupsbyauthorid = $groupsbyauthorid;
  86          $this->tagsbypostid = $tagsbypostid;
  87          $this->ratingbypostid = $ratingbypostid;
  88          return parent::__construct([], $related);
  89      }
  90  
  91      /**
  92       * Return the list of additional properties.
  93       *
  94       * @return array
  95       */
  96      protected static function define_other_properties() {
  97          return [
  98              'posts' => [
  99                  'type' => post_exporter::read_properties_definition(),
 100                  'multiple' => true
 101              ]
 102          ];
 103      }
 104  
 105      /**
 106       * Get the additional values to inject while exporting.
 107       *
 108       * @param renderer_base $output The renderer.
 109       * @return array Keys are the property names, values are their values.
 110       */
 111      protected function get_other_values(renderer_base $output) {
 112          $related = $this->related;
 113          $authorsbyid = $this->authorsbyid;
 114          $authorcontextids = $this->authorcontextids;
 115          $attachmentsbypostid = $this->attachmentsbypostid;
 116          $groupsbyauthorid = $this->groupsbyauthorid;
 117          $tagsbypostid = $this->tagsbypostid;
 118          $ratingbypostid = $this->ratingbypostid;
 119          $exportedposts = array_map(
 120              function($post) use (
 121                  $related,
 122                  $authorsbyid,
 123                  $authorcontextids,
 124                  $attachmentsbypostid,
 125                  $groupsbyauthorid,
 126                  $tagsbypostid,
 127                  $ratingbypostid,
 128                  $output
 129              ) {
 130                  $authorid = $post->get_author_id();
 131                  $postid = $post->get_id();
 132                  $author = isset($authorsbyid[$authorid]) ? $authorsbyid[$authorid] : [];
 133                  $authorcontextid = isset($authorcontextids[$authorid]) ? $authorcontextids[$authorid] : null;
 134                  $attachments = isset($attachmentsbypostid[$postid]) ? $attachmentsbypostid[$postid] : [];
 135                  $authorgroups = isset($groupsbyauthorid[$authorid]) ? $groupsbyauthorid[$authorid] : [];
 136                  $tags = isset($tagsbypostid[$postid]) ? $tagsbypostid[$postid] : [];
 137                  $rating = isset($ratingbypostid[$postid]) ? $ratingbypostid[$postid] : null;
 138                  $exporter = new post_exporter($post, array_merge($related, [
 139                      'author' => $author,
 140                      'authorcontextid' => $authorcontextid,
 141                      'attachments' => $attachments,
 142                      'authorgroups' => $authorgroups,
 143                      'tags' => $tags,
 144                      'rating' => $rating
 145                  ]));
 146                  return $exporter->export($output);
 147              },
 148              $this->posts
 149          );
 150  
 151          return [
 152              'posts' => $exportedposts
 153          ];
 154      }
 155  
 156      /**
 157       * Returns a list of objects that are related.
 158       *
 159       * @return array
 160       */
 161      protected static function define_related() {
 162          return [
 163              'capabilitymanager' => 'mod_forum\local\managers\capability',
 164              'urlfactory' => 'mod_forum\local\factories\url',
 165              'forum' => 'mod_forum\local\entities\forum',
 166              'discussion' => 'mod_forum\local\entities\discussion',
 167              'readreceiptcollection' => 'mod_forum\local\entities\post_read_receipt_collection?',
 168              'user' => 'stdClass',
 169              'context' => 'context',
 170              'includehtml' => 'bool'
 171          ];
 172      }
 173  }