group_view.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.social
  5. *
  6. * @author Julio Montoya <gugli100@gmail.com>
  7. */
  8. $cidReset = true;
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. api_block_anonymous_users();
  11. if (api_get_setting('allow_social_tool') !== 'true') {
  12. api_not_allowed();
  13. }
  14. $this_section = SECTION_SOCIAL;
  15. // prepare anchor for message group topic
  16. $anchor = '';
  17. if (isset($_GET['anchor_topic'])) {
  18. $anchor = Security::remove_XSS($_GET['anchor_topic']);
  19. } else {
  20. $match = 0;
  21. $param_names = array_keys($_GET);
  22. foreach ($param_names as $param) {
  23. if (preg_match('/^items_(\d)_page_nr$/', $param, $match)) {
  24. break;
  25. }
  26. }
  27. if (isset($match[1])) {
  28. $anchor = 'topic_'.$match[1];
  29. }
  30. }
  31. $htmlHeadXtra[] = '<script>
  32. var counter_image = 1;
  33. function remove_image_form(id_elem1) {
  34. var elem1 = document.getElementById(id_elem1);
  35. elem1.parentNode.removeChild(elem1);
  36. counter_image--;
  37. var filepaths = document.getElementById("filepaths");
  38. if (filepaths.childNodes.length < 3) {
  39. var link_attach = document.getElementById("link-more-attach");
  40. if (link_attach) {
  41. link_attach.innerHTML=\'<a href="javascript://" class="btn btn-default" onclick="return add_image_form()">'.get_lang('Add one more file').'</a>\';
  42. }
  43. }
  44. }
  45. function add_image_form() {
  46. // Multiple filepaths for image form
  47. var filepaths = document.getElementById("filepaths");
  48. if (document.getElementById("filepath_"+counter_image)) {
  49. counter_image = counter_image + 1;
  50. } else {
  51. counter_image = counter_image;
  52. }
  53. var elem1 = document.createElement("div");
  54. elem1.setAttribute("id","filepath_"+counter_image);
  55. filepaths.appendChild(elem1);
  56. id_elem1 = "filepath_"+counter_image;
  57. id_elem1 = "\'"+id_elem1+"\'";
  58. document.getElementById("filepath_"+counter_image).innerHTML = "\n\
  59. <input type=\"file\" name=\"attach_"+counter_image+"\" size=\"20\" />\n\
  60. <a href=\"javascript:remove_image_form("+id_elem1+")\">\n\
  61. <img src=\"'.Display::returnIconPath('delete.gif').'\">\n\
  62. </a>\n\
  63. ";
  64. if (filepaths.childNodes.length == 3) {
  65. var link_attach = document.getElementById("link-more-attach");
  66. if (link_attach) {
  67. link_attach.innerHTML="";
  68. }
  69. }
  70. }
  71. </script>';
  72. $allowed_views = ['mygroups', 'newest', 'pop'];
  73. $content = null;
  74. if (isset($_GET['view']) && in_array($_GET['view'], $allowed_views)) {
  75. if ($_GET['view'] == 'mygroups') {
  76. $interbreadcrumb[] = ['url' => 'groups.php', 'name' => get_lang('Groups')];
  77. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('My groups')];
  78. } elseif ($_GET['view'] == 'newest') {
  79. $interbreadcrumb[] = ['url' => 'groups.php', 'name' => get_lang('Groups')];
  80. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Newest')];
  81. } else {
  82. $interbreadcrumb[] = ['url' => 'groups.php', 'name' => get_lang('Groups')];
  83. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Popular')];
  84. }
  85. } else {
  86. $interbreadcrumb[] = ['url' => 'groups.php', 'name' => get_lang('Groups')];
  87. if (!isset($_GET['id'])) {
  88. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Groups list')];
  89. } else {
  90. //$interbreadcrumb[]= array ('url' =>'#','name' => get_lang('Group'));
  91. }
  92. }
  93. // getting group information
  94. $group_id = isset($_GET['id']) ? intval($_GET['id']) : null;
  95. $relation_group_title = '';
  96. $role = 0;
  97. $usergroup = new UserGroup();
  98. if ($group_id != 0) {
  99. $groupInfo = $usergroup->get($group_id);
  100. $groupInfo['name'] = Security::remove_XSS($groupInfo['name']);
  101. $groupInfo['description'] = Security::remove_XSS($groupInfo['description']);
  102. $interbreadcrumb[] = ['url' => '#', 'name' => $groupInfo['name']];
  103. if (isset($_GET['action']) && $_GET['action'] == 'leave') {
  104. $user_leaved = intval($_GET['u']);
  105. // I can "leave me myself"
  106. if (api_get_user_id() == $user_leaved) {
  107. if (UserGroup::canLeave($groupInfo)) {
  108. $usergroup->delete_user_rel_group($user_leaved, $group_id);
  109. Display::addFlash(
  110. Display::return_message(get_lang('User is not subscribed to this group'), 'confirmation', false)
  111. );
  112. }
  113. }
  114. }
  115. // add a user to a group if its open
  116. if (isset($_GET['action']) && $_GET['action'] == 'join') {
  117. // we add a user only if is a open group
  118. $user_join = intval($_GET['u']);
  119. if (api_get_user_id() == $user_join && !empty($group_id)) {
  120. if ($groupInfo['visibility'] == GROUP_PERMISSION_OPEN) {
  121. $usergroup->add_user_to_group($user_join, $group_id);
  122. Display::addFlash(
  123. Display::return_message(get_lang('User is subscribed to this group'), 'confirmation', false)
  124. );
  125. } else {
  126. $usergroup->add_user_to_group(
  127. $user_join,
  128. $group_id,
  129. GROUP_USER_PERMISSION_PENDING_INVITATION_SENT_BY_USER
  130. );
  131. Display::addFlash(
  132. Display::return_message(get_lang('Invitation sent'), 'confirmation', false)
  133. );
  134. }
  135. }
  136. }
  137. }
  138. $create_thread_link = '';
  139. $social_right_content = null;
  140. $socialForum = '';
  141. $groupInfo = $usergroup->get($group_id);
  142. $groupInfo['name'] = Security::remove_XSS($groupInfo['name']);
  143. $groupInfo['description'] = Security::remove_XSS($groupInfo['description']);
  144. //Loading group information
  145. if (isset($_GET['status']) && $_GET['status'] == 'sent') {
  146. $social_right_content .= Display::return_message(get_lang('Your message has been sent.'), 'confirmation', false);
  147. }
  148. $is_group_member = $usergroup->is_group_member($group_id);
  149. $role = $usergroup->get_user_group_role(api_get_user_id(), $group_id);
  150. if (!$is_group_member && $groupInfo['visibility'] == GROUP_PERMISSION_CLOSED) {
  151. if ($role == GROUP_USER_PERMISSION_PENDING_INVITATION_SENT_BY_USER) {
  152. $social_right_content .= Display::return_message(get_lang('You already sent an invitation'));
  153. }
  154. }
  155. if ($is_group_member || $groupInfo['visibility'] == GROUP_PERMISSION_OPEN) {
  156. if (!$is_group_member) {
  157. if (!in_array(
  158. $role,
  159. [GROUP_USER_PERMISSION_PENDING_INVITATION_SENT_BY_USER, GROUP_USER_PERMISSION_PENDING_INVITATION]
  160. )) {
  161. $social_right_content .= '<div class="group-tool">';
  162. $social_right_content .= '<div class="pull-right">';
  163. $social_right_content .= '<a class="btn btn-default btn-sm" href="group_view.php?id='.$group_id.'&action=join&u='.api_get_user_id().'">'.
  164. get_lang('Join group').'</a>';
  165. $social_right_content .= '</div>';
  166. $social_right_content .= '</div>';
  167. } elseif ($role == GROUP_USER_PERMISSION_PENDING_INVITATION) {
  168. $social_right_content .= '<div class="group-tool">';
  169. $social_right_content .= '<div class="pull-right">';
  170. $social_right_content .= '<a class="btn btn-default btn-sm" href="group_view.php?id='.$group_id.'&action=join&u='.api_get_user_id().'">'.
  171. Display::returnFontAwesomeIcon('envelope').' '.
  172. get_lang('You have been invited to join now').'</a>';
  173. }
  174. $social_right_content .= '</div>';
  175. $social_right_content .= '</div>';
  176. }
  177. $content = MessageManager::display_messages_for_group($group_id);
  178. if ($is_group_member) {
  179. if (empty($content)) {
  180. $createThreadUrl = api_get_path(WEB_CODE_PATH)
  181. .'social/message_for_group_form.inc.php?'
  182. .http_build_query([
  183. 'view_panel' => 1,
  184. 'user_friend' => api_get_user_id(),
  185. 'group_id' => $group_id,
  186. 'action' => 'add_message_group',
  187. ]);
  188. $create_thread_link = Display::url(
  189. Display::returnFontAwesomeIcon('commenting').' '.
  190. get_lang('You should create a topic'),
  191. $createThreadUrl,
  192. [
  193. 'class' => 'ajax btn btn-primary',
  194. 'title' => get_lang('Compose message'),
  195. 'data-title' => get_lang('Compose message'),
  196. 'data-size' => 'lg',
  197. ]
  198. );
  199. } else {
  200. $createThreadUrl = api_get_path(WEB_CODE_PATH)
  201. .'social/message_for_group_form.inc.php?'
  202. .http_build_query([
  203. 'view_panel' => 1,
  204. 'user_friend' => api_get_user_id(),
  205. 'group_id' => $group_id,
  206. 'action' => 'add_message_group',
  207. ]);
  208. $create_thread_link = Display::url(
  209. Display::returnFontAwesomeIcon('commenting').' '.
  210. get_lang('Create thread'),
  211. $createThreadUrl,
  212. [
  213. 'class' => 'ajax btn btn-default',
  214. 'title' => get_lang('Compose message'),
  215. 'data-title' => get_lang('Compose message'),
  216. 'data-size' => 'lg',
  217. ]
  218. );
  219. }
  220. }
  221. $members = $usergroup->get_users_by_group($group_id, true);
  222. $member_content = '';
  223. // My friends
  224. $friend_html = SocialManager::listMyFriendsBlock(
  225. api_get_user_id(),
  226. '',
  227. ''
  228. );
  229. // Members
  230. if (count($members) > 0) {
  231. if ($role == GROUP_USER_PERMISSION_ADMIN) {
  232. $member_content .= '<div class="group-tool">';
  233. $member_content .= '<div class="pull-right">';
  234. $member_content .= Display::url(
  235. Display::returnFontAwesomeIcon('pencil').' '.get_lang('Edit members list'),
  236. 'group_members.php?id='.$group_id,
  237. ['class' => 'btn btn-default btn-sm', 'title' => get_lang('Edit members list')]
  238. );
  239. $member_content .= '</div>';
  240. $member_content .= '</div>';
  241. }
  242. $member_content .= '<div class="user-list">';
  243. $member_content .= '<div class="row">';
  244. foreach ($members as $member) {
  245. // if is a member
  246. if (in_array(
  247. $member['relation_type'],
  248. [GROUP_USER_PERMISSION_ADMIN, GROUP_USER_PERMISSION_READER, GROUP_USER_PERMISSION_MODERATOR]
  249. )) {
  250. //add icons
  251. if ($member['relation_type'] == GROUP_USER_PERMISSION_ADMIN) {
  252. $icon = Display::return_icon('social_group_admin.png', get_lang('Admin'));
  253. } elseif ($member['relation_type'] == GROUP_USER_PERMISSION_MODERATOR) {
  254. $icon = Display::return_icon('social_group_moderator.png', get_lang('Moderator'));
  255. } else {
  256. $icon = '';
  257. }
  258. $userPicture = UserManager::getUserPicture($member['id']);
  259. $member_content .= '<div class="col-md-3">';
  260. $member_content .= '<div class="items-user">';
  261. $member_name = Display::url(
  262. api_get_person_name(
  263. cut($member['user_info']['firstname'], 15),
  264. cut($member['user_info']['lastname'], 15)
  265. ).'&nbsp;'.$icon,
  266. $member['user_info']['profile_url']
  267. );
  268. $member_content .= Display::div('<img class="img-circle" src="'.$userPicture.'"/>', ['class' => 'avatar']);
  269. $member_content .= Display::div($member_name, ['class' => 'name']);
  270. $member_content .= '</div>';
  271. $member_content .= '</div>';
  272. }
  273. }
  274. $member_content .= '</div>';
  275. $member_content .= '</div>';
  276. }
  277. if (!empty($create_thread_link)) {
  278. $create_thread_link = Display::div($create_thread_link, ['class' => 'pull-right']);
  279. }
  280. $headers = [get_lang('Discussions'), get_lang('Members')];
  281. $socialForum = Display::tabs($headers, [$content, $member_content], 'tabs');
  282. } else {
  283. // if I already sent an invitation message
  284. if (!in_array(
  285. $role,
  286. [
  287. GROUP_USER_PERMISSION_PENDING_INVITATION_SENT_BY_USER,
  288. GROUP_USER_PERMISSION_PENDING_INVITATION,
  289. ]
  290. )) {
  291. $social_right_content .= '<a class="btn" href="group_view.php?id='.$group_id.'&action=join&u='.api_get_user_id().'">'.get_lang('Join group').'</a>';
  292. } elseif ($role == GROUP_USER_PERMISSION_PENDING_INVITATION) {
  293. $social_right_content .= '<a class="btn" href="group_view.php?id='.$group_id.'&action=join&u='.api_get_user_id().'">'.get_lang('You have been invited to join now').'</a>';
  294. }
  295. }
  296. $tpl = new Template(null);
  297. // Block Social Avatar
  298. SocialManager::setSocialUserBlock($tpl, api_get_user_id(), 'groups', $group_id);
  299. $social_menu_block = SocialManager::show_social_menu('groups', $group_id);
  300. $tpl->setHelp('Groups');
  301. $tpl->assign('create_link', $create_thread_link);
  302. $tpl->assign('is_group_member', $is_group_member);
  303. $tpl->assign('group_info', $groupInfo);
  304. $tpl->assign('social_friend_block', $friend_html);
  305. $tpl->assign('social_menu_block', $social_menu_block);
  306. $tpl->assign('social_forum', $socialForum);
  307. $tpl->assign('social_right_content', $social_right_content);
  308. $social_layout = $tpl->get_template('social/group_view.tpl');
  309. $tpl->display($social_layout);