function make_post($discussion, &$allposts, &$userids, $ratingpercent)
{
    // Make reply
    static $index = 0;
    $index++;
    $replyto = $allposts[rand(0, count($allposts) - 1)];
    $newpostid = $replyto->reply(my_random_percentage(25) ? 'Reply ' . $index : null, get_post_text(), FORMAT_HTML, array(), false, $userids[mt_rand(0, count($userids) - 1)], false);
    $newpost = forum_post::get_from_id($newpostid, forum::CLONE_DIRECT);
    $allposts[] = $newpost;
    // Add ratings
    for ($i = 0; $i < count($userids); $i++) {
        if (my_random_percentage($ratingpercent)) {
            $newpost->rate(2, $userids[$i]);
        }
    }
}
<?php

require_once '../../config.php';
require_once 'forum.php';
if (class_exists('ouflags')) {
    require_once '../../local/mobile/ou_lib.php';
    global $OUMOBILESUPPORT;
    $OUMOBILESUPPORT = true;
    ou_set_is_mobile(ou_get_is_mobile_from_cookies());
}
// Post ID
$postid = required_param('p', PARAM_INT);
$cloneid = optional_param('clone', 0, PARAM_INT);
try {
    // Get post
    $post = forum_post::get_from_id($postid, $cloneid, true);
    // Get convenience variables
    $discussion = $post->get_discussion();
    $forum = $post->get_forum();
    $course = $forum->get_course();
    $cm = $forum->get_course_module();
    // Do all access security checks
    $post->require_view();
    if (!$post->can_view_history($whynot)) {
        print_error($whynot, 'forumng');
    }
    // Work out navigation for header
    $pagename = get_string('historypage', 'forumng', $post->get_effective_subject(true));
    $navigation = array();
    $navigation[] = array('name' => shorten_text(htmlspecialchars($discussion->get_subject())), 'link' => $discussion->get_url(), 'type' => 'forumng');
    $navigation[] = array('name' => $pagename, 'type' => 'forumng');
 /**
  * Prints AJAX version of the post to output, and exits.
  * @param mixed $postorid Post object or ID of post
  * @param int $cloneid If $postorid is an id, a clone id may be necessary
  *   to construct the post
  * @param array $options Post options if any
  * @param int $postid ID of post
  */
 public static function print_for_ajax_and_exit($postorid, $cloneid = null, $options = array())
 {
     if (is_object($postorid)) {
         $post = $postorid;
     } else {
         $post = forum_post::get_from_id($postorid, $cloneid, true);
     }
     header('Content-Type: text/plain');
     print trim($post->display(true, $options));
     exit;
 }
 /**
  * Creates a new discussion in this forum.
  * @param int $groupid Group ID for the discussion or null if it should show
  *   to all groups
  * @param string $subject Subject of message
  * @param string $message Message content
  * @param int $format Format of message content
  * @param array $attachments Array of attachment files. These should have
  *   already been checked and renamed etc by a Moodle upload manager.
  * @param bool $mailnow True to mail ASAP, else false
  * @param int $timestart Visibility time of discussion (seconds since epoch) or null
  * @param int $timeend Time at which discussion disappears (seconds since epoch) or null
  * @param bool $locked True if discussion should be locked
  * @param bool $sticky True if discussion should be sticky
  * @param int $userid User ID or 0 for current user
  * @param bool $log True to log this
  * @return array Array with 2 elements ($discussionid, $postid)
  */
 public function create_discussion($groupid, $subject, $message, $format, $attachments = array(), $mailnow = false, $timestart = 0, $timeend = 0, $locked = false, $sticky = false, $userid = 0, $log = true)
 {
     $userid = forum_utils::get_real_userid($userid);
     // Prepare discussion object
     $discussionobj = new StdClass();
     $discussionobj->forumid = $this->forumfields->id;
     $discussionobj->groupid = $groupid == self::ALL_GROUPS || $groupid == self::NO_GROUPS ? null : $groupid;
     $discussionobj->postid = null;
     // Temporary until we create that first post
     $discussionobj->lastpostid = null;
     $discussionobj->timestart = $timestart;
     $discussionobj->timeend = $timeend;
     $discussionobj->deleted = 0;
     $discussionobj->locked = $locked ? 1 : 0;
     $discussionobj->sticky = $sticky ? 1 : 0;
     // Create discussion
     forum_utils::start_transaction();
     $discussionobj->id = forum_utils::insert_record('forumng_discussions', $discussionobj);
     $newdiscussion = new forum_discussion($this, $discussionobj, false, -1);
     // Create initial post
     $postid = $newdiscussion->create_root_post($subject, $message, $format, $attachments, $mailnow, $userid);
     // Update discussion so that it contains the post id
     $changes = new StdClass();
     $changes->id = $discussionobj->id;
     $changes->postid = $postid;
     $changes->lastpostid = $postid;
     forum_utils::update_record('forumng_discussions', $changes);
     $newdiscussion->log('add discussion');
     if (forum::search_installed()) {
         forum_post::get_from_id($postid, $this->get_course_module_id())->search_update();
     }
     forum_utils::finish_transaction();
     return array($newdiscussion->get_id(), $postid);
 }
     $post = reset($posts);
     $pagename = $post->u_firstname . ' ' . $post->u_lastname;
     $pagename .= $CFG->forumng_showusername ? ' (' . $post->u_username . ')' : '';
 } else {
     if (!($user = get_record('user', 'id', $userid))) {
         throw new forum_exception("Cannot find user (id={$userid}) in the user table");
     }
     $pagename = $user->firstname . ' ' . $user->lastname . ' (' . $user->username . ')';
 }
 // Print page header
 $prevpage = get_string('userposts', 'forumng');
 $navigation = array();
 $navigation[] = array('name' => $prevpage, 'link' => $CFG->wwwroot . '/mod/forumng/feature/userposts/list.php?id=' . $cmid);
 $forum->print_subpage_header($pagename, $navigation);
 foreach ($posts as $postid => $post) {
     $fp = forum_post::get_from_id($postid, $cloneid, false, false, $userid);
     print "<div class='forumng-userpostheading'>";
     // If this post is a reply, then print a link to the discussion
     if (isset($post->parentpostid)) {
         $url = $CFG->wwwroot . '/mod/forumng/discuss.php?d=' . $post->discussionid;
         $title = $post->fd_subject;
         print get_string('re', 'forumng', "<a href='{$url}'>{$title}</a>");
     } else {
         print get_string('newdiscussion', 'forumng');
     }
     print "</div>";
     // Display this post
     $options = array(forum_post::OPTION_NO_COMMANDS => true, forum_post::OPTION_FIRST_UNREAD => false, forum_post::OPTION_UNREAD_NOT_HIGHLIGHTED => true);
     print $fp->display(true, $options);
 }
 // Display link to the discussion