lp_subscribe_users.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CItemProperty;
  4. use Chamilo\CoreBundle\Entity\Repository\ItemPropertyRepository;
  5. use Chamilo\UserBundle\Entity\User;
  6. use Chamilo\CoreBundle\Entity\Repository\CourseRepository;
  7. use Chamilo\CoreBundle\Entity\Session;
  8. use Chamilo\CoreBundle\Entity\SessionRelCourseRelUser;
  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[] = array(
  25. 'url' => 'lp_controller.php?action=list&'.api_get_cidreq(),
  26. 'name' => get_lang('LearningPaths')
  27. );
  28. $interbreadcrumb[] = array(
  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 = array();
  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 = array();
  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 = array();
  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 = array();
  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. Display::addFlash(Display::return_message(get_lang('UserLpSubscriptionDescription')));
  125. $defaults = array();
  126. if (!empty($selectedGroupChoices)) {
  127. $defaults['groups'] = $selectedGroupChoices;
  128. }
  129. $form->setDefaults($defaults);
  130. $tpl = new Template();
  131. $currentUser = api_get_user_entity(api_get_user_id());
  132. if ($form->validate()) {
  133. $values = $form->getSubmitValues();
  134. // Subscribing users
  135. $users = isset($values['users']) ? $values['users'] : [];
  136. $userForm = isset($values['user_form']) ? $values['user_form'] : [];
  137. if (!empty($userForm)) {
  138. $itemRepo->subscribeUsersToItem(
  139. $currentUser,
  140. 'learnpath',
  141. $course,
  142. $session,
  143. $lpId,
  144. $users
  145. );
  146. Display::addFlash(Display::return_message(get_lang('Updated')));
  147. }
  148. // Subscribing groups
  149. $groups = isset($values['groups']) ? $values['groups'] : [];
  150. $groupForm = isset($values['group_form']) ? $values['group_form'] : [];
  151. if (!empty($groupForm)) {
  152. $itemRepo->subscribeGroupsToItem(
  153. $currentUser,
  154. 'learnpath',
  155. $course,
  156. $session,
  157. $lpId,
  158. $groups
  159. );
  160. Display::addFlash(Display::return_message(get_lang('Updated')));
  161. }
  162. header("Location: $url");
  163. exit;
  164. } else {
  165. $headers = [
  166. get_lang('SubscribeUsersToLp'),
  167. get_lang('SubscribeGroupsToLp')
  168. ];
  169. $tabs = Display::tabs($headers, [$formUsers->toHtml(), $form->toHtml()]);
  170. $tpl->assign('tabs', $tabs);
  171. }
  172. $layout = $tpl->get_template('learnpath/subscribe_users.tpl');
  173. $tpl->display($layout);