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   * Discussion 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 mod_forum\local\entities\post as post_entity;
  30  
  31  /**
  32   * Discussion 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 discussion {
  38      /** @var int $id ID */
  39      private $id;
  40      /** @var int $courseid Course id */
  41      private $courseid;
  42      /** @var int $forumid Forum id */
  43      private $forumid;
  44      /** @var string $name Discussion name */
  45      private $name;
  46      /** @var int $firstpostid Id of the first post in the discussion */
  47      private $firstpostid;
  48      /** @var int $userid Id of the user that created the discussion */
  49      private $userid;
  50      /** @var int $groupid Group id if it's a group dicussion */
  51      private $groupid;
  52      /** @var bool $assessed Is the discussion assessed? */
  53      private $assessed;
  54      /** @var int $timemodified Timestamp for last modification to the discussion */
  55      private $timemodified;
  56      /** @var int $usermodified Id of user that last modified the discussion */
  57      private $usermodified;
  58      /** @var int $timestart Start time for the discussion */
  59      private $timestart;
  60      /** @var int $timeend End time for the discussion */
  61      private $timeend;
  62      /** @var bool $pinned Is the discussion pinned? */
  63      private $pinned;
  64      /** @var int $locked The timestamp of when the discussion was locked */
  65      private $timelocked;
  66  
  67      /**
  68       * Constructor.
  69       *
  70       * @param int $id ID
  71       * @param int $courseid Course id
  72       * @param int $forumid Forum id
  73       * @param string $name Discussion name
  74       * @param int $firstpostid Id of the first post in the discussion
  75       * @param int $userid Id of the user that created the discussion
  76       * @param int $groupid Group id if it's a group dicussion
  77       * @param bool $assessed Is the discussion assessed?
  78       * @param int $timemodified Timestamp for last modification to the discussion
  79       * @param int $usermodified Id of user that last modified the discussion
  80       * @param int $timestart Start time for the discussion
  81       * @param int $timeend End time for the discussion
  82       * @param bool $pinned Is the discussion pinned?
  83       * @param int $locked Time this discussion was locked
  84       */
  85      public function __construct(
  86          int $id,
  87          int $courseid,
  88          int $forumid,
  89          string $name,
  90          int $firstpostid,
  91          int $userid,
  92          int $groupid,
  93          bool $assessed,
  94          int $timemodified,
  95          int $usermodified,
  96          int $timestart,
  97          int $timeend,
  98          bool $pinned,
  99          int $locked
 100      ) {
 101          $this->id = $id;
 102          $this->courseid = $courseid;
 103          $this->forumid = $forumid;
 104          $this->name = $name;
 105          $this->firstpostid = $firstpostid;
 106          $this->userid = $userid;
 107          $this->groupid = $groupid;
 108          $this->assessed = $assessed;
 109          $this->timemodified = $timemodified;
 110          $this->usermodified = $usermodified;
 111          $this->timestart = $timestart;
 112          $this->timeend = $timeend;
 113          $this->pinned = $pinned;
 114          $this->timelocked = $locked;
 115      }
 116  
 117      /**
 118       * Get the discussion id.
 119       *
 120       * @return int
 121       */
 122      public function get_id() : int {
 123          return $this->id;
 124      }
 125  
 126      /**
 127       * Get the course id.
 128       *
 129       * @return int
 130       */
 131      public function get_course_id() : int {
 132          return $this->courseid;
 133      }
 134  
 135      /**
 136       * Get the forum id.
 137       *
 138       * @return int
 139       */
 140      public function get_forum_id() : int {
 141          return $this->forumid;
 142      }
 143  
 144      /**
 145       * Get the name of the discussion.
 146       *
 147       * @return string
 148       */
 149      public function get_name() : string {
 150          return $this->name;
 151      }
 152  
 153      /**
 154       * Get the id of the fist post in the discussion.
 155       *
 156       * @return int
 157       */
 158      public function get_first_post_id() : int {
 159          return $this->firstpostid;
 160      }
 161  
 162      /**
 163       * Get the id of the user that created the discussion.
 164       *
 165       * @return int
 166       */
 167      public function get_user_id() : int {
 168          return $this->userid;
 169      }
 170  
 171      /**
 172       * Get the id of the group that this discussion belongs to.
 173       *
 174       * @return int
 175       */
 176      public function get_group_id() : int {
 177          return $this->groupid;
 178      }
 179  
 180      /**
 181       * Check if this discussion is assessed.
 182       *
 183       * @return bool
 184       */
 185      public function is_assessed() : bool {
 186          return $this->assessed;
 187      }
 188  
 189      /**
 190       * Get the timestamp for when this discussion was last modified.
 191       *
 192       * @return int
 193       */
 194      public function get_time_modified() : int {
 195          return $this->timemodified;
 196      }
 197  
 198      /**
 199       * Get the id of the user that last modified this discussion.
 200       *
 201       * @return int
 202       */
 203      public function get_user_modified() : int {
 204          return $this->usermodified;
 205      }
 206  
 207      /**
 208       * Get the start time of this discussion. Returns zero if the discussion
 209       * has no designated start time.
 210       *
 211       * @return int
 212       */
 213      public function get_time_start() : int {
 214          return $this->timestart;
 215      }
 216  
 217      /**
 218       * Get the end time of this discussion. Returns zero if the discussion
 219       * has no designated end time.
 220       *
 221       * @return int
 222       */
 223      public function get_time_end() : int {
 224          return $this->timeend;
 225      }
 226  
 227      /**
 228       * Check if this discussion is pinned.
 229       *
 230       * @return bool
 231       */
 232      public function is_pinned() : bool {
 233          return $this->pinned;
 234      }
 235  
 236      /**
 237       * Get the locked time of this discussion.
 238       *
 239       * @return bool
 240       */
 241      public function get_locked() : int {
 242          return $this->timelocked;
 243      }
 244  
 245      /**
 246       * Is this discussion locked based on it's locked attribute
 247       *
 248       * @return bool
 249       */
 250      public function is_locked() : bool {
 251          return ($this->timelocked ? true : false);
 252      }
 253  
 254      /**
 255       * Set the locked timestamp
 256       *
 257       * @param int $timestamp The value we want to store into 'locked'
 258       */
 259      public function toggle_locked_state(int $timestamp) {
 260          // Check the current value against what we want the value to be i.e. '$timestamp'.
 261          $this->timelocked = ($this->timelocked && $timestamp ? $this->timelocked : $timestamp);
 262      }
 263  
 264      /**
 265       * Check if the given post is the first post in this discussion.
 266       *
 267       * @param post_entity $post The post to check
 268       * @return bool
 269       */
 270      public function is_first_post(post_entity $post) : bool {
 271          return $this->get_first_post_id() === $post->get_id();
 272      }
 273  
 274      /**
 275       * Check if the discussion has started yet. DEFAULTS: true if not set
 276       *
 277       * @return bool
 278       */
 279      public function has_started() : bool {
 280          $startime = $this->get_time_start();
 281          return empty($startime) || $startime < time();
 282      }
 283  
 284      /**
 285       * Check if the discussion has ended. DEFAULTS: false if not set
 286       *
 287       * @return bool
 288       */
 289      public function has_ended() : bool {
 290          $endtime = $this->get_time_end();
 291          return !empty($endtime) && $endtime < time();
 292      }
 293  
 294      /**
 295       * Check if the discussion belongs to a group.
 296       *
 297       * @return bool
 298       */
 299      public function has_group() : bool {
 300          return $this->get_group_id() > 0;
 301      }
 302  
 303      /**
 304       * Set the pinned value for this entity
 305       *
 306       * @param int $targetstate The state to change the pin to
 307       * @return bool
 308       */
 309      public function set_pinned(int $targetstate): void {
 310          if ($targetstate != $this->pinned) {
 311              $this->pinned = $targetstate;
 312          }
 313      }
 314  
 315      /**
 316       * Check if the discussion is timed.
 317       *
 318       * @return bool
 319       */
 320      public function is_timed_discussion() : bool {
 321          global $CFG;
 322  
 323          return !empty($CFG->forum_enabletimedposts) &&
 324                ($this->get_time_start() || $this->get_time_end());
 325      }
 326  
 327      /**
 328       * Check if the timed discussion is visible.
 329       *
 330       * @return bool
 331       */
 332      public function is_timed_discussion_visible() : bool {
 333          return !$this->is_timed_discussion() || ($this->has_started() && !$this->has_ended());
 334      }
 335  }