/** * Merges the contents of this discussion into another discussion. * @param forum_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 $CFG; forum_utils::start_transaction(); // Update parent post id of root post $record = new stdClass(); $record->id = $this->discussionfields->postid; $record->parentpostid = $targetdiscussion->discussionfields->postid; forum_utils::update_record('forumng_posts', $record); // Move all posts into new discussion forum_utils::execute_sql("UPDATE {$CFG->prefix}forumng_posts SET " . "discussionid=" . $targetdiscussion->get_id() . " WHERE discussionid=" . $this->get_id()); // Delete this discussion forum_utils::delete_records('forumng_discussions', 'id', $this->discussionfields->id); // Merge attachments (if any) $oldfolder = $this->get_attachment_folder(); $newfolder = $targetdiscussion->get_attachment_folder(); if (is_dir($oldfolder)) { $handle = forum_utils::opendir($oldfolder); $madenewfolder = false; while (false !== ($name = readdir($handle))) { if ($name != '.' && $name != '..') { if (!$madenewfolder) { check_dir_exists($newfolder, true, true); $madenewfolder = true; } $oldname = $oldfolder . '/' . $name; $newname = $newfolder . '/' . $name; forum_utils::rename($oldname, $newname); } } closedir($handle); } // 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); if ($log) { $this->log('merge discussion d' . $targetdiscussion->get_id()); } forum_utils::finish_transaction(); $this->uncache(); $targetdiscussion->uncache(); }