From 83de577b2616966a15fafe5b3b279427c29a17d7 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Tue, 22 Dec 2015 19:12:41 -0500 Subject: [PATCH] Allow dissociate a forum to this LP item - refs BT#10629 --- main/newscorm/learnpath.class.php | 9 ++- main/newscorm/learnpathItem.class.php | 85 ++++++++++++++++++++------- main/newscorm/lp_controller.php | 38 ++++++++++++ 3 files changed, 110 insertions(+), 22 deletions(-) diff --git a/main/newscorm/learnpath.class.php b/main/newscorm/learnpath.class.php index be7e57d153..5b85a510fa 100755 --- a/main/newscorm/learnpath.class.php +++ b/main/newscorm/learnpath.class.php @@ -5641,10 +5641,15 @@ class learnpath $this->lp_session_id ) ) { + $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([ + 'action' => 'dissociate_forum', + 'id' => $arrLP[$i]['id'], + 'lp_id' => $this->lp_id + ]); $forumIcon = Display::url( Display::return_icon('forum.png', get_lang('DissociateForumToLPItem'), [], ICON_SIZE_TINY), - '#', - ['class' => 'btn btn-default disabled lp-btn-dissociate-forum'] + $forumIconUrl, + ['class' => 'btn btn-default lp-btn-dissociate-forum'] ); } else { $forumIconUrl = api_get_self() . '?' . api_get_cidreq() . '&' . http_build_query([ diff --git a/main/newscorm/learnpathItem.class.php b/main/newscorm/learnpathItem.class.php index 8845cfee44..89c480fef0 100755 --- a/main/newscorm/learnpathItem.class.php +++ b/main/newscorm/learnpathItem.class.php @@ -4490,7 +4490,11 @@ class learnpathItem '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) + '(ft.lp_item_id = ? OR (ft.thread_title = ? AND ft.lp_item_id = ?))' => [ + intval($this->db_id), + "{$this->title}-{$this->db_id}", + intval($this->db_id) + ] ] ], 'first' @@ -4512,27 +4516,68 @@ class learnpathItem { require_once api_get_path(SYS_CODE_PATH) . '/forum/forumfunction.inc.php'; - $forumInfo = get_forum_information($currentForumId); + $em = Database::getManager(); + $threadRepo = $em->getRepository('ChamiloCourseBundle:CForumThread'); + $forumThread = $threadRepo->findOneBy([ + 'threadTitle' => "{$this->title}-{$this->db_id}", + 'forumId' => intval($currentForumId) + ]); + + if (!$forumThread) { + $forumInfo = get_forum_information($currentForumId); + + 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 + ); - $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; + } + + $forumThread->setLpItemId($this->db_id); + + $em->persist($forumThread); + $em->flush(); + } + + /** + * Allow dissociate a forum to this LP item + * @param int $threadIid The thread id + * @return boolean + */ + public function dissociateForumThread($threadIid) + { + $threadIid = intval($threadIid); + $em = Database::getManager(); + + $forumThread = $em->find('ChamiloCourseBundle:CForumThread', $threadIid); + + if (!$forumThread) { + return false; + } + + $forumThread->setThreadTitle( + "{$this->get_title()}-{$this->db_id}" ); + $forumThread->setLpItemId(0); - return $threadId; + $em->persist($forumThread); + $em->flush(); + + return true; } } diff --git a/main/newscorm/lp_controller.php b/main/newscorm/lp_controller.php index ff4e20a5c7..18b0fb9d95 100755 --- a/main/newscorm/lp_controller.php +++ b/main/newscorm/lp_controller.php @@ -1340,6 +1340,44 @@ switch ($action) { case 'report': require 'lp_report.php'; break; + case 'dissociate_forum': + if (!isset($_GET['id'])) { + break; + } + + $selectedItem = null; + + foreach ($_SESSION['oLP']->items as $item) { + if ($item->db_id != $_GET['id']) { + continue; + } + + $selectedItem = $item; + } + + if (!empty($selectedItem)) { + $forumThread = $selectedItem->getForumThread( + $_SESSION['oLP']->course_int_id, + $_SESSION['oLP']->lp_session_id + ); + + if (!empty($forumThread)) { + $dissoaciated = $selectedItem->dissociateForumThread($forumThread['iid']); + + if ($dissoaciated) { + Display::addFlash( + Display::return_message(get_lang('ForumDissociate'), 'success') + ); + } + } + } + + 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; default: if ($debug > 0) error_log('New LP - default action triggered', 0); require 'lp_list.php';