Browse Source

Allow attach a forum to LP item - refs #7972

Angel Fernando Quiroz Campos 9 years ago
parent
commit
9a3d190be9

+ 8 - 0
app/Resources/public/css/base.css

@@ -1742,6 +1742,14 @@ div.admin_section h4 {
     /* float:left;*/
 }
 
+.item_data .button_actions {
+    display: none;
+    margin: 5px 0px;
+}
+.item_data:hover .button_actions {
+    display: block;
+}
+
 #lp_item_list .item {
     border:none;
 }

+ 56 - 0
main/forum/forumfunction.inc.php

@@ -714,6 +714,7 @@ function store_forum($values, $courseInfo = array(), $returnId = false)
             'forum_group_public_private'=> isset($values['public_private_group_forum_group']['public_private_group_forum']) ? $values['public_private_group_forum_group']['public_private_group_forum'] : null,
             'forum_order'=> isset($new_max) ? $new_max : null,
             'session_id'=> $session_id,
+            'lp_id' => isset($values['lp_id']) ? intval($values['lp_id']) : null
         ];
 
         Database::update(
@@ -753,6 +754,7 @@ function store_forum($values, $courseInfo = array(), $returnId = false)
             'forum_group_public_private'=> isset($values['public_private_group_forum_group']['public_private_group_forum']) ? $values['public_private_group_forum_group']['public_private_group_forum'] : null,
             'forum_order'=> isset($new_max) ? $new_max : null,
             'session_id'=> $session_id,
+            'lp_id' => isset($values['lp_id']) ? intval($values['lp_id']) : null
         ];
         $last_id = Database::insert($table_forums, $params);
         if ($last_id > 0) {
@@ -2372,6 +2374,7 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
                 'thread_weight' => isset($values['weight_calification']) ? $values['weight_calification'] : '',
                 'thread_peer_qualify' => isset($values['thread_peer_qualify']) ? $values['thread_peer_qualify'] : '',
                 'session_id' => api_get_session_id(),
+                'lp_item_id' => isset($values['lp_item_id']) ? intval($values['lp_item_id']) : null
             ]
         );
 
@@ -5669,3 +5672,56 @@ function getAttachmentIdsByPostId($postId, $courseId = null)
     }
     return $array;
 }
+
+/**
+ * Check if the forum category exists looking for its title
+ * @param string $title The forum category title
+ * @param int $courseId The course ID
+ * @param int $sessionId Optional. The session ID
+ * @return boolean
+ */
+function getForumCategoryByTitle($title, $courseId, $sessionId = 0)
+{
+    $sessionId = intval($sessionId);
+
+    $forumCategoryTable = Database::get_course_table(TABLE_FORUM_CATEGORY);
+    $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
+
+    $fakeFrom = "$forumCategoryTable fc
+        INNER JOIN $itemProperty ip ";
+
+    if ($sessionId === 0) {
+        $fakeFrom .= "
+            ON (
+                fc.cat_id = ip.ref AND fc.c_id = ip.c_id AND (fc.session_id = ip.session_id OR ip.session_id IS NULL)
+            )
+        ";
+    } else {
+        $fakeFrom .= "
+            ON (
+                fc.cat_id = ip.ref AND fc.c_id = ip.c_id AND fc.session_id = ip.session_id
+            )
+        ";
+    }
+
+    $resultData = Database::select(
+        'fc.*',
+        $fakeFrom,
+        [
+            'where' => [
+                'ip.visibility != ? AND ' => 2,
+                'ip.tool = ? AND ' => TOOL_FORUM_CATEGORY,
+                'fc.session_id = ? AND ' => $sessionId,
+                'fc.cat_title = ? AND ' => $title,
+                'fc.c_id = ?' => intval($courseId)
+            ]
+        ],
+        'first'
+    );
+
+    if (empty($resultData)) {
+        return false;
+    }
+
+    return $resultData;
+}

+ 7 - 2
main/inc/lib/display.lib.php

@@ -2135,12 +2135,17 @@ class Display
         $url,
         $icon = 'check',
         $type = 'default',
-        array $attributes = []
+        array $attributes = [],
+        $includeText = true
     ) {
         $buttonClass = "btn btn-$type";
-        $icon = self::tag('i', null, ['class' => "fa fa-$icon"]);
+        $icon = self::tag('i', null, ['class' => "fa fa-$icon fa-fw", 'aria-hidden' => 'true']);
         $attributes['class'] = isset($attributes['class']) ? "$buttonClass {$attributes['class']}" : $buttonClass;
 
+        if (!$includeText) {
+            $text = '<span class="sr-only">' . $text . '</span>';
+        }
+
         return self::url("$icon $text", $url, $attributes);
     }
 

+ 119 - 7
main/newscorm/learnpath.class.php

@@ -5600,6 +5600,7 @@ class learnpath
             $delete_icon = '';
             $audio_icon = '';
             $prerequisities_icon = '';
+            $forumIcon = '';
 
             if ($is_allowed_to_edit) {
                 if (!$update_audio || $update_audio <> 'true') {
@@ -5611,30 +5612,62 @@ class learnpath
                 // No edit for this item types
                 if (!in_array($arrLP[$i]['item_type'], array('sco', 'asset'))) {
                     if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module'))) {
-                        $edit_icon .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=edit_item&view=build&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id . '&path_item=' . $arrLP[$i]['path'] . '">';
+                        $edit_icon .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=edit_item&view=build&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id . '&path_item=' . $arrLP[$i]['path'] . '" class="btn btn-default">';
                         $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
                         $edit_icon .= '</a>';
+
+                        if ($arrLP[$i]['item_type'] != 'forum') {
+                            if (
+                                $this->items[$arrLP[$i]['id']]->getForumThread(
+                                    $this->course_int_id,
+                                    $this->lp_session_id
+                                )
+                            ) {
+                                $forumIcon = Display::url(
+                                    Display::return_icon('forum.png', get_lang('CreateForum'), [], ICON_SIZE_TINY),
+                                    '#',
+                                    ['class' => 'btn btn-default disabled']
+                                );
+                            } else {
+                                $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([
+                                    'action' => 'create_forum',
+                                    'id' => $arrLP[$i]['id'],
+                                    'lp_id' => $this->lp_id
+                                ]);
+                                $forumIcon = Display::url(
+                                    Display::return_icon('forum.png', get_lang('CreateForum'), [], ICON_SIZE_TINY),
+                                    $forumIconUrl,
+                                    ['class' => "btn btn-default"]
+                                );
+                            }
+                        }
                     } else {
-                        $edit_icon .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=edit_item&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id . '&path_item=' . $arrLP[$i]['path'] . '">';
+                        $edit_icon .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=edit_item&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id . '&path_item=' . $arrLP[$i]['path'] . '" class="btn btn-default">';
                         $edit_icon .= Display::return_icon('edit.png', get_lang('LearnpathEditModule'), array(), ICON_SIZE_TINY);
                         $edit_icon .= '</a>';
                     }
                 }
 
-                $delete_icon .= ' <a href="'.api_get_self().'?'.api_get_cidreq().'&action=delete_item&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id . '" onClick="return confirmation(\'' . addslashes($title) . '\');">';
+                $delete_icon .= ' <a href="'.api_get_self().'?'.api_get_cidreq().'&action=delete_item&id=' . $arrLP[$i]['id'] . '&lp_id=' . $this->lp_id . '" onClick="return confirmation(\'' . addslashes($title) . '\');" class="btn btn-default">';
                 $delete_icon .= Display::return_icon('delete.png', get_lang('LearnpathDeleteModule'), array(), ICON_SIZE_TINY);
                 $delete_icon .= '</a>';
 
                 $url = api_get_self() . '?'.api_get_cidreq().'&view=build&id='.$arrLP[$i]['id'] .'&lp_id='.$this->lp_id;
 
                 if (!in_array($arrLP[$i]['item_type'], array('dokeos_chapter', 'dokeos_module', 'dir'))) {
-                    $prerequisities_icon = Display::url(Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY), $url.'&action=edit_item_prereq');
-                    $move_item_icon = Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY), $url.'&action=move_item');
-                    $audio_icon = Display::url(Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY), $url.'&action=add_audio');
+                    $prerequisities_icon = Display::url(Display::return_icon('accept.png', get_lang('LearnpathPrerequisites'), array(), ICON_SIZE_TINY), $url.'&action=edit_item_prereq', ['class' => 'btn btn-default']);
+                    $move_item_icon = Display::url(Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_TINY), $url.'&action=move_item', ['class' => 'btn btn-default']);
+                    $audio_icon = Display::url(Display::return_icon('audio.png', get_lang('UplUpload'), array(), ICON_SIZE_TINY), $url.'&action=add_audio', ['class' => 'btn btn-default']);
                 }
             }
             if ($update_audio != 'true') {
-                $row = $move_icon.' '.$icon.Display::span($title_cut).Display::span($audio.$edit_icon.$prerequisities_icon.$move_item_icon.$audio_icon.$delete_icon, array('class'=>'button_actions'));
+                $row = $move_icon . ' ' . $icon .
+                    Display::span($title_cut) . 
+                    Display::tag(
+                        'div',
+                        "<div class=\"btn-group btn-group-xs\">$audio $edit_icon $forumIcon $prerequisities_icon $move_item_icon $audio_icon $delete_icon</div>",
+                        array('class'=>'btn-toolbar button_actions')
+                    );
             } else {
                 $row = Display::span($title.$icon).Display::span($audio, array('class'=>'button_actions'));
             }
@@ -10440,6 +10473,85 @@ EOD;
 
         return $src;
     }
+
+    /**
+     * Get the forum for this learning path
+     * @return boolean
+     */
+    public function getForum($sessionId = 0)
+    {
+        $forumTable = Database::get_course_table(TABLE_FORUM);
+        $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
+
+        $fakeFrom = "$forumTable f
+            INNER JOIN $itemProperty ip ";
+
+        if ($this->lp_session_id == 0) {
+            $fakeFrom .= "
+                ON (
+                    f.forum_id = ip.ref AND f.c_id = ip.c_id AND (
+                        f.session_id = ip.session_id OR ip.session_id IS NULL
+                    )
+                )
+            ";
+        } else {
+            $fakeFrom .= "
+                ON (
+                    f.forum_id = ip.ref AND f.c_id = ip.c_id AND f.session_id = ip.session_id
+                )
+            ";
+        }
+
+        $resultData = Database::select(
+            'f.*',
+            $fakeFrom,
+            [
+                'where' => [
+                    'ip.visibility != ? AND ' => 2,
+                    'ip.tool = ? AND ' => TOOL_FORUM,
+                    'f.session_id = ? AND ' => $sessionId,
+                    'f.c_id = ? AND ' => intval($this->course_int_id),
+                    'f.lp_id = ?' => intval($this->lp_id)
+                ]
+            ],
+            'first'
+        );
+
+        if (empty($resultData)) {
+            return false;
+        }
+
+        return $resultData;
+    }
+
+    /**
+     * Create a forum for this learning path
+     * @param type $forumCategoryId
+     * @return int The forum ID if was created. Otherwise return false
+     */
+    public function createForum($forumCategoryId)
+    {
+        require_once api_get_path(SYS_CODE_PATH) . '/forum/forumfunction.inc.php';
+
+        $forumId = store_forum(
+            [
+                'lp_id' => $this->lp_id,
+                'forum_title' => $this->name,
+                'forum_comment' => null,
+                'forum_category' => intval($forumCategoryId),
+                'students_can_edit_group' => ['students_can_edit' => 0],
+                'allow_new_threads_group' => ['allow_new_threads' => 0],
+                'default_view_type_group' => ['default_view_type' => 'flat'],
+                'group_forum' => 0,
+                'public_private_group_forum_group' => ['public_private_group_forum' => 'public']
+            ],
+            [],
+            true
+        );
+
+        return $forumId;
+    }
+
 }
 
 if (!function_exists('trim_value')) {

+ 86 - 0
main/newscorm/learnpathItem.class.php

@@ -4412,4 +4412,90 @@ class learnpathItem
     {
         $this->prerequisiteMinScore = $prerequisiteMinScore;
     }
+
+    /**
+     * Get the forum thread info
+     * @param int $lpCourseId The course ID from the learning path
+     * @param int $lpSessionId Optional. The session ID from the learning path
+     * @return boolean
+     */
+    public function getForumThread($lpCourseId, $lpSessionId = 0)
+    {
+        $lpSessionId = intval($lpSessionId);
+        $forumThreadTable = Database::get_course_table(TABLE_FORUM_THREAD);
+        $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
+
+        $fakeFrom = "$forumThreadTable ft
+            INNER JOIN $itemProperty ip ";
+
+        if ($lpSessionId == 0) {
+            $fakeFrom .= "
+                ON (
+                    ft.thread_id = ip.ref AND ft.c_id = ip.c_id AND (
+                        ft.session_id = ip.session_id OR ip.session_id IS NULL
+                    )
+                )
+            ";
+        } else {
+            $fakeFrom .= "
+                ON (
+                    ft.thread_id = ip.ref AND ft.c_id = ip.c_id AND ft.session_id = ip.session_id
+                )
+            ";
+        }
+
+        $resultData = Database::select(
+            'ft.*',
+            $fakeFrom,
+            [
+                'where' => [
+                    'ip.visibility != ? AND ' => 2,
+                    'ip.tool = ? AND ' => TOOL_FORUM_THREAD,
+                    'ft.session_id = ? AND ' => $lpSessionId,
+                    'ft.c_id = ? AND ' => intval($lpCourseId),
+                    'ft.lp_item_id = ?' => intval($this->db_id)
+                ]
+            ],
+            'first'
+        );
+
+        if (empty($resultData)) {
+            return false;
+        }
+
+        return $resultData;
+    }
+
+    /**
+     * Create a forum thread for this learning path item
+     * @param int $currentForumId The forum ID to add the new thread
+     * @return int The forum thread if was created. Otherwise return false
+     */
+    public function createForumTthread($currentForumId)
+    {
+        require_once api_get_path(SYS_CODE_PATH) . '/forum/forumfunction.inc.php';
+
+        $forumInfo = get_forum_information($currentForumId);
+
+        $threadId = store_thread(
+            $forumInfo,
+            [
+                'forum_id' => intval($currentForumId),
+                'thread_id' => 0,
+                'gradebook' => 0,
+                'post_title' => $this->name,
+                'post_text' => $this->description,
+                'category_id' => 1,
+                'numeric_calification' => 0,
+                'calification_notebook_title' => 0,
+                'weight_calification' => 0.00,
+                'thread_peer_qualify' => 0,
+                'lp_item_id' => $this->db_id
+            ],
+            [],
+            false
+        );
+
+        return $threadId;
+    }
 }

+ 67 - 11
main/newscorm/lp_controller.php

@@ -134,17 +134,6 @@ $htmlHeadXtra[] = '
     }
 
     $(function() {
-
-        $(".item_data").on("mouseover", function(event) {
-            $(".button_actions", this).show();
-        });
-
-        $(".item_data").on("mouseout", function() {
-            $(".button_actions",this).hide();
-        });
-
-        $(".button_actions").hide();
-
         $(".lp_resource").sortable({
             items: ".lp_resource_element ",
             handle: ".moved", //only the class "moved"
@@ -1280,6 +1269,73 @@ switch ($action) {
         $_SESSION['refresh'] = 1;
         $_SESSION['oLP']->set_seriousgame_mode();
         require 'lp_list.php';
+        break;
+    case 'create_forum':
+        if (!isset($_GET['id'])) {
+            break;
+        }
+
+        $selectedItem = null;
+
+        foreach ($_SESSION['oLP']->items as $item) {
+            if ($item->db_id == $_GET['id']) {
+                $selectedItem = $item;
+            }
+        }
+
+        if (!empty($selectedItem)) {
+            $forumThread = $selectedItem->getForumThread(
+                $_SESSION['oLP']->course_int_id,
+                $_SESSION['oLP']->lp_session_id
+            );
+
+            if (empty($forumThread)) {
+                require '../forum/forumfunction.inc.php';
+
+                $forumCategory = getForumCategoryByTitle(
+                    get_lang('LearningPaths'),
+                    $_SESSION['oLP']->course_int_id,
+                    $_SESSION['oLP']->lp_session_id
+                );
+
+                $forumCategoryId = !empty($forumCategory) ? $forumCategory['cat_id']: 0;
+
+                if (empty($forumCategoryId)) {
+                    $forumCategoryId = store_forumcategory(
+                        [
+                            'lp_id' => 0,
+                            'forum_category_title' => get_lang('LearningPaths'),
+                            'forum_category_comment' => null
+                        ],
+                        [],
+                        false
+                    );
+                }
+
+                if (!empty($forumCategoryId)) {
+                    $forum = $_SESSION['oLP']->getForum(
+                        $_SESSION['oLP']->lp_session_id
+                    );
+
+                    $forumId = !empty($forum) ? $forum['forum_id'] : 0;
+
+                    if (empty($forumId)) {
+                        $forumId = $_SESSION['oLP']->createForum($forumCategoryId);
+                    }
+
+                    if (!empty($forumId)) {
+                        $selectedItem->createForumTthread($forumId);
+                    }
+                }
+            }
+        }
+
+        header('Location:' . api_get_path(WEB_PATH) . api_get_self() . '?' . http_build_query([
+            'action' => 'add_item',
+            'type' => 'step',
+            'lp_id' => $_SESSION['oLP']->lp_id
+        ]));
+
         break;
     case 'report':
         require 'lp_report.php';