$cloneid = optional_param('clone', 0, PARAM_INT);
if ($cloneid) {
    $pageparams['clone'] = $cloneid;
}
// Construct discussion variable (will check id is valid)
// Retrieve new copy of discussion from database, but store it in cache
// for further use.
$discussion = mod_forumng_discussion::get_from_id($discussionid, $cloneid, 0, false, true);
$forum = $discussion->get_forum();
$course = $forum->get_course();
$cm = $forum->get_course_module();
$context = $forum->get_context();
$draftid = optional_param('draft', 0, PARAM_INT);
if ($draftid) {
    $pageparams['draft'] = $draftid;
    $draft = mod_forumng_draft::get_from_id($draftid);
    if (!$draft->is_reply() || $draft->get_discussion_id() != $discussionid) {
        print_error('draft_mismatch', 'forumng', $forum->get_url(mod_forumng::PARAM_HTML));
    }
    $root = $discussion->get_root_post();
    $inreplyto = $root->find_child($draft->get_parent_post_id(), false);
    if (!$inreplyto || !$inreplyto->can_reply($whynot) || !$discussion->can_view()) {
        if (!$whynot) {
            $whynot = 'reply_missing';
        }
        print_error('draft_cannotreply', 'forumng', $forum->get_url(mod_forumng::PARAM_HTML), get_string($whynot, 'forumng'));
    }
    $inreplyto->force_expand();
}
// Check that discussion can be viewed [Handles all other permissions]
$discussion->require_view();
Example #2
0
function mod_forumng_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload)
{
    global $CFG, $USER;
    require_once $CFG->dirroot . '/mod/forumng/mod_forumng.php';
    // Check remaining slash arguments
    if (count($args) != 2) {
        send_file_not_found();
    }
    list($itemid, $filename) = $args;
    if ($filearea == 'attachment' || $filearea == 'message') {
        // Get post object and check permissions
        $cloneid = optional_param('clone', 0, PARAM_INT);
        $post = mod_forumng_post::get_from_id($itemid, $cloneid);
        $post->require_view();
        if ($cloneid) {
            // File is actually in other context
            $context = $post->get_forum()->get_context(true);
        }
    } else {
        if ($filearea == 'draft' || $filearea == 'draftmessage') {
            // Get draft object and check it's yours (note: I'm not sure whether it is possible to
            // ever access draft attachments in this manner, as while editing, this access is not
            // used; maybe from the X view, but I don't think it works there, however perhaps in
            // future).
            $draft = mod_forumng_draft::get_from_id($itemid);
            if ($draft->get_user_id() !== $USER->id) {
                send_file_not_found();
            }
        } else {
            send_file_not_found();
        }
    }
    // Get file object and send it
    $fs = get_file_storage();
    $file = $fs->get_file($context->id, 'mod_forumng', $filearea, $itemid, '/', $filename);
    if (!$file || $file->is_directory()) {
        send_file_not_found();
    }
    $lifetime = isset($CFG->filelifetime) ? $CFG->filelifetime : 86400;
    send_stored_file($file, $lifetime, 0);
}
         // Update the draft itself
         $draft->update($fromform->subject, $fromform->message['text'], $fromform->message['format'], $hasattachments, $isdiscussion && $fromform->group ? $fromform->group : null, $options);
         // Redirect to edit it again
         $transaction->allow_commit();
         finish(0, $cloneid, 'editpost.php?draft=' . $draft->get_id() . $forum->get_clone_param(mod_forumng::PARAM_PLAIN) . $expandparam, $fromform, $draft->get_id() . ':' . $date, true);
     } else {
         // This is a new draft
         $transaction = $DB->start_delegated_transaction();
         // Save the draft
         $newdraftid = mod_forumng_draft::save_new($forum, $isdiscussion ? $groupid : null, $replytoid ? $replytoid : null, $fromform->subject, $fromform->message['text'], $fromform->message['format'], $hasattachments, $options);
         // Save any attachments
         file_save_draft_area_files($fromform->attachments, $filecontext->id, 'mod_forumng', 'draft', $newdraftid, $fileoptions);
         if (!empty($fromform->message['itemid'])) {
             $newtext = file_save_draft_area_files($fromform->message['itemid'], $filecontext->id, 'mod_forumng', 'draftmessage', $newdraftid, $fileoptions, $fromform->message['text']);
             if ($newtext !== $fromform->message['text']) {
                 mod_forumng_draft::update_message_for_files($newdraftid, $newtext);
             }
         }
         // Redirect to edit it again
         $transaction->allow_commit();
         finish(0, $cloneid, 'editpost.php?draft=' . $newdraftid . $forum->get_clone_param(mod_forumng::PARAM_PLAIN) . $expandparam, $fromform, $newdraftid . ':' . $date, true);
     }
 } else {
     if (!$edit) {
         // Check the random number is unique in session
         $random = optional_param('random', 0, PARAM_INT);
         if ($random) {
             if (!isset($SESSION->forumng_createdrandoms)) {
                 $SESSION->forumng_createdrandoms = array();
             }
             $now = time();
 /**
  * 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 mod_forumng_draft objects
  */
 public function get_drafts($userid = 0)
 {
     $userid = mod_forumng_utils::get_real_userid($userid);
     return mod_forumng_draft::query_drafts("fdr.forumngid = ? AND fdr.userid = ?", array($this->get_id(), $userid));
 }