dashboard_add_users_to_user.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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" class="form-control" name="NoAssignedUsersList[]" multiple="multiple" size="15" ">';
  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. $filterData = array();
  230. if ($searchForm->validate()) {
  231. $filterData = $searchForm->getSubmitValues();
  232. }
  233. $conditions = array();
  234. if (!empty($filters) && !empty($filterData)) {
  235. foreach ($filters as $filter) {
  236. if (isset($filter['name']) && isset($filterData[$filter['name']])) {
  237. $value = $filterData[$filter['name']];
  238. if (!empty($value)) {
  239. $conditions[$filter['name']] = $value;
  240. }
  241. }
  242. }
  243. }
  244. $msg = '';
  245. if (isset($_POST['formSent']) && intval($_POST['formSent']) == 1) {
  246. $user_list = $_POST['UsersList'];
  247. switch ($userStatus) {
  248. case DRH:
  249. $affected_rows = UserManager::suscribe_users_to_hr_manager($user_id, $user_list);
  250. break;
  251. case STUDENT_BOSS;
  252. $affected_rows = UserManager::subscribeUsersToBoss($user_id, $user_list);
  253. break;
  254. default:
  255. $affected_rows = 0;
  256. }
  257. if ($affected_rows) {
  258. $msg = get_lang('AssignedUsersHaveBeenUpdatedSuccessfully');
  259. }
  260. }
  261. // Display header
  262. Display::display_header($tool_name);
  263. // actions
  264. if ($userStatus != STUDENT_BOSS) {
  265. $actionsLeft = Display::url(
  266. Display::return_icon('course-add.png', get_lang('AssignCourses'), null, ICON_SIZE_MEDIUM ), "dashboard_add_courses_to_user.php?user=$user_id"
  267. );
  268. $actionsLeft .= Display::url(
  269. Display::return_icon('session-add.png', get_lang('AssignSessions'), null, ICON_SIZE_MEDIUM ) , "dashboard_add_sessions_to_user.php?user=$user_id"
  270. );
  271. }
  272. $actionsRight = Display::url('<i class="fa fa-search"></i> ' . get_lang('AdvancedSearch'), '#', array('class' => 'btn btn-default advanced_options', 'id' => 'advanced_search'));
  273. $toolbar = Display::toolbarAction('toolbar-dashboard', $content = array( 0 => $actionsLeft, 1 => $actionsRight ));
  274. echo $toolbar;
  275. echo '<div id="advanced_search_options" style="display:none">';
  276. $searchForm->display();
  277. echo '</div>';
  278. echo Display::page_header(
  279. sprintf(get_lang('AssignUsersToX'), api_get_person_name($user_info['firstname'], $user_info['lastname'])),
  280. null, $size = 'h3'
  281. );
  282. switch ($userStatus) {
  283. case DRH:
  284. $assigned_users_to_hrm = UserManager::get_users_followed_by_drh($user_id);
  285. break;
  286. case STUDENT_BOSS;
  287. $assigned_users_to_hrm = UserManager::getUsersFollowedByStudentBoss($user_id);
  288. break;
  289. }
  290. $assigned_users_id = array_keys($assigned_users_to_hrm);
  291. $without_assigned_users = '';
  292. if (count($assigned_users_id) > 0) {
  293. $without_assigned_users = " user.user_id NOT IN(".implode(',',$assigned_users_id).") AND ";
  294. }
  295. $search_user = '';
  296. if (!empty($firstLetterUser)) {
  297. $needle = Database::escape_string($firstLetterUser);
  298. $search_user ="AND ".(api_sort_by_first_name() ? 'firstname' : 'lastname')." LIKE '$needle%'";
  299. }
  300. $sqlConditions = null;
  301. if (!empty($conditions)) {
  302. $temp_conditions = array();
  303. foreach ($conditions as $field => $value) {
  304. $field = Database::escape_string($field);
  305. $value = Database::escape_string($value);
  306. $temp_conditions[] = $field.' LIKE \'%'.$value.'%\'';
  307. }
  308. if (!empty($temp_conditions)) {
  309. $sqlConditions .= implode(' AND ', $temp_conditions);
  310. }
  311. if (!empty($sqlConditions)) {
  312. $sqlConditions = " AND $sqlConditions";
  313. }
  314. }
  315. if (api_is_multiple_url_enabled()) {
  316. $sql = "SELECT user.user_id, username, lastname, firstname
  317. FROM $tbl_user user LEFT JOIN $tbl_access_url_rel_user au ON (au.user_id = user.user_id)
  318. WHERE
  319. $without_assigned_users
  320. user.user_id NOT IN ($user_anonymous, $current_user_id, $user_id) AND
  321. status NOT IN(".DRH.", ".SESSIONADMIN.") $search_user AND
  322. access_url_id = ".api_get_current_access_url_id()."
  323. $sqlConditions
  324. ORDER BY firstname";
  325. } else {
  326. $sql = "SELECT user_id, username, lastname, firstname
  327. FROM $tbl_user user
  328. WHERE
  329. $without_assigned_users
  330. user_id NOT IN ($user_anonymous, $current_user_id, $user_id) AND
  331. status NOT IN(".DRH.", ".SESSIONADMIN.")
  332. $search_user
  333. $sqlConditions
  334. ORDER BY firstname ";
  335. }
  336. $result = Database::query($sql);
  337. ?>
  338. <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();"';}?>>
  339. <input type="hidden" name="formSent" value="1" />
  340. <?php
  341. if(!empty($msg)) {
  342. Display::display_normal_message($msg); //main API
  343. }
  344. ?>
  345. <div class="row">
  346. <div class="col-md-4">
  347. <?php echo get_lang('UserListInPlatform') ?>
  348. <?php if($add_type == 'multiple') { ?>
  349. <div class="form-group">
  350. <label class="col-sm-7 control-label"><?php echo get_lang('FirstLetterUser');?></label>
  351. <div class="col-sm-5">
  352. <select class="selectpicker show-tick form-control" name="firstLetterUser" onchange = "xajax_search_users(this.value,'multiple')">
  353. <option value="%">--</option>
  354. <?php echo Display::get_alphabet_options($firstLetterUser); ?>
  355. </select>
  356. </div>
  357. </div>
  358. <!-- <div class="form-group">
  359. <input type="text" id="user_to_add" onkeyup="xajax_search_users(this.value,'single')" onclick="moveItem(document.getElementById('user_to_add'), document.getElementById('destination'))" />
  360. <div id="ajax_list_users_single"></div>
  361. </div> -->
  362. <?php } ?>
  363. <div class="form-group">
  364. <div class="col-sm-12">
  365. <div id="ajax_list_users_multiple">
  366. <select id="origin" class="form-control" name="NoAssignedUsersList[]" multiple="multiple" size="15">
  367. <?php while ($enreg = Database::fetch_array($result)) {
  368. $person_name = api_get_person_name($enreg['firstname'], $enreg['lastname']); ?>
  369. <option value="<?php echo $enreg['user_id']; ?>" <?php echo 'title="'.htmlspecialchars($person_name,ENT_QUOTES).'"';?>>
  370. <?php echo $person_name.' ('.$enreg['username'].')'; ?>
  371. </option>
  372. <?php } ?>
  373. </select>
  374. </div>
  375. </div>
  376. </div>
  377. </div>
  378. <div class="col-md-4">
  379. <?php if ($ajax_search) { ?>
  380. <button class="btn btn-primary" type="button" onclick="remove_item(document.getElementById('destination'))"></button>
  381. <?php } else { ?>
  382. <button class="btn btn-primary" type="button" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))" onclick="moveItem(document.getElementById('origin'), document.getElementById('destination'))">
  383. <i class="fa fa-chevron-right"></i>
  384. </button>
  385. <br /><br />
  386. <button class="btn btn-primary" type="button" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))" onclick="moveItem(document.getElementById('destination'), document.getElementById('origin'))">
  387. <i class="fa fa-chevron-left"></i>
  388. </button>
  389. <?php
  390. }
  391. ?>
  392. <?php
  393. echo '<button class="btn btn-success" type="button" value="" onclick="valide()" >'.$tool_name.'</button>';
  394. ?>
  395. </div>
  396. <div class="col-md-4">
  397. <?php
  398. if (UserManager::is_admin($user_id)) {
  399. echo get_lang('AssignedUsersListToPlatformAdministrator');
  400. } else if ($user_info['status'] == SESSIONADMIN) {
  401. echo get_lang('AssignedUsersListToSessionsAdministrator');
  402. } else if ($user_info['status'] == STUDENT_BOSS) {
  403. echo get_lang('AssignedUsersListToStudentBoss');
  404. } else {
  405. echo get_lang('AssignedUsersListToHumanResourcesManager');
  406. }
  407. ?>
  408. <div class="form-group">
  409. <div class="col-sm-12">
  410. <br>
  411. <select id='destination' class="form-control" name="UsersList[]" multiple="multiple" size="15" >
  412. <?php
  413. if (is_array($assigned_users_to_hrm)) {
  414. foreach($assigned_users_to_hrm as $enreg) {
  415. $person_name = api_get_person_name($enreg['firstname'], $enreg['lastname']);
  416. ?>
  417. <option value="<?php echo $enreg['user_id']; ?>" <?php echo 'title="'.htmlspecialchars($person_name,ENT_QUOTES).'"'; ?>>
  418. <?php echo $person_name.' ('.$enreg['username'].')'; ?>
  419. </option>
  420. <?php }
  421. }?>
  422. </select>
  423. </div>
  424. </div>
  425. </div>
  426. </div>
  427. </form>
  428. <?php
  429. Display::display_footer();