/** * Merges the contents of this discussion into another discussion. * @param mod_forumng_discussion $targetdiscussion Target discussion * @param int $userid User ID (0 = current) * @param bool $log True to log this action */ public function merge_into($targetdiscussion, $userid = 0, $log = true) { global $DB; $transaction = $DB->start_delegated_transaction(); // Delete search data for the source discussion $this->ismakingsearchchange = true; $root = $this->get_root_post(); $root->search_update(); $root->search_update_children(); $this->ismakingsearchchange = false; // Update parent post id of root post $record = new stdClass(); $record->id = $this->discussionfields->postid; $record->parentpostid = $targetdiscussion->discussionfields->postid; $DB->update_record('forumng_posts', $record); // Move all posts into new discussion $DB->execute("UPDATE {forumng_posts} SET discussionid = ? WHERE discussionid = ?", array($targetdiscussion->get_id(), $this->get_id())); // Delete this discussion $DB->delete_records('forumng_discussions', array('id' => $this->discussionfields->id)); // Attachments are OK because they are still in the same context, postid // etc (note this would NOT be the case if we allowed merging between // forums). if ($this->get_forum()->get_id() != $targetdiscussion->get_forum()->get_id()) { throw new coding_exception('Cannot merge into different forum'); } // Merging the discussion into another might cause completion changes // (if there was a requirement for discussions and this is no longer // a discussion in its own right). $this->update_completion(false); // Update the search data for the target discussion after the merge $newroot = $targetdiscussion->get_root_post(); $newroot->search_update(); $newroot->search_update_children(); if ($log) { $this->log('merge discussion', 'd' . $this->get_id() . ' into d' . $targetdiscussion->get_id()); } $transaction->allow_commit(); $this->uncache(); $targetdiscussion->uncache(); }