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.
   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   * Entity factory.
  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\factories;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use mod_forum\local\entities\author as author_entity;
  30  use mod_forum\local\entities\discussion as discussion_entity;
  31  use mod_forum\local\entities\discussion_summary as discussion_summary_entity;
  32  use mod_forum\local\entities\forum as forum_entity;
  33  use mod_forum\local\entities\post as post_entity;
  34  use mod_forum\local\entities\post_read_receipt_collection as post_read_receipt_collection_entity;
  35  use mod_forum\local\entities\sorter as sorter_entity;
  36  use stdClass;
  37  use context;
  38  use cm_info;
  39  use user_picture;
  40  use moodle_url;
  41  
  42  /**
  43   * Entity factory to create the forum entities.
  44   *
  45   * See:
  46   * https://designpatternsphp.readthedocs.io/en/latest/Creational/SimpleFactory/README.html
  47   *
  48   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  49   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  50   */
  51  class entity {
  52      /**
  53       * Create a forum entity from a stdClass (legacy forum object).
  54       *
  55       * @param stdClass $record The forum record
  56       * @param context $context The forum module context
  57       * @param stdClass $coursemodule Course module record for the forum
  58       * @param stdClass $course Course the forum belongs to
  59       * @return forum_entity
  60       */
  61      public function get_forum_from_stdclass(
  62          stdClass $record,
  63          context $context,
  64          stdClass $coursemodule,
  65          stdClass $course
  66      ) : forum_entity {
  67          // Note: cm_info::create loads a cm_info in the context of the current user which
  68          // creates hidden dependency on the logged in user (very bad) however it's the best
  69          // option to load some data we need which doesn't require the logged in user.
  70          // Only use properties which do not require the logged in user.
  71          $cm = \cm_info::create($coursemodule);
  72  
  73          return new forum_entity(
  74              $context,
  75              $coursemodule,
  76              $course,
  77              // This property is a general module property that isn't affected by the logged in user.
  78              $cm->effectivegroupmode,
  79              $record->id,
  80              $record->course,
  81              $record->type,
  82              $record->name,
  83              $record->intro,
  84              $record->introformat,
  85              $record->assessed,
  86              $record->assesstimestart,
  87              $record->assesstimefinish,
  88              $record->scale,
  89              $record->grade_forum,
  90              $record->grade_forum_notify,
  91              $record->maxbytes,
  92              $record->maxattachments,
  93              $record->forcesubscribe,
  94              $record->trackingtype,
  95              $record->rsstype,
  96              $record->rssarticles,
  97              $record->timemodified,
  98              $record->warnafter,
  99              $record->blockafter,
 100              $record->blockperiod,
 101              $record->completiondiscussions,
 102              $record->completionreplies,
 103              $record->completionposts,
 104              $record->displaywordcount,
 105              $record->lockdiscussionafter,
 106              $record->duedate,
 107              $record->cutoffdate
 108          );
 109      }
 110  
 111      /**
 112       * Create a discussion entity from an stdClass (legacy dicussion object).
 113       *
 114       * @param stdClass $record Discussion record
 115       * @return discussion_entity
 116       */
 117      public function get_discussion_from_stdclass(stdClass $record) : discussion_entity {
 118          return new discussion_entity(
 119              $record->id,
 120              $record->course,
 121              $record->forum,
 122              $record->name,
 123              $record->firstpost,
 124              $record->userid,
 125              $record->groupid,
 126              $record->assessed,
 127              $record->timemodified,
 128              $record->usermodified,
 129              $record->timestart,
 130              $record->timeend,
 131              $record->pinned,
 132              $record->timelocked
 133          );
 134      }
 135  
 136      /**
 137       * Create a post entity from an stdClass (legacy post object).
 138       *
 139       * @param stdClass $record The post record
 140       * @return post_entity
 141       */
 142      public function get_post_from_stdclass(stdClass $record) : post_entity {
 143          return new post_entity(
 144              $record->id,
 145              $record->discussion,
 146              $record->parent,
 147              $record->userid,
 148              $record->created,
 149              $record->modified,
 150              $record->mailed,
 151              $record->subject,
 152              $record->message,
 153              $record->messageformat,
 154              $record->messagetrust,
 155              $record->attachment,
 156              $record->totalscore,
 157              $record->mailnow,
 158              $record->deleted,
 159              $record->privatereplyto,
 160              $record->wordcount,
 161              $record->charcount
 162          );
 163      }
 164  
 165      /**
 166       * Create an author entity from a user record.
 167       *
 168       * @param stdClass $record The user record
 169       * @return author_entity
 170       */
 171      public function get_author_from_stdclass(stdClass $record) : author_entity {
 172          return new author_entity(
 173              $record->id,
 174              $record->picture,
 175              $record->firstname,
 176              $record->lastname,
 177              fullname($record),
 178              $record->email,
 179              $record->deleted,
 180              $record->middlename,
 181              $record->firstnamephonetic,
 182              $record->lastnamephonetic,
 183              $record->alternatename,
 184              $record->imagealt
 185          );
 186      }
 187  
 188      /**
 189       * Create a discussion summary enttiy from stdClasses.
 190       *
 191       * @param stdClass $discussion The discussion record
 192       * @param stdClass $firstpost A post record for the first post in the discussion
 193       * @param stdClass $firstpostauthor A user record for the author of the first post
 194       * @param stdClass $latestpostauthor A user record for the author of the latest post in the discussion
 195       * @return discussion_summary_entity
 196       */
 197      public function get_discussion_summary_from_stdclass(
 198          stdClass $discussion,
 199          stdClass $firstpost,
 200          stdClass $firstpostauthor,
 201          stdClass $latestpostauthor
 202      ) : discussion_summary_entity {
 203  
 204          $firstpostauthorentity = $this->get_author_from_stdclass($firstpostauthor);
 205          return new discussion_summary_entity(
 206              $this->get_discussion_from_stdclass($discussion),
 207              $this->get_post_from_stdclass($firstpost, $firstpostauthorentity),
 208              $firstpostauthorentity,
 209              $this->get_author_from_stdclass($latestpostauthor)
 210          );
 211      }
 212  
 213      /**
 214       * Create a post read receipt collection entity from a list of read receipt records.
 215       *
 216       * @param array $records A list of read receipt records.
 217       * @return post_read_receipt_collection_entity
 218       */
 219      public function get_post_read_receipt_collection_from_stdclasses(array $records) : post_read_receipt_collection_entity {
 220          return new post_read_receipt_collection_entity($records);
 221      }
 222  
 223      /**
 224       * Create a sorter entity to sort post entities.
 225       *
 226       * @return sorter_entity
 227       */
 228      public function get_posts_sorter() : sorter_entity {
 229          return new sorter_entity(
 230              // Get id function for a post_entity.
 231              function(post_entity $post) {
 232                  return $post->get_id();
 233              },
 234              // Get parent id function for a post_entity.
 235              function(post_entity $post) {
 236                  return $post->get_parent_id();
 237              }
 238          );
 239      }
 240  
 241      /**
 242       * Create a sorter entity to sort exported posts.
 243       *
 244       * @return sorter_entity
 245       */
 246      public function get_exported_posts_sorter() : sorter_entity {
 247          return new sorter_entity(
 248              // Get id function for an exported post.
 249              function(stdClass $post) {
 250                  return $post->id;
 251              },
 252              // Get parent id function for an exported post.
 253              function(stdClass $post) {
 254                  return $post->parentid;
 255              }
 256          );
 257      }
 258  }