lp_subscribe_users.php 5.4 KB

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