public function get_unread_restriction_sql($forum, $userid = 0)
 {
     $userid = forum_utils::get_real_userid($userid);
     // See if they're already allowed to view all discussions
     if ($forum->can_view_hidden($userid)) {
         return '';
     }
     // Otherwise restrict it
     return 'fpfirst.userid=' . $userid;
 }
 /**
  * Saves a new draft message.
  * @param int $forumid ID of forum
  * @param int $groupid Group ID (null if none)
  * @param int $parentpostid ID of post this is in reply to, or 0 for 
  *   a new discussion
  * @param string $subject Subject of draft post
  * @param string $message Message of draft post
  * @param int $format Format (FORMAT_xx) of message
  * @param array $attachments Array of paths to attachments
  * @param string $options Options (null if none)
  * @param int $userid User ID or 0 for current
  * @return int ID of new draft
  */
 static function save_new($forum, $groupid, $parentpostid, $subject, $message, $format, $attachments, $options, $userid = 0)
 {
     $userid = forum_utils::get_real_userid($userid);
     $serializedoptions = $options ? addslashes(serialize($options)) : null;
     $record = (object) array('userid' => $userid, 'forumid' => $forum->get_id(), 'parentpostid' => $parentpostid ? $parentpostid : null, 'subject' => addslashes($subject), 'message' => addslashes($message), 'format' => $format, 'attachments' => count($attachments) > 0 ? 1 : 0, 'saved' => time(), 'groupid' => $groupid, 'options' => $serializedoptions);
     forum_utils::start_transaction();
     $draftid = forum_utils::insert_record('forumng_drafts', $record);
     foreach ($attachments as $path) {
         self::add_attachment($path, $forum->get_course()->id, $forum->get_id(), $draftid);
     }
     forum_utils::finish_transaction();
     return $draftid;
 }
 /**
  * Obtains all draft posts in this forum by the given or current user,
  * in reverse date order.
  * @param int $userid User whose drafts will be retrieved. If zero,
  *   retrieves draft for current user
  * @return array Array of forum_draft objects
  */
 public function get_drafts($userid = 0)
 {
     $userid = forum_utils::get_real_userid($userid);
     return forum_draft::query_drafts("fdr.forumid = " . $this->get_id() . " AND fdr.userid = {$userid}");
 }
 /**
  * Gets URL for an Atom/RSS feed to this discussion.
  * @param int $feedformat FEEDFORMAT_xx constant
  * @param int $userid User ID or 0 for current
  * @return string URL for feed
  */
 public function get_feed_url($feedformat, $userid = 0)
 {
     global $CFG;
     $userid = forum_utils::get_real_userid($userid);
     $groupid = $this->get_group_id();
     return $CFG->wwwroot . '/mod/forumng/feed.php?' . $this->get_link_params(forum::PARAM_PLAIN) . '&user='******'&key=' . $this->get_forum()->get_feed_key($groupid, $userid) . '&format=' . ($feedformat == forum::FEEDFORMAT_RSS ? 'rss' : 'atom');
 }
 /**
  * Checks whether the user can edit the post, assuming that they can
  * view the discussion.
  * @param string &$whynot If returning false, set to the language string defining
  *   reason for not being able to edit
  * @param int $userid User ID or 0 if current
  * @return bool True if user can edit this post
  */
 function can_edit(&$whynot, $userid = 0)
 {
     global $CFG;
     $userid = forum_utils::get_real_userid($userid);
     $context = $this->get_forum()->get_context();
     // Check if post is a special case
     if ($this->get_deleted() || $this->is_old_version() || $this->get_discussion()->is_deleted()) {
         $whynot = 'edit_notcurrentpost';
         return false;
     }
     // Check if discussion is different group
     if (!$this->get_discussion()->can_write_to_group()) {
         $whynot = 'edit_wronggroup';
         return false;
     }
     // Check if discussion is locked
     if ($this->get_discussion()->is_locked()) {
         $whynot = 'edit_locked';
         return false;
     }
     // Check the 'edit any' capability
     $editanypost = has_capability('mod/forumng:editanypost', $context, $userid);
     if (!$editanypost) {
         // If they don't have edit any, they must have either the
         // 'start discussion' or 'reply post' capability (the same
         // one they needed to create the post in the first place)
         if ($this->is_root_post() && !has_capability('mod/forumng:startdiscussion', $context, $userid) && (!$this->is_root_post() && !has_capability('mod/forumng:replypost', $context, $userid))) {
             $whynot = 'edit_nopermission';
             return false;
         }
     }
     // Check post belongs to specified user
     if ($this->get_user()->id != $userid && !$editanypost) {
         $whynot = 'edit_notyours';
         return false;
     }
     // Check editing timeout
     if (time() > $this->get_edit_time_limit() && !$editanypost) {
         $whynot = 'edit_timeout';
         return false;
     }
     // Check read-only dates
     if ($this->get_forum()->is_read_only($userid)) {
         $whynot = 'edit_readonly';
         return false;
     }
     // OK! They're allowed to edit (whew)
     $whynot = '';
     return true;
 }