dashboard_add_users_to_user.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Interface for assigning users to Human Resources Manager.
  5. *
  6. * @package chamilo.admin
  7. */
  8. // resetting the course id
  9. $cidReset = true;
  10. require_once __DIR__.'/../inc/global.inc.php';
  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[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
  21. $interbreadcrumb[] = ['url' => 'user_list.php', 'name' => get_lang('User list')];
  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 = isset($_GET['user']) ? (int) $_GET['user'] : 0;
  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. $isAdmin = UserManager::is_admin($user_id);
  34. if ($isAdmin) {
  35. $userStatus = PLATFORM_ADMIN;
  36. $tool_name = get_lang('AssignUsersToAdministrationistrator');
  37. } elseif ($user_info['status'] == SESSIONADMIN) {
  38. $tool_name = get_lang('Assign users to sessions administrator');
  39. } elseif ($user_info['status'] == STUDENT_BOSS) {
  40. $tool_name = get_lang('Assign users to superior');
  41. } else {
  42. $tool_name = get_lang('Assign users to Human Resources manager');
  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 = 'multiple')
  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. $assigned_users_to_hrm = [];
  58. switch ($userStatus) {
  59. case DRH:
  60. case PLATFORM_ADMIN:
  61. $assigned_users_to_hrm = UserManager::get_users_followed_by_drh($user_id);
  62. break;
  63. case STUDENT_BOSS:
  64. $assigned_users_to_hrm = UserManager::getUsersFollowedByStudentBoss($user_id);
  65. break;
  66. }
  67. $assigned_users_id = array_keys($assigned_users_to_hrm);
  68. $without_assigned_users = '';
  69. $westernOrder = api_is_western_name_order();
  70. if ($westernOrder) {
  71. $order_clause = " ORDER BY firstname, lastname";
  72. } else {
  73. $order_clause = " ORDER BY lastname, firstname";
  74. }
  75. if (count($assigned_users_id) > 0) {
  76. $without_assigned_users = " AND user.user_id NOT IN(".implode(',', $assigned_users_id).")";
  77. }
  78. if (api_is_multiple_url_enabled()) {
  79. $sql = "SELECT user.user_id, username, lastname, firstname
  80. FROM $tbl_user user
  81. LEFT JOIN $tbl_access_url_rel_user au ON (au.user_id = user.user_id)
  82. WHERE
  83. ".(api_sort_by_first_name() ? 'firstname' : 'lastname')." LIKE '$needle%' AND
  84. status NOT IN(".DRH.", ".SESSIONADMIN.", ".STUDENT_BOSS.") AND
  85. user.user_id NOT IN ($user_anonymous, $current_user_id, $user_id)
  86. $without_assigned_users AND
  87. access_url_id = ".api_get_current_access_url_id()."
  88. $order_clause
  89. ";
  90. } else {
  91. $sql = "SELECT user_id, username, lastname, firstname
  92. FROM $tbl_user user
  93. WHERE
  94. ".(api_sort_by_first_name() ? 'firstname' : 'lastname')." LIKE '$needle%' AND
  95. status NOT IN(".DRH.", ".SESSIONADMIN.", ".STUDENT_BOSS.") AND
  96. user_id NOT IN ($user_anonymous, $current_user_id, $user_id)
  97. $without_assigned_users
  98. $order_clause
  99. ";
  100. }
  101. $rs = Database::query($sql);
  102. $xajax_response->addAssign('ajax_list_users_multiple', 'innerHTML', api_utf8_encode($return));
  103. if ($type == 'single') {
  104. $tbl_user_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  105. $access_url_id = api_get_current_access_url_id();
  106. $sql = 'SELECT user.user_id, username, lastname, firstname
  107. FROM '.$tbl_user.' user
  108. INNER JOIN '.$tbl_user_rel_access_url.' url_user ON (url_user.user_id=user.user_id)
  109. WHERE
  110. access_url_id = '.$access_url_id.' AND
  111. (
  112. username LIKE "'.$needle.'%" OR
  113. firstname LIKE "'.$needle.'%" OR
  114. lastname LIKE "'.$needle.'%"
  115. ) AND ';
  116. switch ($userStatus) {
  117. case DRH:
  118. $sql .= " user.status <> 6 AND user.status <> ".DRH;
  119. break;
  120. case STUDENT_BOSS:
  121. $sql .= " user.status <> 6 AND user.status <> ".STUDENT_BOSS;
  122. break;
  123. }
  124. $sql .= " $order_clause LIMIT 11";
  125. $rs = Database::query($sql);
  126. $i = 0;
  127. while ($user = Database :: fetch_array($rs)) {
  128. $i++;
  129. if ($i <= 10) {
  130. $person_name = api_get_person_name($user['firstname'], $user['lastname']);
  131. $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 />';
  132. } else {
  133. $return .= '...<br />';
  134. }
  135. }
  136. $xajax_response->addAssign(
  137. 'ajax_list_users_single',
  138. 'innerHTML',
  139. api_utf8_encode($return)
  140. );
  141. } else {
  142. $return .= '<select id="origin" class="form-control" name="NoAssignedUsersList[]" multiple="multiple" size="15" ">';
  143. while ($user = Database :: fetch_array($rs)) {
  144. $person_name = api_get_person_name($user['firstname'], $user['lastname']);
  145. $return .= '<option value="'.$user['user_id'].'" title="'.htmlspecialchars($person_name, ENT_QUOTES).'">'.$person_name.' ('.$user['username'].')</option>';
  146. }
  147. $return .= '</select>';
  148. $xajax_response->addAssign('ajax_list_users_multiple', 'innerHTML', api_utf8_encode($return));
  149. }
  150. }
  151. return $xajax_response;
  152. }
  153. $xajax->processRequests();
  154. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  155. $htmlHeadXtra[] = '<script>
  156. function add_user_to_user (code, content) {
  157. document.getElementById("user_to_add").value = "";
  158. document.getElementById("ajax_list_users_single").innerHTML = "";
  159. destination = document.getElementById("destination");
  160. for (i=0;i<destination.length;i++) {
  161. if(destination.options[i].text == content) {
  162. return false;
  163. }
  164. }
  165. destination.options[destination.length] = new Option(content,code);
  166. destination.selectedIndex = -1;
  167. sortOptions(destination.options);
  168. }
  169. function moveItem(origin , destination) {
  170. for(var i = 0 ; i<origin.options.length ; i++) {
  171. if(origin.options[i].selected) {
  172. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  173. origin.options[i]=null;
  174. i = i-1;
  175. }
  176. }
  177. destination.selectedIndex = -1;
  178. sortOptions(destination.options);
  179. }
  180. function sortOptions(options) {
  181. var newOptions = new Array();
  182. for (i = 0 ; i<options.length ; i++) {
  183. newOptions[i] = options[i];
  184. }
  185. newOptions = newOptions.sort(mysort);
  186. options.length = 0;
  187. for(i = 0 ; i < newOptions.length ; i++){
  188. options[i] = newOptions[i];
  189. }
  190. }
  191. function mysort(a, b) {
  192. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  193. return 1;
  194. }
  195. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  196. return -1;
  197. }
  198. return 0;
  199. }
  200. function valide() {
  201. var options = document.getElementById("destination").options;
  202. for (i = 0 ; i<options.length ; i++) {
  203. options[i].selected = true;
  204. }
  205. document.forms.formulaire.submit();
  206. }
  207. function remove_item(origin) {
  208. for(var i = 0 ; i<origin.options.length ; i++) {
  209. if(origin.options[i].selected) {
  210. origin.options[i]=null;
  211. i = i-1;
  212. }
  213. }
  214. }
  215. </script>';
  216. $formSent = 0;
  217. $errorMsg = '';
  218. $UserList = [];
  219. // Filters
  220. $filters = [
  221. ['type' => 'text', 'name' => 'username', 'label' => get_lang('Username')],
  222. ['type' => 'text', 'name' => 'firstname', 'label' => get_lang('First name')],
  223. ['type' => 'text', 'name' => 'lastname', 'label' => get_lang('Last name')],
  224. ['type' => 'text', 'name' => 'official_code', 'label' => get_lang('Code')],
  225. ['type' => 'text', 'name' => 'email', 'label' => get_lang('e-mail')],
  226. ];
  227. $searchForm = new FormValidator('search', 'get', api_get_self().'?user='.$user_id);
  228. $searchForm->addHeader(get_lang('Advanced search'));
  229. $renderer = &$searchForm->defaultRenderer();
  230. $searchForm->addElement('hidden', 'user', $user_id);
  231. foreach ($filters as $param) {
  232. $searchForm->addElement($param['type'], $param['name'], $param['label']);
  233. }
  234. $searchForm->addButtonSearch(get_lang('Search'));
  235. $filterData = [];
  236. if ($searchForm->validate()) {
  237. $filterData = $searchForm->getSubmitValues();
  238. }
  239. $conditions = [];
  240. if (!empty($filters) && !empty($filterData)) {
  241. foreach ($filters as $filter) {
  242. if (isset($filter['name']) && isset($filterData[$filter['name']])) {
  243. $value = $filterData[$filter['name']];
  244. if (!empty($value)) {
  245. $conditions[$filter['name']] = $value;
  246. }
  247. }
  248. }
  249. }
  250. if (isset($_POST['formSent']) && intval($_POST['formSent']) == 1) {
  251. $user_list = isset($_POST['UsersList']) ? $_POST['UsersList'] : null;
  252. switch ($userStatus) {
  253. case DRH:
  254. case PLATFORM_ADMIN:
  255. $affected_rows = UserManager::subscribeUsersToHRManager($user_id, $user_list);
  256. break;
  257. case STUDENT_BOSS:
  258. $affected_rows = UserManager::subscribeBossToUsers($user_id, $user_list);
  259. break;
  260. default:
  261. $affected_rows = 0;
  262. }
  263. Display::addFlash(
  264. Display::return_message(
  265. get_lang('The assigned users have been updated'),
  266. 'normal'
  267. )
  268. );
  269. }
  270. // Display header
  271. Display::display_header($tool_name);
  272. // actions
  273. $actionsLeft = '';
  274. if ($userStatus != STUDENT_BOSS) {
  275. $actionsLeft = Display::url(
  276. Display::return_icon('course-add.png', get_lang('Assign courses'), null, ICON_SIZE_MEDIUM),
  277. "dashboard_add_courses_to_user.php?user=$user_id"
  278. );
  279. $actionsLeft .= Display::url(
  280. Display::return_icon('session-add.png', get_lang('Assign sessions'), null, ICON_SIZE_MEDIUM),
  281. "dashboard_add_sessions_to_user.php?user=$user_id"
  282. );
  283. }
  284. $actionsRight = Display::url(
  285. '<em class="fa fa-search"></em> '.get_lang('Advanced search'),
  286. '#',
  287. ['class' => 'btn btn-default advanced_options', 'id' => 'advanced_search']
  288. );
  289. $toolbar = Display::toolbarAction('toolbar-dashboard', [$actionsLeft, $actionsRight]);
  290. echo $toolbar;
  291. echo '<div id="advanced_search_options" style="display:none">';
  292. $searchForm->display();
  293. echo '</div>';
  294. echo Display::page_header(
  295. sprintf(
  296. get_lang('Assign users to %s'),
  297. api_get_person_name($user_info['firstname'], $user_info['lastname'])
  298. ),
  299. null,
  300. 'h3'
  301. );
  302. $assigned_users_to_hrm = [];
  303. switch ($userStatus) {
  304. case DRH:
  305. case PLATFORM_ADMIN:
  306. $assigned_users_to_hrm = UserManager::get_users_followed_by_drh($user_id);
  307. break;
  308. case STUDENT_BOSS:
  309. $assigned_users_to_hrm = UserManager::getUsersFollowedByStudentBoss($user_id);
  310. break;
  311. }
  312. $assigned_users_id = array_keys($assigned_users_to_hrm);
  313. $without_assigned_users = '';
  314. if (count($assigned_users_id) > 0) {
  315. $without_assigned_users = " user.user_id NOT IN(".implode(',', $assigned_users_id).") AND ";
  316. }
  317. $search_user = '';
  318. $needle = '';
  319. if (!empty($firstLetterUser)) {
  320. $needle = Database::escape_string($firstLetterUser);
  321. $search_user = "AND ".(api_sort_by_first_name() ? 'firstname' : 'lastname')." LIKE '$needle%'";
  322. }
  323. $sqlConditions = null;
  324. if (!empty($conditions)) {
  325. $temp_conditions = [];
  326. foreach ($conditions as $field => $value) {
  327. $field = Database::escape_string($field);
  328. $value = Database::escape_string($value);
  329. $temp_conditions[] = $field.' LIKE \'%'.$value.'%\'';
  330. }
  331. if (!empty($temp_conditions)) {
  332. $sqlConditions .= implode(' AND ', $temp_conditions);
  333. }
  334. if (!empty($sqlConditions)) {
  335. $sqlConditions = " AND $sqlConditions";
  336. }
  337. }
  338. if (api_is_multiple_url_enabled()) {
  339. $sql = "SELECT user.user_id, username, lastname, firstname
  340. FROM $tbl_user user
  341. LEFT JOIN $tbl_access_url_rel_user au
  342. ON (au.user_id = user.user_id)
  343. WHERE
  344. $without_assigned_users
  345. user.user_id NOT IN ($user_anonymous, $current_user_id, $user_id) AND
  346. status NOT IN(".DRH.", ".SESSIONADMIN.", ".ANONYMOUS.") $search_user AND
  347. access_url_id = ".api_get_current_access_url_id()."
  348. $sqlConditions
  349. ORDER BY firstname";
  350. } else {
  351. $sql = "SELECT user_id, username, lastname, firstname
  352. FROM $tbl_user user
  353. WHERE
  354. $without_assigned_users
  355. user_id NOT IN ($user_anonymous, $current_user_id, $user_id) AND
  356. status NOT IN(".DRH.", ".SESSIONADMIN.", ".ANONYMOUS.")
  357. $search_user
  358. $sqlConditions
  359. ORDER BY firstname ";
  360. }
  361. $result = Database::query($sql);
  362. ?>
  363. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?user=<?php echo $user_id; ?>" class="form-horizontal" <?php if ($ajax_search) {
  364. echo ' onsubmit="valide();"';
  365. }?>>
  366. <input type="hidden" name="formSent" value="1" />
  367. <div class="row">
  368. <div class="col-md-4">
  369. <?php echo get_lang('User listInPlatform'); ?>
  370. <div class="form-group">
  371. <div class="col-sm-12">
  372. <div id="ajax_list_users_multiple">
  373. <select id="origin" class="form-control" name="NoAssignedUsersList[]" multiple="multiple" size="15">
  374. <?php
  375. while ($enreg = Database::fetch_array($result)) {
  376. $person_name = api_get_person_name($enreg['firstname'], $enreg['lastname']); ?>
  377. <option value="<?php echo $enreg['user_id']; ?>" <?php echo 'title="'.htmlspecialchars($person_name, ENT_QUOTES).'"'; ?>>
  378. <?php echo $person_name.' ('.$enreg['username'].')'; ?>
  379. </option>
  380. <?php
  381. } ?>
  382. </select>
  383. </div>
  384. </div>
  385. </div>
  386. </div>
  387. <div class="col-md-4">
  388. <div class="code-course">
  389. <?php if ($add_type == 'multiple') {
  390. ?>
  391. <p><?php echo get_lang('First letter (last name)'); ?></p>
  392. <select class="selectpicker show-tick form-control" name="firstLetterUser" onchange = "xajax_search_users(this.value,'multiple')">
  393. <option value="%">--</option>
  394. <?php echo Display::get_alphabet_options($firstLetterUser); ?>
  395. </select>
  396. <?php
  397. } ?>
  398. </div>
  399. <div class="control-course">
  400. <?php if ($ajax_search) {
  401. ?>
  402. <div class="separate-action">
  403. <button class="btn btn-primary" type="button" onclick="remove_item(document.getElementById('destination'))"></button>
  404. </div>
  405. <?php
  406. } else {
  407. ?>
  408. <div class="separate-action">
  409. <button id="add_user_button" class="btn btn-primary" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))">
  410. <em class="fa fa-chevron-right"></em>
  411. </button>
  412. </div>
  413. <div class="separate-action">
  414. <button id="remove_user_button" class="btn btn-primary" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))">
  415. <em class="fa fa-chevron-left"></em>
  416. </button>
  417. </div>
  418. <?php
  419. } ?>
  420. <div class="separate-action">
  421. <?php
  422. echo '<button id="assign_user" class="btn btn-success" type="button" value="" onclick="valide()" >'.$tool_name.'</button>';
  423. ?>
  424. </div>
  425. </div>
  426. </div>
  427. <div class="col-md-4">
  428. <?php
  429. if (UserManager::is_admin($user_id)) {
  430. echo get_lang('AssignedUsersListToAdministrationistrator');
  431. } else {
  432. if ($user_info['status'] == SESSIONADMIN) {
  433. echo get_lang('Assign a users list to the sessions administrator');
  434. } else {
  435. if ($user_info['status'] == STUDENT_BOSS) {
  436. echo get_lang('Users assigned to their superior');
  437. } else {
  438. echo get_lang('List of users assigned to Human Resources manager');
  439. }
  440. }
  441. }
  442. ?>
  443. <div class="form-group">
  444. <div class="col-sm-12">
  445. <br>
  446. <select id='destination' class="form-control" name="UsersList[]" multiple="multiple" size="15" >
  447. <?php
  448. if (is_array($assigned_users_to_hrm)) {
  449. foreach ($assigned_users_to_hrm as $enreg) {
  450. $person_name = api_get_person_name($enreg['firstname'], $enreg['lastname']); ?>
  451. <option value="<?php echo $enreg['user_id']; ?>" <?php echo 'title="'.htmlspecialchars($person_name, ENT_QUOTES).'"'; ?>>
  452. <?php echo $person_name.' ('.$enreg['username'].')'; ?>
  453. </option>
  454. <?php
  455. }
  456. }?>
  457. </select>
  458. </div>
  459. </div>
  460. </div>
  461. </div>
  462. </form>
  463. <?php
  464. Display::display_footer();