lp_subscribe_users.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\Framework\Container;
  6. //require_once '../inc/global.inc.php';
  7. api_protect_course_script();
  8. $is_allowed_to_edit = api_is_allowed_to_edit(false, true, false, false);
  9. if (!$is_allowed_to_edit) {
  10. api_not_allowed(true);
  11. }
  12. $lpId = isset($_GET['lp_id']) ? intval($_GET['lp_id']) : 0;
  13. if (empty($lpId)) {
  14. api_not_allowed(true);
  15. }
  16. $oLP = new learnpath(api_get_course_id(), $lpId, api_get_user_id());
  17. $interbreadcrumb[] = array('url' => 'lp_controller.php?action=list', 'name' => get_lang('LearningPaths'));
  18. $interbreadcrumb[] = array('url' => api_get_self()."?action=build&lp_id=".$oLP->get_id(), 'name' => $oLP->get_name());
  19. $courseId = api_get_course_int_id();
  20. $courseCode = api_get_course_id();
  21. $url = api_get_self().'?'.api_get_cidreq().'&lp_id='.$lpId;
  22. $lp = new \learnpath($courseCode, $lpId, api_get_user_id());
  23. $em = Database::getManager();
  24. $session = null;
  25. if (!empty($sessionId)) {
  26. $session = $em->getRepository('ChamiloCoreBundle:Session')->find($sessionId);
  27. }
  28. // Find course.
  29. $course = $em->getRepository('ChamiloCoreBundle:Course')->find($courseId);
  30. // Getting subscribe users to the course.
  31. $subscribedUsers = $em->getRepository('ChamiloCoreBundle:Course')->getSubscribedStudents($course);
  32. $subscribedUsers = $subscribedUsers->getQuery();
  33. $subscribedUsers = $subscribedUsers->execute();
  34. // Getting all users in a nice format.
  35. $choices = array();
  36. /** @var User $user */
  37. foreach ($subscribedUsers as $user) {
  38. $choices[$user->getUserId()] = $user->getCompleteNameWithClasses();
  39. }
  40. // Getting subscribed users to a LP.
  41. $subscribedUsersInLp = $em->getRepository('ChamiloCourseBundle:CItemProperty')->getUsersSubscribedToItem(
  42. 'learnpath',
  43. $lpId,
  44. $course,
  45. $session
  46. );
  47. $selectedChoices = array();
  48. foreach ($subscribedUsersInLp as $itemProperty) {
  49. $selectedChoices[] = $itemProperty->getToUser()->getId();
  50. }
  51. //Building the form for Users
  52. $formUsers = new \FormValidator('lp_edit', 'post', $url);
  53. $formUsers->addElement('hidden', 'user_form', 1);
  54. $userMultiSelect = $formUsers->addElement('advmultiselect', 'users', get_lang('Users'), $choices);
  55. $formUsers->addButtonSave(get_lang('Save'));
  56. $defaults = array();
  57. if (!empty($selectedChoices)) {
  58. $defaults['users'] = $selectedChoices;
  59. }
  60. $formUsers->setDefaults($defaults);
  61. //Building the form for Groups
  62. $form = new \FormValidator('lp_edit', 'post', $url);
  63. $form->addElement('hidden', 'group_form', 1);
  64. // Group list
  65. $groupList = \CourseManager::get_group_list_of_course(
  66. api_get_course_id(),
  67. api_get_session_id(),
  68. 1
  69. );
  70. $groupChoices = array_column($groupList, 'name', 'id');
  71. // Subscribed groups to a LP
  72. $subscribedGroupsInLp = $em->getRepository('ChamiloCourseBundle:CItemProperty')->getGroupsSubscribedToItem(
  73. 'learnpath',
  74. $lpId,
  75. $course,
  76. $session
  77. );
  78. $selectedGroupChoices = array();
  79. /** @var CItemProperty $itemProperty */
  80. foreach ($subscribedGroupsInLp as $itemProperty) {
  81. $selectedGroupChoices[] = $itemProperty->getGroup()->getId();
  82. }
  83. $groupMultiSelect = $form->addElement('advmultiselect', 'groups', get_lang('Groups'), $groupChoices);
  84. // submit button
  85. $form->addButtonSave(get_lang('Save'));
  86. $defaults = array();
  87. if (!empty($selectedGroupChoices)) {
  88. $defaults['groups'] = $selectedGroupChoices;
  89. }
  90. $form->setDefaults($defaults);
  91. $tpl = Container::getTwig();
  92. $currentUser = $em->getRepository('ChamiloUserBundle:User')->find(api_get_user_id());
  93. if ($form->validate()) {
  94. $values = $form->getSubmitValues();
  95. // Subscribing users
  96. $users = isset($values['users']) ? $values['users'] : [];
  97. $userForm = isset($values['user_form']) ? $values['user_form'] : [];
  98. if (!empty($userForm)) {
  99. $em->getRepository('ChamiloCourseBundle:CItemProperty')->subscribeUsersToItem(
  100. $currentUser,
  101. 'learnpath',
  102. $course,
  103. $session,
  104. $lpId,
  105. $users
  106. );
  107. Display::addFlash(Display::return_message(get_lang('Updated')));
  108. }
  109. // Subscribing groups
  110. $groups = isset($values['groups']) ? $values['groups'] : [];
  111. $groupForm = isset($values['group_form']) ? $values['group_form'] : [];
  112. if (!empty($groupForm)) {
  113. $em->getRepository('ChamiloCourseBundle:CItemProperty')->subscribeGroupsToItem(
  114. $currentUser,
  115. 'learnpath',
  116. $course,
  117. $session,
  118. $lpId,
  119. $groups
  120. );
  121. Display::addFlash(Display::return_message(get_lang('Updated')));
  122. }
  123. header("Location: $url");
  124. exit;
  125. } else {
  126. $headers = [get_lang('SubscribeUsersToLp'), get_lang('SubscribeGroupsToLp')];
  127. $tabs = Display::tabs($headers, [$formUsers->toHtml(),$form->toHtml()]);
  128. $tpl->addGlobal('tabs', $tabs);
  129. }
  130. echo $tpl->render('@template_style/learnpath/subscribe_users.html.twig');