From 0aeedf0ac484603ada68eb15bf48b38f69e6e9c1 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Thu, 27 Sep 2018 17:35:42 -0500 Subject: [PATCH] Add association mapping forum category - forum - forum thread - forum post #2644 --- main/forum/forumfunction.inc.php | 12 +++--- main/lp/learnpath.class.php | 2 +- main/webservices/cm_webservice_forum.php | 2 +- .../Schema/V200/Version20180927172830.php | 39 +++++++++++++++++++ src/CourseBundle/Entity/CForumCategory.php | 18 +++++++++ src/CourseBundle/Entity/CForumForum.php | 39 ++++++++++++++++--- src/CourseBundle/Entity/CForumPost.php | 23 +++++------ src/CourseBundle/Entity/CForumThread.php | 37 +++++++++++++----- 8 files changed, 139 insertions(+), 33 deletions(-) create mode 100644 src/CoreBundle/Migrations/Schema/V200/Version20180927172830.php diff --git a/main/forum/forumfunction.inc.php b/main/forum/forumfunction.inc.php index 884e30a4e8..0ef86122cb 100755 --- a/main/forum/forumfunction.inc.php +++ b/main/forum/forumfunction.inc.php @@ -1002,7 +1002,7 @@ function delete_post($post_id) 'parent_of_deleted_post' => $post->getPostParentId(), 'course' => $course_id, 'post' => $post->getPostId(), - 'thread_of_deleted_post' => $post->getThreadId(), + 'thread_of_deleted_post' => $post->getThread() ? $post->getThread()->getIid() : 0, 'forum_of_deleted_post' => $post->getForumId(), ]); @@ -2020,7 +2020,7 @@ function getThreadInfo($threadId, $cId) if ($forumThread) { $thread['threadId'] = $forumThread->getThreadId(); $thread['threadTitle'] = $forumThread->getThreadTitle(); - $thread['forumId'] = $forumThread->getForumId(); + $thread['forumId'] = $forumThread->getForum() ? $forumThread->getForum()->getIid() : 0; $thread['sessionId'] = $forumThread->getSessionId(); $thread['threadSticky'] = $forumThread->getThreadSticky(); $thread['locked'] = $forumThread->getLocked(); @@ -2121,7 +2121,7 @@ function getPosts( 'post_id' => $post->getPostId(), 'post_title' => $post->getPostTitle(), 'post_text' => $post->getPostText(), - 'thread_id' => $post->getThreadId(), + 'thread_id' => $post->getThread() ? $post->getThread()->getIid() : 0, 'forum_id' => $post->getForumId(), 'poster_id' => $post->getPosterId(), 'poster_name' => $post->getPosterName(), @@ -2710,12 +2710,14 @@ function store_thread( } $clean_post_title = $values['post_title']; + $forum = $em->find('ChamiloCourseBundle:CForumForum', $values['forum_id']); + // We first store an entry in the forum_thread table because the thread_id is used in the forum_post table. $lastThread = new CForumThread(); $lastThread ->setCId($course_id) ->setThreadTitle($clean_post_title) - ->setForumId($values['forum_id']) + ->setForum($forum) ->setThreadPosterId($userId) ->setThreadPosterName(isset($values['poster_name']) ? $values['poster_name'] : null) ->setThreadDate($post_date) @@ -2810,7 +2812,7 @@ function store_thread( ->setCId($course_id) ->setPostTitle($clean_post_title) ->setPostText($values['post_text']) - ->setThreadId($lastThread->getIid()) + ->setThread($lastThread) ->setForumId($values['forum_id']) ->setPosterId($userId) ->setPosterName(isset($values['poster_name']) ? $values['poster_name'] : null) diff --git a/main/lp/learnpath.class.php b/main/lp/learnpath.class.php index f6cf460581..0fd6bce524 100755 --- a/main/lp/learnpath.class.php +++ b/main/lp/learnpath.class.php @@ -13224,7 +13224,7 @@ EOD; /** @var \Chamilo\CourseBundle\Entity\CForumThread $thread */ $thread = $repo->find($threadId); if ($thread) { - $itemList['forum'][] = $thread->getForumId(); + $itemList['forum'][] = $thread->getForum() ? $thread->getForum()->getIid() : 0; $threadList[] = $thread->getIid(); } } diff --git a/main/webservices/cm_webservice_forum.php b/main/webservices/cm_webservice_forum.php index 7126543809..cf01ca0e36 100755 --- a/main/webservices/cm_webservice_forum.php +++ b/main/webservices/cm_webservice_forum.php @@ -276,7 +276,7 @@ class WSCMForum extends WSCM $post ->setPostTitle($title) ->setPostText(isset($content) ? (api_html_entity_decode($content)) : null) - ->setThreadId($thread_id) + ->setThread($thread_id) ->setForumId($forum_id) ->setPosterId($user_id) ->setPostDate($postDate) diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20180927172830.php b/src/CoreBundle/Migrations/Schema/V200/Version20180927172830.php new file mode 100644 index 0000000000..caa43daa68 --- /dev/null +++ b/src/CoreBundle/Migrations/Schema/V200/Version20180927172830.php @@ -0,0 +1,39 @@ +addSql('UPDATE c_forum_post SET thread_id = NULL WHERE thread_id NOT IN (SELECT iid FROM c_forum_thread)'); + $this->addSql('UPDATE c_forum_thread SET forum_id = NULL WHERE forum_id NOT IN (SELECT iid FROM c_forum_forum)'); + $this->addSql('UPDATE c_forum_forum SET forum_category = NULL WHERE forum_category NOT IN (SELECT iid FROM c_forum_category)'); + + $this->addSql('ALTER TABLE c_forum_post ADD CONSTRAINT FK_B5BEF559E2904019 FOREIGN KEY (thread_id) REFERENCES c_forum_thread (iid)'); + $this->addSql('ALTER TABLE c_forum_forum ADD CONSTRAINT FK_47A9C9921BF9426 FOREIGN KEY (forum_category) REFERENCES c_forum_category (iid)'); + $this->addSql('CREATE INDEX IDX_47A9C9921BF9426 ON c_forum_forum (forum_category)'); + $this->addSql('ALTER TABLE c_forum_thread ADD CONSTRAINT FK_5DA7884C29CCBAD0 FOREIGN KEY (forum_id) REFERENCES c_forum_forum (iid)'); + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema) + { + } +} \ No newline at end of file diff --git a/src/CourseBundle/Entity/CForumCategory.php b/src/CourseBundle/Entity/CForumCategory.php index 9f27a1346b..9db253ff01 100644 --- a/src/CourseBundle/Entity/CForumCategory.php +++ b/src/CourseBundle/Entity/CForumCategory.php @@ -3,6 +3,7 @@ namespace Chamilo\CourseBundle\Entity; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; /** @@ -82,6 +83,13 @@ class CForumCategory */ private $itemProperty; + /** + * @var ArrayCollection + * + * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CForumForum", mappedBy="forumCategory") + */ + private $forums; + /** * Get iid. * @@ -260,6 +268,16 @@ class CForumCategory return $this->cId; } + /** + * Get forums. + * + * @return ArrayCollection + */ + public function getForums() + { + return $this->forums; + } + /** * @param CItemProperty $itemProperty * diff --git a/src/CourseBundle/Entity/CForumForum.php b/src/CourseBundle/Entity/CForumForum.php index ea83687908..b58f357899 100644 --- a/src/CourseBundle/Entity/CForumForum.php +++ b/src/CourseBundle/Entity/CForumForum.php @@ -3,6 +3,7 @@ namespace Chamilo\CourseBundle\Entity; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; /** @@ -77,9 +78,10 @@ class CForumForum protected $forumLastPost; /** - * @var int + * @var CForumCategory|null * - * @ORM\Column(name="forum_category", type="integer", nullable=true) + * @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CForumCategory", inversedBy="forums") + * @ORM\JoinColumn(name="forum_category", referencedColumnName="iid") */ protected $forumCategory; @@ -195,6 +197,13 @@ class CForumForum */ protected $moderated; + /** + * @var ArrayCollection + * + * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CForumThread", mappedBy="forum") + */ + protected $threads; + /** * Set forumTitle. * @@ -318,11 +327,11 @@ class CForumForum /** * Set forumCategory. * - * @param int $forumCategory + * @param CForumCategory|null $forumCategory * * @return CForumForum */ - public function setForumCategory($forumCategory) + public function setForumCategory(CForumCategory $forumCategory = null) { $this->forumCategory = $forumCategory; @@ -332,7 +341,7 @@ class CForumForum /** * Get forumCategory. * - * @return int + * @return CForumCategory|null */ public function getForumCategory() { @@ -762,4 +771,24 @@ class CForumForum return $this; } + + /** + * Get iid. + * + * @return int + */ + public function getIid() + { + return $this->iid; + } + + /** + * Get threads. + * + * @return ArrayCollection + */ + public function getThreads() + { + return $this->threads; + } } diff --git a/src/CourseBundle/Entity/CForumPost.php b/src/CourseBundle/Entity/CForumPost.php index 7fa8cf2451..1d982dbeee 100644 --- a/src/CourseBundle/Entity/CForumPost.php +++ b/src/CourseBundle/Entity/CForumPost.php @@ -65,11 +65,12 @@ class CForumPost protected $postText; /** - * @var int + * @var CForumThread|null * - * @ORM\Column(name="thread_id", type="integer", nullable=true) + * @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CForumThread", inversedBy="posts") + * @ORM\JoinColumn(name="thread_id", referencedColumnName="iid") */ - protected $threadId; + protected $thread; /** * @var int @@ -176,27 +177,27 @@ class CForumPost } /** - * Set threadId. + * Set thread. * - * @param int $threadId + * @param CForumThread|null $thread * * @return CForumPost */ - public function setThreadId($threadId) + public function setThread(CForumThread $thread = null) { - $this->threadId = $threadId; + $this->thread = $thread; return $this; } /** - * Get threadId. + * Get thread. * - * @return int + * @return CForumThread|null */ - public function getThreadId() + public function getThread() { - return $this->threadId; + return $this->thread; } /** diff --git a/src/CourseBundle/Entity/CForumThread.php b/src/CourseBundle/Entity/CForumThread.php index 4b5448317f..6f88d271c8 100644 --- a/src/CourseBundle/Entity/CForumThread.php +++ b/src/CourseBundle/Entity/CForumThread.php @@ -3,6 +3,7 @@ namespace Chamilo\CourseBundle\Entity; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; /** @@ -50,11 +51,12 @@ class CForumThread protected $threadTitle; /** - * @var int + * @var CForumForum|null * - * @ORM\Column(name="forum_id", type="integer", nullable=true) + * @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CForumForum", inversedBy="threads") + * @ORM\JoinColumn(name="forum_id", referencedColumnName="iid") */ - protected $forumId; + protected $forum; /** * @var int @@ -161,6 +163,13 @@ class CForumThread */ protected $lpItemId; + /** + * @var ArrayCollection + * + * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CForumPost", mappedBy="thread") + */ + protected $posts; + /** * Constructor. */ @@ -218,15 +227,15 @@ class CForumThread } /** - * Set forumId. + * Set forum. * - * @param int $forumId + * @param CForumForum|null $forum * * @return CForumThread */ - public function setForumId($forumId) + public function setForum(CForumForum $forum = null) { - $this->forumId = $forumId; + $this->forum = $forum; return $this; } @@ -234,11 +243,11 @@ class CForumThread /** * Get forumId. * - * @return int + * @return CForumForum|null */ - public function getForumId() + public function getForum() { - return $this->forumId; + return $this->forum; } /** @@ -634,4 +643,12 @@ class CForumThread { return $this->iid; } + + /** + * @return ArrayCollection + */ + public function getPosts() + { + return $this->posts; + } }