lp_subscribe_users_to_category.php 5.8 KB

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