dashboard_add_users_to_user.php 17 KB

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