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