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