group_invitation.php 19 KB

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