dashboard_add_users_to_user.php 17 KB

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