Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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

   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   * Post 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\entities;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use stdClass;
  30  
  31  /**
  32   * Post class.
  33   *
  34   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class post {
  38      /** @var int $id ID */
  39      private $id;
  40      /** @var int $discussionid The id of the discussion this post belongs to */
  41      private $discussionid;
  42      /** @var int $parentid The id of the post that this post is replying to. Zero if it isn't a reply. */
  43      private $parentid;
  44      /** @var int $authorid The id of user who authored the post */
  45      private $authorid;
  46      /** @var int $timecreated Timestamp for when the post was created */
  47      private $timecreated;
  48      /** @var int $timemodified Timestamp for when the post last modified */
  49      private $timemodified;
  50      /** @var bool $mailed If the post has been mailed */
  51      private $mailed;
  52      /** @var string $subject Post subject */
  53      private $subject;
  54      /** @var string $message Post message */
  55      private $message;
  56      /** @var int $messageformat Format of the post message */
  57      private $messageformat;
  58      /** @var bool $messagetrust Is this a trusted message, i.e. created by a trusted user. */
  59      private $messagetrust;
  60      /** @var bool $hasattachments Does the post have attachments */
  61      private $hasattachments;
  62      /** @var int $totalscore Total score */
  63      private $totalscore;
  64      /** @var bool $mailnow Should this post be mailed immediately */
  65      private $mailnow;
  66      /** @var bool $deleted Is the post deleted */
  67      private $deleted;
  68      /** @var int $privatereplyto The user being privately replied to */
  69      private $privatereplyto;
  70      /** @var int $wordcount Number of words in the message */
  71      private $wordcount;
  72      /** @var int $charcount Number of chars in the message */
  73      private $charcount;
  74  
  75      /**
  76       * Constructor.
  77       *
  78       * @param int $id ID
  79       * @param int $discussionid The id of the discussion this post belongs to
  80       * @param int $parentid The id of the post that this post is replying to. Zero if it isn't a reply.
  81       * @param int $authorid The id of user who authored the post
  82       * @param int $timecreated Timestamp for when the post was created
  83       * @param int $timemodified Timestamp for when the post last modified
  84       * @param bool $mailed If the post has been mailed
  85       * @param string $subject Post subject
  86       * @param string $message Post message
  87       * @param int $messageformat Format of the post message
  88       * @param bool $messagetrust Is this a trusted message, i.e. created by a trusted user.
  89       * @param bool $hasattachments Does the post have attachments
  90       * @param int $totalscore Total score
  91       * @param bool $mailnow Should this post be mailed immediately
  92       * @param bool $deleted Is the post deleted
  93       * @param int $privatereplyto Which user this reply is intended for in a private reply situation
  94       */
  95      public function __construct(
  96          int $id,
  97          int $discussionid,
  98          int $parentid,
  99          int $authorid,
 100          int $timecreated,
 101          int $timemodified,
 102          bool $mailed,
 103          string $subject,
 104          string $message,
 105          int $messageformat,
 106          bool $messagetrust,
 107          bool $hasattachments,
 108          int $totalscore,
 109          bool $mailnow,
 110          bool $deleted,
 111          int $privatereplyto,
 112          ?int $wordcount,
 113          ?int $charcount
 114      ) {
 115          $this->id = $id;
 116          $this->discussionid = $discussionid;
 117          $this->parentid = $parentid;
 118          $this->authorid = $authorid;
 119          $this->timecreated = $timecreated;
 120          $this->timemodified = $timemodified;
 121          $this->mailed = $mailed;
 122          $this->subject = $subject;
 123          $this->message = $message;
 124          $this->messageformat = $messageformat;
 125          $this->messagetrust = $messagetrust;
 126          $this->hasattachments = $hasattachments;
 127          $this->totalscore = $totalscore;
 128          $this->mailnow = $mailnow;
 129          $this->deleted = $deleted;
 130          $this->privatereplyto = $privatereplyto;
 131          $this->wordcount = $wordcount;
 132          $this->charcount = $charcount;
 133      }
 134  
 135      /**
 136       * Get the post id.
 137       *
 138       * @return int
 139       */
 140      public function get_id() : int {
 141          return $this->id;
 142      }
 143  
 144      /**
 145       * Get the discussion id.
 146       *
 147       * @return int
 148       */
 149      public function get_discussion_id() : int {
 150          return $this->discussionid;
 151      }
 152  
 153      /**
 154       * Get the id of the parent post. Returns zero if this post is not a reply.
 155       *
 156       * @return int
 157       */
 158      public function get_parent_id() : int {
 159          return $this->parentid;
 160      }
 161  
 162      /**
 163       * Does this post have a parent? I.e. is it a reply?
 164       *
 165       * @return bool
 166       */
 167      public function has_parent() : bool {
 168          return $this->get_parent_id() > 0;
 169      }
 170  
 171      /**
 172       * Get the id of the user that authored the post.
 173       *
 174       * @return int
 175       */
 176      public function get_author_id() : int {
 177          return $this->authorid;
 178      }
 179  
 180      /**
 181       * Get the timestamp for when this post was created.
 182       *
 183       * @return int
 184       */
 185      public function get_time_created() : int {
 186          return $this->timecreated;
 187      }
 188  
 189      /**
 190       * Get the timestamp for when this post was last modified.
 191       *
 192       * @return int
 193       */
 194      public function get_time_modified() : int {
 195          return $this->timemodified;
 196      }
 197  
 198      /**
 199       * Has this post been mailed?
 200       *
 201       * @return bool
 202       */
 203      public function has_been_mailed() : bool {
 204          return $this->mailed;
 205      }
 206  
 207      /**
 208       * Get the post subject.
 209       *
 210       * @return string
 211       */
 212      public function get_subject() : string {
 213          return $this->subject;
 214      }
 215  
 216      /**
 217       * Get the post message.
 218       *
 219       * @return string
 220       */
 221      public function get_message() : string {
 222          return $this->message;
 223      }
 224  
 225      /**
 226       * Get the post message format.
 227       *
 228       * @return int
 229       */
 230      public function get_message_format() : int {
 231          return $this->messageformat;
 232      }
 233  
 234      /**
 235       * Is this a trusted message? I.e. was it authored by a trusted user?
 236       *
 237       * @return bool
 238       */
 239      public function is_message_trusted() : bool {
 240          return $this->messagetrust;
 241      }
 242  
 243      /**
 244       * Does this post have attachments?
 245       *
 246       * @return bool
 247       */
 248      public function has_attachments() : bool {
 249          return $this->hasattachments;
 250      }
 251  
 252      /**
 253       * Get the total score.
 254       *
 255       * @return int
 256       */
 257      public function get_total_score() : int {
 258          return $this->totalscore;
 259      }
 260  
 261      /**
 262       * Should this post be mailed now?
 263       *
 264       * @return bool
 265       */
 266      public function should_mail_now() : bool {
 267          return $this->mailnow;
 268      }
 269  
 270      /**
 271       * Is this post deleted?
 272       *
 273       * @return bool
 274       */
 275      public function is_deleted() : bool {
 276          return $this->deleted;
 277      }
 278  
 279      /**
 280       * Is this post private?
 281       *
 282       * @return bool
 283       */
 284      public function is_private_reply() : bool {
 285          return !empty($this->privatereplyto);
 286      }
 287  
 288      /**
 289       * Get the id of the user that this post was intended for.
 290       *
 291       * @return int
 292       */
 293      public function get_private_reply_recipient_id() : int {
 294          return $this->privatereplyto;
 295      }
 296  
 297  
 298      /**
 299       * Get the post's age in seconds.
 300       *
 301       * @return int
 302       */
 303      public function get_age() : int {
 304          return time() - $this->get_time_created();
 305      }
 306  
 307      /**
 308       * Check if the given user authored this post.
 309       *
 310       * @param stdClass $user The user to check.
 311       * @return bool
 312       */
 313      public function is_owned_by_user(stdClass $user) : bool {
 314          return $this->get_author_id() == $user->id;
 315      }
 316  
 317      /**
 318       * Check if the given post is a private reply intended for the given user.
 319       *
 320       * @param stdClass $user The user to check.
 321       * @return bool
 322       */
 323      public function is_private_reply_intended_for_user(stdClass $user) : bool {
 324          return $this->get_private_reply_recipient_id() == $user->id;
 325      }
 326  
 327      /**
 328       * Returns the word count.
 329       *
 330       * @return int|null
 331       */
 332      public function get_wordcount() : ?int {
 333          return $this->wordcount;
 334      }
 335  
 336      /**
 337       * Returns the char count.
 338       *
 339       * @return int|null
 340       */
 341      public function get_charcount() : ?int {
 342          return $this->charcount;
 343      }
 344  
 345      /**
 346       * This methods adds/updates forum posts' word count and char count attributes based on $data->message.
 347       *
 348       * @param \stdClass $record A record ready to be inserted / updated in DB.
 349       * @return void.
 350       */
 351      public static function add_message_counts(\stdClass $record) : void {
 352          if (!empty($record->message)) {
 353              $record->wordcount = count_words($record->message, $record->messageformat);
 354              $record->charcount = count_letters($record->message, $record->messageformat);
 355          }
 356      }
 357  }