dashboard_add_users_to_user.php 11 KB

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