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.
   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.
  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\forum as forum_entity;
  30  use mod_forum\local\exporters\post as post_exporter;
  31  use core\external\exporter;
  32  use renderer_base;
  33  use stdClass;
  34  
  35  /**
  36   * Forum class.
  37   *
  38   * @copyright   2019 Andrew Nicols <andrew@nicols.co.uk>
  39   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class forum extends exporter {
  42      /** @var forum_entity The entity relating to the forum being displayed */
  43      private $forum;
  44  
  45      /**
  46       * Constructor for the forum exporter.
  47       *
  48       * @param   forum_entity    $forum The forum being displayed
  49       * @param   array           $related The related objects
  50       */
  51      public function __construct(forum_entity $forum, $related = []) {
  52          $this->forum = $forum;
  53          return parent::__construct([], $related);
  54      }
  55  
  56      /**
  57       * Return the list of additional properties.
  58       *
  59       * @return array
  60       */
  61      protected static function define_other_properties() {
  62          return [
  63              'id' => ['type' => PARAM_INT],
  64              'name' => ['type' => PARAM_RAW],
  65              'state' => [
  66                  'type' => [
  67                      'groupmode' => ['type' => PARAM_INT],
  68                      'gradingenabled' => ['type' => PARAM_BOOL],
  69                  ],
  70              ],
  71              'userstate' => [
  72                  'type' => [
  73                      'tracked' => ['type' => PARAM_INT],
  74                  ],
  75              ],
  76              'capabilities' => [
  77                  'type' => [
  78                      'viewdiscussions' => ['type' => PARAM_BOOL],
  79                      'create' => ['type' => PARAM_BOOL],
  80                      'subscribe' => ['type' => PARAM_BOOL],
  81                      'grade' => ['type' => PARAM_BOOL],
  82                  ]
  83              ],
  84              'urls' => [
  85                  'type' => [
  86                      'create' => ['type' => PARAM_URL],
  87                      'markasread' => ['type' => PARAM_URL],
  88                      'view' => ['type' => PARAM_URL],
  89                      'sortrepliesasc' => ['type' => PARAM_URL],
  90                      'sortrepliesdesc' => ['type' => PARAM_URL],
  91                      'sortlastpostasc' => ['type' => PARAM_URL],
  92                      'sortlastpostdesc' => ['type' => PARAM_URL],
  93                      'sortcreatedasc' => ['type' => PARAM_URL],
  94                      'sortcreateddesc' => ['type' => PARAM_URL],
  95                      'sortdiscussionasc' => ['type' => PARAM_URL],
  96                      'sortdiscussiondesc' => ['type' => PARAM_URL],
  97                      'sortstarterasc' => ['type' => PARAM_URL],
  98                      'sortstarterdesc' => ['type' => PARAM_URL],
  99                      'sortgroupasc' => ['type' => PARAM_URL],
 100                      'sortgroupdesc' => ['type' => PARAM_URL],
 101                  ],
 102              ],
 103          ];
 104      }
 105  
 106      /**
 107       * Get the additional values to inject while exporting.
 108       *
 109       * @param renderer_base $output The renderer.
 110       * @return array Keys are the property names, values are their values.
 111       */
 112      protected function get_other_values(renderer_base $output) {
 113          $capabilitymanager = $this->related['capabilitymanager'];
 114          $urlfactory = $this->related['urlfactory'];
 115          $user = $this->related['user'];
 116          $currentgroup = $this->related['currentgroup'];
 117          $vaultfactory = $this->related['vaultfactory'];
 118          $discussionvault = $vaultfactory->get_discussions_in_forum_vault();
 119  
 120          return [
 121              'id' => $this->forum->get_id(),
 122              'name' => $this->forum->get_name(),
 123              'state' => [
 124                  'groupmode' => $this->forum->get_effective_group_mode(),
 125                  'gradingenabled' => $this->forum->is_grading_enabled()
 126              ],
 127              'userstate' => [
 128                  'tracked' => forum_tp_is_tracked($this->get_forum_record(), $this->related['user']),
 129              ],
 130              'capabilities' => [
 131                  'viewdiscussions' => $capabilitymanager->can_view_discussions($user),
 132                  'create' => $capabilitymanager->can_create_discussions($user, $currentgroup),
 133                  'selfenrol' => $capabilitymanager->can_self_enrol($user),
 134                  'subscribe' => $capabilitymanager->can_subscribe_to_forum($user),
 135                  'grade' => $capabilitymanager->can_grade($user),
 136              ],
 137              'urls' => [
 138                  'create' => $urlfactory->get_discussion_create_url($this->forum)->out(false),
 139                  'markasread' => $urlfactory->get_mark_all_discussions_as_read_url($this->forum)->out(false),
 140                  'view' => $urlfactory->get_forum_view_url_from_forum($this->forum)->out(false),
 141                  'sortrepliesasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
 142                      $discussionvault::SORTORDER_REPLIES_ASC)->out(false),
 143                  'sortrepliesdesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
 144                      $discussionvault::SORTORDER_REPLIES_DESC)->out(false),
 145                  'sortlastpostasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
 146                      $discussionvault::SORTORDER_LASTPOST_ASC)->out(false),
 147                  'sortlastpostdesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
 148                      $discussionvault::SORTORDER_LASTPOST_DESC)->out(false),
 149                  'sortcreatedasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
 150                      $discussionvault::SORTORDER_CREATED_ASC)->out(false),
 151                  'sortcreateddesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
 152                      $discussionvault::SORTORDER_CREATED_DESC)->out(false),
 153                  'sortdiscussionasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
 154                      $discussionvault::SORTORDER_DISCUSSION_ASC)->out(false),
 155                  'sortdiscussiondesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
 156                      $discussionvault::SORTORDER_DISCUSSION_DESC)->out(false),
 157                  'sortstarterasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
 158                      $discussionvault::SORTORDER_STARTER_ASC)->out(false),
 159                  'sortstarterdesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
 160                      $discussionvault::SORTORDER_STARTER_DESC)->out(false),
 161                  'sortgroupasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
 162                      $discussionvault::SORTORDER_GROUP_ASC)->out(false),
 163                  'sortgroupdesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
 164                      $discussionvault::SORTORDER_GROUP_DESC)->out(false),
 165              ],
 166          ];
 167      }
 168  
 169      /**
 170       * Returns a list of objects that are related.
 171       *
 172       * @return array
 173       */
 174      protected static function define_related() {
 175          return [
 176              'legacydatamapperfactory' => 'mod_forum\local\factories\legacy_data_mapper',
 177              'capabilitymanager' => 'mod_forum\local\managers\capability',
 178              'urlfactory' => 'mod_forum\local\factories\url',
 179              'user' => 'stdClass',
 180              'currentgroup' => 'int?',
 181              'vaultfactory' => 'mod_forum\local\factories\vault'
 182          ];
 183      }
 184  
 185      /**
 186       * Get the legacy forum record for this forum.
 187       *
 188       * @return  stdClass
 189       */
 190      private function get_forum_record() : stdClass {
 191          $forumdbdatamapper = $this->related['legacydatamapperfactory']->get_forum_data_mapper();
 192          return $forumdbdatamapper->to_legacy_object($this->forum);
 193      }
 194  }