dashboard_add_users_to_user.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. $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' => 'index.php', '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[] = '
  152. <script type="text/javascript">
  153. function add_user_to_user (code, content) {
  154. document.getElementById("user_to_add").value = "";
  155. document.getElementById("ajax_list_users_single").innerHTML = "";
  156. destination = document.getElementById("destination");
  157. for (i=0;i<destination.length;i++) {
  158. if(destination.options[i].text == content) {
  159. return false;
  160. }
  161. }
  162. destination.options[destination.length] = new Option(content,code);
  163. destination.selectedIndex = -1;
  164. sortOptions(destination.options);
  165. }
  166. function moveItem(origin , destination) {
  167. for(var i = 0 ; i<origin.options.length ; i++) {
  168. if(origin.options[i].selected) {
  169. destination.options[destination.length] = new Option(origin.options[i].text,origin.options[i].value);
  170. origin.options[i]=null;
  171. i = i-1;
  172. }
  173. }
  174. destination.selectedIndex = -1;
  175. sortOptions(destination.options);
  176. }
  177. function sortOptions(options) {
  178. var newOptions = new Array();
  179. for (i = 0 ; i<options.length ; i++) {
  180. newOptions[i] = options[i];
  181. }
  182. newOptions = newOptions.sort(mysort);
  183. options.length = 0;
  184. for(i = 0 ; i < newOptions.length ; i++){
  185. options[i] = newOptions[i];
  186. }
  187. }
  188. function mysort(a, b) {
  189. if (a.text.toLowerCase() > b.text.toLowerCase()) {
  190. return 1;
  191. }
  192. if (a.text.toLowerCase() < b.text.toLowerCase()) {
  193. return -1;
  194. }
  195. return 0;
  196. }
  197. function valide() {
  198. var options = document.getElementById("destination").options;
  199. for (i = 0 ; i<options.length ; i++) {
  200. options[i].selected = true;
  201. }
  202. document.forms.formulaire.submit();
  203. }
  204. function remove_item(origin) {
  205. for(var i = 0 ; i<origin.options.length ; i++) {
  206. if(origin.options[i].selected) {
  207. origin.options[i]=null;
  208. i = i-1;
  209. }
  210. }
  211. }
  212. </script>';
  213. $formSent=0;
  214. $errorMsg = '';
  215. $UserList = array();
  216. // Filters
  217. $filters = array(
  218. array('type' => 'text', 'name' => 'username', 'label' => get_lang('Username')),
  219. array('type' => 'text', 'name' => 'firstname', 'label' => get_lang('FirstName')),
  220. array('type' => 'text', 'name' => 'lastname', 'label' => get_lang('LastName')),
  221. array('type' => 'text', 'name' => 'official_code', 'label' => get_lang('OfficialCode')),
  222. array('type' => 'text', 'name' => 'email', 'label' => get_lang('Email'))
  223. );
  224. $searchForm = new FormValidator('search', 'get', api_get_self().'?user='.$user_id);
  225. $searchForm->addHeader(get_lang('AdvancedSearch'));
  226. $renderer =& $searchForm->defaultRenderer();
  227. $searchForm->addElement('hidden', 'user', $user_id);
  228. foreach ($filters as $param) {
  229. $searchForm->addElement($param['type'], $param['name'], $param['label']);
  230. }
  231. $searchForm->addButtonSearch(get_lang('Search'));
  232. $filterData = array();
  233. if ($searchForm->validate()) {
  234. $filterData = $searchForm->getSubmitValues();
  235. }
  236. $conditions = array();
  237. if (!empty($filters) && !empty($filterData)) {
  238. foreach ($filters as $filter) {
  239. if (isset($filter['name']) && isset($filterData[$filter['name']])) {
  240. $value = $filterData[$filter['name']];
  241. if (!empty($value)) {
  242. $conditions[$filter['name']] = $value;
  243. }
  244. }
  245. }
  246. }
  247. $msg = '';
  248. if (isset($_POST['formSent']) && intval($_POST['formSent']) == 1) {
  249. $user_list = $_POST['UsersList'];
  250. switch ($userStatus) {
  251. case DRH:
  252. //no break;
  253. case PLATFORM_ADMIN:
  254. $affected_rows = UserManager::suscribe_users_to_hr_manager($user_id, $user_list);
  255. break;
  256. case STUDENT_BOSS:
  257. $affected_rows = UserManager::subscribeBossToUsers($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. $actionsLeft = '';
  270. if ($userStatus != STUDENT_BOSS) {
  271. $actionsLeft = Display::url(
  272. Display::return_icon('course-add.png', get_lang('AssignCourses'), null, ICON_SIZE_MEDIUM ),
  273. "dashboard_add_courses_to_user.php?user=$user_id"
  274. );
  275. $actionsLeft .= Display::url(
  276. Display::return_icon('session-add.png', get_lang('AssignSessions'), null, ICON_SIZE_MEDIUM ) ,
  277. "dashboard_add_sessions_to_user.php?user=$user_id"
  278. );
  279. }
  280. $actionsRight = Display::url(
  281. '<em class="fa fa-search"></em> ' . get_lang('AdvancedSearch'),
  282. '#',
  283. array('class' => 'btn btn-default advanced_options', 'id' => 'advanced_search')
  284. );
  285. $toolbar = Display::toolbarAction('toolbar-dashboard', [$actionsLeft, $actionsRight]);
  286. echo $toolbar;
  287. echo '<div id="advanced_search_options" style="display:none">';
  288. $searchForm->display();
  289. echo '</div>';
  290. echo Display::page_header(
  291. sprintf(
  292. get_lang('AssignUsersToX'),
  293. api_get_person_name($user_info['firstname'], $user_info['lastname'])
  294. ),
  295. null,
  296. 'h3'
  297. );
  298. $assigned_users_to_hrm = array();
  299. switch ($userStatus) {
  300. case DRH:
  301. //no break;
  302. case PLATFORM_ADMIN:
  303. $assigned_users_to_hrm = UserManager::get_users_followed_by_drh($user_id);
  304. break;
  305. case STUDENT_BOSS:
  306. $assigned_users_to_hrm = UserManager::getUsersFollowedByStudentBoss($user_id);
  307. break;
  308. }
  309. $assigned_users_id = array_keys($assigned_users_to_hrm);
  310. $without_assigned_users = '';
  311. if (count($assigned_users_id) > 0) {
  312. $without_assigned_users = " user.user_id NOT IN(".implode(',',$assigned_users_id).") AND ";
  313. }
  314. $search_user = '';
  315. if (!empty($firstLetterUser)) {
  316. $needle = Database::escape_string($firstLetterUser);
  317. $search_user ="AND ".(api_sort_by_first_name() ? 'firstname' : 'lastname')." LIKE '$needle%'";
  318. }
  319. $sqlConditions = null;
  320. if (!empty($conditions)) {
  321. $temp_conditions = array();
  322. foreach ($conditions as $field => $value) {
  323. $field = Database::escape_string($field);
  324. $value = Database::escape_string($value);
  325. $temp_conditions[] = $field.' LIKE \'%'.$value.'%\'';
  326. }
  327. if (!empty($temp_conditions)) {
  328. $sqlConditions .= implode(' AND ', $temp_conditions);
  329. }
  330. if (!empty($sqlConditions)) {
  331. $sqlConditions = " AND $sqlConditions";
  332. }
  333. }
  334. if (api_is_multiple_url_enabled()) {
  335. $sql = "SELECT user.user_id, username, lastname, firstname
  336. FROM $tbl_user user LEFT JOIN $tbl_access_url_rel_user au 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 while ($enreg = Database::fetch_array($result)) {
  372. $person_name = api_get_person_name($enreg['firstname'], $enreg['lastname']); ?>
  373. <option value="<?php echo $enreg['user_id']; ?>" <?php echo 'title="'.htmlspecialchars($person_name,ENT_QUOTES).'"';?>>
  374. <?php echo $person_name.' ('.$enreg['username'].')'; ?>
  375. </option>
  376. <?php } ?>
  377. </select>
  378. </div>
  379. </div>
  380. </div>
  381. </div>
  382. <div class="col-md-4">
  383. <div class="code-course">
  384. <?php if($add_type == 'multiple') { ?>
  385. <p><?php echo get_lang('FirstLetterUser');?></p>
  386. <select class="selectpicker show-tick form-control" name="firstLetterUser" onchange = "xajax_search_users(this.value,'multiple')">
  387. <option value="%">--</option>
  388. <?php echo Display::get_alphabet_options($firstLetterUser); ?>
  389. </select>
  390. <?php } ?>
  391. </div>
  392. <div class="control-course">
  393. <?php if ($ajax_search) { ?>
  394. <div class="separate-action">
  395. <button class="btn btn-primary" type="button" onclick="remove_item(document.getElementById('destination'))"></button>
  396. </div>
  397. <?php } else { ?>
  398. <div class="separate-action">
  399. <button class="btn btn-primary" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))">
  400. <em class="fa fa-chevron-right"></em>
  401. </button>
  402. </div>
  403. <div class="separate-action">
  404. <button class="btn btn-primary" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))">
  405. <em class="fa fa-chevron-left"></em>
  406. </button>
  407. </div>
  408. <?php
  409. }
  410. ?>
  411. <div class="separate-action">
  412. <?php
  413. echo '<button class="btn btn-success" type="button" value="" onclick="valide()" >'.$tool_name.'</button>';
  414. ?>
  415. </div>
  416. </div>
  417. </div>
  418. <div class="col-md-4">
  419. <?php
  420. if (UserManager::is_admin($user_id)) {
  421. echo get_lang('AssignedUsersListToPlatformAdministrator');
  422. } else if ($user_info['status'] == SESSIONADMIN) {
  423. echo get_lang('AssignedUsersListToSessionsAdministrator');
  424. } else if ($user_info['status'] == STUDENT_BOSS) {
  425. echo get_lang('AssignedUsersListToStudentBoss');
  426. } else {
  427. echo get_lang('AssignedUsersListToHumanResourcesManager');
  428. }
  429. ?>
  430. <div class="form-group">
  431. <div class="col-sm-12">
  432. <br>
  433. <select id='destination' class="form-control" name="UsersList[]" multiple="multiple" size="15" >
  434. <?php
  435. if (is_array($assigned_users_to_hrm)) {
  436. foreach($assigned_users_to_hrm as $enreg) {
  437. $person_name = api_get_person_name($enreg['firstname'], $enreg['lastname']);
  438. ?>
  439. <option value="<?php echo $enreg['user_id']; ?>" <?php echo 'title="'.htmlspecialchars($person_name,ENT_QUOTES).'"'; ?>>
  440. <?php echo $person_name.' ('.$enreg['username'].')'; ?>
  441. </option>
  442. <?php }
  443. }?>
  444. </select>
  445. </div>
  446. </div>
  447. </div>
  448. </div>
  449. </form>
  450. <?php
  451. Display::display_footer();