dashboard_add_users_to_user.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. $return .= '<select id="origin" name="NoAssignedUsersList[]" multiple="multiple" size="20" style="width:340px;">';
  72. while($user = Database :: fetch_array($rs)) {
  73. $person_name = api_get_person_name($user['firstname'], $user['lastname']);
  74. $return .= '<option value="'.$user['user_id'].'" title="'.htmlspecialchars($person_name,ENT_QUOTES).'">'.$person_name.' ('.$user['username'].')</option>';
  75. }
  76. $return .= '</select>';
  77. $xajax_response -> addAssign('ajax_list_users_multiple','innerHTML',api_utf8_encode($return));
  78. }
  79. return $xajax_response;
  80. }
  81. $xajax -> processRequests();
  82. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  83. $htmlHeadXtra[] = '
  84. <script type="text/javascript">
  85. <!--
  86. function moveItem(origin , destination) {
  87. for(var i = 0 ; i<origin.options.length ; i++) {
  88. if(origin.options[i].selected) {
  89. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  90. origin.options[i]=null;
  91. i = i-1;
  92. }
  93. }
  94. destination.selectedIndex = -1;
  95. sortOptions(destination.options);
  96. }
  97. function sortOptions(options) {
  98. var newOptions = new Array();
  99. for (i = 0 ; i<options.length ; i++) {
  100. newOptions[i] = options[i];
  101. }
  102. newOptions = newOptions.sort(mysort);
  103. options.length = 0;
  104. for(i = 0 ; i < newOptions.length ; i++){
  105. options[i] = newOptions[i];
  106. }
  107. }
  108. function mysort(a, b) {
  109. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  110. return 1;
  111. }
  112. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  113. return -1;
  114. }
  115. return 0;
  116. }
  117. function valide() {
  118. var options = document.getElementById("destination").options;
  119. for (i = 0 ; i<options.length ; i++) {
  120. options[i].selected = true;
  121. }
  122. document.forms.formulaire.submit();
  123. }
  124. function remove_item(origin) {
  125. for(var i = 0 ; i<origin.options.length ; i++) {
  126. if(origin.options[i].selected) {
  127. origin.options[i]=null;
  128. i = i-1;
  129. }
  130. }
  131. }
  132. -->
  133. </script>';
  134. $formSent=0;
  135. $errorMsg = $firstLetterUser = '';
  136. $UserList = array();
  137. $msg = '';
  138. if (intval($_POST['formSent']) == 1) {
  139. $user_list = $_POST['UsersList'];
  140. $affected_rows = UserManager::suscribe_users_to_hr_manager($user_id,$user_list);
  141. if ($affected_rows) {
  142. $msg = get_lang('AssignedUsersHaveBeenUpdatedSuccessfully');
  143. }
  144. }
  145. // display header
  146. Display::display_header($tool_name);
  147. // actions
  148. echo '<div class="actions">
  149. <span style="float: right;margin:0px;padding:0px;">
  150. <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>
  151. <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>
  152. </div>';
  153. echo Display::page_header(sprintf(get_lang('AssignUsersToX'), api_get_person_name($user_info['firstname'], $user_info['lastname'])));
  154. $assigned_users_to_hrm = UserManager::get_users_followed_by_drh($user_id);
  155. $assigned_users_id = array_keys($assigned_users_to_hrm);
  156. $without_assigned_users = '';
  157. if (count($assigned_users_id) > 0) {
  158. $without_assigned_users = " user.user_id NOT IN(".implode(',',$assigned_users_id).") AND ";
  159. }
  160. $search_user = '';
  161. if (isset($_POST['firstLetterUser'])) {
  162. $needle = Database::escape_string($_POST['firstLetterUser']);
  163. $search_user ="AND ".(api_sort_by_first_name() ? 'firstname' : 'lastname')." LIKE '$needle%'";
  164. }
  165. if ($_configuration['multiple_access_urls']) {
  166. $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)
  167. 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()."
  168. ORDER BY firstname";
  169. } else {
  170. $sql = "SELECT user_id, username, lastname, firstname FROM $tbl_user user
  171. WHERE $without_assigned_users user_id NOT IN ($user_anonymous, $current_user_id, $user_id) AND status NOT IN(".DRH.", ".SESSIONADMIN.") $search_user
  172. ORDER BY firstname ";
  173. }
  174. $result = Database::query($sql);
  175. ?>
  176. <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();"';}?>>
  177. <input type="hidden" name="formSent" value="1" />
  178. <?php
  179. if(!empty($msg)) {
  180. Display::display_normal_message($msg); //main API
  181. }
  182. ?>
  183. <table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">
  184. <tr>
  185. <td align="left"></td>
  186. <td align="left"></td>
  187. <td width="" align="center"> &nbsp; </td>
  188. </tr>
  189. <tr>
  190. <td width="45%" align="center"><b><?php echo get_lang('UserListInPlatform') ?> :</b></td>
  191. <td width="10%">&nbsp;</td>
  192. <td align="center" width="45%"><b>
  193. <?php
  194. if (UserManager::is_admin($user_id)) {
  195. echo get_lang('AssignedUsersListToPlatformAdministrator');
  196. } else if ($user_info['status'] == SESSIONADMIN) {
  197. echo get_lang('AssignedUsersListToSessionsAdministrator');
  198. } else {
  199. echo get_lang('AssignedUsersListToHumanResourcesManager');
  200. }
  201. ?>
  202. :</b></td>
  203. </tr>
  204. <?php if($add_type == 'multiple') { ?>
  205. <tr><td width="45%" align="center">
  206. <?php echo get_lang('FirstLetterUser');?> :
  207. <select name="firstLetterUser" onchange = "xajax_search_users(this.value,'multiple')">
  208. <option value="%">--</option>
  209. <?php
  210. echo Display :: get_alphabet_options($_POST['firstLetterUser']);
  211. ?>
  212. </select>
  213. </td>
  214. <td>&nbsp;</td></tr>
  215. <?php } ?>
  216. <tr>
  217. <td width="45%" align="center">
  218. <div id="ajax_list_users_multiple">
  219. <select id="origin" name="NoAssignedUsersList[]" multiple="multiple" size="20" style="width:340px;">
  220. <?php
  221. while ($enreg = Database::fetch_array($result)) {
  222. $person_name = api_get_person_name($enreg['firstname'], $enreg['lastname']);
  223. ?>
  224. <option value="<?php echo $enreg['user_id']; ?>" <?php echo 'title="'.htmlspecialchars($person_name,ENT_QUOTES).'"';?>><?php echo $person_name.' ('.$enreg['username'].')'; ?></option>
  225. <?php } ?>
  226. </select></div>
  227. </td>
  228. <td width="10%" valign="middle" align="center">
  229. <?php
  230. if ($ajax_search) {
  231. ?>
  232. <button class="arrowl" type="button" onclick="remove_item(document.getElementById('destination'))"></button>
  233. <?php
  234. }
  235. else
  236. {
  237. ?>
  238. <button class="arrowr" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))"></button>
  239. <br /><br />
  240. <button class="arrowl" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))"></button>
  241. <?php
  242. }
  243. ?>
  244. <br /><br /><br /><br /><br /><br />
  245. <?php
  246. echo '<button class="save" type="button" value="" onclick="valide()" >'.$tool_name.'</button>';
  247. ?>
  248. </td>
  249. <td width="45%" align="center">
  250. <select id='destination' name="UsersList[]" multiple="multiple" size="20" style="width:320px;">
  251. <?php
  252. if (is_array($assigned_users_to_hrm)) {
  253. foreach($assigned_users_to_hrm as $enreg) {
  254. $person_name = api_get_person_name($enreg['firstname'], $enreg['lastname']);
  255. ?>
  256. <option value="<?php echo $enreg['user_id']; ?>" <?php echo 'title="'.htmlspecialchars($person_name,ENT_QUOTES).'"'; ?>><?php echo $person_name.' ('.$enreg['username'].')'; ?></option>
  257. <?php }
  258. }?>
  259. </select></td>
  260. </tr>
  261. </table>
  262. </form>
  263. <?php
  264. Display::display_footer();