/**
  * Obtains list of posts to include in an Atom/RSS feed.
  * @param int $groupid Group ID (may be ALL_GROUPS)
  * @param int $userid User ID
  * @param forum_discussion $discussion Discussion object (intended only
  *   for calls via the forum_discussion method)
  * @return array Array of forum_post objects
  */
 public function get_feed_posts($groupid, $userid, $discussion = null)
 {
     // Don't let user view any posts in a discussion feed they can't see
     // (I don't think they should be given a key in this case, but just
     // to be sure).
     if ($discussion && !$this->get_type()->can_view_discussion($discussion, $userid)) {
         return array();
     }
     // Number of items to output
     $items = $this->get_effective_feed_items();
     // Get most recent N posts from db
     if ($discussion) {
         $where = 'fd.id=' . $discussion->get_id();
     } else {
         $where = 'fd.forumid=' . $this->get_id();
         if ($this->get_group_mode() && $groupid != self::ALL_GROUPS) {
             $where .= ' AND fd.groupid=' . $groupid;
         }
     }
     // Don't include deleted or old-version posts
     $where .= ' AND fp.oldversion=0 AND fp.deleted=0 AND fd.deleted=0';
     // Or ones out of time
     $now = time();
     $where .= " AND (fd.timestart < {$now})" . " AND (fd.timeend = 0 OR fd.timeend > {$now})";
     $postrecs = forum_post::query_posts($where, 'GREATEST(fp.created, fd.timestart) DESC', false, false, false, $userid, true, false, 0, $items);
     if (count($postrecs) == 0) {
         // No posts!
         return array();
     }
     $result = array();
     if ($discussion) {
         foreach ($postrecs as $rec) {
             $post = new forum_post($discussion, $rec, null);
             $result[$rec->id] = $post;
         }
     } else {
         // Based on these posts, get all mentioned discussions
         $discussionids = array();
         $discussionposts = array();
         foreach ($postrecs as $rec) {
             $discussionids[] = $rec->discussionid;
             $discussionposts[$rec->discussionid][] = $rec->id;
         }
         $discussionpart = forum_utils::in_or_equals($discussionids);
         $rs = forum_discussion::query_discussions("fd.id " . $discussionpart, -1, 'id');
         // Build the discussion and post objects
         $posts = array();
         while ($rec = rs_fetch_next_record($rs)) {
             $discussion = new forum_discussion($this, $rec, true, -1);
             if ($discussion->can_view($userid)) {
                 foreach ($discussionposts[$discussion->get_id()] as $postid) {
                     $post = new forum_post($discussion, $postrecs[$postid], null);
                     $posts[$postid] = $post;
                 }
             }
         }
         rs_close($rs);
         // Put them back in order of the post records, and return
         foreach ($postrecs as $rec) {
             // Records might be excluded if user can't view discussion
             if (array_key_exists($rec->id, $posts)) {
                 $result[$rec->id] = $posts[$rec->id];
             }
         }
     }
     return $result;
 }