group_invitation.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.social
  5. *
  6. * @author Julio Montoya <gugli100@gmail.com>
  7. */
  8. // resetting the course id
  9. $cidReset = true;
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. api_block_anonymous_users();
  12. // setting breadcrumbs
  13. $this_section = SECTION_SOCIAL;
  14. // Database Table Definitions
  15. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  16. $tbl_group_rel_user = Database::get_main_table(TABLE_USERGROUP_REL_USER);
  17. // setting the name of the tool
  18. $tool_name = get_lang('SubscribeUsersToGroup');
  19. $group_id = intval($_REQUEST['id']);
  20. $usergroup = new UserGroup();
  21. // todo @this validation could be in a function in group_portal_manager
  22. if (empty($group_id)) {
  23. api_not_allowed();
  24. } else {
  25. $group_info = $usergroup->get($group_id);
  26. if (empty($group_info)) {
  27. api_not_allowed();
  28. }
  29. //only admin or moderator can do that
  30. if (!$usergroup->is_group_member($group_id)) {
  31. api_not_allowed();
  32. }
  33. }
  34. $interbreadcrumb[] = ['url' => 'groups.php', 'name' => get_lang('Groups')];
  35. $interbreadcrumb[] = ['url' => 'group_view.php?id='.$group_id, 'name' => $group_info['name']];
  36. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('SubscribeUsersToGroup')];
  37. $form_sent = 0;
  38. $errorMsg = $firstLetterUser = $firstLetterSession = '';
  39. $UserList = $SessionList = [];
  40. $users = $sessions = [];
  41. $content = null;
  42. if (isset($_POST['form_sent']) && $_POST['form_sent']) {
  43. $form_sent = $_POST['form_sent'];
  44. $user_list = isset($_POST['invitation']) ? $_POST['invitation'] : null;
  45. $group_id = intval($_POST['id']);
  46. if (!is_array($user_list)) {
  47. $user_list = [];
  48. }
  49. if ($form_sent == 1) {
  50. // invite this users
  51. $result = $usergroup->add_users_to_groups(
  52. $user_list,
  53. [$group_id],
  54. GROUP_USER_PERMISSION_PENDING_INVITATION
  55. );
  56. $title = get_lang('YouAreInvitedToGroup').' '.$group_info['name'];
  57. $content = get_lang('YouAreInvitedToGroupContent').' '.$group_info['name'].' <br />';
  58. $content .= get_lang('ToSubscribeClickInTheLinkBelow').' <br />';
  59. $content .= '<a href="'.api_get_path(WEB_CODE_PATH).'social/invitations.php?accept='.$group_id.'">'.
  60. get_lang('Subscribe').'</a>';
  61. if (is_array($user_list) && count($user_list) > 0) {
  62. //send invitation message
  63. foreach ($user_list as $user_id) {
  64. $result = MessageManager::send_message(
  65. $user_id,
  66. $title,
  67. $content
  68. );
  69. }
  70. Display::addFlash(Display::return_message(get_lang('InvitationSent')));
  71. }
  72. header('Location: '.api_get_self().'?id='.$group_id);
  73. exit;
  74. }
  75. }
  76. $nosessionUsersList = $sessionUsersList = [];
  77. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname, username' : ' ORDER BY lastname, firstname, username';
  78. $friends = SocialManager::get_friends(api_get_user_id());
  79. $suggest_friends = false;
  80. $Users = [];
  81. if (!$friends) {
  82. $suggest_friends = true;
  83. } else {
  84. foreach ($friends as $friend) {
  85. $group_friend_list = $usergroup->get_groups_by_user($friend['friend_user_id'], 0);
  86. if (!empty($group_friend_list)) {
  87. $friend_group_id = '';
  88. if (isset($group_friend_list[$group_id]) &&
  89. $group_friend_list[$group_id]['id'] == $group_id
  90. ) {
  91. $friend_group_id = $group_id;
  92. }
  93. if (!isset($group_friend_list[$group_id]) ||
  94. isset($group_friend_list[$group_id]) &&
  95. $group_friend_list[$group_id]['relation_type'] == '') {
  96. $Users[$friend['friend_user_id']] = [
  97. 'user_id' => $friend['friend_user_id'],
  98. 'firstname' => $friend['firstName'],
  99. 'lastname' => $friend['lastName'],
  100. 'username' => $friend['username'],
  101. 'group_id' => $friend_group_id,
  102. ];
  103. }
  104. } else {
  105. $Users[$friend['friend_user_id']] = [
  106. 'user_id' => $friend['friend_user_id'],
  107. 'firstname' => $friend['firstName'],
  108. 'lastname' => $friend['lastName'],
  109. 'username' => $friend['username'],
  110. 'group_id' => null,
  111. ];
  112. }
  113. }
  114. }
  115. if (is_array($Users) && count($Users) > 0) {
  116. foreach ($Users as $user) {
  117. if ($user['group_id'] != $group_id) {
  118. $nosessionUsersList[$user['user_id']] = api_get_person_name(
  119. $user['firstname'],
  120. $user['lastname']
  121. );
  122. }
  123. }
  124. }
  125. $social_left_content = SocialManager::show_social_menu('invite_friends', $group_id);
  126. $social_right_content = '<h3 class="group-title">'.Security::remove_XSS($group_info['name'], STUDENT, true).'</h3>';
  127. if (count($nosessionUsersList) == 0) {
  128. $friends = SocialManager::get_friends(api_get_user_id());
  129. if ($friends == 0) {
  130. $social_right_content .= Display::return_message(get_lang('YouNeedToHaveFriendsInYourSocialNetwork'), 'warning');
  131. } else {
  132. $social_right_content .= Display::return_message(get_lang('YouAlreadyInviteAllYourContacts'), 'info');
  133. }
  134. $social_right_content .= '<div>';
  135. $social_right_content .= '<a href="search.php" class="btn btn-default btn-sm">'.Display::returnFontAwesomeIcon('search').' '.get_lang('TryAndFindSomeFriends').'</a>';
  136. $social_right_content .= '</div>';
  137. $social_right_content .= '<br />';
  138. }
  139. $form = new FormValidator('frm_invitation', 'post', api_get_self().'?id='.$group_id);
  140. $form->addHidden('form_sent', 1);
  141. $form->addHidden('id', $group_id);
  142. $group_members_element = $form->addElement(
  143. 'advmultiselect',
  144. 'invitation',
  145. get_lang('Friends'),
  146. $nosessionUsersList
  147. );
  148. $form->addButtonSave(get_lang('InviteUsersToGroup'));
  149. $social_right_content .= $form->returnForm();
  150. // Current group members
  151. $members = $usergroup->get_users_by_group(
  152. $group_id,
  153. false,
  154. [GROUP_USER_PERMISSION_PENDING_INVITATION]
  155. );
  156. if (is_array($members) && count($members) > 0) {
  157. foreach ($members as &$member) {
  158. $image = UserManager::getUserPicture($member['id']);
  159. $member['image'] = '<img class="img-circle" src="'.$image.'" width="50px" height="50px" />';
  160. }
  161. $userList = Display::return_sortable_grid(
  162. 'invitation_profile',
  163. [],
  164. $members,
  165. ['hide_navigation' => true, 'per_page' => 100],
  166. [],
  167. false,
  168. [true, false, true, false]
  169. );
  170. $social_right_content .= Display::panel($userList, get_lang('UsersAlreadyInvited'));
  171. }
  172. $tpl = new Template(null);
  173. SocialManager::setSocialUserBlock($tpl, api_get_user_id(), 'groups', $group_id);
  174. $social_menu_block = SocialManager::show_social_menu('member_list', $group_id);
  175. $tpl->assign('social_menu_block', $social_menu_block);
  176. $tpl->setHelp('Groups');
  177. $tpl->assign('social_right_content', $social_right_content);
  178. $social_layout = $tpl->get_template('social/add_groups.tpl');
  179. $tpl->display($social_layout);