group_invitation.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.social
  5. * @author Julio Montoya <gugli100@gmail.com>
  6. */
  7. /**
  8. * Initialization
  9. */
  10. // name of the language file that needs to be included
  11. $language_file=array('userInfo');
  12. // resetting the course id
  13. $cidReset=true;
  14. api_block_anonymous_users();
  15. $xajax = new xajax();
  16. $xajax -> registerFunction ('search_users');
  17. // setting the section (for the tabs)
  18. $this_section = SECTION_PLATFORM_ADMIN;
  19. // setting breadcrumbs
  20. $this_section = SECTION_SOCIAL;
  21. $interbreadcrumb[]= array ('url' =>'groups.php','name' => get_lang('Groups'));
  22. // Database Table Definitions
  23. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  24. $tbl_group_rel_user = Database::get_main_table(TABLE_USERGROUP_REL_USER);
  25. // setting the name of the tool
  26. $tool_name = get_lang('SubscribeUsersToGroup');
  27. $group_id = intval($_REQUEST['id']);
  28. $add_type = 'multiple';
  29. if(isset($_REQUEST['add_type']) && $_REQUEST['add_type']!=''){
  30. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  31. }
  32. $usergroup = new UserGroup();
  33. //todo @this validation could be in a function in group_portal_manager
  34. if (empty($group_id)) {
  35. api_not_allowed();
  36. } else {
  37. $group_info = $usergroup->get($group_id);
  38. if (empty($group_info)) {
  39. api_not_allowed();
  40. }
  41. //only admin or moderator can do that
  42. if (!$usergroup->is_group_member($group_id)) {
  43. api_not_allowed();
  44. }
  45. }
  46. function search_users($needle,$type) {
  47. global $tbl_user,$tbl_group_rel_user,$group_id;
  48. $xajax_response = new XajaxResponse();
  49. $return = '';
  50. if (!empty($needle) && !empty($type)) {
  51. // xajax send utf8 datas... datas in db can be non-utf8 datas
  52. $charset = api_get_system_encoding();
  53. $needle = Database::escape_string($needle);
  54. $needle = api_convert_encoding($needle, $charset, 'utf-8');
  55. $user_anonymous=api_get_anonymous_id();
  56. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname, username' : ' ORDER BY lastname, firstname, username';
  57. $cond_user_id = '';
  58. if (!empty($id_session)) {
  59. $group_id = Database::escape_string($group_id);
  60. // check id_user from session_rel_user table
  61. $sql = 'SELECT id_user FROM '.$tbl_group_rel_user.' WHERE usergroup_id ="'.(int)$group_id.'"';
  62. $res = Database::query($sql);
  63. $user_ids = array();
  64. if (Database::num_rows($res) > 0) {
  65. while ($row = Database::fetch_row($res)) {
  66. $user_ids[] = (int)$row[0];
  67. }
  68. }
  69. if (count($user_ids) > 0){
  70. $cond_user_id = ' AND user_id NOT IN('.implode(",",$user_ids).')';
  71. }
  72. }
  73. if ($type == 'single') {
  74. // search users where username or firstname or lastname begins likes $needle
  75. $sql = 'SELECT user_id, username, lastname, firstname FROM '.$tbl_user.' user
  76. WHERE ( username LIKE "'.$needle.'%" OR
  77. firstname LIKE "'.$needle.'%" OR
  78. lastname LIKE "'.$needle.'%"
  79. ) AND
  80. user_id<>"'.$user_anonymous.'"'.
  81. $order_clause.
  82. ' LIMIT 11';
  83. } else {
  84. $sql = 'SELECT user_id, username, lastname, firstname FROM '.$tbl_user.' user
  85. WHERE '.(api_sort_by_first_name() ? 'firstname' : 'lastname').' LIKE "'.$needle.'%" AND user_id<>"'.$user_anonymous.'"'.$cond_user_id.
  86. $order_clause;
  87. }
  88. if (api_is_multiple_url_enabled()) {
  89. $tbl_user_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  90. $access_url_id = api_get_current_access_url_id();
  91. if ($access_url_id != -1){
  92. if ($type == 'single') {
  93. $sql = 'SELECT user.user_id, username, lastname, firstname
  94. FROM '.$tbl_user.' user
  95. INNER JOIN '.$tbl_user_rel_access_url.' url_user ON (url_user.user_id=user.user_id)
  96. WHERE access_url_id = '.$access_url_id.' AND (
  97. username LIKE "'.$needle.'%" OR
  98. firstname LIKE "'.$needle.'%" OR
  99. lastname LIKE "'.$needle.'%")
  100. AND user.user_id <> "'.$user_anonymous.'"'.
  101. $order_clause.
  102. ' LIMIT 11';
  103. } else {
  104. $sql = 'SELECT user.user_id, username, lastname, firstname
  105. FROM '.$tbl_user.' user
  106. INNER JOIN '.$tbl_user_rel_access_url.' url_user ON (url_user.user_id=user.user_id)
  107. WHERE access_url_id = '.$access_url_id.' AND
  108. '.(api_sort_by_first_name() ? 'firstname' : 'lastname').' LIKE "'.$needle.'%" AND
  109. user.user_id<>"'.$user_anonymous.'"'.$cond_user_id.
  110. $order_clause;
  111. }
  112. }
  113. }
  114. $rs = Database::query($sql);
  115. $i=0;
  116. if ($type=='single') {
  117. while ($user = Database :: fetch_array($rs)) {
  118. $i++;
  119. if ($i<=10) {
  120. $person_name = api_get_person_name($user['firstname'], $user['lastname']);
  121. $return .= '<a href="javascript: void(0);" onclick="javascript: add_user(\''.$user['user_id'].'\',\''.$person_name.' ('.$user['username'].')'.'\')">'.$person_name.' ('.$user['username'].')</a><br />';
  122. } else {
  123. $return .= '...<br />';
  124. }
  125. }
  126. $xajax_response -> addAssign('ajax_list_users_single','innerHTML',api_utf8_encode($return));
  127. } else {
  128. global $nosessionUsersList;
  129. $return .= '<select id="origin_users" name="nosessionUsersList[]" multiple="multiple" size="15" style="width:360px;">';
  130. while ($user = Database :: fetch_array($rs)) {
  131. $person_name = api_get_person_name($user['firstname'], $user['lastname']);
  132. $return .= '<option value="'.$user['user_id'].'">'.$person_name.' ('.$user['username'].')</option>';
  133. }
  134. $return .= '</select>';
  135. $xajax_response -> addAssign('ajax_list_users_multiple','innerHTML',api_utf8_encode($return));
  136. }
  137. }
  138. return $xajax_response;
  139. }
  140. $xajax -> processRequests();
  141. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  142. $htmlHeadXtra[] = '<script>
  143. function add_user (code, content) {
  144. // document.getElementById("user_to_add").value = "";
  145. //document.getElementById("ajax_list_users_single").innerHTML = "";
  146. destination = document.getElementById("destination_users");
  147. for (i=0;i<destination.length;i++) {
  148. if(destination.options[i].text == content) {
  149. return false;
  150. }
  151. }
  152. destination.options[destination.length] = new Option(content,code);
  153. destination.selectedIndex = -1;
  154. sortOptions(destination.options);
  155. }
  156. function remove_item(origin) {
  157. for(var i = 0 ; i<origin.options.length ; i++) {
  158. if(origin.options[i].selected) {
  159. origin.options[i]=null;
  160. i = i-1;
  161. }
  162. }
  163. }
  164. function validate_filter() {
  165. document.formulaire.add_type.value = \''.$add_type.'\';
  166. document.formulaire.form_sent.value=0;
  167. document.formulaire.submit();
  168. }
  169. </script>';
  170. $form_sent=0;
  171. $errorMsg=$firstLetterUser=$firstLetterSession='';
  172. $UserList=$SessionList=array();
  173. $users=$sessions=array();
  174. //Display :: display_header($tool_name, 'Groups');
  175. $content = null;
  176. if (isset($_POST['form_sent']) && $_POST['form_sent']) {
  177. $form_sent = $_POST['form_sent'];
  178. $firstLetterUser = $_POST['firstLetterUser'];
  179. $firstLetterSession = $_POST['firstLetterSession'];
  180. $user_list = $_POST['sessionUsersList'];
  181. $group_id = intval($_POST['id']);
  182. if (!is_array($user_list)) {
  183. $user_list=array();
  184. }
  185. if ($form_sent == 1) {
  186. //invite this users
  187. $result = $usergroup->add_users_to_groups($user_list, array($group_id), GROUP_USER_PERMISSION_PENDING_INVITATION);
  188. $title = get_lang('YouAreInvitedToGroup').' '.$group_info['name'];
  189. $content = get_lang('YouAreInvitedToGroupContent').' '.$group_info['name'].' <br />';
  190. $content .= get_lang('ToSubscribeClickInTheLinkBelow').' <br />';
  191. $content .= '<a href="'.api_get_path(WEB_CODE_PATH).'social/invitations.php?accept='.$group_id.'">'.get_lang('Subscribe').'</a>';
  192. if (is_array($user_list) && count($user_list) > 0) {
  193. //send invitation message
  194. foreach($user_list as $user_id ){
  195. $result = MessageManager::send_message($user_id, $title, $content);
  196. }
  197. }
  198. }
  199. }
  200. $nosessionUsersList = $sessionUsersList = array();
  201. $ajax_search = $add_type == 'unique' ? true : false;
  202. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname, username' : ' ORDER BY lastname, firstname, username';
  203. if ($ajax_search) {
  204. $sql="SELECT u.user_id, lastname, firstname, username, group_id
  205. FROM $tbl_user u
  206. LEFT JOIN $tbl_group_rel_user gu
  207. ON (gu.user_id = u.user_id) WHERE gu.group_id = $group_id ".
  208. $order_clause;
  209. if (api_is_multiple_url_enabled()) {
  210. $tbl_user_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  211. $access_url_id = api_get_current_access_url_id();
  212. if ($access_url_id != -1){
  213. $sql="SELECT u.user_id, lastname, firstname, username, id_session
  214. FROM $tbl_user u
  215. INNER JOIN $tbl_session_rel_user
  216. ON $tbl_session_rel_user.id_user = u.user_id
  217. AND $tbl_session_rel_user.id_session = ".intval($id_session)."
  218. INNER JOIN $tbl_user_rel_access_url url_user ON (url_user.user_id=u.user_id)
  219. WHERE access_url_id = $access_url_id
  220. $order_clause";
  221. }
  222. }
  223. $result = Database::query($sql);
  224. $Users = Database::store_result($result);
  225. foreach ($Users as $user) {
  226. $sessionUsersList[$user['user_id']] = $user ;
  227. }
  228. } else {
  229. $friends = SocialManager::get_friends(api_get_user_id());
  230. $suggest_friends = false;
  231. $Users = array();
  232. if (!$friends) {
  233. $suggest_friends = true;
  234. } else {
  235. foreach ($friends as $friend) {
  236. $group_friend_list = $usergroup->get_groups_by_user($friend['friend_user_id'], 0);
  237. if (!empty($group_friend_list)) {
  238. $friend_group_id = '';
  239. if (isset($group_friend_list[$group_id]) && $group_friend_list[$group_id]['id'] == $group_id) {
  240. $friend_group_id = $group_id;
  241. }
  242. //var_dump ($group_friend_list[$group_id]['relation_type']);
  243. if ($group_friend_list[$group_id]['relation_type'] == '' ) {
  244. $Users[$friend['friend_user_id']]= array(
  245. 'user_id' => $friend['friend_user_id'],
  246. 'firstname' => $friend['firstName'],
  247. 'lastname' => $friend['lastName'],
  248. 'username' => $friend['username'],
  249. 'group_id' => $friend_group_id
  250. );
  251. }
  252. } else {
  253. $Users[$friend['friend_user_id']]= array(
  254. 'user_id' => $friend['friend_user_id'],
  255. 'firstname' =>$friend['firstName'],
  256. 'lastname' => $friend['lastName'],
  257. 'username' =>$friend['username'],
  258. 'group_id' => null
  259. );
  260. }
  261. }
  262. }
  263. if (is_array($Users) && count($Users) > 0 ) {
  264. foreach ($Users as $user) {
  265. if ($user['group_id'] != $group_id) {
  266. $nosessionUsersList[$user['user_id']] = $user ;
  267. }
  268. }
  269. }
  270. //deleting anonymous users
  271. $user_anonymous = api_get_anonymous_id();
  272. foreach ($nosessionUsersList as $key_user_list =>$value_user_list) {
  273. if ($nosessionUsersList[$key_user_list]['user_id']==$user_anonymous) {
  274. unset($nosessionUsersList[$key_user_list]);
  275. }
  276. }
  277. }
  278. if ($add_type == 'multiple') {
  279. $link_add_type_unique = '<a href="'.api_get_self().'?id='.$group_id.'&add_type=unique">'.Display::return_icon('single.gif').get_lang('SessionAddTypeUnique').'</a>';
  280. $link_add_type_multiple = Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple');
  281. } else {
  282. $link_add_type_unique = Display::return_icon('single.gif').get_lang('SessionAddTypeUnique');
  283. $link_add_type_multiple = '<a href="'.api_get_self().'?id='.$group_id.'&add_type=multiple">'.Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').'</a>';
  284. }
  285. $social_left_content = SocialManager::show_social_menu('invite_friends',$group_id);
  286. $social_right_content = '<h2>'.Security::remove_XSS($group_info['name'], STUDENT, true).'</h2>';
  287. if (count($nosessionUsersList) == 0) {
  288. $friends = SocialManager::get_friends(api_get_user_id());
  289. if ($friends == 0) {
  290. $social_right_content .= get_lang('YouNeedToHaveFriendsInYourSocialNetwork');
  291. } else {
  292. $social_right_content .= get_lang('YouAlreadyInviteAllYourContacts');
  293. }
  294. $social_right_content .= '<div>';
  295. $social_right_content .= '<a href="search.php">'.get_lang('TryAndFindSomeFriends').'</a>';
  296. $social_right_content .= '</div>';
  297. }
  298. $add_true = null;
  299. $ajax = null;
  300. if (!empty($_GET['add'])) $add_true = '&add=true';
  301. if ($ajax_search) $ajax = 'onsubmit="valide();"';
  302. $form = '<form name="formulaire" method="post" action="'.api_get_self().'?id='.$group_id.$add_true.'" style="margin:0px;" '.$ajax.'>';
  303. if ($add_type=='multiple') {
  304. /*
  305. if (is_array($extra_field_list)) {
  306. if (is_array($new_field_list) && count($new_field_list)>0 ) {
  307. $form .= '<h3>'.get_lang('FilterUsers').'</h3>';
  308. foreach ($new_field_list as $new_field) {
  309. $form .= $new_field['name'];
  310. $varname = 'field_'.$new_field['variable'];
  311. $form .= '&nbsp;<select name="'.$varname.'">';
  312. $form .= '<option value="0">--'.get_lang('Select').'--</option>';
  313. foreach ($new_field['data'] as $option) {
  314. $checked='';
  315. if (isset($_POST[$varname])) {
  316. if ($_POST[$varname]==$option[1]) {
  317. $checked = 'selected="true"';
  318. }
  319. }
  320. $form .= '<option value="'.$option[1].'" '.$checked.'>'.$option[1].'</option>';
  321. }
  322. $form .= '</select>';
  323. $form .= '&nbsp;&nbsp;';
  324. }
  325. $form .= '<input type="button" value="'.get_lang('Filter').'" onclick="validate_filter()" />';
  326. $form .= '<br /><br />';
  327. }
  328. }*/
  329. }
  330. $form .= '<input type="hidden" name="form_sent" value="1" />';
  331. $form .= '<input type="hidden" name="id" value="'.$group_id.'">';
  332. $form .= '<input type="hidden" name="add_type" />';
  333. if(!empty($errorMsg)) {
  334. $form .= Display::return_message($errorMsg,'error'); //main API
  335. }
  336. $form .= '<table border="0" cellpadding="5" cellspacing="0" width="100%">
  337. <tr>
  338. <td align="center"><b>'.get_lang('Friends').' :</b>
  339. </td>
  340. <td></td>
  341. <td align="center"><b>'.get_lang('SendInvitationTo').':</b></td></tr>';
  342. if ($add_type=='no') {
  343. $form .='
  344. <tr>
  345. <td align="center">'.get_lang('FirstLetterUser').' :
  346. <select name="firstLetterUser" onchange = "xajax_search_users(this.value,\'multiple\')" >
  347. <option value = "%">--</option>
  348. '.Display :: get_alphabet_options().'
  349. </select>
  350. </td>
  351. <td align="center">&nbsp;</td>
  352. </tr>';
  353. }
  354. $form .= '
  355. <tr>
  356. <td align="center">
  357. <div id="content_source">';
  358. if (!($add_type=='multiple')) {
  359. $form .='<input type="text" id="user_to_add" onkeyup="xajax_search_users(this.value,\'single\')" /><div id="ajax_list_users_single"></div>';
  360. } else {
  361. $form .= '<div id="ajax_list_users_multiple">
  362. <select id="origin_users" name="nosessionUsersList[]" multiple="multiple" size="15" style="width:290px;">';
  363. foreach($nosessionUsersList as $enreg) {
  364. $selected = '';
  365. if(in_array($enreg['user_id'],$UserList)) $selected = 'selected="selected"';
  366. $form .= '<option value="'.$enreg['user_id'].'" '.$selected.'>'.api_get_person_name($enreg['firstname'], $enreg['lastname']).' ('.$enreg['username'].') </option>';
  367. }
  368. $form .= '</select>';
  369. $form .= '</div>';
  370. }
  371. unset($nosessionUsersList);
  372. $form .= '</div>';
  373. $form .= '</td><td width="10%" valign="middle" align="center">';
  374. if ($ajax_search) {
  375. $form .= '<button class="arrowl" type="button" onclick="remove_item(document.getElementById(\'destination_users\'))" ></button>';
  376. } else {
  377. $form .= '<button class="arrowr" type="button" onclick="moveItem(document.getElementById(\'origin_users\'), document.getElementById(\'destination_users\'))" ></button>
  378. <br /><br />
  379. <button class="arrowl" type="button" onclick="moveItem(document.getElementById(\'destination_users\'), document.getElementById(\'origin_users\'))" ></button>
  380. <br /><br />';
  381. }
  382. $form .= ' <br /><br /><br /><br /><br />
  383. </td>
  384. <td align="center">
  385. <select id="destination_users" name="sessionUsersList[]" multiple="multiple" size="15" style="width:290px;">';
  386. foreach($sessionUsersList as $enreg) {
  387. $form .= ' <option value="'.$enreg['user_id'].'">'.api_get_person_name($enreg['firstname'], $enreg['lastname']).' ('.$enreg['username'].')</option>';
  388. }
  389. unset($sessionUsersList);
  390. $form .= '</select></td>
  391. </tr>
  392. <tr>
  393. <td colspan="3" align="center">
  394. <br />
  395. <button class="save" type="button" value="" onclick="valide()" >'.get_lang('InviteUsersToGroup').'</button>
  396. </td>
  397. </tr>
  398. </table>
  399. </form>';
  400. $social_right_content .= $form;
  401. //current group members
  402. $members = $usergroup->get_users_by_group($group_id, false, array(GROUP_USER_PERMISSION_PENDING_INVITATION));
  403. if (is_array($members) && count($members)>0) {
  404. foreach ($members as &$member) {
  405. $image_path = UserManager::get_user_picture_path_by_id($member['user_id'], 'web', false, true);
  406. $picture = UserManager::get_picture_user($member['user_id'], $image_path['file'],80);
  407. $member['image'] = '<img src="'.$picture['file'].'" width="50px" height="50px" />';
  408. }
  409. $social_right_content .= '<h3>'.get_lang('UsersAlreadyInvited').'</h3>';
  410. $social_right_content .= Display::return_sortable_grid('invitation_profile', array(), $members, array('hide_navigation'=>true, 'per_page' => 100), array(), false, array(true, false, true,true));
  411. }
  412. $htmlHeadXtra[] = '<script>
  413. function valide(){
  414. var options = document.getElementById(\'destination_users\').options;
  415. for (i = 0 ; i<options.length ; i++)
  416. options[i].selected = true;
  417. document.forms.formulaire.submit();
  418. }
  419. function loadUsersInSelect(select){
  420. var xhr_object = null;
  421. if(window.XMLHttpRequest) // Firefox
  422. xhr_object = new XMLHttpRequest();
  423. else if(window.ActiveXObject) // Internet Explorer
  424. xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  425. else // XMLHttpRequest non supporté par le navigateur
  426. alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
  427. xhr_object.open("POST", "loadUsersInSelect.ajax.php");
  428. xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  429. nosessionUsers = makepost(document.getElementById("origin_users"));
  430. sessionUsers = makepost(document.getElementById("destination_users"));
  431. nosessionClasses = makepost(document.getElementById("origin_classes"));
  432. sessionClasses = makepost(document.getElementById("destination_classes"));
  433. xhr_object.send("nosessionusers="+nosessionUsers+"&sessionusers="+sessionUsers+"&nosessionclasses="+nosessionClasses+"&sessionclasses="+sessionClasses);
  434. xhr_object.onreadystatechange = function() {
  435. if(xhr_object.readyState == 4) {
  436. document.getElementById("content_source").innerHTML = result = xhr_object.responseText;
  437. //alert(xhr_object.responseText);
  438. }
  439. }
  440. }
  441. function makepost(select) {
  442. var options = select.options;
  443. var ret = "";
  444. for (i = 0 ; i<options.length ; i++)
  445. ret = ret + options[i].value +\'::\'+options[i].text+";;";
  446. return ret;
  447. }
  448. </script>';
  449. $social_right_content = Display::div($social_right_content, array('class' => 'span9'));
  450. $app['title'] = $tool_name;
  451. $tpl = $app['template'];
  452. $tpl->setHelp('Groups');
  453. $tpl->assign('content', $social_right_content);