/** * 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; }
unset($data); } $formheading = ''; if (!empty($parent)) { $heading = get_string("yourreply", "quora"); $formheading = get_string('reply', 'quora'); } else { if ($quora->type == 'qanda') { $heading = get_string('yournewquestion', 'quora'); } else { $heading = get_string('yournewtopic', 'quora'); } } $postid = empty($post->id) ? null : $post->id; $draftid_editor = file_get_submitted_draft_itemid('message'); $currenttext = file_prepare_draft_area($draftid_editor, $modcontext->id, 'mod_quora', 'post', $postid, mod_quora_post_form::editor_options($modcontext, $postid), $post->message); $manageactivities = has_capability('moodle/course:manageactivities', $coursecontext); if (\mod_quora\subscriptions::subscription_disabled($quora) && !$manageactivities) { // User does not have permission to subscribe to this discussion at all. $discussionsubscribe = false; } else { if (\mod_quora\subscriptions::is_forcesubscribed($quora)) { // User does not have permission to unsubscribe from this discussion at all. $discussionsubscribe = true; } else { if (isset($discussion) && \mod_quora\subscriptions::is_subscribed($USER->id, $quora, $discussion->id, $cm)) { // User is subscribed to the discussion - continue the subscription. $discussionsubscribe = true; } else { if (!isset($discussion) && \mod_quora\subscriptions::is_subscribed($USER->id, $quora, null, $cm)) { // Starting a new discussion, and the user is subscribed to the quora - subscribe to the discussion.