/** * @param $obj_id * @param $source_id * @param $target_id * @throws ilException */ public static function mergeThreads($obj_id, $source_id, $target_id) { // selected source & target objects $source_thread_obj = new ilForumTopic((int) $source_id); $target_thread_obj = new ilForumTopic((int) $target_id); if ($source_thread_obj->getForumId() != $target_thread_obj->getForumId()) { throw new ilException('not_allowed_to_merge_into_another_forum'); } // use the "older" thread as target if ($source_thread_obj->getCreateDate() > $target_thread_obj->getCreateDate()) { $merge_thread_source = $source_thread_obj; $merge_thread_target = $target_thread_obj; } else { $merge_thread_source = $target_thread_obj; $merge_thread_target = $source_thread_obj; } $thread_subject = $target_thread_obj->getSubject(); // remember if the threads are open or closed and then close both threads ! $targed_was_closed = $merge_thread_target->isClosed(); $merge_thread_source->close(); if ($targed_was_closed == false) { $merge_thread_target->close(); } $source_all_posts = $merge_thread_source->getAllPosts(); $source_root_node = $merge_thread_source->getFirstPostNode(); $target_root_node = $merge_thread_target->getFirstPostNode(); $add_difference = $target_root_node->getRgt(); // update target root node rgt include_once 'Modules/Forum/classes/class.ilForumPostsTree.php'; // $new_target_rgt = ($target_root_node->getRgt() + $source_root_node->getRgt() + 1); $new_target_rgt = $target_root_node->getRgt() + $source_root_node->getRgt(); ilForumPostsTree::updateTargetRootRgt($target_root_node->getId(), $new_target_rgt); $new_target_root = $target_root_node->getId(); // get source post tree and update posts tree foreach ($source_all_posts as $post) { $post_obj = new ilForumPost($post->pos_pk); $posts_tree_obj = new ilForumPostsTree(); $posts_tree_obj->setPosFk($post->pos_pk); if ($post_obj->getParentId() == 0) { $posts_tree_obj->setParentPos($new_target_root); //$posts_tree_obj->setRgt(($post_obj->getRgt() + $add_difference)); $posts_tree_obj->setRgt($post_obj->getRgt() + $add_difference - 1); $posts_tree_obj->setLft($target_root_node->getRgt()); $posts_tree_obj->setDepth($post_obj->getDepth() + 1); $posts_tree_obj->setSourceThreadId($merge_thread_source->getId()); $posts_tree_obj->setTargetThreadId($merge_thread_target->getId()); $posts_tree_obj->mergeParentPos(); } else { $posts_tree_obj->setRgt($post_obj->getRgt() + $add_difference - 1); $posts_tree_obj->setLft($post_obj->getLft() + $add_difference - 1); $posts_tree_obj->setDepth($post_obj->getDepth() + 1); $posts_tree_obj->setSourceThreadId($merge_thread_source->getId()); $posts_tree_obj->setParentPos($post_obj->getParentId()); $posts_tree_obj->setTargetThreadId($merge_thread_target->getId()); $posts_tree_obj->merge(); } } // update frm_posts pos_thr_fk = target_thr_id include_once 'Modules/Forum/classes/class.ilForumPost.php'; ilForumPost::mergePosts($merge_thread_source->getId(), $merge_thread_target->getId()); // check notifications include_once 'Modules/Forum/classes/class.ilForumNotification.php'; ilForumNotification::mergeThreadNotificiations($merge_thread_source->getId(), $merge_thread_target->getId()); // delete frm_thread_access entries include_once './Modules/Forum/classes/class.ilObjForum.php'; ilObjForum::_deleteAccessEntries($merge_thread_source->getId()); // update frm_user_read ilObjForum::mergeForumUserRead($merge_thread_source->getId(), $merge_thread_target->getId()); // update visits, thr_num_posts, last_post, subject $post_date_source = $merge_thread_source->getLastPost()->getCreateDate(); $post_date_target = $merge_thread_target->getLastPost()->getCreateDate(); $target_last_post = $merge_thread_target->getLastPostString(); $exp = explode('#', $target_last_post); if ($post_date_source > $post_date_target) { $exp[2] = $merge_thread_source->getLastPost()->getId(); } else { $exp[2] = $merge_thread_target->getLastPost()->getId(); } $new_thr_last_post = implode('#', $exp); $num_posts_source = (int) $merge_thread_source->getNumPosts(); $num_visits_source = (int) $merge_thread_source->getVisits(); $num_posts_target = (int) $merge_thread_target->getNumPosts(); $num_visits_target = (int) $merge_thread_source->getVisits(); $frm_topic_obj = new ilForumTopic(0, false, true); $frm_topic_obj->setNumPosts($num_posts_source + $num_posts_target); $frm_topic_obj->setVisits($num_visits_source + $num_visits_target); $frm_topic_obj->setLastPostString($new_thr_last_post); $frm_topic_obj->setSubject($thread_subject); $frm_topic_obj->setId($merge_thread_target->getId()); $frm_topic_obj->updateMergedThread(); // update frm_data: top_last_post , top_num_threads ilForum::updateLastPostByObjId($obj_id); // reopen target if was not "closed" before merging if (!$targed_was_closed) { $merge_thread_target->reopen(); } // delete source thread ilForumTopic::deleteByThreadId($merge_thread_source->getId()); }