Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   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 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\discussion as discussion_entity;
  30  use mod_forum\local\exporters\post as post_exporter;
  31  use mod_forum\local\factories\exporter as exporter_factory;
  32  use core\external\exporter;
  33  use renderer_base;
  34  
  35  /**
  36   * Discussion exporter class.
  37   *
  38   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class discussion extends exporter {
  42      /** @var discussion_entity $discussion Discussion to export */
  43      private $discussion;
  44  
  45      /**
  46       * Constructor.
  47       *
  48       * @param discussion_entity $discussion Discussion to export
  49       * @param array $related The related export data
  50       */
  51      public function __construct(discussion_entity $discussion, array $related = []) {
  52          $this->discussion = $discussion;
  53  
  54          return parent::__construct([], $related);
  55      }
  56  
  57      /**
  58       * Return the list of additional properties.
  59       *
  60       * @return array
  61       */
  62      protected static function define_other_properties() {
  63          return [
  64              'id' => ['type' => PARAM_INT],
  65              'forumid' => ['type' => PARAM_INT],
  66              'pinned' => ['type' => PARAM_BOOL],
  67              'locked' => ['type' => PARAM_BOOL],
  68              'istimelocked' => ['type' => PARAM_BOOL],
  69              'name' => ['type' => PARAM_TEXT],
  70              'firstpostid' => ['type' => PARAM_INT],
  71              'group' => [
  72                  'optional' => true,
  73                  'type' => [
  74                      'name' => ['type' => PARAM_TEXT],
  75                      'urls' => [
  76                          'type' => [
  77                              'picture' => [
  78                                  'optional' => true,
  79                                  'type' => PARAM_URL,
  80                              ],
  81                              'userlist' => [
  82                                  'optional' => true,
  83                                  'type' => PARAM_URL,
  84                              ],
  85                          ],
  86                      ],
  87                  ],
  88              ],
  89              'times' => [
  90                  'type' => [
  91                      'modified' => ['type' => PARAM_INT],
  92                      'start' => ['type' => PARAM_INT],
  93                      'end' => ['type' => PARAM_INT],
  94                      'locked' => ['type' => PARAM_INT],
  95                  ],
  96              ],
  97              'userstate' => [
  98                  'type' => [
  99                      'subscribed' => ['type' => PARAM_BOOL],
 100                      'favourited' => ['type' => PARAM_BOOL],
 101                  ],
 102              ],
 103              'capabilities' => [
 104                  'type' => [
 105                      'subscribe' => ['type' => PARAM_BOOL],
 106                      'move' => ['type' => PARAM_BOOL],
 107                      'pin' => ['type' => PARAM_BOOL],
 108                      'post' => ['type' => PARAM_BOOL],
 109                      'manage' => ['type' => PARAM_BOOL],
 110                      'favourite' => ['type' => PARAM_BOOL]
 111                  ]
 112              ],
 113              'urls' => [
 114                  'type' => [
 115                      'view' => ['type' => PARAM_URL],
 116                      'viewlatest' => [
 117                          'optional' => true,
 118                          'type' => PARAM_URL
 119                      ],
 120                      'viewfirstunread' => [
 121                          'optional' => true,
 122                          'type' => PARAM_URL,
 123                      ],
 124                      'markasread' => ['type' => PARAM_URL],
 125                      'subscribe' => ['type' => PARAM_URL],
 126                      'pin' => [
 127                          'optional' => true,
 128                          'type' => PARAM_URL,
 129                      ],
 130                  ],
 131              ],
 132              'timed' => [
 133                  'type' => [
 134                      'istimed' => [
 135                          'type' => PARAM_BOOL,
 136                          'optional' => true,
 137                          'default' => null,
 138                          'null' => NULL_ALLOWED
 139                      ],
 140                      'visible' => [
 141                          'type' => PARAM_BOOL,
 142                          'optional' => true,
 143                          'default' => null,
 144                          'null' => NULL_ALLOWED
 145                      ]
 146                  ]
 147              ]
 148          ];
 149      }
 150  
 151      /**
 152       * Get the additional values to inject while exporting.
 153       *
 154       * @param renderer_base $output The renderer.
 155       * @return array Keys are the property names, values are their values.
 156       */
 157      protected function get_other_values(renderer_base $output) {
 158  
 159          $capabilitymanager = $this->related['capabilitymanager'];
 160          $urlfactory = $this->related['urlfactory'];
 161          $favouriteids = isset($this->related['favouriteids']) ? $this->related['favouriteids'] : [];
 162  
 163          $forum = $this->related['forum'];
 164          $forumrecord = $this->get_forum_record();
 165          $user = $this->related['user'];
 166          $discussion = $this->discussion;
 167  
 168          $groupdata = null;
 169          if ($discussion->has_group()) {
 170              $groupsbyid = $this->related['groupsbyid'];
 171              $group = $groupsbyid[$discussion->get_group_id()] ?? null;
 172  
 173              // We may not have received the group if the caller doesn't want to include it in the export
 174              // or if it's been deleted and the discussion record hasn't been updated.
 175              if ($group) {
 176                  $groupdata = [
 177                      'name' => format_string($group->name, true, ['context' => $this->related['context']]),
 178                      'urls' => [],
 179                  ];
 180  
 181                  // If not hiding the group picture, and the group has a picture then use it. Fallback to generic group image.
 182                  if ($url = get_group_picture_url($group, $forum->get_course_id(), true)) {
 183  
 184                      $groupdata['urls']['picture'] = $url;
 185                  } else {
 186                      $groupdata['urls']['picture'] = $output->image_url('g/g1')->out(false);
 187                  }
 188  
 189                  if ($capabilitymanager->can_view_participants($user, $discussion)) {
 190                      $groupdata['urls']['userlist'] = (new \moodle_url('/user/index.php', [
 191                          'id' => $forum->get_course_id(),
 192                          'group' => $group->id,
 193                      ]));
 194                  }
 195              }
 196          }
 197  
 198          $viewfirstunreadurl = $urlfactory->get_discussion_view_first_unread_post_url_from_discussion($discussion);
 199          $data = [
 200              'id' => $discussion->get_id(),
 201              'forumid' => $forum->get_id(),
 202              'pinned' => $discussion->is_pinned(),
 203              'locked' => $forum->is_discussion_locked($discussion),
 204              'istimelocked' => $forum->is_discussion_time_locked($discussion),
 205              'name' => format_string($discussion->get_name(), true, [
 206                  'context' => $this->related['context']
 207              ]),
 208              'firstpostid' => $discussion->get_first_post_id(),
 209              'times' => [
 210                  'modified' => $discussion->get_time_modified(),
 211                  'start' => $discussion->get_time_start(),
 212                  'end' => $discussion->get_time_end(),
 213                  'locked' => $discussion->get_locked()
 214              ],
 215              'userstate' => [
 216                  'subscribed' => \mod_forum\subscriptions::is_subscribed($user->id, $forumrecord, $discussion->get_id()),
 217                  'favourited' => in_array($discussion->get_id(), $favouriteids) ? true : false,
 218              ],
 219              'capabilities' => [
 220                  'subscribe' => $capabilitymanager->can_subscribe_to_discussion($user, $discussion),
 221                  'move' => $capabilitymanager->can_move_discussion($user, $discussion),
 222                  'pin' => $capabilitymanager->can_pin_discussion($user, $discussion),
 223                  'post' => $capabilitymanager->can_post_in_discussion($user, $discussion),
 224                  'manage' => $capabilitymanager->can_manage_forum($user),
 225                  'favourite' => $capabilitymanager->can_favourite_discussion($user) // Defaulting to true until we get capabilities sorted
 226              ],
 227              'urls' => [
 228                  'view' => $urlfactory->get_discussion_view_url_from_discussion($discussion)->out(false),
 229                  'viewfirstunread' => $viewfirstunreadurl->out(false),
 230                  'markasread' => $urlfactory->get_mark_discussion_as_read_url_from_discussion($forum, $discussion)->out(false),
 231                  'subscribe' => $urlfactory->get_discussion_subscribe_url($discussion)->out(false)
 232              ]
 233          ];
 234  
 235          if (!empty($this->related['latestpostid'])) {
 236              $data['urls']['viewlatest'] = $urlfactory->get_discussion_view_latest_post_url_from_discussion(
 237                      $discussion,
 238                      $this->related['latestpostid']
 239                  )->out(false);
 240          }
 241  
 242          if ($capabilitymanager->can_pin_discussions($user)) {
 243              $data['urls']['pin'] = $urlfactory->get_pin_discussion_url_from_discussion($discussion)->out(false);
 244          }
 245  
 246          if ($groupdata) {
 247              $data['group'] = $groupdata;
 248          }
 249  
 250          $canviewhiddentimedposts = $capabilitymanager->can_view_hidden_posts($user);
 251          $canalwaysseetimedpost = $user->id == $discussion->get_user_id() || $canviewhiddentimedposts;
 252          $data['timed']['istimed'] = $canalwaysseetimedpost ? $discussion->is_timed_discussion() : null;
 253          $data['timed']['visible'] = $canalwaysseetimedpost ? $discussion->is_timed_discussion_visible() : null;
 254  
 255          return $data;
 256      }
 257  
 258      /**
 259       * Get the legacy forum record from the forum entity.
 260       *
 261       * @return stdClass
 262       */
 263      private function get_forum_record() {
 264          $forumdbdatamapper = $this->related['legacydatamapperfactory']->get_forum_data_mapper();
 265          return $forumdbdatamapper->to_legacy_object($this->related['forum']);
 266      }
 267  
 268      /**
 269       * Returns a list of objects that are related.
 270       *
 271       * @return array
 272       */
 273      protected static function define_related() {
 274          return [
 275              'legacydatamapperfactory' => 'mod_forum\local\factories\legacy_data_mapper',
 276              'context' => 'context',
 277              'forum' => 'mod_forum\local\entities\forum',
 278              'capabilitymanager' => 'mod_forum\local\managers\capability',
 279              'urlfactory' => 'mod_forum\local\factories\url',
 280              'user' => 'stdClass',
 281              'groupsbyid' => 'stdClass[]',
 282              'latestpostid' => 'int?',
 283              'favouriteids' => 'int[]?'
 284          ];
 285      }
 286  }