/** * Fetches and returns an array of posts from the post tree, starting with the node object passed by * the first paramter. * * @param ilForumPost $a_post_node node-object of a post * @return array array of post objects * @access public */ public function getPostTree(ilForumPost $a_post_node) { global $ilUser; $posts = array(); $data = array(); $data_types = array(); $query = ' SELECT is_author_moderator, pos_author_id, pos_pk, fpt_date, rgt, pos_top_fk, pos_thr_fk, pos_display_user_id, pos_usr_alias, pos_subject, pos_status, pos_message, pos_date, pos_update, update_user, pos_cens, pos_cens_com, notify, import_name, fpt_pk, parent_pos, lft, depth, (CASE WHEN fur.post_id IS NULL ' . ($ilUser->getId() == ANONYMOUS_USER_ID ? ' AND 1 = 2 ' : '') . ' THEN 0 ELSE 1 END) post_read, firstname, lastname, title, login FROM frm_posts_tree INNER JOIN frm_posts ON pos_fk = pos_pk LEFT JOIN usr_data ON pos_display_user_id = usr_id LEFT JOIN frm_user_read fur ON fur.thread_id = pos_thr_fk AND fur.post_id = pos_pk AND fur.usr_id = %s WHERE lft BETWEEN %s AND %s AND thr_fk = %s'; array_push($data_types, 'integer', 'integer', 'integer', 'integer'); array_push($data, $ilUser->getId(), $a_post_node->getLft(), $a_post_node->getRgt(), $a_post_node->getThreadId()); if ($this->orderField != "") { $query .= " ORDER BY " . $this->orderField . " " . $this->getOrderDirection(); } $res = $this->db->queryf($query, $data_types, $data); $usr_ids = array(); $deactivated = array(); while ($row = $this->db->fetchAssoc($res)) { $tmp_object = new ilForumPost($row['pos_pk'], false, true); $tmp_object->assignData($row); if (!$this->is_moderator) { if (!$tmp_object->isActivated() && $tmp_object->getDisplayUserId() != $ilUser->getId()) { $deactivated[] = $tmp_object; unset($tmp_object); continue; } foreach ($deactivated as $deactivated_node) { if ($deactivated_node->getLft() < $tmp_object->getLft() && $deactivated_node->getRgt() > $tmp_object->getLft()) { $deactivated[] = $tmp_object; unset($tmp_object); continue 2; } } } if ((int) $row['pos_display_user_id']) { $usr_ids[] = (int) $row['pos_display_user_id']; } if ((int) $row['update_user']) { $usr_ids[] = (int) $row['update_user']; } $posts[] = $tmp_object; unset($tmp_object); } require_once 'Modules/Forum/classes/class.ilForumAuthorInformationCache.php'; ilForumAuthorInformationCache::preloadUserObjects(array_unique($usr_ids)); return $posts; }
/** * @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()); }