dashboard_add_users_to_user.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. require_once '../inc/global.inc.php';
  12. require_once '../inc/lib/xajax/xajax.inc.php';
  13. global $_configuration;
  14. $ajax_search = false;
  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. $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. $firstLetterUser = isset($_POST['firstLetterUser']) ? $_POST['firstLetterUser'] : null;
  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. {
  51. global $tbl_access_url_rel_user, $tbl_user, $user_anonymous, $current_user_id, $user_id;
  52. $xajax_response = new XajaxResponse();
  53. $return = '';
  54. if (!empty($needle) && !empty($type)) {
  55. // xajax send utf8 datas... datas in db can be non-utf8 datas
  56. $charset = api_get_system_encoding();
  57. $needle = api_convert_encoding($needle, $charset, 'utf-8');
  58. $assigned_users_to_hrm = UserManager::get_users_followed_by_drh($user_id);
  59. $assigned_users_id = array_keys($assigned_users_to_hrm);
  60. $without_assigned_users = '';
  61. $westernOrder = api_is_western_name_order();
  62. if ($westernOrder) {
  63. $order_clause = " ORDER BY firstname, lastname";
  64. } else {
  65. $order_clause = " ORDER BY lastname, firstname";
  66. }
  67. if (count($assigned_users_id) > 0) {
  68. $without_assigned_users = " AND user.user_id NOT IN(".implode(',', $assigned_users_id).")";
  69. }
  70. if (api_is_multiple_url_enabled()) {
  71. $sql = "SELECT user.user_id, username, lastname, firstname
  72. FROM $tbl_user user
  73. LEFT JOIN $tbl_access_url_rel_user au ON (au.user_id = user.user_id)
  74. WHERE
  75. ".(api_sort_by_first_name() ? 'firstname' : 'lastname')." LIKE '$needle%' AND
  76. status NOT IN(".DRH.", ".SESSIONADMIN.") AND
  77. user.user_id NOT IN ($user_anonymous, $current_user_id, $user_id)
  78. $without_assigned_users AND
  79. access_url_id = ".api_get_current_access_url_id()."
  80. $order_clause
  81. ";
  82. } else {
  83. $sql = "SELECT user_id, username, lastname, firstname
  84. FROM $tbl_user user
  85. WHERE
  86. ".(api_sort_by_first_name() ? 'firstname' : 'lastname')." LIKE '$needle%' AND
  87. status NOT IN(".DRH.", ".SESSIONADMIN.") AND
  88. user_id NOT IN ($user_anonymous, $current_user_id, $user_id)
  89. $without_assigned_users
  90. $order_clause
  91. ";
  92. }
  93. $rs = Database::query($sql);
  94. $xajax_response->addAssign('ajax_list_users_multiple','innerHTML',api_utf8_encode($return));
  95. if ($type == 'single') {
  96. $tbl_user_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  97. $access_url_id = api_get_current_access_url_id();
  98. $sql = 'SELECT user.user_id, username, lastname, firstname
  99. FROM '.$tbl_user.' user
  100. INNER JOIN '.$tbl_user_rel_access_url.' url_user ON (url_user.user_id=user.user_id)
  101. WHERE
  102. access_url_id = '.$access_url_id.' AND
  103. (
  104. username LIKE "'.$needle.'%" OR
  105. firstname LIKE "'.$needle.'%" OR
  106. lastname LIKE "'.$needle.'%"
  107. ) AND
  108. user.status<>6 AND user.status<>'.DRH.' '.
  109. $order_clause.
  110. ' LIMIT 11';
  111. $rs = Database::query($sql);
  112. $i = 0;
  113. while ($user = Database :: fetch_array($rs)) {
  114. $i++;
  115. if ($i <= 10) {
  116. $person_name = api_get_person_name($user['firstname'], $user['lastname']);
  117. $return .= '<a href="javascript: void(0);" onclick="javascript: add_user_to_user(\''.$user['user_id'].'\',\''.$person_name.' ('.$user['username'].')'.'\')">'.$person_name.' ('.$user['username'].')</a><br />';
  118. } else {
  119. $return .= '...<br />';
  120. }
  121. }
  122. $xajax_response->addAssign('ajax_list_users_single','innerHTML',api_utf8_encode($return));
  123. } else {
  124. $return .= '<select id="origin" name="NoAssignedUsersList[]" multiple="multiple" size="20" style="width:340px;">';
  125. while($user = Database :: fetch_array($rs)) {
  126. $person_name = api_get_person_name($user['firstname'], $user['lastname']);
  127. $return .= '<option value="'.$user['user_id'].'" title="'.htmlspecialchars($person_name,ENT_QUOTES).'">'.$person_name.' ('.$user['username'].')</option>';
  128. }
  129. $return .= '</select>';
  130. $xajax_response->addAssign('ajax_list_users_multiple', 'innerHTML', api_utf8_encode($return));
  131. }
  132. }
  133. return $xajax_response;
  134. }
  135. $xajax->processRequests();
  136. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  137. $htmlHeadXtra[] = '
  138. <script type="text/javascript">
  139. function add_user_to_user (code, content) {
  140. document.getElementById("user_to_add").value = "";
  141. document.getElementById("ajax_list_users_single").innerHTML = "";
  142. destination = document.getElementById("destination");
  143. for (i=0;i<destination.length;i++) {
  144. if(destination.options[i].text == content) {
  145. return false;
  146. }
  147. }
  148. destination.options[destination.length] = new Option(content,code);
  149. destination.selectedIndex = -1;
  150. sortOptions(destination.options);
  151. }
  152. function moveItem(origin , destination) {
  153. for(var i = 0 ; i<origin.options.length ; i++) {
  154. if(origin.options[i].selected) {
  155. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  156. origin.options[i]=null;
  157. i = i-1;
  158. }
  159. }
  160. destination.selectedIndex = -1;
  161. sortOptions(destination.options);
  162. }
  163. function sortOptions(options) {
  164. var newOptions = new Array();
  165. for (i = 0 ; i<options.length ; i++) {
  166. newOptions[i] = options[i];
  167. }
  168. newOptions = newOptions.sort(mysort);
  169. options.length = 0;
  170. for(i = 0 ; i < newOptions.length ; i++){
  171. options[i] = newOptions[i];
  172. }
  173. }
  174. function mysort(a, b) {
  175. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  176. return 1;
  177. }
  178. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  179. return -1;
  180. }
  181. return 0;
  182. }
  183. function valide() {
  184. var options = document.getElementById("destination").options;
  185. for (i = 0 ; i<options.length ; i++) {
  186. options[i].selected = true;
  187. }
  188. document.forms.formulaire.submit();
  189. }
  190. function remove_item(origin) {
  191. for(var i = 0 ; i<origin.options.length ; i++) {
  192. if(origin.options[i].selected) {
  193. origin.options[i]=null;
  194. i = i-1;
  195. }
  196. }
  197. }
  198. </script>';
  199. $formSent=0;
  200. $errorMsg = '';
  201. $UserList = array();
  202. // Filters
  203. $filters = array(
  204. array('type' => 'text', 'name' => 'username', 'label' => get_lang('Username')),
  205. array('type' => 'text', 'name' => 'firstname', 'label' => get_lang('FirstName')),
  206. array('type' => 'text', 'name' => 'lastname', 'label' => get_lang('LastName')),
  207. array('type' => 'text', 'name' => 'official_code', 'label' => get_lang('OfficialCode')),
  208. array('type' => 'text', 'name' => 'email', 'label' => get_lang('Email'))
  209. );
  210. $searchForm = new FormValidator('search', 'get', api_get_self().'?user='.$user_id);
  211. $searchForm->add_header(get_lang('AdvancedSearch'));
  212. $renderer =& $searchForm->defaultRenderer();
  213. $searchForm->addElement('hidden', 'user', $user_id);
  214. foreach ($filters as $param) {
  215. $searchForm->addElement($param['type'], $param['name'], $param['label']);
  216. }
  217. $searchForm->addElement('button', 'submit', get_lang('Search'));
  218. $filterData = array();
  219. if ($searchForm->validate()) {
  220. $filterData = $searchForm->getSubmitValues();
  221. }
  222. $conditions = array();
  223. if (!empty($filters) && !empty($filterData)) {
  224. foreach ($filters as $filter) {
  225. if (isset($filter['name']) && isset($filterData[$filter['name']])) {
  226. $value = $filterData[$filter['name']];
  227. if (!empty($value)) {
  228. $conditions[$filter['name']] = $value;
  229. }
  230. }
  231. }
  232. }
  233. $msg = '';
  234. if (isset($_POST['formSent']) && intval($_POST['formSent']) == 1) {
  235. $user_list = $_POST['UsersList'];
  236. $affected_rows = UserManager::suscribe_users_to_hr_manager($user_id, $user_list);
  237. if ($affected_rows) {
  238. $msg = get_lang('AssignedUsersHaveBeenUpdatedSuccessfully');
  239. }
  240. }
  241. // Display header
  242. Display::display_header($tool_name);
  243. // actions
  244. echo '<div class="actions">';
  245. echo '<span style="float: right;margin:0px;padding:0px;">
  246. <a href="dashboard_add_courses_to_user.php?user='.$user_id.'">'.
  247. Display::return_icon('course_add.gif', get_lang('AssignCourses'), array('style'=>'vertical-align:middle')).' '.get_lang('AssignCourses').'</a>
  248. <a href="dashboard_add_sessions_to_user.php?user='.$user_id.'">'.
  249. Display::return_icon('view_more_stats.gif', get_lang('AssignSessions'), array('style'=>'vertical-align:middle')).' '.get_lang('AssignSessions').'</a></span>';
  250. echo Display::url(get_lang('AdvancedSearch'), '#', array('class' => 'advanced_options', 'id' => 'advanced_search'));
  251. echo '</div>';
  252. echo '<div id="advanced_search_options" style="display:none">';
  253. $searchForm->display();
  254. echo '</div>';
  255. echo Display::page_header(
  256. sprintf(get_lang('AssignUsersToX'), api_get_person_name($user_info['firstname'], $user_info['lastname']))
  257. );
  258. $assigned_users_to_hrm = UserManager::get_users_followed_by_drh($user_id);
  259. $assigned_users_id = array_keys($assigned_users_to_hrm);
  260. $without_assigned_users = '';
  261. if (count($assigned_users_id) > 0) {
  262. $without_assigned_users = " user.user_id NOT IN(".implode(',',$assigned_users_id).") AND ";
  263. }
  264. $search_user = '';
  265. if (!empty($firstLetterUser)) {
  266. $needle = Database::escape_string($firstLetterUser);
  267. $search_user ="AND ".(api_sort_by_first_name() ? 'firstname' : 'lastname')." LIKE '$needle%'";
  268. }
  269. $sqlConditions = null;
  270. if (!empty($conditions)) {
  271. $temp_conditions = array();
  272. foreach ($conditions as $field => $value) {
  273. $field = Database::escape_string($field);
  274. $value = Database::escape_string($value);
  275. $temp_conditions[] = $field.' LIKE \'%'.$value.'%\'';
  276. }
  277. if (!empty($temp_conditions)) {
  278. $sqlConditions .= implode(' AND ', $temp_conditions);
  279. }
  280. if (!empty($sqlConditions)) {
  281. $sqlConditions = " AND $sqlConditions";
  282. }
  283. }
  284. if (api_is_multiple_url_enabled()) {
  285. $sql = "SELECT user.user_id, username, lastname, firstname
  286. FROM $tbl_user user LEFT JOIN $tbl_access_url_rel_user au ON (au.user_id = user.user_id)
  287. WHERE
  288. $without_assigned_users
  289. user.user_id NOT IN ($user_anonymous, $current_user_id, $user_id) AND
  290. status NOT IN(".DRH.", ".SESSIONADMIN.") $search_user AND
  291. access_url_id = ".api_get_current_access_url_id()."
  292. $sqlConditions
  293. ORDER BY firstname";
  294. } else {
  295. $sql = "SELECT user_id, username, lastname, firstname
  296. FROM $tbl_user user
  297. WHERE
  298. $without_assigned_users
  299. user_id NOT IN ($user_anonymous, $current_user_id, $user_id) AND
  300. status NOT IN(".DRH.", ".SESSIONADMIN.")
  301. $search_user
  302. $sqlConditions
  303. ORDER BY firstname ";
  304. }
  305. $result = Database::query($sql);
  306. ?>
  307. <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();"';}?>>
  308. <input type="hidden" name="formSent" value="1" />
  309. <?php
  310. if(!empty($msg)) {
  311. Display::display_normal_message($msg); //main API
  312. }
  313. ?>
  314. <table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">
  315. <tr>
  316. <td align="left"></td>
  317. <td align="left"></td>
  318. <td width="" align="center"> &nbsp; </td>
  319. </tr>
  320. <tr>
  321. <td width="45%" align="center"><b><?php echo get_lang('UserListInPlatform') ?> :</b></td>
  322. <td width="10%">&nbsp;</td>
  323. <td align="center" width="45%"><b>
  324. <?php
  325. if (UserManager::is_admin($user_id)) {
  326. echo get_lang('AssignedUsersListToPlatformAdministrator');
  327. } else if ($user_info['status'] == SESSIONADMIN) {
  328. echo get_lang('AssignedUsersListToSessionsAdministrator');
  329. } else {
  330. echo get_lang('AssignedUsersListToHumanResourcesManager');
  331. }
  332. ?>
  333. :</b></td>
  334. </tr>
  335. <?php if($add_type == 'multiple') { ?>
  336. <tr><td width="45%" align="center">
  337. <?php echo get_lang('FirstLetterUser');?> :
  338. <select name="firstLetterUser" onchange = "xajax_search_users(this.value,'multiple')">
  339. <option value="%">--</option>
  340. <?php
  341. echo Display::get_alphabet_options($firstLetterUser);
  342. ?>
  343. </select>
  344. <input type="text" id="user_to_add" onkeyup="xajax_search_users(this.value,'single')" onclick="moveItem(document.getElementById('user_to_add'), document.getElementById('destination'))" />
  345. <div id="ajax_list_users_single"></div>
  346. </td>
  347. <td>&nbsp;</td></tr>
  348. <?php } ?>
  349. <tr>
  350. <td width="45%" align="center">
  351. <div id="ajax_list_users_multiple">
  352. <select id="origin" name="NoAssignedUsersList[]" multiple="multiple" size="20" style="width:340px;">
  353. <?php
  354. while ($enreg = Database::fetch_array($result)) {
  355. $person_name = api_get_person_name($enreg['firstname'], $enreg['lastname']);
  356. ?>
  357. <option value="<?php echo $enreg['user_id']; ?>" <?php echo 'title="'.htmlspecialchars($person_name,ENT_QUOTES).'"';?>>
  358. <?php echo $person_name.' ('.$enreg['username'].')'; ?>
  359. </option>
  360. <?php } ?>
  361. </select></div>
  362. </td>
  363. <td width="10%" valign="middle" align="center">
  364. <?php if ($ajax_search) { ?>
  365. <button class="arrowl" type="button" onclick="remove_item(document.getElementById('destination'))"></button>
  366. <?php } else { ?>
  367. <button class="arrowr" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))"></button>
  368. <br /><br />
  369. <button class="arrowl" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))"></button>
  370. <?php
  371. }
  372. ?>
  373. <br /><br /><br /><br /><br /><br />
  374. <?php
  375. echo '<button class="save" type="button" value="" onclick="valide()" >'.$tool_name.'</button>';
  376. ?>
  377. </td>
  378. <td width="45%" align="center">
  379. <select id='destination' name="UsersList[]" multiple="multiple" size="20" style="width:320px;">
  380. <?php
  381. if (is_array($assigned_users_to_hrm)) {
  382. foreach($assigned_users_to_hrm as $enreg) {
  383. $person_name = api_get_person_name($enreg['firstname'], $enreg['lastname']);
  384. ?>
  385. <option value="<?php echo $enreg['user_id']; ?>" <?php echo 'title="'.htmlspecialchars($person_name,ENT_QUOTES).'"'; ?>>
  386. <?php echo $person_name.' ('.$enreg['username'].')'; ?>
  387. </option>
  388. <?php }
  389. }?>
  390. </select></td>
  391. </tr>
  392. </table>
  393. </form>
  394. <?php
  395. Display::display_footer();