lp_subscribe_users_to_category.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\UserBundle\Entity\User;
  4. use Chamilo\CourseBundle\Entity\CLpCategory;
  5. use Chamilo\CourseBundle\Entity\CLpCategoryUser;
  6. use Chamilo\CoreBundle\Entity\Repository\CourseRepository;
  7. use Chamilo\CoreBundle\Entity\Repository\ItemPropertyRepository;
  8. require_once __DIR__.'/../inc/global.inc.php';
  9. api_protect_course_script();
  10. $is_allowed_to_edit = api_is_allowed_to_edit(false, true, false, false);
  11. if (!$is_allowed_to_edit) {
  12. api_not_allowed(true);
  13. }
  14. $categoryId = isset($_GET['id']) ? intval($_GET['id']) : 0;
  15. if (empty($categoryId)) {
  16. api_not_allowed(true);
  17. }
  18. $subscriptionSettings = learnpath::getSubscriptionSettings();
  19. if ($subscriptionSettings['allow_add_users_to_lp_category'] == false) {
  20. api_not_allowed(true);
  21. }
  22. $courseId = api_get_course_int_id();
  23. $courseCode = api_get_course_id();
  24. $em = Database::getManager();
  25. /** @var CLpCategory $category */
  26. $category = $em->getRepository('ChamiloCourseBundle:CLpCategory')->find($categoryId);
  27. if (!$category) {
  28. api_not_allowed(true);
  29. }
  30. $interbreadcrumb[] = array(
  31. 'url' => 'lp_controller.php?action=list&'.api_get_cidreq(),
  32. 'name' => get_lang('LearningPaths')
  33. );
  34. $interbreadcrumb[] = array('url' => '#', 'name' => strip_tags($category->getName()));
  35. $url = api_get_self().'?'.api_get_cidreq().'&action=add_users_to_category&id='.$categoryId;
  36. Display::addFlash(Display::return_message(get_lang('UserLpCategorySubscriptionDescription')));
  37. // Building the form for Groups
  38. $form = new FormValidator('lp_edit', 'post', $url);
  39. $form->addElement('hidden', 'group_form', 1);
  40. // Group list
  41. $groupList = \CourseManager::get_group_list_of_course(
  42. api_get_course_id(),
  43. api_get_session_id(),
  44. 1
  45. );
  46. $groupChoices = array_column($groupList, 'name', 'id');
  47. /** @var Session $session */
  48. $session = null;
  49. if (!empty($sessionId)) {
  50. $session = $em->getRepository('ChamiloCoreBundle:Session')->find($sessionId);
  51. }
  52. /** @var CourseRepository $courseRepo */
  53. $courseRepo = $em->getRepository('ChamiloCoreBundle:Course');
  54. /** @var ItemPropertyRepository $itemRepo */
  55. $itemRepo = $em->getRepository('ChamiloCourseBundle:CItemProperty');
  56. $course = $courseRepo->find($courseId);
  57. // Subscribed groups to a LP
  58. $subscribedGroupsInLp = $itemRepo->getGroupsSubscribedToItem(
  59. 'learnpath_category',
  60. $categoryId,
  61. $course,
  62. $session
  63. );
  64. $selectedGroupChoices = array();
  65. /** @var CItemProperty $itemProperty */
  66. foreach ($subscribedGroupsInLp as $itemProperty) {
  67. $selectedGroupChoices[] = $itemProperty->getGroup()->getId();
  68. }
  69. $groupMultiSelect = $form->addElement(
  70. 'advmultiselect',
  71. 'groups',
  72. get_lang('Groups'),
  73. $groupChoices
  74. );
  75. // submit button
  76. $form->addButtonSave(get_lang('Save'));
  77. $defaults = array();
  78. if (!empty($selectedGroupChoices)) {
  79. $defaults['groups'] = $selectedGroupChoices;
  80. }
  81. $form->setDefaults($defaults);
  82. // Getting subscribe users to the course.
  83. $subscribedUsers = $courseRepo->getSubscribedStudents($course);
  84. $subscribedUsers = $subscribedUsers->getQuery();
  85. $subscribedUsers = $subscribedUsers->execute();
  86. // Getting all users in a nice format.
  87. $choices = array();
  88. /** @var User $user */
  89. foreach ($subscribedUsers as $user) {
  90. $choices[$user->getUserId()] = $user->getCompleteNameWithClasses();
  91. }
  92. // Getting subscribed users to a category.
  93. $subscribedUsersInCategory = $category->getUsers();
  94. $selectedChoices = array();
  95. foreach ($subscribedUsersInCategory as $item) {
  96. $selectedChoices[] = $item->getUser()->getId();
  97. }
  98. // Building the form for Users
  99. $formUsers = new FormValidator('lp_edit', 'post', $url);
  100. $formUsers->addElement('hidden', 'user_form', 1);
  101. $userMultiSelect = $formUsers->addElement(
  102. 'advmultiselect',
  103. 'users',
  104. get_lang('Users'),
  105. $choices
  106. );
  107. $formUsers->addButtonSave(get_lang('Save'));
  108. $defaults = array();
  109. if (!empty($selectedChoices)) {
  110. $defaults['users'] = $selectedChoices;
  111. }
  112. $formUsers->setDefaults($defaults);
  113. // Building the form for Groups
  114. $tpl = new Template();
  115. $currentUser = api_get_user_entity(api_get_user_id());
  116. if ($formUsers->validate()) {
  117. $values = $formUsers->getSubmitValues();
  118. // Subscribing users
  119. $users = isset($values['users']) ? $values['users'] : [];
  120. $userForm = isset($values['user_form']) ? $values['user_form'] : [];
  121. if (!empty($userForm)) {
  122. $deleteUsers = [];
  123. if ($subscribedUsersInCategory) {
  124. /** @var CLpCategoryUser $user */
  125. foreach ($subscribedUsersInCategory as $user) {
  126. $userId = $user->getUser()->getId();
  127. if (!in_array($userId, $users)) {
  128. $category->removeUsers($user);
  129. }
  130. }
  131. }
  132. foreach ($users as $userId) {
  133. $categoryUser = new CLpCategoryUser();
  134. $user = UserManager::getRepository()->find($userId);
  135. $categoryUser->setUser($user);
  136. $category->addUser($categoryUser);
  137. }
  138. $em->merge($category);
  139. $em->flush();
  140. Display::addFlash(Display::return_message(get_lang('Updated')));
  141. }
  142. // Subscribing groups
  143. $groups = isset($values['groups']) ? $values['groups'] : [];
  144. $groupForm = isset($values['group_form']) ? $values['group_form'] : [];
  145. if (!empty($groupForm)) {
  146. $itemRepo->subscribeGroupsToItem(
  147. $currentUser,
  148. 'learnpath_category',
  149. $course,
  150. $session,
  151. $categoryId,
  152. $groups
  153. );
  154. Display::addFlash(Display::return_message(get_lang('Updated')));
  155. }
  156. header("Location: $url");
  157. exit;
  158. } else {
  159. $headers = [
  160. get_lang('SubscribeUsersToLpCategory'),
  161. get_lang('SubscribeGroupsToLpCategory')
  162. ];
  163. $tabs = Display::tabs($headers, [$formUsers->toHtml(), $form->toHtml()]);
  164. $tpl->assign('tabs', $tabs);
  165. }
  166. $layout = $tpl->get_template('learnpath/subscribe_users.tpl');
  167. $tpl->display($layout);