/**
  * Obtains all flagged post in this forum by the given or current user,
  * in reverse data order (of when they were flagged).
  * @param int $userid User whose flags will be retrieved; 0 = current
  * @return array Array of forum_post objects
  */
 public function get_flagged_posts($userid = 0)
 {
     // Get all flagged posts. Note that we request the discussion row as
     // well, this is necessary (a) so we can include its forumid field in
     // the query, and (b) because we will use that data to construct
     // basic discussion objects (without having to do another query).
     $records = forum_post::query_posts('fd.forumid = ' . $this->get_id() . ' AND ff.flagged IS NOT NULL AND fp.deleted = 0', 'ff.flagged DESC', false, true, false, $userid, true, true);
     // Construct post object for each one
     $result = array();
     foreach ($records as $record) {
         // Get discussion details from record
         $discussionfields = forum_utils::extract_subobject($record, 'fd_');
         $discussion = new forum_discussion($this, $discussionfields, false, -1);
         // Create post object
         $post = new forum_post($discussion, $record);
         $result[$record->id] = $post;
     }
     return $result;
 }
 /**
  * Obtains the root post of the discussion. This actually requests all
  * posts from the database; the first is returned, but others are
  * accessible from methods in the first.
  * If available, cached information is used unless
  * you set $usecache to false. The cache is stored within the discussion
  * object so will not persist beyond a request unless you make the
  * discussion object persist too.
  * @param bool $usecache True to use cache if available, false to
  *    request fresh data
  * @param int $userid User ID to get user-specific data (initially, post
  *   flags) for; 0 = current
  * @return forum_post Post object
  */
 function get_root_post($usecache = true, $userid = 0)
 {
     if (!$usecache || !$this->rootpost) {
         global $CFG;
         if (!$usecache || !$this->postscache) {
             // Retrieve most posts in the discussion - even deleted
             // ones. These are necessary in case somebody deletes a post that has
             // replies. They will display as 'deleted post'. We don't retrieve
             // old versions of edited posts. Posts are retrieved in created order
             // so that the order of replies remains constant when we build the tree.
             $posts = forum_post::query_posts('fp.discussionid=' . $this->discussionfields->id . ' AND fp.oldversion=0', 'fp.created', $this->forum->has_ratings(), true, false, $userid);
             $this->postscache = serialize($posts);
         } else {
             $posts = unserialize($this->postscache);
         }
         // Add numbers to posts
         $i = 1;
         foreach ($posts as $post) {
             $post->number = $i++;
         }
         // Obtain post relationships
         $children = array();
         foreach ($posts as $id => $fields) {
             if (!array_key_exists($fields->parentpostid, $children)) {
                 $children[$fields->parentpostid] = array();
             }
             $children[$fields->parentpostid][] = $id;
         }
         // Recursively build posts
         $this->rootpost = $this->build_posts($posts, $children, $this->discussionfields->postid, null);
         // Update the 'next/previous' unread lists stored in posts
         if ($this->get_unread_data_user_id() != -1) {
             $linear = array();
             $this->rootpost->build_linear_children($linear);
             $nextunread = array();
             foreach ($linear as $index => $post) {
                 $nextunread[$index] = null;
                 if ($post->is_unread()) {
                     for ($j = $index - 1; $j >= 0; $j--) {
                         if ($nextunread[$j]) {
                             break;
                         }
                         $nextunread[$j] = $post;
                     }
                 }
             }
             $previous = null;
             foreach ($linear as $index => $post) {
                 $post->set_unread_list($nextunread[$index], $previous);
                 if ($post->is_unread()) {
                     $previous = $post;
                 }
             }
             // Update cached version to include this data
             if ($this->incache) {
                 $this->cache();
             }
         }
     }
     return $this->rootpost;
 }
 if ($course->groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context)) {
     // Must share a group with the user within the selected grouping
     if ($course->defaultgroupingid) {
         $groupingjoin = "INNER JOIN {$CFG->prefix}groupings_groups gg " . "ON gg.groupid = g.id";
         $groupingwhere = "AND gg.groupingid = {$course->defaultgroupingid}";
     }
     $ok = record_exists_sql("\nSELECT\n    g.id\nFROM\n    {$CFG->prefix}groups g\n    INNER JOIN {$CFG->prefix}groups_members gm1 ON gm1.groupid = g.id\n    INNER JOIN {$CFG->prefix}groups_members gm2 ON gm2.groupid = g.id\n    {$groupingjoin}\nWHERE\n    g.courseid = {$course->id}\n    AND gm1.userid = {$USER->id}\n    AND gm2.userid = {$userid}\n    {$groupingwhere}");
     if (!$ok) {
         // We know they don't have this, but call require to get the
         // appropriate error message
         require_capability('moodle/site:accessallgroups', $context);
     }
 }
 $where = " fd.forumid = {$forumid} AND fp.userid = {$userid} AND fp.oldversion = 0 AND fp.deleted = 0";
 $order = 'fp.modified';
 $posts = forum_post::query_posts($where, $order, true, false, false, $userid, true, true);
 // Set pagename
 if ($posts) {
     $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);