Browse Source

Fix link import/export see BT#12024

jmontoyaa 8 years ago
parent
commit
90406b5d22

+ 74 - 24
main/inc/lib/link.lib.php

@@ -782,15 +782,23 @@ class Link extends Model
      * session
      * @param   int $courseId
      * @param   int $sessionId
+     * @param   bool $withBaseContent
+     *
      * @return array
      */
-    public static function getLinkCategories($courseId, $sessionId)
+    public static function getLinkCategories($courseId, $sessionId, $withBaseContent = true)
     {
         $tblLinkCategory = Database:: get_course_table(TABLE_LINK_CATEGORY);
         $tblItemProperty = Database:: get_course_table(TABLE_ITEM_PROPERTY);
         $courseId = intval($courseId);
+
         // Condition for the session.
-        $sessionCondition = api_get_session_condition($sessionId, true, true, 'linkcat.session_id');
+        $sessionCondition = api_get_session_condition(
+            $sessionId,
+            true,
+            $withBaseContent,
+            'linkcat.session_id'
+        );
 
         // Getting links
         $sql = "SELECT *, linkcat.id
@@ -846,42 +854,71 @@ class Link extends Model
     }
 
     /**
-     * Displays all the links of a given category.
-     * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+     * @param $categoryId
+     * @param $courseId
+     * @param $sessionId
+     * @param bool $withBaseContent
+     *
+     * @return array
      */
-    public static function showlinksofcategory($catid)
+    public static function getLinksPerCategory($categoryId, $courseId, $sessionId, $withBaseContent = true)
     {
-        global $token;
-        $_user = api_get_user_info();
-        $course_id = api_get_course_int_id();
-        $session_id = api_get_session_id();
-        $catid = intval($catid);
-
         $tbl_link = Database:: get_course_table(TABLE_LINK);
         $TABLE_ITEM_PROPERTY = Database:: get_course_table(TABLE_ITEM_PROPERTY);
+        $courseId = (int) $courseId;
+        $sessionId = (int) $sessionId;
+        $categoryId = (int) $categoryId;
 
         // Condition for the session.
-        $condition_session = api_get_session_condition($session_id, true, true, 'link.session_id');
-        $content = '';
+        $condition_session = api_get_session_condition(
+            $sessionId,
+            true,
+            $withBaseContent,
+            'link.session_id'
+        );
+
         $sql = "SELECT *, link.id 
                 FROM $tbl_link link
                 INNER JOIN $TABLE_ITEM_PROPERTY ip
-                ON (link.id=ip.ref AND link.c_id = ip.c_id)
+                ON (link.id = ip.ref AND link.c_id = ip.c_id)
                 WHERE
-                    ip.tool='" . TOOL_LINK . "' AND
-                    link.category_id='" . $catid . "' AND
-                    (ip.visibility='0' OR ip.visibility='1')
+                    ip.tool = '" . TOOL_LINK . "' AND
+                    link.category_id = '" . $categoryId . "' AND
+                    (ip.visibility = '0' OR ip.visibility = '1')
                     $condition_session AND
-                    link.c_id = " . $course_id . " AND
-                    ip.c_id = " . $course_id . "
+                    link.c_id = $courseId AND
+                    ip.c_id = $courseId 
                 ORDER BY link.display_order ASC";
 
         $result = Database:: query($sql);
-        $numberoflinks = Database:: num_rows($result);
-        if ($numberoflinks > 0) {
+
+        return Database::store_result($result);
+    }
+
+    /**
+     * Displays all the links of a given category.
+     * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
+     * @author Julio Montoya
+     *
+     * @param $catid
+     * @param $courseId
+     * @param $session_id
+     * @return string
+     */
+    public static function showLinksPerCategory($catid, $courseId, $session_id)
+    {
+        global $token;
+        $_user = api_get_user_info();
+        $catid = intval($catid);
+
+        $links = self::getLinksPerCategory($catid, $courseId, $session_id);
+        $content = '';
+        $numberoflinks = count($links);
+
+        if (!empty($links)) {
             $content .= '<div class="link list-group">';
             $i = 1;
-            while ($myrow = Database:: fetch_array($result)) {
+            foreach ($links as $myrow) {
                 // Validation when belongs to a session.
                 $session_img = api_get_session_image(
                     $myrow['session_id'],
@@ -962,6 +999,7 @@ class Link extends Model
                             'category_id' => $myrow['category_id'],
                             'action' => 'move_link_up'
                         ];
+
                         $toolbar .= Display::toolbarButton(
                             get_lang('MoveUp'),
                             'link.php?' . api_get_cidreq() . '&' . http_build_query($moveLinkParams),
@@ -970,6 +1008,7 @@ class Link extends Model
                             ['class' => 'btn-sm ' . ($i === 1 ? 'disabled' : '')],
                             false
                         );
+
                         $moveLinkParams['action'] = 'move_link_down';
                         $toolbar .= Display::toolbarButton(
                             get_lang('MoveDown'),
@@ -1530,7 +1569,14 @@ class Link extends Model
         $count = Database::num_rows($result);
 
         if ($count !== 0) {
-            echo Display::panel(Link::showlinksofcategory(0), get_lang('General'));
+            echo Display::panel(
+                Link::showLinksPerCategory(
+                    0,
+                    api_get_course_int_id(),
+                    api_get_session_id()
+                ),
+                get_lang('General')
+            );
         }
 
         $counter = 0;
@@ -1579,7 +1625,11 @@ class Link extends Model
 
             $childrenContent = '';
             if ($showChildren) {
-                $childrenContent = Link::showlinksofcategory($myrow['id']);
+                $childrenContent = Link::showLinksPerCategory(
+                    $myrow['id'],
+                    api_get_course_int_id(),
+                    api_get_session_id()
+                );
             }
 
             echo Display::panel($myrow['description'].$childrenContent, $header);

+ 1 - 3
main/link/link.php

@@ -97,10 +97,8 @@ $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
 $scope = isset($_REQUEST['scope']) ? $_REQUEST['scope'] : null;
 $show = isset($_REQUEST['show']) && in_array(trim($_REQUEST['show']), ['all', 'none']) ? $_REQUEST['show'] : '';
 $categoryId = isset($_REQUEST['category_id']) ? intval($_REQUEST['category_id']) : '';
-
 $linkListUrl = api_get_self().'?'.api_get_cidreq().'&category_id='.$categoryId.'&show='.$show;
-
-$content = null;
+$content = '';
 
 switch ($action) {
     case 'addlink':

+ 39 - 91
src/Chamilo/CourseBundle/Component/CourseCopy/CourseBuilder.php

@@ -35,6 +35,8 @@ use Chamilo\CourseBundle\Component\CourseCopy\Resources\ToolIntro;
 use Chamilo\CourseBundle\Component\CourseCopy\Resources\Wiki;
 use Chamilo\CourseBundle\Component\CourseCopy\Resources\Work;
 
+use \Link as LinkManager;
+
 /**
  * Class CourseBuilder
  * Builds a course-object from a Chamilo-course.
@@ -440,82 +442,38 @@ class CourseBuilder
         $with_base_content = false,
         $id_list = array()
     ) {
-        $table = Database :: get_course_table(TABLE_LINK);
-        $table_prop = Database :: get_course_table(TABLE_ITEM_PROPERTY);
+        $categories = LinkManager::getLinkCategories(
+            $courseId,
+            $session_id,
+            $with_base_content
+        );
 
-        if (!empty($session_id) && !empty($courseId)) {
-            $session_id = intval($session_id);
-            if ($with_base_content) {
-                $session_condition = api_get_session_condition(
-                    $session_id,
-                    true,
-                    true,
-                    'l.session_id'
-                );
-            } else {
-                $session_condition = api_get_session_condition(
-                    $session_id,
-                    true,
-                    false,
-                    'l.session_id'
-                );
-            }
-            $sql = "SELECT  
-                        l.id, 
-                        l.title, 
-                        l.url, 
-                        l.description, 
-                        l.category_id, 
-                        l.on_homepage
-                    FROM $table l INNER JOIN $table_prop p
-                    ON (l.c_id = p.c_id AND p.ref = l.id)
-                    WHERE
-                        l.c_id = $courseId AND
-                        p.c_id = $courseId AND                        
-                        p.tool = '".TOOL_LINK."' AND
-                        p.visibility != 2 $session_condition
-                    ORDER BY l.display_order";
-        } else {
-            $sql = "SELECT 
-                        l.id, 
-                        l.title, 
-                        l.url, 
-                        l.description, 
-                        l.category_id, 
-                        l.on_homepage
-                    FROM $table l INNER JOIN $table_prop p
-                    ON (l.c_id = p.c_id AND p.ref = l.id)
-                    WHERE
-                        l.c_id = $courseId AND
-                        p.c_id = $courseId AND                        
-                        p.tool = '".TOOL_LINK."' AND
-                        p.visibility != 2 AND
-                        (l.session_id = 0 OR l.session_id IS NULL)
-                    ORDER BY l.display_order";
-        }
+        // Adding empty category
+        $categories[] = ['id' => 0];
 
-        $db_result = Database::query($sql);
-        while ($obj = Database::fetch_object($db_result)) {
-            $link = new Link(
-                $obj->id,
-                $obj->title,
-                $obj->url,
-                $obj->description,
-                $obj->category_id,
-                $obj->on_homepage
-            );
-            $this->course->add_resource($link);
+        foreach ($categories as $category) {
+            $this->build_link_category($category);
 
-            if (!empty($courseId)) {
-                $res = $this->build_link_category($obj->category_id, $courseId);
-            } else {
-                $res = $this->build_link_category($obj->category_id);
-            }
+            $links = LinkManager::getLinksPerCategory(
+                $category['id'],
+                $courseId,
+                $session_id,
+                $with_base_content
+            );
 
-            if ($res > 0) {
-                $this->course->resources[RESOURCE_LINK][$obj->id]->add_linked_resource(
+            foreach ($links as $item) {
+                $link = new Link(
+                    $item['id'],
+                    $item['title'],
+                    $item['url'],
+                    $item['description'],
+                    $item['category_id'],
+                    $item['on_homepage']
+                );
+                $this->course->add_resource($link);
+                $this->course->resources[RESOURCE_LINK][$item['id']]->add_linked_resource(
                     RESOURCE_LINKCATEGORY,
-                    $obj->category_id
+                    $item['category_id']
                 );
             }
         }
@@ -558,31 +516,21 @@ class CourseBuilder
      * @param int $courseId Internal course ID
      * @return int
      */
-    public function build_link_category($id, $courseId = 0)
+    public function build_link_category($category)
     {
-        $link_cat_table = Database :: get_course_table(TABLE_LINK_CATEGORY);
-        $courseId = intval($courseId);
-        $id = intval($id);
-
-        if (empty($id) || empty($courseId)) {
+        if (empty($category) || empty($category['category_title'])) {
             return 0;
         }
-        $sql = "SELECT * FROM $link_cat_table
-                WHERE c_id = $courseId AND id = $id";
-        $result = Database::query($sql);
-        while ($obj = Database::fetch_object($result)) {
-            $linkCategory = new LinkCategory(
-                $obj->id,
-                $obj->category_title,
-                $obj->description,
-                $obj->display_order
-            );
-            $this->course->add_resource($linkCategory);
 
-            return $id;
-        }
+        $linkCategory = new LinkCategory(
+            $category['id'],
+            $category['category_title'],
+            $category['description'],
+            $category['display_order']
+        );
+        $this->course->add_resource($linkCategory);
 
-        return 0;
+        return $category['id'];
     }
 
     /**