From bcf58eccebc4a4e7091986a48bc29c510dbfb29a Mon Sep 17 00:00:00 2001 From: jmontoyaa Date: Tue, 29 Aug 2017 13:37:09 +0200 Subject: [PATCH] Add lp category to groups see BT#10882 --- main/lp/learnpath.class.php | 49 ++++++- main/lp/lp_subscribe_users.php | 58 +++++--- main/lp/lp_subscribe_users_to_category.php | 136 ++++++++++++++---- main/lp/lp_view.php | 19 ++- .../Repository/ItemPropertyRepository.php | 17 --- 5 files changed, 206 insertions(+), 73 deletions(-) diff --git a/main/lp/learnpath.class.php b/main/lp/learnpath.class.php index 79ea0e5bce..cae99428fa 100755 --- a/main/lp/learnpath.class.php +++ b/main/lp/learnpath.class.php @@ -13,6 +13,9 @@ use Chamilo\CourseBundle\Entity\CTool; use Chamilo\UserBundle\Entity\User; use Chamilo\CourseBundle\Entity\CLpItem; use Chamilo\CourseBundle\Entity\CLpItemView; +use Chamilo\CourseBundle\Entity\CItemProperty; +use Chamilo\CoreBundle\Entity\Repository\CourseRepository; +use Chamilo\CoreBundle\Entity\Repository\ItemPropertyRepository; /** * Class learnpath @@ -2346,14 +2349,14 @@ class learnpath $lp_id = (int) $lp_id; $sessionId = (int) $sessionId; - if (empty($sessionId)) { - $sessionId = api_get_session_id(); - } - if (empty($courseInfo)) { return false; } + if (empty($sessionId)) { + $sessionId = api_get_session_id(); + } + $itemInfo = api_get_item_property_info( $courseInfo['real_id'], TOOL_LEARNPATH, @@ -4422,6 +4425,44 @@ class learnpath return true; } + $groups = GroupManager::getAllGroupPerUserSubscription($user->getId()); + if (!empty($groups)) { + $em = Database::getManager(); + + /** @var ItemPropertyRepository $itemRepo */ + $itemRepo = $em->getRepository('ChamiloCourseBundle:CItemProperty'); + + /** @var CourseRepository $courseRepo */ + $courseRepo = $em->getRepository('ChamiloCoreBundle:Course'); + $sessionId = api_get_session_id(); + $session = null; + if (!empty($sessionId)) { + $session = $em->getRepository('ChamiloCoreBundle:Session')->find($sessionId); + } + + $course = $courseRepo->find(api_get_course_int_id()); + + // Subscribed groups to a LP + $subscribedGroupsInLp = $itemRepo->getGroupsSubscribedToItem( + 'learnpath_category', + $category->getId(), + $course, + $session + ); + + if (!empty($subscribedGroupsInLp)) { + $groups = array_column($groups, 'iid'); + /** @var CItemProperty $item */ + foreach ($subscribedGroupsInLp as $item) { + if ($item->getGroup() && + in_array($item->getGroup()->getId(), $groups) + ) { + return true; + } + } + } + } + return false; } diff --git a/main/lp/lp_subscribe_users.php b/main/lp/lp_subscribe_users.php index 28b60af6b3..f7e6da416d 100644 --- a/main/lp/lp_subscribe_users.php +++ b/main/lp/lp_subscribe_users.php @@ -2,6 +2,7 @@ /* For licensing terms, see /license.txt */ use Chamilo\CourseBundle\Entity\CItemProperty; +use Chamilo\CoreBundle\Entity\Repository\ItemPropertyRepository; use Chamilo\UserBundle\Entity\User; use Chamilo\CoreBundle\Entity\Repository\CourseRepository; use Chamilo\CoreBundle\Entity\Session; @@ -45,10 +46,12 @@ $courseCode = api_get_course_id(); $sessionId = api_get_session_id(); $url = api_get_self().'?'.api_get_cidreq().'&lp_id='.$lpId; -$lp = new \learnpath($courseCode, $lpId, api_get_user_id()); +$lp = new learnpath($courseCode, $lpId, api_get_user_id()); $em = Database::getManager(); /** @var CourseRepository $courseRepo */ $courseRepo = $em->getRepository('ChamiloCoreBundle:Course'); +/** @var ItemPropertyRepository $itemRepo */ +$itemRepo = $em->getRepository('ChamiloCourseBundle:CItemProperty'); /** @var Session $session */ $session = null; @@ -56,22 +59,21 @@ if (!empty($sessionId)) { $session = $em->getRepository('ChamiloCoreBundle:Session')->find($sessionId); } -// Find course. $course = $courseRepo->find($courseId); $subscribedUsers = []; // Getting subscribe users to the course. if (!$session) { - $subscribedUsers = $courseRepo - ->getSubscribedStudents($course) + $subscribedUsers = $courseRepo->getSubscribedStudents($course) ->getQuery() ->getResult(); } else { - $session - ->getUserCourseSubscriptionsByStatus($course, Session::STUDENT) - ->forAll(function ($i, SessionRelCourseRelUser $sessionCourseUser) use (&$subscribedUsers) { - $subscribedUsers[$i] = $sessionCourseUser->getUser(); - }); + $session->getUserCourseSubscriptionsByStatus($course, Session::STUDENT) + ->forAll( + function ($i, SessionRelCourseRelUser $sessionCourseUser) use (&$subscribedUsers) { + $subscribedUsers[$i] = $sessionCourseUser->getUser(); + } + ); } // Getting all users in a nice format. @@ -82,7 +84,7 @@ foreach ($subscribedUsers as $user) { } // Getting subscribed users to a LP. -$subscribedUsersInLp = $em->getRepository('ChamiloCourseBundle:CItemProperty')->getUsersSubscribedToItem( +$subscribedUsersInLp = $itemRepo->getUsersSubscribedToItem( 'learnpath', $lpId, $course, @@ -95,10 +97,15 @@ foreach ($subscribedUsersInLp as $itemProperty) { } //Building the form for Users -$formUsers = new \FormValidator('lp_edit', 'post', $url); +$formUsers = new FormValidator('lp_edit', 'post', $url); $formUsers->addElement('hidden', 'user_form', 1); -$userMultiSelect = $formUsers->addElement('advmultiselect', 'users', get_lang('Users'), $choices); +$userMultiSelect = $formUsers->addElement( + 'advmultiselect', + 'users', + get_lang('Users'), + $choices +); $formUsers->addButtonSave(get_lang('Save')); $defaults = array(); @@ -109,9 +116,8 @@ if (!empty($selectedChoices)) { $formUsers->setDefaults($defaults); -//Building the form for Groups - -$form = new \FormValidator('lp_edit', 'post', $url); +// Building the form for Groups +$form = new FormValidator('lp_edit', 'post', $url); $form->addElement('hidden', 'group_form', 1); // Group list @@ -123,7 +129,7 @@ $groupList = \CourseManager::get_group_list_of_course( $groupChoices = array_column($groupList, 'name', 'id'); // Subscribed groups to a LP -$subscribedGroupsInLp = $em->getRepository('ChamiloCourseBundle:CItemProperty')->getGroupsSubscribedToItem( +$subscribedGroupsInLp = $itemRepo->getGroupsSubscribedToItem( 'learnpath', $lpId, $course, @@ -136,11 +142,18 @@ foreach ($subscribedGroupsInLp as $itemProperty) { $selectedGroupChoices[] = $itemProperty->getGroup()->getId(); } -$groupMultiSelect = $form->addElement('advmultiselect', 'groups', get_lang('Groups'), $groupChoices); +$groupMultiSelect = $form->addElement( + 'advmultiselect', + 'groups', + get_lang('Groups'), + $groupChoices +); // submit button $form->addButtonSave(get_lang('Save')); +Display::addFlash(Display::return_message(get_lang('UserLpSubscriptionDescription'))); + $defaults = array(); if (!empty($selectedGroupChoices)) { $defaults['groups'] = $selectedGroupChoices; @@ -149,7 +162,7 @@ $form->setDefaults($defaults); $tpl = new Template(); -$currentUser = $em->getRepository('ChamiloUserBundle:User')->find(api_get_user_id()); +$currentUser = api_get_user_entity(api_get_user_id()); if ($form->validate()) { $values = $form->getSubmitValues(); @@ -159,7 +172,7 @@ if ($form->validate()) { $userForm = isset($values['user_form']) ? $values['user_form'] : []; if (!empty($userForm)) { - $em->getRepository('ChamiloCourseBundle:CItemProperty')->subscribeUsersToItem( + $itemRepo->subscribeUsersToItem( $currentUser, 'learnpath', $course, @@ -175,7 +188,7 @@ if ($form->validate()) { $groupForm = isset($values['group_form']) ? $values['group_form'] : []; if (!empty($groupForm)) { - $em->getRepository('ChamiloCourseBundle:CItemProperty')->subscribeGroupsToItem( + $itemRepo->subscribeGroupsToItem( $currentUser, 'learnpath', $course, @@ -189,7 +202,10 @@ if ($form->validate()) { header("Location: $url"); exit; } else { - $headers = [get_lang('SubscribeUsersToLp'), get_lang('SubscribeGroupsToLp')]; + $headers = [ + get_lang('SubscribeUsersToLp'), + get_lang('SubscribeGroupsToLp') + ]; $tabs = Display::tabs($headers, [$formUsers->toHtml(), $form->toHtml()]); $tpl->assign('tabs', $tabs); } diff --git a/main/lp/lp_subscribe_users_to_category.php b/main/lp/lp_subscribe_users_to_category.php index fd9a2642e2..d3bded5f18 100644 --- a/main/lp/lp_subscribe_users_to_category.php +++ b/main/lp/lp_subscribe_users_to_category.php @@ -4,6 +4,9 @@ use Chamilo\UserBundle\Entity\User; use Chamilo\CourseBundle\Entity\CLpCategory; use Chamilo\CourseBundle\Entity\CLpCategoryUser; +use Chamilo\CoreBundle\Entity\Repository\CourseRepository; +use Chamilo\CoreBundle\Entity\Repository\ItemPropertyRepository; + require_once __DIR__.'/../inc/global.inc.php'; api_protect_course_script(); @@ -30,7 +33,7 @@ $courseCode = api_get_course_id(); $em = Database::getManager(); -/** @var \Chamilo\CourseBundle\Entity\CLpCategory $category */ +/** @var CLpCategory $category */ $category = $em->getRepository('ChamiloCourseBundle:CLpCategory')->find($categoryId); if (!$category) { @@ -43,11 +46,67 @@ $interbreadcrumb[] = array( ); $interbreadcrumb[] = array('url' => '#', 'name' => strip_tags($category->getName())); -// Find course. -$course = $em->getRepository('ChamiloCoreBundle:Course')->find($courseId); +$url = api_get_self().'?'.api_get_cidreq().'&action=add_users_to_category&id='.$categoryId; + +Display::addFlash(Display::return_message(get_lang('UserLpCategorySubscriptionDescription'))); + +// Building the form for Groups +$form = new FormValidator('lp_edit', 'post', $url); +$form->addElement('hidden', 'group_form', 1); + +// Group list +$groupList = \CourseManager::get_group_list_of_course( + api_get_course_id(), + api_get_session_id(), + 1 +); +$groupChoices = array_column($groupList, 'name', 'id'); + +/** @var Session $session */ +$session = null; +if (!empty($sessionId)) { + $session = $em->getRepository('ChamiloCoreBundle:Session')->find($sessionId); +} + +/** @var CourseRepository $courseRepo */ +$courseRepo = $em->getRepository('ChamiloCoreBundle:Course'); +/** @var ItemPropertyRepository $itemRepo */ +$itemRepo = $em->getRepository('ChamiloCourseBundle:CItemProperty'); + +$course = $courseRepo->find($courseId); + +// Subscribed groups to a LP +$subscribedGroupsInLp = $itemRepo->getGroupsSubscribedToItem( + 'learnpath_category', + $categoryId, + $course, + $session +); + +$selectedGroupChoices = array(); +/** @var CItemProperty $itemProperty */ +foreach ($subscribedGroupsInLp as $itemProperty) { + $selectedGroupChoices[] = $itemProperty->getGroup()->getId(); +} + +$groupMultiSelect = $form->addElement( + 'advmultiselect', + 'groups', + get_lang('Groups'), + $groupChoices +); + +// submit button +$form->addButtonSave(get_lang('Save')); + +$defaults = array(); +if (!empty($selectedGroupChoices)) { + $defaults['groups'] = $selectedGroupChoices; +} +$form->setDefaults($defaults); // Getting subscribe users to the course. -$subscribedUsers = $em->getRepository('ChamiloCoreBundle:Course')->getSubscribedStudents($course); +$subscribedUsers = $courseRepo->getSubscribedStudents($course); $subscribedUsers = $subscribedUsers->getQuery(); $subscribedUsers = $subscribedUsers->execute(); @@ -66,10 +125,8 @@ foreach ($subscribedUsersInCategory as $item) { $selectedChoices[] = $item->getUser()->getId(); } -$url = api_get_self().'?'.api_get_cidreq().'&action=add_users_to_category&id='.$categoryId; - -//Building the form for Users -$formUsers = new \FormValidator('lp_edit', 'post', $url); +// Building the form for Users +$formUsers = new FormValidator('lp_edit', 'post', $url); $formUsers->addElement('hidden', 'user_form', 1); $userMultiSelect = $formUsers->addElement( @@ -91,42 +148,63 @@ $formUsers->setDefaults($defaults); // Building the form for Groups $tpl = new Template(); -$currentUser = $em->getRepository('ChamiloUserBundle:User')->find(api_get_user_id()); +$currentUser = api_get_user_entity(api_get_user_id()); if ($formUsers->validate()) { $values = $formUsers->getSubmitValues(); // Subscribing users $users = isset($values['users']) ? $values['users'] : []; - - $deleteUsers = []; - if ($subscribedUsersInCategory) { - /** @var CLpCategoryUser $user */ - foreach ($subscribedUsersInCategory as $user) { - $userId = $user->getUser()->getId(); - - if (!in_array($userId, $users)) { - $category->removeUsers($user); + $userForm = isset($values['user_form']) ? $values['user_form'] : []; + if (!empty($userForm)) { + $deleteUsers = []; + if ($subscribedUsersInCategory) { + /** @var CLpCategoryUser $user */ + foreach ($subscribedUsersInCategory as $user) { + $userId = $user->getUser()->getId(); + + if (!in_array($userId, $users)) { + $category->removeUsers($user); + } } } - } - foreach ($users as $userId) { - $categoryUser = new CLpCategoryUser(); - $user = UserManager::getRepository()->find($userId); - $categoryUser->setUser($user); - $category->addUser($categoryUser); + foreach ($users as $userId) { + $categoryUser = new CLpCategoryUser(); + $user = UserManager::getRepository()->find($userId); + $categoryUser->setUser($user); + $category->addUser($categoryUser); + } + + $em->merge($category); + $em->flush(); + Display::addFlash(Display::return_message(get_lang('Updated'))); } - $em->merge($category); - $em->flush(); - Display::addFlash(Display::return_message(get_lang('Updated'))); + // Subscribing groups + $groups = isset($values['groups']) ? $values['groups'] : []; + $groupForm = isset($values['group_form']) ? $values['group_form'] : []; + + if (!empty($groupForm)) { + $itemRepo->subscribeGroupsToItem( + $currentUser, + 'learnpath_category', + $course, + $session, + $categoryId, + $groups + ); + Display::addFlash(Display::return_message(get_lang('Updated'))); + } header("Location: $url"); exit; } else { - $headers = [get_lang('SubscribeUsersToLp'), get_lang('SubscribeGroupsToLp')]; - $tabs = $formUsers->toHtml(); + $headers = [ + get_lang('SubscribeUsersToLpCategory'), + get_lang('SubscribeGroupsToLpCategory') + ]; + $tabs = Display::tabs($headers, [$formUsers->toHtml(), $form->toHtml()]); $tpl->assign('tabs', $tabs); } diff --git a/main/lp/lp_view.php b/main/lp/lp_view.php index 48849fc9fa..596f7a35d2 100755 --- a/main/lp/lp_view.php +++ b/main/lp/lp_view.php @@ -2,6 +2,7 @@ /* For licensing terms, see /license.txt */ use ChamiloSession as Session; +use Chamilo\CourseBundle\Entity\CLpCategory; /** * This file was originally the copy of document.php, but many modifications happened since then ; @@ -81,16 +82,30 @@ if (!$is_allowed_to_edit) { $categoryId = $_SESSION['oLP']->getCategoryId(); $em = Database::getManager(); if (!empty($categoryId)) { - /** @var \Chamilo\CourseBundle\Entity\CLpCategory $category */ + /** @var CLpCategory $category */ $category = $em->getRepository('ChamiloCourseBundle:CLpCategory')->find($categoryId); + $block = false; if ($category) { $users = $category->getUsers(); if (!empty($users) && $users->count() > 0) { $user = UserManager::getRepository()->find($user_id); if (!$category->hasUserAdded($user)) { - api_not_allowed(true); + $block = true; } } + + $isVisible = learnpath::categoryIsVisibleForStudent( + $category, + $user + ); + + if ($isVisible) { + $block = false; + } + + if ($block) { + api_not_allowed(true); + } } } } diff --git a/src/Chamilo/CoreBundle/Entity/Repository/ItemPropertyRepository.php b/src/Chamilo/CoreBundle/Entity/Repository/ItemPropertyRepository.php index 9852478aff..8d16b9bd4d 100644 --- a/src/Chamilo/CoreBundle/Entity/Repository/ItemPropertyRepository.php +++ b/src/Chamilo/CoreBundle/Entity/Repository/ItemPropertyRepository.php @@ -138,23 +138,6 @@ class ItemPropertyRepository extends EntityRepository $item->setVisibility('1'); $em->persist($item); //$em is an instance of EntityManager } - - //Adding users from this group to the item - /*$users = \GroupManager::getStudentsAndTutors($groupId); - $newUserList = array(); - if (!empty($users)) { - foreach ($users as $user) { - $newUserList[] = $user['user_id']; - } - $this->subscribeUsersToItem( - $currentUser, - 'learnpath', - $course, - $session, - $itemId, - $newUserList - ); - }*/ } $em->flush();