dashboard_add_users_to_user.php 11 KB

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