lp_subscribe_users.php 4.9 KB

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