group_invitation.php 7.0 KB

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