Browse Source

Add export link pages to a PDF see BT#15679

Julio Montoya 5 years ago
parent
commit
52fb31926b
2 changed files with 84 additions and 41 deletions
  1. 67 36
      main/inc/lib/link.lib.php
  2. 17 5
      main/link/link.php

+ 67 - 36
main/inc/lib/link.lib.php

@@ -964,20 +964,21 @@ class Link extends Model
     /**
      * Displays all the links of a given category.
      *
-     * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
-     * @author Julio Montoya
-     *
-     * @param $catid
-     * @param int $courseId
-     * @param int $session_id
+     * @param int  $catid
+     * @param int  $courseId
+     * @param int  $session_id
+     * @param bool $showActionLinks
      *
      * @return string
+     * @author Julio Montoya
+     *
+     * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
      */
-    public static function showLinksPerCategory($catid, $courseId, $session_id)
+    public static function showLinksPerCategory($catid, $courseId, $session_id, $showActionLinks = true)
     {
         global $token;
         $_user = api_get_user_info();
-        $catid = intval($catid);
+        $catid = (int) $catid;
 
         $links = self::getLinksPerCategory($catid, $courseId, $session_id);
         $content = '';
@@ -1137,7 +1138,9 @@ class Link extends Model
                     );
                     $url = api_get_path(WEB_CODE_PATH).'link/link_goto.php?'.api_get_cidreq().'&link_id='.$linkId.'&link_url='.urlencode($myrow['url']);
                     $content .= '<div class="list-group-item">';
-                    $content .= '<div class="pull-right"><div class="btn-group">'.$toolbar.'</div></div>';
+                    if ($showActionLinks) {
+                        $content .= '<div class="pull-right"><div class="btn-group">'.$toolbar.'</div></div>';
+                    }
                     $content .= '<h4 class="list-group-item-heading">';
                     $content .= $iconLink;
                     $content .= Display::tag(
@@ -1558,42 +1561,68 @@ class Link extends Model
      * @param int    $categoryId
      * @param string $show
      * @param null   $token
+     * @param bool   $showActionLinks
+     *
+     * @return string
      */
     public static function listLinksAndCategories(
         $course_id,
         $session_id,
         $categoryId,
         $show = 'none',
-        $token = null
+        $token = null,
+        $showActionLinks = true
     ) {
         $categoryId = (int) $categoryId;
 
-        /*	Action Links */
-        echo '<div class="actions">';
-        if (api_is_allowed_to_edit(null, true)) {
-            echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=addlink&category_id='.$categoryId.'">'.
-                Display::return_icon('new_link.png', get_lang('LinkAdd'), '', ICON_SIZE_MEDIUM).'</a>';
-            echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=addcategory&category_id='.$categoryId.'">'.
-                Display::return_icon('new_folder.png', get_lang('CategoryAdd'), '', ICON_SIZE_MEDIUM).'</a>';
-        }
+        $content = '';
 
         $categories = self::getLinkCategories($course_id, $session_id);
         $countCategories = count($categories);
-        if (!empty($countCategories)) {
-            echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=list&show=none">';
-            echo Display::return_icon('forum_listview.png', get_lang('FlatView'), '', ICON_SIZE_MEDIUM).' </a>';
+        $linksPerCategory = self::showLinksPerCategory(0, $course_id, $session_id, $showActionLinks);
+
+        if ($showActionLinks) {
+            /*	Action Links */
+            $content = '<div class="actions">';
+            if (api_is_allowed_to_edit(null, true)) {
+                $content .= '<a href="'.api_get_self().'?'.api_get_cidreq(
+                    ).'&action=addlink&category_id='.$categoryId.'">'.
+                    Display::return_icon('new_link.png', get_lang('LinkAdd'), '', ICON_SIZE_MEDIUM).'</a>';
+                $content .= '<a href="'.api_get_self().'?'.api_get_cidreq(
+                    ).'&action=addcategory&category_id='.$categoryId.'">'.
+                    Display::return_icon('new_folder.png', get_lang('CategoryAdd'), '', ICON_SIZE_MEDIUM).'</a>';
+            }
 
-            echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=list&show=all">';
-            echo Display::return_icon('forum_nestedview.png', get_lang('NestedView'), '', ICON_SIZE_MEDIUM).'</a>';
+            if (!empty($countCategories)) {
+                $content .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=list&show=none">';
+                $content .= Display::return_icon(
+                        'forum_listview.png',
+                        get_lang('FlatView'),
+                        '',
+                        ICON_SIZE_MEDIUM
+                    ).' </a>';
+
+                $content .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=list&show=all">';
+                $content .= Display::return_icon(
+                        'forum_nestedview.png',
+                        get_lang('NestedView'),
+                        '',
+                        ICON_SIZE_MEDIUM
+                    ).'</a>';
+            }
+
+            $content .= Display::url(
+                Display::return_icon('pdf.png', get_lang('ExportToPdf'), '', ICON_SIZE_MEDIUM),
+                api_get_self().'?'.api_get_cidreq().'&action=export'
+            );
+            $content .= '</div>';
         }
-        echo '</div>';
-        $linksPerCategory = self::showLinksPerCategory(0, $course_id, $session_id);
 
         if (empty($countCategories)) {
-            echo $linksPerCategory;
+            $content .= $linksPerCategory;
         } else {
             if (!empty($linksPerCategory)) {
-                echo Display::panel($linksPerCategory, get_lang('NoCategory'));
+                $content .= Display::panel($linksPerCategory, get_lang('NoCategory'));
             }
         }
 
@@ -1629,16 +1658,16 @@ class Link extends Model
                 $header .= '<a class="'.$visibilityClass.'" href="'.api_get_self().'?'.api_get_cidreq().'&category_id='.$myrow['id'].'">';
                 $header .= Display::return_icon('forum_listview.png');
             }
-
             $header .= Security::remove_XSS($myrow['category_title']).'</a>';
-            $header .= '<div class="pull-right">';
 
-            if (api_is_allowed_to_edit(null, true)) {
-                if ($session_id == $myrow['session_id']) {
-                    $header .= $strVisibility;
-                    $header .= self::showCategoryAdminTools($myrow, $counter, count($categories));
-                } else {
-                    $header .= get_lang('EditionNotAvailableFromSession');
+            if ($showActionLinks) {
+                if (api_is_allowed_to_edit(null, true)) {
+                    if ($session_id == $myrow['session_id']) {
+                        $header .= $strVisibility;
+                        $header .= self::showCategoryAdminTools($myrow, $counter, count($categories));
+                    } else {
+                        $header .= get_lang('EditionNotAvailableFromSession');
+                    }
                 }
             }
 
@@ -1651,10 +1680,12 @@ class Link extends Model
                 );
             }
 
-            echo Display::panel($myrow['description'].$childrenContent, $header);
+            $content .= Display::panel($myrow['description'].$childrenContent, $header);
 
             $counter++;
         }
+
+        return $content;
     }
 
     /**

+ 17 - 5
main/link/link.php

@@ -20,7 +20,6 @@
  */
 require_once __DIR__.'/../inc/global.inc.php';
 $current_course_tool = TOOL_LINK;
-
 $this_section = SECTION_COURSES;
 api_protect_course_script(true);
 
@@ -93,7 +92,7 @@ Event::event_access_tool(TOOL_LINK);
 $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']) : '';
+$categoryId = isset($_REQUEST['category_id']) ? (int) $_REQUEST['category_id'] : '';
 $linkListUrl = api_get_self().'?'.api_get_cidreq().'&category_id='.$categoryId.'&show='.$show;
 $content = '';
 $token = Security::get_existing_token();
@@ -212,11 +211,24 @@ switch ($action) {
         header('Location: '.$linkListUrl);
         exit;
         break;
+    case 'export':
+        $content = Link::listLinksAndCategories($course_id, $session_id, $categoryId, $show, null, false);
+        $pdf = new PDF();
+        $pdf->content_to_pdf(
+            $content,
+            null,
+            $courseInfo['code'].get_lang('Link'),
+            $courseInfo['code'],
+            'D',
+            false,
+            null,
+            false,
+            true
+        );
+        break;
     case 'list':
     default:
-        ob_start();
-        Link::listLinksAndCategories($course_id, $session_id, $categoryId, $show);
-        $content = ob_get_clean();
+        $content = Link::listLinksAndCategories($course_id, $session_id, $categoryId, $show);
         break;
 }