dashboard_add_users_to_user.php 9.7 KB

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