Răsfoiți Sursa

Add association mapping forum category - forum - forum thread - forum post #2644

Angel Fernando Quiroz Campos 6 ani în urmă
părinte
comite
0aeedf0ac4

+ 7 - 5
main/forum/forumfunction.inc.php

@@ -1002,7 +1002,7 @@ function delete_post($post_id)
                 'parent_of_deleted_post' => $post->getPostParentId(),
                 'parent_of_deleted_post' => $post->getPostParentId(),
                 'course' => $course_id,
                 'course' => $course_id,
                 'post' => $post->getPostId(),
                 '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(),
                 'forum_of_deleted_post' => $post->getForumId(),
             ]);
             ]);
 
 
@@ -2020,7 +2020,7 @@ function getThreadInfo($threadId, $cId)
     if ($forumThread) {
     if ($forumThread) {
         $thread['threadId'] = $forumThread->getThreadId();
         $thread['threadId'] = $forumThread->getThreadId();
         $thread['threadTitle'] = $forumThread->getThreadTitle();
         $thread['threadTitle'] = $forumThread->getThreadTitle();
-        $thread['forumId'] = $forumThread->getForumId();
+        $thread['forumId'] = $forumThread->getForum() ? $forumThread->getForum()->getIid() : 0;
         $thread['sessionId'] = $forumThread->getSessionId();
         $thread['sessionId'] = $forumThread->getSessionId();
         $thread['threadSticky'] = $forumThread->getThreadSticky();
         $thread['threadSticky'] = $forumThread->getThreadSticky();
         $thread['locked'] = $forumThread->getLocked();
         $thread['locked'] = $forumThread->getLocked();
@@ -2121,7 +2121,7 @@ function getPosts(
             'post_id' => $post->getPostId(),
             'post_id' => $post->getPostId(),
             'post_title' => $post->getPostTitle(),
             'post_title' => $post->getPostTitle(),
             'post_text' => $post->getPostText(),
             'post_text' => $post->getPostText(),
-            'thread_id' => $post->getThreadId(),
+            'thread_id' => $post->getThread() ? $post->getThread()->getIid() : 0,
             'forum_id' => $post->getForumId(),
             'forum_id' => $post->getForumId(),
             'poster_id' => $post->getPosterId(),
             'poster_id' => $post->getPosterId(),
             'poster_name' => $post->getPosterName(),
             'poster_name' => $post->getPosterName(),
@@ -2710,12 +2710,14 @@ function store_thread(
     }
     }
     $clean_post_title = $values['post_title'];
     $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.
     // 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 = new CForumThread();
     $lastThread
     $lastThread
         ->setCId($course_id)
         ->setCId($course_id)
         ->setThreadTitle($clean_post_title)
         ->setThreadTitle($clean_post_title)
-        ->setForumId($values['forum_id'])
+        ->setForum($forum)
         ->setThreadPosterId($userId)
         ->setThreadPosterId($userId)
         ->setThreadPosterName(isset($values['poster_name']) ? $values['poster_name'] : null)
         ->setThreadPosterName(isset($values['poster_name']) ? $values['poster_name'] : null)
         ->setThreadDate($post_date)
         ->setThreadDate($post_date)
@@ -2810,7 +2812,7 @@ function store_thread(
         ->setCId($course_id)
         ->setCId($course_id)
         ->setPostTitle($clean_post_title)
         ->setPostTitle($clean_post_title)
         ->setPostText($values['post_text'])
         ->setPostText($values['post_text'])
-        ->setThreadId($lastThread->getIid())
+        ->setThread($lastThread)
         ->setForumId($values['forum_id'])
         ->setForumId($values['forum_id'])
         ->setPosterId($userId)
         ->setPosterId($userId)
         ->setPosterName(isset($values['poster_name']) ? $values['poster_name'] : null)
         ->setPosterName(isset($values['poster_name']) ? $values['poster_name'] : null)

+ 1 - 1
main/lp/learnpath.class.php

@@ -13224,7 +13224,7 @@ EOD;
                 /** @var \Chamilo\CourseBundle\Entity\CForumThread $thread */
                 /** @var \Chamilo\CourseBundle\Entity\CForumThread $thread */
                 $thread = $repo->find($threadId);
                 $thread = $repo->find($threadId);
                 if ($thread) {
                 if ($thread) {
-                    $itemList['forum'][] = $thread->getForumId();
+                    $itemList['forum'][] = $thread->getForum() ? $thread->getForum()->getIid() : 0;
                     $threadList[] = $thread->getIid();
                     $threadList[] = $thread->getIid();
                 }
                 }
             }
             }

+ 1 - 1
main/webservices/cm_webservice_forum.php

@@ -276,7 +276,7 @@ class WSCMForum extends WSCM
             $post
             $post
                 ->setPostTitle($title)
                 ->setPostTitle($title)
                 ->setPostText(isset($content) ? (api_html_entity_decode($content)) : null)
                 ->setPostText(isset($content) ? (api_html_entity_decode($content)) : null)
-                ->setThreadId($thread_id)
+                ->setThread($thread_id)
                 ->setForumId($forum_id)
                 ->setForumId($forum_id)
                 ->setPosterId($user_id)
                 ->setPosterId($user_id)
                 ->setPostDate($postDate)
                 ->setPostDate($postDate)

+ 39 - 0
src/CoreBundle/Migrations/Schema/V200/Version20180927172830.php

@@ -0,0 +1,39 @@
+<?php
+/* For licensing terms, see /license.txt */
+
+namespace Chamilo\CoreBundle\Migrations\Schema\V200;
+
+use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
+use Doctrine\DBAL\Schema\Schema;
+
+/**
+ * Class Version20180927172830.
+ *
+ * Add foreing keys between forum category - forum - forum thread - forum post
+ *
+ * @package Chamilo\CoreBundle\Migrations\Schema\V200
+ */
+class Version20180927172830 extends AbstractMigrationChamilo
+{
+    /**
+     * @param Schema $schema
+     */
+    public function up(Schema $schema)
+    {
+        $this->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)
+    {
+    }
+}

+ 18 - 0
src/CourseBundle/Entity/CForumCategory.php

@@ -3,6 +3,7 @@
 
 
 namespace Chamilo\CourseBundle\Entity;
 namespace Chamilo\CourseBundle\Entity;
 
 
+use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\ORM\Mapping as ORM;
 use Doctrine\ORM\Mapping as ORM;
 
 
 /**
 /**
@@ -82,6 +83,13 @@ class CForumCategory
      */
      */
     private $itemProperty;
     private $itemProperty;
 
 
+    /**
+     * @var ArrayCollection
+     *
+     * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CForumForum", mappedBy="forumCategory")
+     */
+    private $forums;
+
     /**
     /**
      * Get iid.
      * Get iid.
      *
      *
@@ -260,6 +268,16 @@ class CForumCategory
         return $this->cId;
         return $this->cId;
     }
     }
 
 
+    /**
+     * Get forums.
+     *
+     * @return ArrayCollection
+     */
+    public function getForums()
+    {
+        return $this->forums;
+    }
+
     /**
     /**
      * @param CItemProperty $itemProperty
      * @param CItemProperty $itemProperty
      *
      *

+ 34 - 5
src/CourseBundle/Entity/CForumForum.php

@@ -3,6 +3,7 @@
 
 
 namespace Chamilo\CourseBundle\Entity;
 namespace Chamilo\CourseBundle\Entity;
 
 
+use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\ORM\Mapping as ORM;
 use Doctrine\ORM\Mapping as ORM;
 
 
 /**
 /**
@@ -77,9 +78,10 @@ class CForumForum
     protected $forumLastPost;
     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;
     protected $forumCategory;
 
 
@@ -195,6 +197,13 @@ class CForumForum
      */
      */
     protected $moderated;
     protected $moderated;
 
 
+    /**
+     * @var ArrayCollection
+     *
+     * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CForumThread", mappedBy="forum")
+     */
+    protected $threads;
+
     /**
     /**
      * Set forumTitle.
      * Set forumTitle.
      *
      *
@@ -318,11 +327,11 @@ class CForumForum
     /**
     /**
      * Set forumCategory.
      * Set forumCategory.
      *
      *
-     * @param int $forumCategory
+     * @param CForumCategory|null $forumCategory
      *
      *
      * @return CForumForum
      * @return CForumForum
      */
      */
-    public function setForumCategory($forumCategory)
+    public function setForumCategory(CForumCategory $forumCategory = null)
     {
     {
         $this->forumCategory = $forumCategory;
         $this->forumCategory = $forumCategory;
 
 
@@ -332,7 +341,7 @@ class CForumForum
     /**
     /**
      * Get forumCategory.
      * Get forumCategory.
      *
      *
-     * @return int
+     * @return CForumCategory|null
      */
      */
     public function getForumCategory()
     public function getForumCategory()
     {
     {
@@ -762,4 +771,24 @@ class CForumForum
 
 
         return $this;
         return $this;
     }
     }
+
+    /**
+     * Get iid.
+     *
+     * @return int
+     */
+    public function getIid()
+    {
+        return $this->iid;
+    }
+
+    /**
+     * Get threads.
+     *
+     * @return ArrayCollection
+     */
+    public function getThreads()
+    {
+        return $this->threads;
+    }
 }
 }

+ 12 - 11
src/CourseBundle/Entity/CForumPost.php

@@ -65,11 +65,12 @@ class CForumPost
     protected $postText;
     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
      * @var int
@@ -176,27 +177,27 @@ class CForumPost
     }
     }
 
 
     /**
     /**
-     * Set threadId.
+     * Set thread.
      *
      *
-     * @param int $threadId
+     * @param CForumThread|null $thread
      *
      *
      * @return CForumPost
      * @return CForumPost
      */
      */
-    public function setThreadId($threadId)
+    public function setThread(CForumThread $thread = null)
     {
     {
-        $this->threadId = $threadId;
+        $this->thread = $thread;
 
 
         return $this;
         return $this;
     }
     }
 
 
     /**
     /**
-     * Get threadId.
+     * Get thread.
      *
      *
-     * @return int
+     * @return CForumThread|null
      */
      */
-    public function getThreadId()
+    public function getThread()
     {
     {
-        return $this->threadId;
+        return $this->thread;
     }
     }
 
 
     /**
     /**

+ 27 - 10
src/CourseBundle/Entity/CForumThread.php

@@ -3,6 +3,7 @@
 
 
 namespace Chamilo\CourseBundle\Entity;
 namespace Chamilo\CourseBundle\Entity;
 
 
+use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\ORM\Mapping as ORM;
 use Doctrine\ORM\Mapping as ORM;
 
 
 /**
 /**
@@ -50,11 +51,12 @@ class CForumThread
     protected $threadTitle;
     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
      * @var int
@@ -161,6 +163,13 @@ class CForumThread
      */
      */
     protected $lpItemId;
     protected $lpItemId;
 
 
+    /**
+     * @var ArrayCollection
+     *
+     * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CForumPost", mappedBy="thread")
+     */
+    protected $posts;
+
     /**
     /**
      * Constructor.
      * Constructor.
      */
      */
@@ -218,15 +227,15 @@ class CForumThread
     }
     }
 
 
     /**
     /**
-     * Set forumId.
+     * Set forum.
      *
      *
-     * @param int $forumId
+     * @param CForumForum|null $forum
      *
      *
      * @return CForumThread
      * @return CForumThread
      */
      */
-    public function setForumId($forumId)
+    public function setForum(CForumForum $forum = null)
     {
     {
-        $this->forumId = $forumId;
+        $this->forum = $forum;
 
 
         return $this;
         return $this;
     }
     }
@@ -234,11 +243,11 @@ class CForumThread
     /**
     /**
      * Get forumId.
      * 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 $this->iid;
     }
     }
+
+    /**
+     * @return ArrayCollection
+     */
+    public function getPosts()
+    {
+        return $this->posts;
+    }
 }
 }