示例#1
0
/**
 * Given an object containing all the necessary data,
 * create a new discussion and return the id
 *
 * @param object $post
 * @param mixed $mform
 * @param string $unused
 * @param int $userid
 * @return object
 */
function quora_add_discussion($discussion, $mform = null, $unused = null, $userid = null)
{
    global $USER, $CFG, $DB;
    $timenow = time();
    if (is_null($userid)) {
        $userid = $USER->id;
    }
    // The first post is stored as a real post, and linked
    // to from the discuss entry.
    $quora = $DB->get_record('quora', array('id' => $discussion->quora));
    $cm = get_coursemodule_from_instance('quora', $quora->id);
    $post = new stdClass();
    $post->discussion = 0;
    $post->parent = 0;
    $post->userid = $userid;
    $post->created = $timenow;
    $post->modified = $timenow;
    $post->mailed = FORUM_MAILED_PENDING;
    $post->subject = $discussion->name;
    $post->message = $discussion->message;
    $post->messageformat = $discussion->messageformat;
    $post->messagetrust = $discussion->messagetrust;
    $post->attachments = isset($discussion->attachments) ? $discussion->attachments : null;
    $post->quora = $quora->id;
    // speedup
    $post->course = $quora->course;
    // speedup
    $post->mailnow = $discussion->mailnow;
    $post->id = $DB->insert_record("quora_posts", $post);
    // TODO: Fix the calling code so that there always is a $cm when this function is called
    if (!empty($cm->id) && !empty($discussion->itemid)) {
        // In "single simple discussions" this may not exist yet
        $context = context_module::instance($cm->id);
        $text = file_save_draft_area_files($discussion->itemid, $context->id, 'mod_quora', 'post', $post->id, mod_quora_post_form::editor_options($context, null), $post->message);
        $DB->set_field('quora_posts', 'message', $text, array('id' => $post->id));
    }
    // Now do the main entry for the discussion, linking to this first post
    $discussion->firstpost = $post->id;
    $discussion->timemodified = $timenow;
    $discussion->usermodified = $post->userid;
    $discussion->userid = $userid;
    $discussion->assessed = 0;
    $post->discussion = $DB->insert_record("quora_discussions", $discussion);
    // Finally, set the pointer on the post.
    $DB->set_field("quora_posts", "discussion", $post->discussion, array("id" => $post->id));
    if (!empty($cm->id)) {
        quora_add_attachment($post, $quora, $cm, $mform, $unused);
    }
    if (quora_tp_can_track_quoras($quora) && quora_tp_is_tracked($quora)) {
        quora_tp_mark_post_read($post->userid, $post, $post->quora);
    }
    // Let Moodle know that assessable content is uploaded (eg for plagiarism detection)
    if (!empty($cm->id)) {
        quora_trigger_content_uploaded_event($post, $cm, 'quora_add_discussion');
    }
    return $post->discussion;
}
示例#2
0
if (!($cm = get_coursemodule_from_instance('quora', $quora->id, $course->id))) {
    // For the logs
    print_error('invalidcoursemodule');
}
$modcontext = context_module::instance($cm->id);
require_login($course, false, $cm);
if (isguestuser()) {
    // just in case
    print_error('noguest');
}
if (!isset($quora->maxattachments)) {
    // TODO - delete this once we add a field to the quora table
    $quora->maxattachments = 3;
}
$thresholdwarning = quora_check_throttling($quora, $cm);
$mform_post = new mod_quora_post_form('post.php', array('course' => $course, 'cm' => $cm, 'coursecontext' => $coursecontext, 'modcontext' => $modcontext, 'quora' => $quora, 'post' => $post, 'subscribe' => \mod_quora\subscriptions::is_subscribed($USER->id, $quora, null, $cm), 'thresholdwarning' => $thresholdwarning, 'edit' => $edit), 'post', '', array('id' => 'mformquora'));
$draftitemid = file_get_submitted_draft_itemid('attachments');
file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_quora', 'attachment', empty($post->id) ? null : $post->id, mod_quora_post_form::attachment_options($quora));
//load data into form NOW!
if ($USER->id != $post->userid) {
    // Not the original author, so add a message to the end
    $data = new stdClass();
    $data->date = userdate($post->modified);
    if ($post->messageformat == FORMAT_HTML) {
        $data->name = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $USER->id . '&course=' . $post->course . '">' . fullname($USER) . '</a>';
        $post->message .= '<p><span class="edited">(' . get_string('editedby', 'quora', $data) . ')</span></p>';
    } else {
        $data->name = fullname($USER);
        $post->message .= "\n\n(" . get_string('editedby', 'quora', $data) . ')';
    }
    unset($data);