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   * Exported discussion builder class.
  19   *
  20   * @package    mod_forum
  21   * @copyright  2019 Peter Dias<peter@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_forum\local\builders;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use mod_forum\local\entities\discussion as discussion_entity;
  30  use mod_forum\local\entities\forum as forum_entity;
  31  use mod_forum\local\factories\legacy_data_mapper as legacy_data_mapper_factory;
  32  use mod_forum\local\factories\exporter as exporter_factory;
  33  use mod_forum\local\factories\vault as vault_factory;
  34  use rating_manager;
  35  use renderer_base;
  36  use stdClass;
  37  
  38  /**
  39   * Exported discussion builder class
  40   *
  41   * This class is an implementation of the builder pattern (loosely). It is responsible
  42   * for taking a set of related forums, discussions, and posts and generate the exported
  43   * version of the discussion.
  44   *
  45   * It encapsulates the complexity involved with exporting discussions. All of the relevant
  46   * additional resources will be loaded by this class in order to ensure the exporting
  47   * process can happen.
  48   *
  49   * See this doc for more information on the builder pattern:
  50   * https://designpatternsphp.readthedocs.io/en/latest/Creational/Builder/README.html
  51   *
  52   * @copyright  2019 Peter Dias<peter@moodle.com>
  53   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  54   */
  55  class exported_discussion {
  56      /** @var renderer_base $renderer Core renderer */
  57      private $renderer;
  58  
  59      /** @var legacy_data_mapper_factory $legacydatamapperfactory Data mapper factory */
  60      private $legacydatamapperfactory;
  61  
  62      /** @var exporter_factory $exporterfactory Exporter factory */
  63      private $exporterfactory;
  64  
  65      /** @var vault_factory $vaultfactory Vault factory */
  66      private $vaultfactory;
  67  
  68      /** @var rating_manager $ratingmanager Rating manager */
  69      private $ratingmanager;
  70  
  71      /**
  72       * Constructor.
  73       *
  74       * @param renderer_base $renderer Core renderer
  75       * @param legacy_data_mapper_factory $legacydatamapperfactory Legacy data mapper factory
  76       * @param exporter_factory $exporterfactory Exporter factory
  77       * @param vault_factory $vaultfactory Vault factory
  78       * @param rating_manager $ratingmanager Rating manager
  79       */
  80      public function __construct(
  81          renderer_base $renderer,
  82          legacy_data_mapper_factory $legacydatamapperfactory,
  83          exporter_factory $exporterfactory,
  84          vault_factory $vaultfactory,
  85          rating_manager $ratingmanager
  86      ) {
  87          $this->renderer = $renderer;
  88          $this->legacydatamapperfactory = $legacydatamapperfactory;
  89          $this->exporterfactory = $exporterfactory;
  90          $this->vaultfactory = $vaultfactory;
  91          $this->ratingmanager = $ratingmanager;
  92      }
  93  
  94      /**
  95       * Build any additional variables for the exported discussion for a given set of discussions.
  96       *
  97       * This will typically be used for a list of discussions in the same forum.
  98       *
  99       * @param stdClass $user The user to export the posts for.
 100       * @param forum_entity $forum The forum that each of the $discussions belong to
 101       * @param discussion_entity $discussion A list of all discussions that each of the $posts belong to
 102       * @return stdClass[] List of exported posts in the same order as the $posts array.
 103       */
 104      public function build(
 105          stdClass $user,
 106          forum_entity $forum,
 107          discussion_entity $discussion
 108      ) : array {
 109  
 110          $favouriteids = [];
 111          if ($this->is_favourited($discussion, $forum->get_context(), $user)) {
 112              $favouriteids[] = $discussion->get_id();
 113          }
 114  
 115          $groupsbyid = $this->get_groups_available_in_forum($forum);
 116          $discussionexporter = $this->exporterfactory->get_discussion_exporter(
 117              $user, $forum, $discussion, $groupsbyid, $favouriteids
 118          );
 119  
 120          return (array) $discussionexporter->export($this->renderer);
 121      }
 122  
 123      /**
 124       * Get the groups details for all groups available to the forum.
 125       * @param forum_entity $forum The forum entity
 126       * @return stdClass[]
 127       */
 128      private function get_groups_available_in_forum($forum) : array {
 129          $course = $forum->get_course_record();
 130          $coursemodule = $forum->get_course_module_record();
 131  
 132          return groups_get_all_groups($course->id, 0, $coursemodule->groupingid);
 133      }
 134  
 135      /**
 136       * Check whether the provided discussion has been favourited by the user.
 137       *
 138       * @param discussion_entity $discussion The discussion record
 139       * @param \context_module $forumcontext Forum context
 140       * @param \stdClass $user The user to check the favourite against
 141       *
 142       * @return bool Whether or not the user has favourited the discussion
 143       */
 144      public function is_favourited(discussion_entity $discussion, \context_module $forumcontext, \stdClass $user) {
 145          if (!isloggedin()) {
 146              return false;
 147          }
 148  
 149          $usercontext = \context_user::instance($user->id);
 150          $ufservice = \core_favourites\service_factory::get_service_for_user_context($usercontext);
 151          return $ufservice->favourite_exists('mod_forum', 'discussions', $discussion->get_id(), $forumcontext);
 152      }
 153  
 154  
 155  }