dashboard_add_users_to_user.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. ==============================================================================
  5. * Interface for assigning users to Human Resources Manager
  6. * @package chamilo.admin
  7. ==============================================================================
  8. */
  9. // name of the language file that needs to be included
  10. $language_file='admin';
  11. // resetting the course id
  12. $cidReset=true;
  13. // including some necessary dokeos files
  14. require_once '../inc/global.inc.php';
  15. require_once '../inc/lib/xajax/xajax.inc.php';
  16. require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
  17. // create an ajax object
  18. $xajax = new xajax();
  19. $xajax -> registerFunction ('search_users');
  20. // setting the section (for the tabs)
  21. $this_section = SECTION_PLATFORM_ADMIN;
  22. // Access restrictions
  23. api_protect_admin_script(true);
  24. // setting breadcrumbs
  25. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  26. $interbreadcrumb[] = array('url' => 'user_list.php','name' => get_lang('UserList'));
  27. // Database Table Definitions
  28. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  29. $tbl_user_rel_user = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  30. // setting the name of the tool
  31. $tool_name= get_lang('AssignUsersToHumanResourcesManager');
  32. // initializing variables
  33. $id_session=intval($_GET['id_session']);
  34. $hrm_id = intval($_GET['user']);
  35. $hrm_info = api_get_user_info($hrm_id);
  36. $user_anonymous = api_get_anonymous_id();
  37. $current_user_id = api_get_user_id();
  38. $add_type = 'multiple';
  39. if(isset($_GET['add_type']) && $_GET['add_type']!=''){
  40. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  41. }
  42. if (!api_is_platform_admin()) {
  43. api_not_allowed(true);
  44. }
  45. function search_users($needle,$type) {
  46. global $tbl_user, $tbl_user_rel_user, $user_anonymous, $current_user_id, $hrm_id;
  47. $xajax_response = new XajaxResponse();
  48. $return = '';
  49. if(!empty($needle) && !empty($type)) {
  50. // xajax send utf8 datas... datas in db can be non-utf8 datas
  51. $charset = api_get_setting('platform_charset');
  52. $needle = api_convert_encoding($needle, $charset, 'utf-8');
  53. $assigned_users_to_hrm = UserManager::get_assigned_users_to_hr_manager($hrm_id);
  54. $assigned_users_id = array_keys($assigned_users_to_hrm);
  55. $without_assigned_users = '';
  56. if (count($assigned_users_id) > 0) {
  57. $without_assigned_users = " AND user_id NOT IN(".implode(',',$assigned_users_id).")";
  58. }
  59. $sql = "SELECT user_id, username, lastname, firstname FROM $tbl_user user
  60. WHERE ".(api_sort_by_first_name() ? 'firstname' : 'lastname')." LIKE '$needle%' AND user_id NOT IN ($user_anonymous, $current_user_id, $hrm_id) $without_assigned_users";
  61. $rs = Database::query($sql);
  62. $user_list = array();
  63. $return .= '<select id="origin" name="NoAssignedUsersList[]" multiple="multiple" size="20" style="width:340px;">';
  64. while($user = Database :: fetch_array($rs)) {
  65. $user_list[] = $user['id'];
  66. $person_name = api_get_person_name($user['firstname'], $user['lastname']);
  67. $return .= '<option value="'.$user['user_id'].'" title="'.htmlspecialchars($person_name,ENT_QUOTES).'">'.$person_name.' ('.$user['username'].')</option>';
  68. }
  69. $return .= '</select>';
  70. $xajax_response -> addAssign('ajax_list_users_multiple','innerHTML',api_utf8_encode($return));
  71. }
  72. $_SESSION['user_list'] = $user_list;
  73. return $xajax_response;
  74. }
  75. $xajax -> processRequests();
  76. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  77. $htmlHeadXtra[] = '
  78. <script type="text/javascript">
  79. <!--
  80. function moveItem(origin , destination) {
  81. for(var i = 0 ; i<origin.options.length ; i++) {
  82. if(origin.options[i].selected) {
  83. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  84. origin.options[i]=null;
  85. i = i-1;
  86. }
  87. }
  88. destination.selectedIndex = -1;
  89. sortOptions(destination.options);
  90. }
  91. function sortOptions(options) {
  92. var newOptions = new Array();
  93. for (i = 0 ; i<options.length ; i++) {
  94. newOptions[i] = options[i];
  95. }
  96. newOptions = newOptions.sort(mysort);
  97. options.length = 0;
  98. for(i = 0 ; i < newOptions.length ; i++){
  99. options[i] = newOptions[i];
  100. }
  101. }
  102. function mysort(a, b) {
  103. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  104. return 1;
  105. }
  106. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  107. return -1;
  108. }
  109. return 0;
  110. }
  111. function valide() {
  112. var options = document.getElementById("destination").options;
  113. for (i = 0 ; i<options.length ; i++) {
  114. options[i].selected = true;
  115. }
  116. document.forms.formulaire.submit();
  117. }
  118. function remove_item(origin) {
  119. for(var i = 0 ; i<origin.options.length ; i++) {
  120. if(origin.options[i].selected) {
  121. origin.options[i]=null;
  122. i = i-1;
  123. }
  124. }
  125. }
  126. -->
  127. </script>';
  128. $formSent=0;
  129. $errorMsg = $firstLetterUser = '';
  130. $UserList = array();
  131. $msg = '';
  132. if (intval($_POST['formSent']) == 1) {
  133. $user_list = $_POST['UsersList'];
  134. $affected_rows = UserManager::suscribe_users_to_hr_manager($hrm_id,$user_list);
  135. if ($affected_rows) {
  136. $msg = get_lang('AssignedUsersHasBeenUpdatedSuccesslly');
  137. }
  138. }
  139. // display the dokeos header
  140. Display::display_header($tool_name);
  141. echo '<div class="row"><div class="form_header">'.get_lang('AssignedUsersTo').'&nbsp;'.api_get_person_name($hrm_info['firstname'], $hrm_info['lastname']).'</div></div><br />';
  142. // *******************
  143. $assigned_users_to_hrm = UserManager::get_assigned_users_to_hr_manager($hrm_id);
  144. $assigned_users_id = array_keys($assigned_users_to_hrm);
  145. $without_assigned_users = '';
  146. if (count($assigned_users_id) > 0) {
  147. $without_assigned_users = " user_id NOT IN(".implode(',',$assigned_users_id).") AND ";
  148. }
  149. $search_user = '';
  150. if (isset($_POST['firstLetterUser'])) {
  151. $needle = Database::escape_string($_POST['firstLetterUser']);
  152. $search_user ="AND ".(api_sort_by_first_name() ? 'firstname' : 'lastname')." LIKE '$needle%'";
  153. }
  154. $sql = "SELECT user_id, username, lastname, firstname FROM $tbl_user user
  155. WHERE $without_assigned_users user_id NOT IN ($user_anonymous, $current_user_id, $hrm_id) $search_user ";
  156. $result = Database::query($sql);
  157. ?>
  158. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?user=<?php echo $hrm_id ?>" style="margin:0px;" <?php if($ajax_search){echo ' onsubmit="valide();"';}?>>
  159. <input type="hidden" name="formSent" value="1" />
  160. <?php
  161. if(!empty($msg)) {
  162. Display::display_normal_message($msg); //main API
  163. }
  164. ?>
  165. <table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">
  166. <tr>
  167. <td align="left"></td>
  168. <td align="left"></td>
  169. <td width="" align="center"> &nbsp; </td>
  170. </tr>
  171. <tr>
  172. <td width="45%" align="center"><b><?php echo get_lang('UserListInPlatform') ?> :</b></td>
  173. <td width="10%">&nbsp;</td>
  174. <td align="center" width="45%"><b><?php echo get_lang('AssignedUsersListToHumanResourceManager') ?> :</b></td>
  175. </tr>
  176. <?php if($add_type == 'multiple') { ?>
  177. <tr><td width="45%" align="center">
  178. <?php echo get_lang('FirstLetterUser');?> :
  179. <select name="firstLetterUser" onchange = "xajax_search_users(this.value,'multiple')">
  180. <option value="%">--</option>
  181. <?php
  182. echo Display :: get_alphabet_options($_POST['firstLetterUser']);
  183. ?>
  184. </select>
  185. </td>
  186. <td>&nbsp;</td></tr>
  187. <?php } ?>
  188. <tr>
  189. <td width="45%" align="center">
  190. <div id="ajax_list_users_multiple">
  191. <select id="origin" name="NoAssignedUsersList[]" multiple="multiple" size="20" style="width:340px;">
  192. <?php
  193. while ($enreg = Database::fetch_array($result)) {
  194. $person_name = api_get_person_name($enreg['firstname'], $enreg['lastname']);
  195. ?>
  196. <option value="<?php echo $enreg['user_id']; ?>" <?php echo 'title="'.htmlspecialchars($person_name,ENT_QUOTES).'"';?>><?php echo $person_name.' ('.$enreg['username'].')'; ?></option>
  197. <?php } ?>
  198. </select></div>
  199. </td>
  200. <td width="10%" valign="middle" align="center">
  201. <?php
  202. if ($ajax_search) {
  203. ?>
  204. <button class="arrowl" type="button" onclick="remove_item(document.getElementById('destination'))"></button>
  205. <?php
  206. }
  207. else
  208. {
  209. ?>
  210. <button class="arrowr" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))"></button>
  211. <br /><br />
  212. <button class="arrowl" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))"></button>
  213. <?php
  214. }
  215. ?>
  216. <br /><br /><br /><br /><br /><br />
  217. <?php
  218. echo '<button class="save" type="button" value="" onclick="valide()" >'.get_lang('AssignUsersToHumanResourceManager').'</button>';
  219. ?>
  220. </td>
  221. <td width="45%" align="center">
  222. <select id='destination' name="UsersList[]" multiple="multiple" size="20" style="width:320px;">
  223. <?php
  224. if (is_array($assigned_users_to_hrm)) {
  225. foreach($assigned_users_to_hrm as $enreg) {
  226. $person_name = api_get_person_name($enreg['firstname'], $enreg['lastname']);
  227. ?>
  228. <option value="<?php echo $enreg['user_id']; ?>" <?php echo 'title="'.htmlspecialchars($person_name,ENT_QUOTES).'"'; ?>><?php echo $person_name.' ('.$enreg['username'].')'; ?></option>
  229. <?php }
  230. }?>
  231. </select></td>
  232. </tr>
  233. </table>
  234. </form>
  235. <?php
  236. /*
  237. ==============================================================================
  238. FOOTER
  239. ==============================================================================
  240. */
  241. Display::display_footer();
  242. ?>