dashboard_add_users_to_user.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. /**
  5. * Interface for assigning users to Human Resources Manager
  6. * @package chamilo.admin
  7. */
  8. // resetting the course id
  9. $cidReset = true;
  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' => Container::getRouter()->generate('administration'), '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. $user_id = intval($_GET['user']);
  26. $user_info = api_get_user_info($user_id);
  27. $user_anonymous = api_get_anonymous_id();
  28. $current_user_id = api_get_user_id();
  29. $userStatus = api_get_user_status($user_id);
  30. $firstLetterUser = isset($_POST['firstLetterUser']) ? $_POST['firstLetterUser'] : null;
  31. // setting the name of the tool
  32. $isAdmin = UserManager::is_admin($user_id);
  33. if ($isAdmin) {
  34. $userStatus = PLATFORM_ADMIN;
  35. $tool_name= get_lang('AssignUsersToPlatformAdministrator');
  36. } else if ($user_info['status'] == SESSIONADMIN) {
  37. $tool_name= get_lang('AssignUsersToSessionsAdministrator');
  38. } else if ($user_info['status'] == STUDENT_BOSS) {
  39. $tool_name= get_lang('AssignUsersToBoss');
  40. } else {
  41. $tool_name= get_lang('AssignUsersToHumanResourcesManager');
  42. }
  43. $add_type = 'multiple';
  44. if (isset($_GET['add_type']) && $_GET['add_type']!='') {
  45. $add_type = Security::remove_XSS($_REQUEST['add_type']);
  46. }
  47. if (!api_is_platform_admin()) {
  48. api_not_allowed(true);
  49. }
  50. function search_users($needle, $type)
  51. {
  52. global $tbl_access_url_rel_user, $tbl_user, $user_anonymous, $current_user_id, $user_id, $userStatus;
  53. $xajax_response = new xajaxResponse();
  54. $return = '';
  55. if (!empty($needle) && !empty($type)) {
  56. $assigned_users_to_hrm = array();
  57. switch ($userStatus) {
  58. case DRH:
  59. //no break;
  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('ajax_list_users_single','innerHTML',api_utf8_encode($return));
  137. } else {
  138. $return .= '<select id="origin" class="form-control" name="NoAssignedUsersList[]" multiple="multiple" size="15" ">';
  139. while($user = Database :: fetch_array($rs)) {
  140. $person_name = api_get_person_name($user['firstname'], $user['lastname']);
  141. $return .= '<option value="'.$user['user_id'].'" title="'.htmlspecialchars($person_name,ENT_QUOTES).'">'.$person_name.' ('.$user['username'].')</option>';
  142. }
  143. $return .= '</select>';
  144. $xajax_response->addAssign('ajax_list_users_multiple', 'innerHTML', api_utf8_encode($return));
  145. }
  146. }
  147. return $xajax_response;
  148. }
  149. $xajax->processRequests();
  150. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  151. $htmlHeadXtra[] = '<script>
  152. function add_user_to_user (code, content) {
  153. document.getElementById("user_to_add").value = "";
  154. document.getElementById("ajax_list_users_single").innerHTML = "";
  155. destination = document.getElementById("destination");
  156. for (i=0;i<destination.length;i++) {
  157. if(destination.options[i].text == content) {
  158. return false;
  159. }
  160. }
  161. destination.options[destination.length] = new Option(content,code);
  162. destination.selectedIndex = -1;
  163. sortOptions(destination.options);
  164. }
  165. function moveItem(origin , destination) {
  166. for(var i = 0 ; i<origin.options.length ; i++) {
  167. if(origin.options[i].selected) {
  168. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  169. origin.options[i]=null;
  170. i = i-1;
  171. }
  172. }
  173. destination.selectedIndex = -1;
  174. sortOptions(destination.options);
  175. }
  176. function sortOptions(options) {
  177. var newOptions = new Array();
  178. for (i = 0 ; i<options.length ; i++) {
  179. newOptions[i] = options[i];
  180. }
  181. newOptions = newOptions.sort(mysort);
  182. options.length = 0;
  183. for(i = 0 ; i < newOptions.length ; i++){
  184. options[i] = newOptions[i];
  185. }
  186. }
  187. function mysort(a, b) {
  188. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  189. return 1;
  190. }
  191. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  192. return -1;
  193. }
  194. return 0;
  195. }
  196. function valide() {
  197. var options = document.getElementById("destination").options;
  198. for (i = 0 ; i<options.length ; i++) {
  199. options[i].selected = true;
  200. }
  201. document.forms.formulaire.submit();
  202. }
  203. function remove_item(origin) {
  204. for(var i = 0 ; i<origin.options.length ; i++) {
  205. if(origin.options[i].selected) {
  206. origin.options[i]=null;
  207. i = i-1;
  208. }
  209. }
  210. }
  211. </script>';
  212. $formSent=0;
  213. $errorMsg = '';
  214. $UserList = array();
  215. // Filters
  216. $filters = array(
  217. array('type' => 'text', 'name' => 'username', 'label' => get_lang('Username')),
  218. array('type' => 'text', 'name' => 'firstname', 'label' => get_lang('FirstName')),
  219. array('type' => 'text', 'name' => 'lastname', 'label' => get_lang('LastName')),
  220. array('type' => 'text', 'name' => 'official_code', 'label' => get_lang('OfficialCode')),
  221. array('type' => 'text', 'name' => 'email', 'label' => get_lang('Email'))
  222. );
  223. $searchForm = new FormValidator('search', 'get', api_get_self().'?user='.$user_id);
  224. $searchForm->addHeader(get_lang('AdvancedSearch'));
  225. $renderer =& $searchForm->defaultRenderer();
  226. $searchForm->addElement('hidden', 'user', $user_id);
  227. foreach ($filters as $param) {
  228. $searchForm->addElement($param['type'], $param['name'], $param['label']);
  229. }
  230. $searchForm->addButtonSearch(get_lang('Search'));
  231. $filterData = array();
  232. if ($searchForm->validate()) {
  233. $filterData = $searchForm->getSubmitValues();
  234. }
  235. $conditions = array();
  236. if (!empty($filters) && !empty($filterData)) {
  237. foreach ($filters as $filter) {
  238. if (isset($filter['name']) && isset($filterData[$filter['name']])) {
  239. $value = $filterData[$filter['name']];
  240. if (!empty($value)) {
  241. $conditions[$filter['name']] = $value;
  242. }
  243. }
  244. }
  245. }
  246. $msg = '';
  247. if (isset($_POST['formSent']) && intval($_POST['formSent']) == 1) {
  248. $user_list = $_POST['UsersList'];
  249. switch ($userStatus) {
  250. case DRH:
  251. //no break;
  252. case PLATFORM_ADMIN:
  253. $affected_rows = UserManager::subscribeUsersToHRManager($user_id, $user_list);
  254. break;
  255. case STUDENT_BOSS:
  256. $affected_rows = UserManager::subscribeBossToUsers($user_id, $user_list);
  257. break;
  258. default:
  259. $affected_rows = 0;
  260. }
  261. if ($affected_rows) {
  262. $msg = get_lang('AssignedUsersHaveBeenUpdatedSuccessfully');
  263. }
  264. }
  265. // Display header
  266. Display::display_header($tool_name);
  267. // actions
  268. $actionsLeft = '';
  269. if ($userStatus != STUDENT_BOSS) {
  270. $actionsLeft = Display::url(
  271. Display::return_icon('course-add.png', get_lang('AssignCourses'), null, ICON_SIZE_MEDIUM),
  272. "dashboard_add_courses_to_user.php?user=$user_id"
  273. );
  274. $actionsLeft .= Display::url(
  275. Display::return_icon('session-add.png', get_lang('AssignSessions'), null, ICON_SIZE_MEDIUM),
  276. "dashboard_add_sessions_to_user.php?user=$user_id"
  277. );
  278. }
  279. $actionsRight = Display::url(
  280. '<em class="fa fa-search"></em> ' . get_lang('AdvancedSearch'),
  281. '#',
  282. array('class' => 'btn btn-default advanced_options', 'id' => 'advanced_search')
  283. );
  284. $toolbar = Display::toolbarAction('toolbar-dashboard', [$actionsLeft, $actionsRight]);
  285. echo $toolbar;
  286. echo '<div id="advanced_search_options" style="display:none">';
  287. $searchForm->display();
  288. echo '</div>';
  289. echo Display::page_header(
  290. sprintf(
  291. get_lang('AssignUsersToX'),
  292. api_get_person_name($user_info['firstname'], $user_info['lastname'])
  293. ),
  294. null,
  295. 'h3'
  296. );
  297. $assigned_users_to_hrm = array();
  298. switch ($userStatus) {
  299. case DRH:
  300. //no break;
  301. case PLATFORM_ADMIN:
  302. $assigned_users_to_hrm = UserManager::get_users_followed_by_drh($user_id);
  303. break;
  304. case STUDENT_BOSS:
  305. $assigned_users_to_hrm = UserManager::getUsersFollowedByStudentBoss($user_id);
  306. break;
  307. }
  308. $assigned_users_id = array_keys($assigned_users_to_hrm);
  309. $without_assigned_users = '';
  310. if (count($assigned_users_id) > 0) {
  311. $without_assigned_users = " user.user_id NOT IN(".implode(',',$assigned_users_id).") AND ";
  312. }
  313. $search_user = '';
  314. if (!empty($firstLetterUser)) {
  315. $needle = Database::escape_string($firstLetterUser);
  316. $search_user ="AND ".(api_sort_by_first_name() ? 'firstname' : 'lastname')." LIKE '$needle%'";
  317. }
  318. $sqlConditions = null;
  319. if (!empty($conditions)) {
  320. $temp_conditions = array();
  321. foreach ($conditions as $field => $value) {
  322. $field = Database::escape_string($field);
  323. $value = Database::escape_string($value);
  324. $temp_conditions[] = $field.' LIKE \'%'.$value.'%\'';
  325. }
  326. if (!empty($temp_conditions)) {
  327. $sqlConditions .= implode(' AND ', $temp_conditions);
  328. }
  329. if (!empty($sqlConditions)) {
  330. $sqlConditions = " AND $sqlConditions";
  331. }
  332. }
  333. if (api_is_multiple_url_enabled()) {
  334. $sql = "SELECT user.user_id, username, lastname, firstname
  335. FROM $tbl_user user LEFT JOIN $tbl_access_url_rel_user au
  336. ON (au.user_id = user.user_id)
  337. WHERE
  338. $without_assigned_users
  339. user.user_id NOT IN ($user_anonymous, $current_user_id, $user_id) AND
  340. status NOT IN(".DRH.", ".SESSIONADMIN.") $search_user AND
  341. access_url_id = ".api_get_current_access_url_id()."
  342. $sqlConditions
  343. ORDER BY firstname";
  344. } else {
  345. $sql = "SELECT user_id, username, lastname, firstname
  346. FROM $tbl_user user
  347. WHERE
  348. $without_assigned_users
  349. user_id NOT IN ($user_anonymous, $current_user_id, $user_id) AND
  350. status NOT IN(".DRH.", ".SESSIONADMIN.")
  351. $search_user
  352. $sqlConditions
  353. ORDER BY firstname ";
  354. }
  355. $result = Database::query($sql);
  356. ?>
  357. <form name="formulaire" method="post" action="<?php echo api_get_self(); ?>?user=<?php echo $user_id ?>" class="form-horizontal" <?php if($ajax_search){echo ' onsubmit="valide();"';}?>>
  358. <input type="hidden" name="formSent" value="1" />
  359. <?php
  360. if(!empty($msg)) {
  361. Display::display_normal_message($msg); //main API
  362. }
  363. ?>
  364. <div class="row">
  365. <div class="col-md-4">
  366. <?php echo get_lang('UserListInPlatform') ?>
  367. <div class="form-group">
  368. <div class="col-sm-12">
  369. <div id="ajax_list_users_multiple">
  370. <select id="origin" class="form-control" name="NoAssignedUsersList[]" multiple="multiple" size="15">
  371. <?php
  372. while ($enreg = Database::fetch_array($result)) {
  373. $person_name = api_get_person_name($enreg['firstname'], $enreg['lastname']); ?>
  374. <option value="<?php echo $enreg['user_id']; ?>" <?php echo 'title="'.htmlspecialchars($person_name,ENT_QUOTES).'"';?>>
  375. <?php echo $person_name.' ('.$enreg['username'].')'; ?>
  376. </option>
  377. <?php } ?>
  378. </select>
  379. </div>
  380. </div>
  381. </div>
  382. </div>
  383. <div class="col-md-4">
  384. <div class="code-course">
  385. <?php if($add_type == 'multiple') { ?>
  386. <p><?php echo get_lang('FirstLetterUser');?></p>
  387. <select class="selectpicker show-tick form-control" name="firstLetterUser" onchange = "xajax_search_users(this.value,'multiple')">
  388. <option value="%">--</option>
  389. <?php echo Display::get_alphabet_options($firstLetterUser); ?>
  390. </select>
  391. <?php } ?>
  392. </div>
  393. <div class="control-course">
  394. <?php if ($ajax_search) { ?>
  395. <div class="separate-action">
  396. <button class="btn btn-primary" type="button" onclick="remove_item(document.getElementById('destination'))"></button>
  397. </div>
  398. <?php } else { ?>
  399. <div class="separate-action">
  400. <button class="btn btn-primary" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))">
  401. <em class="fa fa-chevron-right"></em>
  402. </button>
  403. </div>
  404. <div class="separate-action">
  405. <button class="btn btn-primary" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))">
  406. <em class="fa fa-chevron-left"></em>
  407. </button>
  408. </div>
  409. <?php
  410. }
  411. ?>
  412. <div class="separate-action">
  413. <?php
  414. echo '<button class="btn btn-success" type="button" value="" onclick="valide()" >'.$tool_name.'</button>';
  415. ?>
  416. </div>
  417. </div>
  418. </div>
  419. <div class="col-md-4">
  420. <?php
  421. if (UserManager::is_admin($user_id)) {
  422. echo get_lang('AssignedUsersListToPlatformAdministrator');
  423. } else if ($user_info['status'] == SESSIONADMIN) {
  424. echo get_lang('AssignedUsersListToSessionsAdministrator');
  425. } else if ($user_info['status'] == STUDENT_BOSS) {
  426. echo get_lang('AssignedUsersListToStudentBoss');
  427. } else {
  428. echo get_lang('AssignedUsersListToHumanResourcesManager');
  429. }
  430. ?>
  431. <div class="form-group">
  432. <div class="col-sm-12">
  433. <br>
  434. <select id='destination' class="form-control" name="UsersList[]" multiple="multiple" size="15" >
  435. <?php
  436. if (is_array($assigned_users_to_hrm)) {
  437. foreach($assigned_users_to_hrm as $enreg) {
  438. $person_name = api_get_person_name($enreg['firstname'], $enreg['lastname']);
  439. ?>
  440. <option value="<?php echo $enreg['user_id']; ?>" <?php echo 'title="'.htmlspecialchars($person_name,ENT_QUOTES).'"'; ?>>
  441. <?php echo $person_name.' ('.$enreg['username'].')'; ?>
  442. </option>
  443. <?php }
  444. }?>
  445. </select>
  446. </div>
  447. </div>
  448. </div>
  449. </div>
  450. </form>
  451. <?php
  452. Display::display_footer();