$newdiscussion->name = $name; $newdiscussion->firstpost = $post->id; $newdiscussion->userid = $discussion->userid; $newdiscussion->groupid = $discussion->groupid; $newdiscussion->assessed = $discussion->assessed; $newdiscussion->usermodified = $post->userid; $newdiscussion->timestart = $discussion->timestart; $newdiscussion->timeend = $discussion->timeend; $newid = $DB->insert_record('hsuforum_discussions', $newdiscussion); $newpost = new stdClass(); $newpost->id = $post->id; $newpost->parent = 0; $newpost->subject = $name; $newpost->privatereply = 0; $DB->update_record("hsuforum_posts", $newpost); hsuforum_change_discussionid($post->id, $newid); // update last post in each discussion hsuforum_discussion_update_last_post($discussion->id); hsuforum_discussion_update_last_post($newid); // Fire events to reflect the split.. $params = array('context' => $modcontext, 'objectid' => $discussion->id, 'other' => array('forumid' => $forum->id)); $event = \mod_hsuforum\event\discussion_updated::create($params); $event->trigger(); $params = array('context' => $modcontext, 'objectid' => $newid, 'other' => array('forumid' => $forum->id)); $event = \mod_hsuforum\event\discussion_created::create($params); $event->trigger(); $params = array('context' => $modcontext, 'objectid' => $post->id, 'other' => array('discussionid' => $newid, 'forumid' => $forum->id, 'forumtype' => $forum->type)); $event = \mod_hsuforum\event\post_updated::create($params); $event->add_record_snapshot('hsuforum_discussions', $discussion); $event->trigger(); redirect(hsuforum_go_back_to("discuss.php?d={$newid}"));
/** * recursively sets the discussion field to $discussionid on $postid and all its children * used when pruning a post * * @global object * @param int $postid * @param int $discussionid * @return bool */ function hsuforum_change_discussionid($postid, $discussionid) { global $DB; $DB->set_field('hsuforum_posts', 'discussion', $discussionid, array('id' => $postid)); if ($posts = $DB->get_records('hsuforum_posts', array('parent' => $postid))) { foreach ($posts as $post) { hsuforum_change_discussionid($post->id, $discussionid); } } return true; }