/**
     * delete post and sub-posts
     * @param	integer	$post: ID	
     * @access	public
     * @return	integer	0 or thread-ID
     */
    public function deletePost($post)
    {
        global $ilDB;
        include_once "./Modules/Forum/classes/class.ilObjForum.php";
        // delete tree and get id's of all posts to delete
        $p_node = $this->getPostNode($post);
        $del_id = $this->deletePostTree($p_node);
        // Delete User read entries
        foreach ($del_id as $post_id) {
            ilObjForum::_deleteReadEntries($post_id);
        }
        // DELETE ATTACHMENTS ASSIGNED TO POST
        $this->__deletePostFiles($del_id);
        $dead_pos = count($del_id);
        $dead_thr = 0;
        // if deletePost is thread opener ...
        if ($p_node["parent"] == 0) {
            // delete thread access data
            include_once './Modules/Forum/classes/class.ilObjForum.php';
            ilObjForum::_deleteAccessEntries($p_node['tree']);
            // delete thread
            $dead_thr = $p_node["tree"];
            $statement = $ilDB->manipulateF('
				DELETE FROM frm_threads
				WHERE thr_pk = %s', array('integer'), array($p_node['tree']));
            // update num_threads
            $statement = $ilDB->manipulateF('
				UPDATE frm_data 
				SET top_num_threads = top_num_threads - 1 
				WHERE top_frm_fk = %s', array('integer'), array($this->id));
            // delete all related news
            $posset = $ilDB->queryf('
				SELECT * FROM frm_posts
				WHERE pos_thr_fk = %s', array('integer'), array($p_node['tree']));
            while ($posrec = $ilDB->fetchAssoc($posset)) {
                include_once "./Services/News/classes/class.ilNewsItem.php";
                $news_id = ilNewsItem::getFirstNewsIdForContext($this->id, "frm", $posrec["pos_pk"], "pos");
                if ($news_id > 0) {
                    $news_item = new ilNewsItem($news_id);
                    $news_item->delete();
                }
                try {
                    include_once 'Services/MediaObjects/classes/class.ilObjMediaObject.php';
                    $mobs = ilObjMediaObject::_getMobsOfObject('frm:html', $posrec['pos_pk']);
                    foreach ($mobs as $mob) {
                        if (ilObjMediaObject::_exists($mob)) {
                            ilObjMediaObject::_removeUsage($mob, 'frm:html', $posrec['pos_pk']);
                            $mob_obj = new ilObjMediaObject($mob);
                            $mob_obj->delete();
                        }
                    }
                } catch (Exception $e) {
                }
            }
            // delete all posts of this thread
            $statement = $ilDB->manipulateF('
				DELETE FROM frm_posts
				WHERE pos_thr_fk = %s', array('integer'), array($p_node['tree']));
        } else {
            // delete this post and its sub-posts
            for ($i = 0; $i < $dead_pos; $i++) {
                $statement = $ilDB->manipulateF('
					DELETE FROM frm_posts
					WHERE pos_pk = %s', array('integer'), array($del_id[$i]));
                // delete related news item
                include_once "./Services/News/classes/class.ilNewsItem.php";
                $news_id = ilNewsItem::getFirstNewsIdForContext($this->id, "frm", $del_id[$i], "pos");
                if ($news_id > 0) {
                    $news_item = new ilNewsItem($news_id);
                    $news_item->delete();
                }
                try {
                    include_once 'Services/MediaObjects/classes/class.ilObjMediaObject.php';
                    $mobs = ilObjMediaObject::_getMobsOfObject('frm:html', $del_id[$i]);
                    foreach ($mobs as $mob) {
                        if (ilObjMediaObject::_exists($mob)) {
                            ilObjMediaObject::_removeUsage($mob, 'frm:html', $del_id[$i]);
                            $mob_obj = new ilObjMediaObject($mob);
                            $mob_obj->delete();
                        }
                    }
                } catch (Exception $e) {
                }
            }
            // update num_posts in frm_threads
            $statement = $ilDB->manipulateF('
				UPDATE frm_threads
				SET thr_num_posts = thr_num_posts - %s
				WHERE thr_pk = %s', array('integer', 'integer'), array($dead_pos, $p_node['tree']));
            // get latest post of thread and update last_post
            $res1 = $ilDB->queryf('
				SELECT * FROM frm_posts 
				WHERE pos_thr_fk = %s
				ORDER BY pos_date DESC', array('integer'), array($p_node['tree']));
            if ($res1->numRows() == 0) {
                $lastPost_thr = "";
            } else {
                $z = 0;
                while ($selData = $ilDB->fetchAssoc($res1)) {
                    if ($z > 0) {
                        break;
                    }
                    $lastPost_thr = $selData["pos_top_fk"] . "#" . $selData["pos_thr_fk"] . "#" . $selData["pos_pk"];
                    $z++;
                }
            }
            $statement = $ilDB->manipulateF('
				UPDATE frm_threads
				SET thr_last_post = %s
				WHERE thr_pk = %s', array('text', 'integer'), array($lastPost_thr, $p_node['tree']));
        }
        // update num_posts in frm_data
        $statement = $ilDB->manipulateF('
			UPDATE frm_data
			SET top_num_posts = top_num_posts - %s
			WHERE top_frm_fk = %s', array('integer', 'integer'), array($dead_pos, $this->id));
        // get latest post of forum and update last_post
        $res2 = $ilDB->queryf('
			SELECT * FROM frm_posts, frm_data 
			WHERE pos_top_fk = top_pk 
			AND top_frm_fk = %s
			ORDER BY pos_date DESC', array('integer'), array($this->id));
        if ($res2->numRows() == 0) {
            $lastPost_top = "";
        } else {
            $z = 0;
            while ($selData = $ilDB->fetchAssoc($res2)) {
                if ($z > 0) {
                    break;
                }
                $lastPost_top = $selData["pos_top_fk"] . "#" . $selData["pos_thr_fk"] . "#" . $selData["pos_pk"];
                $z++;
            }
        }
        $statement = $ilDB->manipulateF('
			UPDATE frm_data
			SET top_last_post = %s
			WHERE top_frm_fk = %s', array('text', 'integer'), array($lastPost_top, $this->id));
        return $dead_thr;
    }
示例#2
0
 /**
  * @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());
 }