lp_subscribe_users.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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']) ? (int) $_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->getNameNoTags(),
  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. $list = $session->getUserCourseSubscriptionsByStatus($course, Session::STUDENT);
  56. if ($list) {
  57. /** @var SessionRelCourseRelUser $sessionCourseUser */
  58. foreach ($list as $sessionCourseUser) {
  59. $subscribedUsers[$sessionCourseUser->getUser()->getUserId()] = $sessionCourseUser->getUser();
  60. }
  61. }
  62. }
  63. // Getting all users in a nice format.
  64. $choices = [];
  65. /** @var User $user */
  66. foreach ($subscribedUsers as $user) {
  67. $choices[$user->getUserId()] = $user->getCompleteNameWithClasses();
  68. }
  69. // Getting subscribed users to a LP.
  70. $subscribedUsersInLp = $itemRepo->getUsersSubscribedToItem(
  71. 'learnpath',
  72. $lpId,
  73. $course,
  74. $session
  75. );
  76. $selectedChoices = [];
  77. foreach ($subscribedUsersInLp as $itemProperty) {
  78. $selectedChoices[] = $itemProperty->getToUser()->getId();
  79. }
  80. //Building the form for Users
  81. $formUsers = new FormValidator('lp_edit', 'post', $url);
  82. $formUsers->addElement('hidden', 'user_form', 1);
  83. $userMultiSelect = $formUsers->addElement(
  84. 'advmultiselect',
  85. 'users',
  86. get_lang('Users'),
  87. $choices
  88. );
  89. $formUsers->addButtonSave(get_lang('Save'));
  90. $defaults = [];
  91. if (!empty($selectedChoices)) {
  92. $defaults['users'] = $selectedChoices;
  93. }
  94. $formUsers->setDefaults($defaults);
  95. // Building the form for Groups
  96. $form = new FormValidator('lp_edit', 'post', $url);
  97. $form->addElement('hidden', 'group_form', 1);
  98. // Group list
  99. $groupList = \CourseManager::get_group_list_of_course(
  100. api_get_course_id(),
  101. api_get_session_id(),
  102. 1
  103. );
  104. $groupChoices = array_column($groupList, 'name', 'id');
  105. // Subscribed groups to a LP
  106. $subscribedGroupsInLp = $itemRepo->getGroupsSubscribedToItem(
  107. 'learnpath',
  108. $lpId,
  109. $course,
  110. $session
  111. );
  112. $selectedGroupChoices = [];
  113. /** @var CItemProperty $itemProperty */
  114. foreach ($subscribedGroupsInLp as $itemProperty) {
  115. $selectedGroupChoices[] = $itemProperty->getGroup()->getId();
  116. }
  117. $groupMultiSelect = $form->addElement(
  118. 'advmultiselect',
  119. 'groups',
  120. get_lang('Groups'),
  121. $groupChoices
  122. );
  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. }
  163. $message = Display::return_message(get_lang('UserLpSubscriptionDescription'));
  164. $headers = [
  165. get_lang('SubscribeUsersToLp'),
  166. get_lang('SubscribeGroupsToLp'),
  167. ];
  168. $menu = $oLP->build_action_menu(true, false, true, false);
  169. $tpl = new Template();
  170. $tabs = Display::tabs($headers, [$formUsers->toHtml(), $form->toHtml()]);
  171. $tpl->assign('content', $menu.$message.$tabs);
  172. $tpl->display_one_col_template();