Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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.
   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   * Discussion summaries exporter.
  19   *
  20   * @package     mod_forum
  21   * @copyright   2019 Andrew Nicols <andrew@nicols.co.uk>
  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\discussion as discussion_entity;
  30  use mod_forum\local\exporters\post as post_exporter;
  31  use core\external\exporter;
  32  use renderer_base;
  33  
  34  /**
  35   * Discussion summaries exporter.
  36   *
  37   * @copyright   2019 Andrew Nicols <andrew@nicols.co.uk>
  38   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class discussion_summaries extends exporter {
  41      /** @var discussion_summary_entity[] The list of discussion summaries to export */
  42      private $discussions;
  43  
  44      /** @var stdClass[] The group information for each author */
  45      private $groupsbyid;
  46  
  47      /** @var stdClass[] The group information for each author */
  48      private $groupsbyauthorid;
  49  
  50      /** @var int[] Discussion reply counts indexed by dicussion id */
  51      private $discussionreplycount;
  52  
  53      /** @var int[] Discussion unread counts indexed by dicussion id */
  54      private $discussionunreadcount;
  55  
  56      /** @var array The latest post in each discussion */
  57      private $latestpostids;
  58  
  59      /** @var int[] The context ids for the first and latest post authors (indexed by author id) */
  60      private $postauthorcontextids;
  61  
  62      /**
  63       * Constructor.
  64       *
  65       * @param discussion_summary_entity[] $discussion The list of discussion summaries to export
  66       * @param stdClass[] $groupsbyid The group information for each author
  67       * @param stdClass[] $groupsbyauthorid The group information for each author
  68       * @param int[] $discussionreplycount Discussion reply counts indexed by dicussion id
  69       * @param int[] $discussionunreadcount Discussion unread counts indexed by dicussion id
  70       * @param int[] $latestpostids List of latest post ids indexed by discussion id
  71       * @param int[] $postauthorcontextids The context ids for the first and latest post authors (indexed by author id)
  72       * @param array $related The related
  73       */
  74      public function __construct(
  75          array $discussions,
  76          array $groupsbyid,
  77          array $groupsbyauthorid,
  78          array $discussionreplycount,
  79          array $discussionunreadcount,
  80          array $latestpostids,
  81          array $postauthorcontextids,
  82          array $related = []
  83      ) {
  84          $this->discussions = $discussions;
  85          $this->groupsbyid = $groupsbyid;
  86          $this->groupsbyauthorid = $groupsbyauthorid;
  87          $this->discussionreplycount = $discussionreplycount;
  88          $this->discussionunreadcount = $discussionunreadcount;
  89          $this->latestpostids = $latestpostids;
  90          $this->postauthorcontextids = $postauthorcontextids;
  91          return parent::__construct([], $related);
  92      }
  93  
  94      /**
  95       * Return the list of additional properties.
  96       *
  97       * @return array
  98       */
  99      protected static function define_other_properties() {
 100          return [
 101              'summaries' => [
 102                  'type' => discussion_summary::read_properties_definition(),
 103                  'multiple' => true
 104              ],
 105              'state' => [
 106                  'type' => [
 107                      'hasdiscussions' => ['type' => PARAM_BOOL],
 108                  ],
 109              ],
 110          ];
 111      }
 112  
 113      /**
 114       * Get the additional values to inject while exporting.
 115       *
 116       * @param renderer_base $output The renderer.
 117       * @return array Keys are the property names, values are their values.
 118       */
 119      protected function get_other_values(renderer_base $output) {
 120          $exporteddiscussions = [];
 121          $related = $this->related;
 122          $latestauthors = $this->related['latestauthors'];
 123  
 124          foreach ($this->discussions as $discussion) {
 125              $discussionid = $discussion->get_discussion()->get_id();
 126              $replycount = isset($this->discussionreplycount[$discussionid]) ? $this->discussionreplycount[$discussionid] : 0;
 127              $unreadcount = isset($this->discussionunreadcount[$discussionid]) ? $this->discussionunreadcount[$discussionid] : 0;
 128              $latestpostid = isset($this->latestpostids[$discussionid]) ? $this->latestpostids[$discussionid] : 0;
 129              $latestauthor = $latestauthors[$discussionid] ?? null;
 130              $related['latestauthor'] = $latestauthor;
 131              $exporter = new discussion_summary(
 132                      $discussion,
 133                      $this->groupsbyid,
 134                      $this->groupsbyauthorid,
 135                      $replycount,
 136                      $unreadcount,
 137                      $latestpostid,
 138                      $this->postauthorcontextids[$discussion->get_first_post_author()->get_id()],
 139                      $this->postauthorcontextids[$latestauthor->get_id()],
 140                      $related
 141                  );
 142              $exporteddiscussions[] = $exporter->export($output);
 143          }
 144  
 145          return [
 146              'summaries' => $exporteddiscussions,
 147              'state' => [
 148                  'hasdiscussions' => !empty($exporteddiscussions),
 149              ],
 150          ];
 151      }
 152  
 153      /**
 154       * Returns a list of objects that are related.
 155       *
 156       * @return array
 157       */
 158      protected static function define_related() {
 159          return [
 160              'legacydatamapperfactory' => 'mod_forum\local\factories\legacy_data_mapper',
 161              'context' => 'context',
 162              'forum' => 'mod_forum\local\entities\forum',
 163              'capabilitymanager' => 'mod_forum\local\managers\capability',
 164              'urlfactory' => 'mod_forum\local\factories\url',
 165              'user' => 'stdClass',
 166              'favouriteids' => 'int[]?',
 167              'latestauthors' => 'mod_forum\local\entities\author[]?'
 168          ];
 169      }
 170  }