/**
  * 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();
 }
 private function add_attachment($path, $courseid = 0, $forumid = 0, $draftid = 0)
 {
     // Check source file exists
     if (!file_exists($path)) {
         throw new forum_exception("Missing file {$path}");
     }
     // Get folder
     if (isset($this)) {
         $folder = $this->get_attachment_folder();
     } else {
         $folder = self::get_attachment_folder($courseid, $forumid, $draftid);
     }
     if (!check_dir_exists($folder, true, true)) {
         throw new forum_exception("Failed to create attachment folder {$folder}");
     }
     // Check target path doesn't already exist. If it does, delete existing
     // file.
     $target = $folder . '/' . basename($path);
     if (file_exists($target)) {
         forum_utils::unlink($target);
     }
     // Move new file into place
     forum_utils::rename($path, $target);
 }
 /**
  * Splits this post to become a new discussion
  * @param $newsubject
  * @param bool $log True to log action
  * @return int ID of new discussion
  */
 function split($newsubject, $log = true)
 {
     global $CFG;
     $this->require_children();
     // Begin a transaction
     forum_utils::start_transaction();
     $olddiscussion = $this->get_discussion();
     // Create new discussion
     $newest = null;
     $this->find_newest_child($newest);
     $newdiscussionid = $olddiscussion->clone_for_split($this->get_id(), $newest->get_id());
     // Update all child posts
     $list = array();
     $this->list_child_ids($list);
     unset($list[0]);
     // Don't include this post itself
     if (count($list) > 0) {
         $inorequals = forum_utils::in_or_equals($list);
         forum_utils::execute_sql("\nUPDATE\n    {$CFG->prefix}forumng_posts\nSET\n    discussionid = {$newdiscussionid}\nWHERE\n    id {$inorequals}");
     }
     // Update this post
     $changes = new stdClass();
     $changes->id = $this->get_id();
     $changes->subject = addslashes($newsubject);
     $changes->parentpostid = null;
     //When split the post, reset the important to 0 so that it is not highlighted.
     $changes->important = 0;
     // Note don't update modified time, or it makes this post unread,
     // which isn't very helpful
     $changes->discussionid = $newdiscussionid;
     forum_utils::update_record('forumng_posts', $changes);
     // Update read data if relevant
     if (forum::enabled_read_tracking() && $newest->get_modified() >= forum::get_read_tracking_deadline()) {
         $rs = forum_utils::get_recordset_sql("\nSELECT\n    userid, time\nFROM\n    {$CFG->prefix}forumng_read\nWHERE\n    discussionid = " . $olddiscussion->get_id() . "\n    AND time >= " . $this->get_created());
         while ($rec = rs_fetch_next_record($rs)) {
             $rec->discussionid = $newdiscussionid;
             forum_utils::insert_record('forumng_read', $rec);
         }
         rs_close($rs);
     }
     $olddiscussion->possible_lastpost_change();
     // Move attachments
     $olddiscussionfolder = $olddiscussion->get_attachment_folder();
     $newdiscussionfolder = $olddiscussion->get_attachment_folder(0, 0, $newdiscussionid);
     if (is_dir($olddiscussionfolder)) {
         // Put this post back on the list
         $list[0] = $this->get_id();
         // Loop through all posts; move attachments if present
         $madenewfolder = false;
         foreach ($list as $id) {
             $oldfolder = $olddiscussionfolder . '/' . $id;
             $newfolder = $newdiscussionfolder . '/' . $id;
             if (is_dir($oldfolder)) {
                 if (!$madenewfolder) {
                     check_dir_exists($newfolder, true, true);
                     $madenewfolder = true;
                 }
                 forum_utils::rename($oldfolder, $newfolder);
             }
         }
     }
     if ($log) {
         $this->log('split post');
     }
     forum_utils::finish_transaction();
     $this->get_discussion()->uncache();
     // If discussion-based completion is turned on, this may enable someone
     // to complete
     if ($this->get_forum()->get_completion_discussions()) {
         $this->update_completion(true);
     }
     return $newdiscussionid;
 }