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   * mod_forum data generator
  19   *
  20   * @package    mod_forum
  21   * @category   test
  22   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  
  29  /**
  30   * Forum module data generator class
  31   *
  32   * @package    mod_forum
  33   * @category   test
  34   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class mod_forum_generator extends testing_module_generator {
  38  
  39      /**
  40       * @var int keep track of how many forum discussions have been created.
  41       */
  42      protected $forumdiscussioncount = 0;
  43  
  44      /**
  45       * @var int keep track of how many forum posts have been created.
  46       */
  47      protected $forumpostcount = 0;
  48  
  49      /**
  50       * @var int keep track of how many forum subscriptions have been created.
  51       */
  52      protected $forumsubscriptionscount = 0;
  53  
  54      /**
  55       * To be called from data reset code only,
  56       * do not use in tests.
  57       * @return void
  58       */
  59      public function reset() {
  60          $this->forumdiscussioncount = 0;
  61          $this->forumpostcount = 0;
  62          $this->forumsubscriptionscount = 0;
  63  
  64          parent::reset();
  65      }
  66  
  67      public function create_instance($record = null, array $options = null) {
  68          global $CFG;
  69          require_once($CFG->dirroot.'/mod/forum/lib.php');
  70          $record = (object)(array)$record;
  71  
  72          if (!isset($record->type)) {
  73              $record->type = 'general';
  74          }
  75          if (!isset($record->assessed)) {
  76              $record->assessed = 0;
  77          }
  78          if (!isset($record->scale)) {
  79              $record->scale = 0;
  80          }
  81          if (!isset($record->forcesubscribe)) {
  82              $record->forcesubscribe = FORUM_CHOOSESUBSCRIBE;
  83          }
  84          if (!isset($record->grade_forum)) {
  85              $record->grade_forum = 0;
  86          }
  87  
  88          return parent::create_instance($record, (array)$options);
  89      }
  90  
  91      /**
  92       * Function to create a dummy subscription.
  93       *
  94       * @param array|stdClass $record
  95       * @return stdClass the subscription object
  96       */
  97      public function create_subscription($record = null) {
  98          global $DB;
  99  
 100          // Increment the forum subscription count.
 101          $this->forumsubscriptionscount++;
 102  
 103          $record = (array)$record;
 104  
 105          if (!isset($record['course'])) {
 106              throw new coding_exception('course must be present in phpunit_util::create_subscription() $record');
 107          }
 108  
 109          if (!isset($record['forum'])) {
 110              throw new coding_exception('forum must be present in phpunit_util::create_subscription() $record');
 111          }
 112  
 113          if (!isset($record['userid'])) {
 114              throw new coding_exception('userid must be present in phpunit_util::create_subscription() $record');
 115          }
 116  
 117          $record = (object)$record;
 118  
 119          // Add the subscription.
 120          $record->id = $DB->insert_record('forum_subscriptions', $record);
 121  
 122          return $record;
 123      }
 124  
 125      /**
 126       * Function to create a dummy discussion.
 127       *
 128       * @param array|stdClass $record
 129       * @return stdClass the discussion object
 130       */
 131      public function create_discussion($record = null) {
 132          global $DB;
 133  
 134          // Increment the forum discussion count.
 135          $this->forumdiscussioncount++;
 136  
 137          $record = (array) $record;
 138  
 139          if (!isset($record['course'])) {
 140              throw new coding_exception('course must be present in phpunit_util::create_discussion() $record');
 141          }
 142  
 143          if (!isset($record['forum'])) {
 144              throw new coding_exception('forum must be present in phpunit_util::create_discussion() $record');
 145          }
 146  
 147          if (!isset($record['userid'])) {
 148              throw new coding_exception('userid must be present in phpunit_util::create_discussion() $record');
 149          }
 150  
 151          if (!isset($record['name'])) {
 152              $record['name'] = "Discussion " . $this->forumdiscussioncount;
 153          }
 154  
 155          if (!isset($record['subject'])) {
 156              $record['subject'] = "Subject for discussion " . $this->forumdiscussioncount;
 157          }
 158  
 159          if (!isset($record['message'])) {
 160              $record['message'] = html_writer::tag('p', 'Message for discussion ' . $this->forumdiscussioncount);
 161          }
 162  
 163          if (!isset($record['messageformat'])) {
 164              $record['messageformat'] = editors_get_preferred_format();
 165          }
 166  
 167          if (!isset($record['messagetrust'])) {
 168              $record['messagetrust'] = "";
 169          }
 170  
 171          if (!isset($record['assessed'])) {
 172              $record['assessed'] = '1';
 173          }
 174  
 175          if (!isset($record['groupid'])) {
 176              $record['groupid'] = "-1";
 177          }
 178  
 179          if (!isset($record['timestart'])) {
 180              $record['timestart'] = "0";
 181          }
 182  
 183          if (!isset($record['timeend'])) {
 184              $record['timeend'] = "0";
 185          }
 186  
 187          if (!isset($record['mailnow'])) {
 188              $record['mailnow'] = "0";
 189          }
 190  
 191          if (isset($record['timemodified'])) {
 192              $timemodified = $record['timemodified'];
 193          }
 194  
 195          if (!isset($record['pinned'])) {
 196              $record['pinned'] = FORUM_DISCUSSION_UNPINNED;
 197          }
 198  
 199          if (!isset($record['timelocked'])) {
 200              $record['timelocked'] = 0;
 201          }
 202  
 203          if (isset($record['mailed'])) {
 204              $mailed = $record['mailed'];
 205          }
 206  
 207          $record = (object) $record;
 208  
 209          // Add the discussion.
 210          $record->id = forum_add_discussion($record, null, null, $record->userid);
 211  
 212          $post = $DB->get_record('forum_posts', array('discussion' => $record->id));
 213  
 214          if (isset($timemodified) || isset($mailed)) {
 215              if (isset($mailed)) {
 216                  $post->mailed = $mailed;
 217              }
 218  
 219              if (isset($timemodified)) {
 220                  // Enforce the time modified.
 221                  $record->timemodified = $timemodified;
 222                  $post->modified = $post->created = $timemodified;
 223  
 224                  $DB->update_record('forum_discussions', $record);
 225              }
 226  
 227              $DB->update_record('forum_posts', $post);
 228          }
 229  
 230          if (property_exists($record, 'tags')) {
 231              $cm = get_coursemodule_from_instance('forum', $record->forum);
 232              $tags = is_array($record->tags) ? $record->tags : preg_split('/,/', $record->tags);
 233  
 234              core_tag_tag::set_item_tags('mod_forum', 'forum_posts', $post->id,
 235                  context_module::instance($cm->id), $tags);
 236          }
 237  
 238          return $record;
 239      }
 240  
 241      /**
 242       * Function to create a dummy post.
 243       *
 244       * @param array|stdClass $record
 245       * @return stdClass the post object
 246       */
 247      public function create_post($record = null) {
 248          global $DB;
 249  
 250          // Increment the forum post count.
 251          $this->forumpostcount++;
 252  
 253          // Variable to store time.
 254          $time = time() + $this->forumpostcount;
 255  
 256          $record = (array) $record;
 257  
 258          if (!isset($record['discussion'])) {
 259              throw new coding_exception('discussion must be present in phpunit_util::create_post() $record');
 260          }
 261  
 262          if (!isset($record['userid'])) {
 263              throw new coding_exception('userid must be present in phpunit_util::create_post() $record');
 264          }
 265  
 266          if (!isset($record['parent'])) {
 267              $record['parent'] = 0;
 268          }
 269  
 270          if (!isset($record['subject'])) {
 271              $record['subject'] = 'Forum post subject ' . $this->forumpostcount;
 272          }
 273  
 274          if (!isset($record['message'])) {
 275              $record['message'] = html_writer::tag('p', 'Forum message post ' . $this->forumpostcount);
 276          }
 277  
 278          if (!isset($record['created'])) {
 279              $record['created'] = $time;
 280          }
 281  
 282          if (!isset($record['modified'])) {
 283              $record['modified'] = $time;
 284          }
 285  
 286          if (!isset($record['mailed'])) {
 287              $record['mailed'] = 0;
 288          }
 289  
 290          if (!isset($record['messageformat'])) {
 291              $record['messageformat'] = 0;
 292          }
 293  
 294          if (!isset($record['messagetrust'])) {
 295              $record['messagetrust'] = 0;
 296          }
 297  
 298          if (!isset($record['attachment'])) {
 299              $record['attachment'] = "";
 300          }
 301  
 302          if (!isset($record['totalscore'])) {
 303              $record['totalscore'] = 0;
 304          }
 305  
 306          if (!isset($record['mailnow'])) {
 307              $record['mailnow'] = 0;
 308          }
 309  
 310          if (!isset($record['deleted'])) {
 311              $record['deleted'] = 0;
 312          }
 313  
 314          if (!isset($record['privatereplyto'])) {
 315              $record['privatereplyto'] = 0;
 316          }
 317  
 318          $record = (object) $record;
 319          \mod_forum\local\entities\post::add_message_counts($record);
 320  
 321          // Add the post.
 322          $record->id = $DB->insert_record('forum_posts', $record);
 323  
 324          if (property_exists($record, 'tags')) {
 325              $discussion = $DB->get_record('forum_discussions', ['id' => $record->discussion]);
 326              $cm = get_coursemodule_from_instance('forum', $discussion->forum);
 327              $tags = is_array($record->tags) ? $record->tags : preg_split('/,/', $record->tags);
 328  
 329              core_tag_tag::set_item_tags('mod_forum', 'forum_posts', $record->id,
 330                  context_module::instance($cm->id), $tags);
 331          }
 332  
 333          // Update the last post.
 334          forum_discussion_update_last_post($record->discussion);
 335  
 336          return $record;
 337      }
 338  
 339      public function create_content($instance, $record = array()) {
 340          global $USER, $DB;
 341          $record = (array)$record + array(
 342              'forum' => $instance->id,
 343              'userid' => $USER->id,
 344              'course' => $instance->course
 345          );
 346          if (empty($record['discussion']) && empty($record['parent'])) {
 347              // Create discussion.
 348              $discussion = $this->create_discussion($record);
 349              $post = $DB->get_record('forum_posts', array('id' => $discussion->firstpost));
 350          } else {
 351              // Create post.
 352              if (empty($record['parent'])) {
 353                  $record['parent'] = $DB->get_field('forum_discussions', 'firstpost', array('id' => $record['discussion']), MUST_EXIST);
 354              } else if (empty($record['discussion'])) {
 355                  $record['discussion'] = $DB->get_field('forum_posts', 'discussion', array('id' => $record['parent']), MUST_EXIST);
 356              }
 357              $post = $this->create_post($record);
 358          }
 359          return $post;
 360      }
 361  
 362      /**
 363       * Extracted from exporter/post.php
 364       *
 365       * Get the HTML to display as a subheading in a post.
 366       *
 367       * @param stdClass $exportedauthor The exported author object
 368       * @param int $timecreated The post time created timestamp if it's to be displayed
 369       * @return string
 370       */
 371      public function get_author_subheading_html(stdClass $exportedauthor, int $timecreated) : string {
 372          $fullname = $exportedauthor->fullname;
 373          $profileurl = $exportedauthor->urls['profile'] ?? null;
 374          $name = $profileurl ? "<a href=\"{$profileurl}\">{$fullname}</a>" : $fullname;
 375          $date = userdate_htmltime($timecreated, get_string('strftimedaydatetime', 'core_langconfig'));
 376          return get_string('bynameondate', 'mod_forum', ['name' => $name, 'date' => $date]);
 377      }
 378  }