/** * Used by forum_post when creating a reply. Do not call directly. * @param forum_post $parentpost Parent post object (NULL when creating root post) * @param string $subject Subject * @param string $message Message * @param int $format Moodle format used for message * @param array $attachments Array of paths to temporary files of * attachments in post. [Note that these should have already been checked * and renamed by the Moodle upload manager. They will be moved or * deleted by the time this method returns.] * @param bool $setimportant If true, highlight the post * @param bool $mailnow If true, sends mail ASAP * @param int $userid User ID (0 = current) * * @return int ID of newly-created post */ function create_reply($parentpost, $subject, $message, $format, $attachments = array(), $setimportant = false, $mailnow = false, $userid = 0) { if ($userid == 0) { global $USER; $userid = $USER->id; if (!$userid) { throw new forum_exception('Cannot determine user ID'); } } // Prepare post object $postobj = new StdClass(); $postobj->discussionid = $this->discussionfields->id; $postobj->parentpostid = $parentpost ? $parentpost->get_id() : null; $postobj->userid = $userid; $postobj->created = time(); $postobj->modified = $postobj->created; $postobj->deleted = 0; $postobj->mailstate = $mailnow ? forum::MAILSTATE_NOW_NOT_MAILED : forum::MAILSTATE_NOT_MAILED; $postobj->important = $setimportant ? 1 : 0; $postobj->oldversion = 0; $postobj->edituserid = null; $postobj->subject = strlen(trim($subject)) == 0 ? null : addslashes($subject); $postobj->message = addslashes($message); $postobj->format = $format; $postobj->attachments = count($attachments) > 0; forum_utils::start_transaction(); try { // Create post $postobj->id = forum_utils::insert_record('forumng_posts', $postobj); $post = new forum_post($this, $postobj); // For replies, update last post id if ($parentpost) { $discussionchange = new stdClass(); $discussionchange->id = $parentpost->get_discussion()->get_id(); $discussionchange->lastpostid = $postobj->id; forum_utils::update_record('forumng_discussions', $discussionchange); } // Place attachments foreach ($attachments as $path) { $post->add_attachment($path); } // Update search index (replies only) if (forum::search_installed() && $parentpost) { $post->search_update(); } // Update completion state $post->update_completion(true); // Outside the catch so we don't commit transaction if something // fails forum_utils::finish_transaction(); return $post->get_id(); } catch (Exception $e) { // Erase attachments from temp storage if error occurs foreach ($attachments as $path) { unlink($path); } throw $e; } }