dashboard_add_users_to_user.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Interface for assigning users to Human Resources Manager
  5. * @package chamilo.admin
  6. */
  7. // name of the language file that needs to be included
  8. $language_file='admin';
  9. // resetting the course id
  10. $cidReset=true;
  11. // including some necessary dokeos files
  12. require_once '../inc/global.inc.php';
  13. require_once '../inc/lib/xajax/xajax.inc.php';
  14. global $_configuration;
  15. // create an ajax object
  16. $xajax = new xajax();
  17. $xajax -> registerFunction ('search_users');
  18. // setting the section (for the tabs)
  19. $this_section = SECTION_PLATFORM_ADMIN;
  20. // Access restrictions
  21. api_protect_admin_script(true);
  22. // setting breadcrumbs
  23. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  24. $interbreadcrumb[] = array('url' => 'user_list.php','name' => get_lang('UserList'));
  25. // Database Table Definitions
  26. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  27. $tbl_access_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  28. // initializing variables
  29. $id_session=intval($_GET['id_session']);
  30. $user_id = intval($_GET['user']);
  31. $user_info = api_get_user_info($user_id);
  32. $user_anonymous = api_get_anonymous_id();
  33. $current_user_id = api_get_user_id();
  34. // setting the name of the tool
  35. if (UserManager::is_admin($user_id)) {
  36. $tool_name= get_lang('AssignUsersToPlatformAdministrator');
  37. } else if ($user_info['status'] == SESSIONADMIN) {
  38. $tool_name= get_lang('AssignUsersToSessionsAdministrator');
  39. } else {
  40. $tool_name= get_lang('AssignUsersToHumanResourcesManager');
  41. }
  42. $add_type = 'multiple';
  43. if(isset($_GET['add_type']) && $_GET['add_type']!=''){
  44. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  45. }
  46. if (!api_is_platform_admin()) {
  47. api_not_allowed(true);
  48. }
  49. function search_users($needle,$type) {
  50. global $_configuration,$tbl_access_url_rel_user, $tbl_user, $user_anonymous, $current_user_id, $user_id;
  51. $xajax_response = new XajaxResponse();
  52. $return = '';
  53. if(!empty($needle) && !empty($type)) {
  54. // xajax send utf8 datas... datas in db can be non-utf8 datas
  55. $charset = api_get_system_encoding();
  56. $needle = api_convert_encoding($needle, $charset, 'utf-8');
  57. $assigned_users_to_hrm = UserManager::get_users_followed_by_drh($user_id);
  58. $assigned_users_id = array_keys($assigned_users_to_hrm);
  59. $without_assigned_users = '';
  60. if (count($assigned_users_id) > 0) {
  61. $without_assigned_users = " AND user.user_id NOT IN(".implode(',',$assigned_users_id).")";
  62. }
  63. if ($_configuration['multiple_access_urls']) {
  64. $sql = "SELECT user.user_id, username, lastname, firstname FROM $tbl_user user LEFT JOIN $tbl_access_url_rel_user au ON (au.user_id = user.user_id)
  65. WHERE ".(api_sort_by_first_name() ? 'firstname' : 'lastname')." LIKE '$needle%' AND status NOT IN(".DRH.", ".SESSIONADMIN.") AND user.user_id NOT IN ($user_anonymous, $current_user_id, $user_id) $without_assigned_users AND access_url_id = ".api_get_current_access_url_id()."";
  66. } else {
  67. $sql = "SELECT user_id, username, lastname, firstname FROM $tbl_user user
  68. WHERE ".(api_sort_by_first_name() ? 'firstname' : 'lastname')." LIKE '$needle%' AND status NOT IN(".DRH.", ".SESSIONADMIN.") AND user_id NOT IN ($user_anonymous, $current_user_id, $user_id) $without_assigned_users";
  69. }
  70. $rs = Database::query($sql);
  71. $xajax_response -> addAssign('ajax_list_users_multiple','innerHTML',api_utf8_encode($return));
  72. if ($type == 'single') {
  73. $tbl_user_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  74. $access_url_id = api_get_current_access_url_id();
  75. $sql = 'SELECT user.user_id, username, lastname, firstname FROM '.$tbl_user.' user
  76. INNER JOIN '.$tbl_user_rel_access_url.' url_user ON (url_user.user_id=user.user_id)
  77. WHERE access_url_id = '.$access_url_id.' AND (username LIKE "'.$needle.'%"
  78. OR firstname LIKE "'.$needle.'%"
  79. OR lastname LIKE "'.$needle.'%") AND user.status<>6 AND user.status<>'.DRH.' '.
  80. $order_clause.
  81. ' LIMIT 11';
  82. $rs = Database::query($sql);
  83. $i = 0;
  84. while ($user = Database :: fetch_array($rs)) {
  85. $i++;
  86. if ($i<=10) {
  87. $person_name = api_get_person_name($user['firstname'], $user['lastname']);
  88. $return .= '<a href="javascript: void(0);" onclick="javascript: add_user_to_user(\''.$user['user_id'].'\',\''.$person_name.' ('.$user['username'].')'.'\')">'.$person_name.' ('.$user['username'].')</a><br />';
  89. } else {
  90. $return .= '...<br />';
  91. }
  92. }
  93. $xajax_response -> addAssign('ajax_list_users_single','innerHTML',api_utf8_encode($return));
  94. } else {
  95. $return .= '<select id="origin" name="NoAssignedUsersList[]" multiple="multiple" size="20" style="width:340px;">';
  96. while($user = Database :: fetch_array($rs)) {
  97. $person_name = api_get_person_name($user['firstname'], $user['lastname']);
  98. $return .= '<option value="'.$user['user_id'].'" title="'.htmlspecialchars($person_name,ENT_QUOTES).'">'.$person_name.' ('.$user['username'].')</option>';
  99. }
  100. $return .= '</select>';
  101. $xajax_response -> addAssign('ajax_list_users_multiple','innerHTML',api_utf8_encode($return));
  102. }
  103. }
  104. return $xajax_response;
  105. }
  106. $xajax -> processRequests();
  107. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  108. $htmlHeadXtra[] = '
  109. <script type="text/javascript">
  110. <!--
  111. function add_user_to_user (code, content) {
  112. document.getElementById("user_to_add").value = "";
  113. document.getElementById("ajax_list_users_single").innerHTML = "";
  114. destination = document.getElementById("destination");
  115. for (i=0;i<destination.length;i++) {
  116. if(destination.options[i].text == content) {
  117. return false;
  118. }
  119. }
  120. destination.options[destination.length] = new Option(content,code);
  121. destination.selectedIndex = -1;
  122. sortOptions(destination.options);
  123. }
  124. function moveItem(origin , destination) {
  125. for(var i = 0 ; i<origin.options.length ; i++) {
  126. if(origin.options[i].selected) {
  127. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  128. origin.options[i]=null;
  129. i = i-1;
  130. }
  131. }
  132. destination.selectedIndex = -1;
  133. sortOptions(destination.options);
  134. }
  135. function sortOptions(options) {
  136. var newOptions = new Array();
  137. for (i = 0 ; i<options.length ; i++) {
  138. newOptions[i] = options[i];
  139. }
  140. newOptions = newOptions.sort(mysort);
  141. options.length = 0;
  142. for(i = 0 ; i < newOptions.length ; i++){
  143. options[i] = newOptions[i];
  144. }
  145. }
  146. function mysort(a, b) {
  147. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  148. return 1;
  149. }
  150. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  151. return -1;
  152. }
  153. return 0;
  154. }
  155. function valide() {
  156. var options = document.getElementById("destination").options;
  157. for (i = 0 ; i<options.length ; i++) {
  158. options[i].selected = true;
  159. }
  160. document.forms.formulaire.submit();
  161. }
  162. function remove_item(origin) {
  163. for(var i = 0 ; i<origin.options.length ; i++) {
  164. if(origin.options[i].selected) {
  165. origin.options[i]=null;
  166. i = i-1;
  167. }
  168. }
  169. }
  170. -->
  171. </script>';
  172. $formSent=0;
  173. $errorMsg = $firstLetterUser = '';
  174. $UserList = array();
  175. $msg = '';
  176. if (intval($_POST['formSent']) == 1) {
  177. $user_list = $_POST['UsersList'];
  178. $affected_rows = UserManager::suscribe_users_to_hr_manager($user_id,$user_list);
  179. if ($affected_rows) {
  180. $msg = get_lang('AssignedUsersHaveBeenUpdatedSuccessfully');
  181. }
  182. }
  183. // display header
  184. Display::display_header($tool_name);
  185. // actions
  186. echo '<div class="actions">
  187. <span style="float: right;margin:0px;padding:0px;">
  188. <a href="dashboard_add_courses_to_user.php?user='.$user_id.'">'.Display::return_icon('course_add.gif', get_lang('AssignCourses'), array('style'=>'vertical-align:middle')).' '.get_lang('AssignCourses').'</a>
  189. <a href="dashboard_add_sessions_to_user.php?user='.$user_id.'">'.Display::return_icon('view_more_stats.gif', get_lang('AssignSessions'), array('style'=>'vertical-align:middle')).' '.get_lang('AssignSessions').'</a></span>
  190. </div>';
  191. echo Display::page_header(sprintf(get_lang('AssignUsersToX'), api_get_person_name($user_info['firstname'], $user_info['lastname'])));
  192. $assigned_users_to_hrm = UserManager::get_users_followed_by_drh($user_id);
  193. $assigned_users_id = array_keys($assigned_users_to_hrm);
  194. $without_assigned_users = '';
  195. if (count($assigned_users_id) > 0) {
  196. $without_assigned_users = " user.user_id NOT IN(".implode(',',$assigned_users_id).") AND ";
  197. }
  198. $search_user = '';
  199. if (isset($_POST['firstLetterUser'])) {
  200. $needle = Database::escape_string($_POST['firstLetterUser']);
  201. $search_user ="AND ".(api_sort_by_first_name() ? 'firstname' : 'lastname')." LIKE '$needle%'";
  202. }
  203. if ($_configuration['multiple_access_urls']) {
  204. $sql = "SELECT user.user_id, username, lastname, firstname FROM $tbl_user user LEFT JOIN $tbl_access_url_rel_user au ON (au.user_id = user.user_id)
  205. WHERE $without_assigned_users user.user_id NOT IN ($user_anonymous, $current_user_id, $user_id) AND status NOT IN(".DRH.", ".SESSIONADMIN.") $search_user AND access_url_id = ".api_get_current_access_url_id()."
  206. ORDER BY firstname";
  207. } else {
  208. $sql = "SELECT user_id, username, lastname, firstname FROM $tbl_user user
  209. WHERE $without_assigned_users user_id NOT IN ($user_anonymous, $current_user_id, $user_id) AND status NOT IN(".DRH.", ".SESSIONADMIN.") $search_user
  210. ORDER BY firstname ";
  211. }
  212. $result = Database::query($sql);
  213. ?>
  214. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?user=<?php echo $user_id ?>" style="margin:0px;" <?php if($ajax_search){echo ' onsubmit="valide();"';}?>>
  215. <input type="hidden" name="formSent" value="1" />
  216. <?php
  217. if(!empty($msg)) {
  218. Display::display_normal_message($msg); //main API
  219. }
  220. ?>
  221. <table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">
  222. <tr>
  223. <td align="left"></td>
  224. <td align="left"></td>
  225. <td width="" align="center"> &nbsp; </td>
  226. </tr>
  227. <tr>
  228. <td width="45%" align="center"><b><?php echo get_lang('UserListInPlatform') ?> :</b></td>
  229. <td width="10%">&nbsp;</td>
  230. <td align="center" width="45%"><b>
  231. <?php
  232. if (UserManager::is_admin($user_id)) {
  233. echo get_lang('AssignedUsersListToPlatformAdministrator');
  234. } else if ($user_info['status'] == SESSIONADMIN) {
  235. echo get_lang('AssignedUsersListToSessionsAdministrator');
  236. } else {
  237. echo get_lang('AssignedUsersListToHumanResourcesManager');
  238. }
  239. ?>
  240. :</b></td>
  241. </tr>
  242. <?php if($add_type == 'multiple') { ?>
  243. <tr><td width="45%" align="center">
  244. <?php echo get_lang('FirstLetterUser');?> :
  245. <select name="firstLetterUser" onchange = "xajax_search_users(this.value,'multiple')">
  246. <option value="%">--</option>
  247. <?php
  248. echo Display :: get_alphabet_options($_POST['firstLetterUser']);
  249. ?>
  250. </select>
  251. <input type="text" id="user_to_add" onkeyup="xajax_search_users(this.value,'single')" onclick="moveItem(document.getElementById('user_to_add'), document.getElementById('destination'))" />
  252. <div id="ajax_list_users_single"></div>
  253. </td>
  254. <td>&nbsp;</td></tr>
  255. <?php } ?>
  256. <tr>
  257. <td width="45%" align="center">
  258. <div id="ajax_list_users_multiple">
  259. <select id="origin" name="NoAssignedUsersList[]" multiple="multiple" size="20" style="width:340px;">
  260. <?php
  261. while ($enreg = Database::fetch_array($result)) {
  262. $person_name = api_get_person_name($enreg['firstname'], $enreg['lastname']);
  263. ?>
  264. <option value="<?php echo $enreg['user_id']; ?>" <?php echo 'title="'.htmlspecialchars($person_name,ENT_QUOTES).'"';?>><?php echo $person_name.' ('.$enreg['username'].')'; ?></option>
  265. <?php } ?>
  266. </select></div>
  267. </td>
  268. <td width="10%" valign="middle" align="center">
  269. <?php
  270. if ($ajax_search) {
  271. ?>
  272. <button class="arrowl" type="button" onclick="remove_item(document.getElementById('destination'))"></button>
  273. <?php
  274. }
  275. else
  276. {
  277. ?>
  278. <button class="arrowr" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))"></button>
  279. <br /><br />
  280. <button class="arrowl" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))"></button>
  281. <?php
  282. }
  283. ?>
  284. <br /><br /><br /><br /><br /><br />
  285. <?php
  286. echo '<button class="save" type="button" value="" onclick="valide()" >'.$tool_name.'</button>';
  287. ?>
  288. </td>
  289. <td width="45%" align="center">
  290. <select id='destination' name="UsersList[]" multiple="multiple" size="20" style="width:320px;">
  291. <?php
  292. if (is_array($assigned_users_to_hrm)) {
  293. foreach($assigned_users_to_hrm as $enreg) {
  294. $person_name = api_get_person_name($enreg['firstname'], $enreg['lastname']);
  295. ?>
  296. <option value="<?php echo $enreg['user_id']; ?>" <?php echo 'title="'.htmlspecialchars($person_name,ENT_QUOTES).'"'; ?>><?php echo $person_name.' ('.$enreg['username'].')'; ?></option>
  297. <?php }
  298. }?>
  299. </select></td>
  300. </tr>
  301. </table>
  302. </form>
  303. <?php
  304. Display::display_footer();