user_list.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * @author Bart Mollet
  6. * @author Julio Montoya <gugli100@gmail.com> BeezNest 2011
  7. *
  8. * @package chamilo.admin
  9. */
  10. $cidReset = true;
  11. require_once __DIR__.'/../inc/global.inc.php';
  12. api_protect_session_admin_list_users();
  13. $urlId = api_get_current_access_url_id();
  14. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
  15. // Login as can be used by different roles
  16. if (isset($_GET['user_id']) && $action == 'login_as') {
  17. $check = Security::check_token('get');
  18. if ($check && api_can_login_as($_GET['user_id'])) {
  19. $result = UserManager::loginAsUser($_GET['user_id']);
  20. if ($result) {
  21. $userInfo = api_get_user_info();
  22. $userId = $userInfo['id'];
  23. $message = sprintf(
  24. get_lang('AttemptingToLoginAs'),
  25. $userInfo['complete_name_with_username'],
  26. '',
  27. $userId
  28. );
  29. $url = api_get_path(WEB_PATH).'user_portal.php';
  30. $goTo = sprintf(get_lang('LoginSuccessfulGoToX'), Display::url($url, $url));
  31. Display::display_header(get_lang('UserList'));
  32. echo Display::return_message($message, 'normal', false);
  33. echo Display::return_message($goTo, 'normal', false);
  34. Display::display_footer();
  35. exit;
  36. } else {
  37. api_not_allowed(true);
  38. }
  39. }
  40. Security::clear_token();
  41. }
  42. api_protect_admin_script(true);
  43. trimVariables();
  44. $url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=get_user_courses';
  45. $urlSession = api_get_path(WEB_AJAX_PATH).'session.ajax.php?a=get_user_sessions';
  46. $extraField = new ExtraField('user');
  47. $variables = $extraField->get_all_extra_field_by_type(ExtraField::FIELD_TYPE_TAG);
  48. $variablesSelect = $extraField->get_all_extra_field_by_type(ExtraField::FIELD_TYPE_SELECT);
  49. if (!empty($variablesSelect)) {
  50. $variables = array_merge($variables, $variablesSelect);
  51. }
  52. $variablesToShow = [];
  53. if ($variables) {
  54. foreach ($variables as $variableId) {
  55. $extraFieldInfo = $extraField->get($variableId);
  56. $variablesToShow[] = $extraFieldInfo['variable'];
  57. }
  58. }
  59. Session::write('variables_to_show', $variablesToShow);
  60. $htmlHeadXtra[] = '<script>
  61. function load_course_list (div_course,my_user_id) {
  62. $.ajax({
  63. contentType: "application/x-www-form-urlencoded",
  64. beforeSend: function(myObject) {
  65. $("div#"+div_course).html("<img src=\'../inc/lib/javascript/indicator.gif\' />"); },
  66. type: "POST",
  67. url: "'.$url.'",
  68. data: "user_id="+my_user_id,
  69. success: function(datos) {
  70. $("div#"+div_course).html(datos);
  71. $("div#div_"+my_user_id).attr("class","blackboard_show");
  72. $("div#div_"+my_user_id).attr("style","");
  73. }
  74. });
  75. }
  76. function load_session_list(div_session, my_user_id) {
  77. $.ajax({
  78. contentType: "application/x-www-form-urlencoded",
  79. beforeSend: function(myObject) {
  80. $("div#"+div_session).html("<img src=\'../inc/lib/javascript/indicator.gif\' />"); },
  81. type: "POST",
  82. url: "'.$urlSession.'",
  83. data: "user_id="+my_user_id,
  84. success: function(datos) {
  85. $("div#"+div_session).html(datos);
  86. $("div#div_s_"+my_user_id).attr("class","blackboard_show");
  87. $("div#div_s_"+my_user_id).attr("style","");
  88. }
  89. });
  90. }
  91. function active_user(element_div) {
  92. id_image=$(element_div).attr("id");
  93. image_clicked=$(element_div).attr("src");
  94. image_clicked_info = image_clicked.split("/");
  95. image_real_clicked = image_clicked_info[image_clicked_info.length-1];
  96. var status = 1;
  97. if (image_real_clicked == "accept.png") {
  98. status = 0;
  99. }
  100. user_id=id_image.split("_");
  101. ident="#img_"+user_id[1];
  102. if (confirm("'.get_lang('AreYouSureToEditTheUserStatus', '').'")) {
  103. $.ajax({
  104. contentType: "application/x-www-form-urlencoded",
  105. beforeSend: function(myObject) {
  106. $(ident).attr("src","'.Display::returnIconPath('loading1.gif').'"); }, //candy eye stuff
  107. type: "GET",
  108. url: "'.api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=active_user",
  109. data: "user_id="+user_id[1]+"&status="+status,
  110. success: function(data) {
  111. if (data == 1) {
  112. $(ident).attr("src", "'.Display::returnIconPath('accept.png', ICON_SIZE_TINY).'");
  113. $(ident).attr("title","'.get_lang('Lock').'");
  114. }
  115. if (data == 0) {
  116. $(ident).attr("src","'.Display::returnIconPath('error.png').'");
  117. $(ident).attr("title","'.get_lang('Unlock').'");
  118. }
  119. if (data == -1) {
  120. $(ident).attr("src", "'.Display::returnIconPath('warning.png').'");
  121. $(ident).attr("title","'.get_lang('ActionNotAllowed').'");
  122. }
  123. }
  124. });
  125. }
  126. }
  127. function clear_course_list(div_course) {
  128. $("div#"+div_course).html("&nbsp;");
  129. $("div#"+div_course).hide("");
  130. }
  131. function clear_session_list(div_session) {
  132. $("div#"+div_session).html("&nbsp;");
  133. $("div#"+div_session).hide("");
  134. }
  135. function display_advanced_search_form () {
  136. if ($("#advanced_search_form").css("display") == "none") {
  137. $("#advanced_search_form").css("display","block");
  138. $("#img_plus_and_minus").html(\'&nbsp;'.Display::returnFontAwesomeIcon('arrow-down').' '.get_lang('AdvancedSearch').'\');
  139. } else {
  140. $("#advanced_search_form").css("display","none");
  141. $("#img_plus_and_minus").html(\'&nbsp;'.Display::returnFontAwesomeIcon('arrow-right').' '.get_lang('AdvancedSearch').'\');
  142. }
  143. }
  144. $(function () {
  145. var select_val = $("#input_select_extra_data").val();
  146. if ( document.getElementById(\'extra_data_text\')) {
  147. if (select_val != 0) {
  148. document.getElementById(\'extra_data_text\').style.display="block";
  149. if (document.getElementById(\'input_extra_text\'))
  150. document.getElementById(\'input_extra_text\').value = "";
  151. } else {
  152. document.getElementById(\'extra_data_text\').style.display="none";
  153. }
  154. }
  155. });
  156. //Load user calendar
  157. function load_calendar(user_id, month, year) {
  158. var url = "'.api_get_path(WEB_AJAX_PATH).'agenda.ajax.php?a=get_user_agenda&user_id=" +user_id + "&month="+month+"&year="+year;
  159. $(".modal-body").load(url);
  160. }
  161. </script>';
  162. $this_section = SECTION_PLATFORM_ADMIN;
  163. /**
  164. * Trim variable values to avoid trailing spaces.
  165. */
  166. function trimVariables()
  167. {
  168. $filterVariables = [
  169. 'keyword',
  170. 'keyword_firstname',
  171. 'keyword_lastname',
  172. 'keyword_username',
  173. 'keyword_email',
  174. 'keyword_officialcode',
  175. ];
  176. foreach ($filterVariables as $variable) {
  177. if (isset($_GET[$variable])) {
  178. $_GET[$variable] = trim($_GET[$variable]);
  179. }
  180. }
  181. }
  182. /**
  183. * Prepares the shared SQL query for the user table.
  184. * See get_user_data() and get_number_of_users().
  185. *
  186. * @param bool $getCount Whether to count, or get data
  187. *
  188. * @return string SQL query
  189. */
  190. function prepare_user_sql_query($getCount)
  191. {
  192. $sql = '';
  193. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  194. $admin_table = Database::get_main_table(TABLE_MAIN_ADMIN);
  195. if ($getCount) {
  196. $sql .= "SELECT COUNT(u.id) AS total_number_of_items FROM $user_table u";
  197. } else {
  198. $sql .= 'SELECT u.id AS col0, u.official_code AS col2, ';
  199. if (api_is_western_name_order()) {
  200. $sql .= 'u.firstname AS col3, u.lastname AS col4, ';
  201. } else {
  202. $sql .= 'u.lastname AS col3, u.firstname AS col4, ';
  203. }
  204. $sql .= " u.username AS col5,
  205. u.email AS col6,
  206. u.status AS col7,
  207. u.active AS col8,
  208. u.registration_date AS col9,
  209. u.last_login as col10,
  210. u.id AS col11,
  211. u.expiration_date AS exp,
  212. u.password
  213. FROM $user_table u";
  214. }
  215. // adding the filter to see the user's only of the current access_url
  216. if ((api_is_platform_admin() || api_is_session_admin()) && api_get_multiple_access_url()) {
  217. $access_url_rel_user_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  218. $sql .= " INNER JOIN $access_url_rel_user_table url_rel_user
  219. ON (u.id=url_rel_user.user_id)";
  220. }
  221. $keywordList = [
  222. 'keyword_firstname',
  223. 'keyword_lastname',
  224. 'keyword_username',
  225. 'keyword_email',
  226. 'keyword_officialcode',
  227. 'keyword_status',
  228. 'keyword_active',
  229. 'keyword_inactive',
  230. 'check_easy_passwords',
  231. ];
  232. $keywordListValues = [];
  233. $atLeastOne = false;
  234. foreach ($keywordList as $keyword) {
  235. $keywordListValues[$keyword] = null;
  236. if (isset($_GET[$keyword]) && !empty($_GET[$keyword])) {
  237. $keywordListValues[$keyword] = $_GET[$keyword];
  238. $atLeastOne = true;
  239. }
  240. }
  241. if ($atLeastOne == false) {
  242. $keywordListValues = [];
  243. }
  244. /*
  245. // This block is never executed because $keyword_extra_data never exists
  246. if (isset($keyword_extra_data) && !empty($keyword_extra_data)) {
  247. $extra_info = UserManager::get_extra_field_information_by_name($keyword_extra_data);
  248. $field_id = $extra_info['id'];
  249. $sql.= " INNER JOIN user_field_values ufv ON u.id=ufv.user_id AND ufv.field_id=$field_id ";
  250. } */
  251. if (isset($_GET['keyword']) && !empty($_GET['keyword'])) {
  252. $keywordFiltered = Database::escape_string("%".$_GET['keyword']."%");
  253. $sql .= " WHERE (
  254. u.firstname LIKE '$keywordFiltered' OR
  255. u.lastname LIKE '$keywordFiltered' OR
  256. concat(u.firstname, ' ', u.lastname) LIKE '$keywordFiltered' OR
  257. concat(u.lastname,' ',u.firstname) LIKE '$keywordFiltered' OR
  258. u.username LIKE '$keywordFiltered' OR
  259. u.official_code LIKE '$keywordFiltered' OR
  260. u.email LIKE '$keywordFiltered'
  261. )
  262. ";
  263. } elseif (isset($keywordListValues) && !empty($keywordListValues)) {
  264. $query_admin_table = '';
  265. $keyword_admin = '';
  266. if (isset($keywordListValues['keyword_status']) &&
  267. $keywordListValues['keyword_status'] == PLATFORM_ADMIN
  268. ) {
  269. $query_admin_table = " , $admin_table a ";
  270. $keyword_admin = ' AND a.user_id = u.id ';
  271. $keywordListValues['keyword_status'] = '%';
  272. }
  273. $keyword_extra_value = '';
  274. // This block is never executed because $keyword_extra_data never exists
  275. /*
  276. if (isset($keyword_extra_data) && !empty($keyword_extra_data) &&
  277. !empty($keyword_extra_data_text)) {
  278. $keyword_extra_value = " AND ufv.field_value LIKE '%".trim($keyword_extra_data_text)."%' ";
  279. }
  280. */
  281. $sql .= " $query_admin_table
  282. WHERE (
  283. u.firstname LIKE '".Database::escape_string("%".$keywordListValues['keyword_firstname']."%")."' AND
  284. u.lastname LIKE '".Database::escape_string("%".$keywordListValues['keyword_lastname']."%")."' AND
  285. u.username LIKE '".Database::escape_string("%".$keywordListValues['keyword_username']."%")."' AND
  286. u.email LIKE '".Database::escape_string("%".$keywordListValues['keyword_email']."%")."' AND
  287. u.status LIKE '".Database::escape_string($keywordListValues['keyword_status'])."' ";
  288. if (!empty($keywordListValues['keyword_officialcode'])) {
  289. $sql .= " AND u.official_code LIKE '".Database::escape_string("%".$keywordListValues['keyword_officialcode']."%")."' ";
  290. }
  291. $sql .= "
  292. $keyword_admin
  293. $keyword_extra_value
  294. ";
  295. if (isset($keywordListValues['keyword_active']) &&
  296. !isset($keywordListValues['keyword_inactive'])
  297. ) {
  298. $sql .= ' AND u.active = 1';
  299. } elseif (isset($keywordListValues['keyword_inactive']) &&
  300. !isset($keywordListValues['keyword_active'])
  301. ) {
  302. $sql .= ' AND u.active = 0';
  303. }
  304. $sql .= ' ) ';
  305. }
  306. $preventSessionAdminsToManageAllUsers = api_get_setting('prevent_session_admins_to_manage_all_users');
  307. if (api_is_session_admin() && $preventSessionAdminsToManageAllUsers === 'true') {
  308. $sql .= ' AND u.creator_id = '.api_get_user_id();
  309. }
  310. $variables = Session::read('variables_to_show', []);
  311. if (!empty($variables)) {
  312. $extraField = new ExtraField('user');
  313. $extraFieldResult = [];
  314. $extraFieldHasData = [];
  315. foreach ($variables as $variable) {
  316. if (isset($_GET['extra_'.$variable])) {
  317. if (is_array($_GET['extra_'.$variable])) {
  318. $values = $_GET['extra_'.$variable];
  319. } else {
  320. $values = [$_GET['extra_'.$variable]];
  321. }
  322. if (empty($values)) {
  323. continue;
  324. }
  325. $info = $extraField->get_handler_field_info_by_field_variable(
  326. $variable
  327. );
  328. if (empty($info)) {
  329. continue;
  330. }
  331. foreach ($values as $value) {
  332. if (empty($value)) {
  333. continue;
  334. }
  335. if ($info['field_type'] == ExtraField::FIELD_TYPE_TAG) {
  336. $result = $extraField->getAllUserPerTag(
  337. $info['id'],
  338. $value
  339. );
  340. $result = empty($result) ? [] : array_column(
  341. $result,
  342. 'user_id'
  343. );
  344. } else {
  345. $result = UserManager::get_extra_user_data_by_value(
  346. $variable,
  347. $value
  348. );
  349. }
  350. $extraFieldHasData[] = true;
  351. if (!empty($result)) {
  352. $extraFieldResult = array_merge(
  353. $extraFieldResult,
  354. $result
  355. );
  356. }
  357. }
  358. }
  359. }
  360. if (!empty($extraFieldHasData)) {
  361. $sql .= " AND (u.id IN ('".implode("','", $extraFieldResult)."')) ";
  362. }
  363. }
  364. // adding the filter to see the user's only of the current access_url
  365. if ((api_is_platform_admin() || api_is_session_admin()) &&
  366. api_get_multiple_access_url()
  367. ) {
  368. $sql .= ' AND url_rel_user.access_url_id = '.api_get_current_access_url_id();
  369. }
  370. return $sql;
  371. }
  372. /**
  373. * Get the total number of users on the platform.
  374. *
  375. * @see SortableTable#get_total_number_of_items()
  376. */
  377. function get_number_of_users()
  378. {
  379. $sql = prepare_user_sql_query(true);
  380. $res = Database::query($sql);
  381. $obj = Database::fetch_object($res);
  382. return $obj->total_number_of_items;
  383. }
  384. /**
  385. * Get the users to display on the current page (fill the sortable-table).
  386. *
  387. * @param int offset of first user to recover
  388. * @param int Number of users to get
  389. * @param int Column to sort on
  390. * @param string Order (ASC,DESC)
  391. *
  392. * @return array Users list
  393. *
  394. * @see SortableTable#get_table_data($from)
  395. */
  396. function get_user_data($from, $number_of_items, $column, $direction)
  397. {
  398. $sql = prepare_user_sql_query(false);
  399. if (!in_array($direction, ['ASC', 'DESC'])) {
  400. $direction = 'ASC';
  401. }
  402. $column = (int) $column;
  403. $from = (int) $from;
  404. $number_of_items = (int) $number_of_items;
  405. $sql .= " ORDER BY col$column $direction ";
  406. $sql .= " LIMIT $from, $number_of_items";
  407. $res = Database::query($sql);
  408. $users = [];
  409. $t = time();
  410. while ($user = Database::fetch_array($res)) {
  411. $userPicture = UserManager::getUserPicture(
  412. $user[0],
  413. USER_IMAGE_SIZE_SMALL
  414. );
  415. $photo = '<img
  416. src="'.$userPicture.'" width="22" height="22"
  417. alt="'.api_get_person_name($user[2], $user[3]).'"
  418. title="'.api_get_person_name($user[2], $user[3]).'" />';
  419. if ($user[7] == 1 && !empty($user['exp'])) {
  420. // check expiration date
  421. $expiration_time = convert_sql_date($user['exp']);
  422. // if expiration date is passed, store a special value for active field
  423. if ($expiration_time < $t) {
  424. $user[7] = '-1';
  425. }
  426. }
  427. // forget about the expiration date field
  428. $users[] = [
  429. $user[0], // id
  430. $photo,
  431. $user[1],
  432. $user[2],
  433. $user[3],
  434. $user[4], // username
  435. $user[5], // email
  436. $user[6],
  437. $user[7], // active
  438. api_get_local_time($user[8]),
  439. api_get_local_time($user[9], null, null, true),
  440. $user[0],
  441. ];
  442. }
  443. return $users;
  444. }
  445. /**
  446. * Returns a mailto-link.
  447. *
  448. * @param string $email An email-address
  449. *
  450. * @return string HTML-code with a mailto-link
  451. */
  452. function email_filter($email)
  453. {
  454. return Display::encrypted_mailto_link($email, cut($email, 26));
  455. }
  456. /**
  457. * Returns a mailto-link.
  458. *
  459. * @param string $name
  460. * @param array $params Deprecated
  461. * @param array $row
  462. *
  463. * @return string HTML-code with a mailto-link
  464. */
  465. function user_filter($name, $params, $row)
  466. {
  467. $name = cut($name, 26);
  468. return '<a href="'.api_get_path(WEB_CODE_PATH).'admin/user_information.php?user_id='.$row[0].'">'.$name.'</a>';
  469. }
  470. /**
  471. * Build the modify-column of the table.
  472. *
  473. * @param int The user id
  474. * @param string URL params to add to table links
  475. * @param array Row of elements to alter
  476. *
  477. * @throws Exception
  478. *
  479. * @return string Some HTML-code with modify-buttons
  480. */
  481. function modify_filter($user_id, $url_params, $row)
  482. {
  483. $_admins_list = Session::read('admin_list', []);
  484. $is_admin = in_array($user_id, $_admins_list);
  485. $statusname = api_get_status_langvars();
  486. $user_is_anonymous = false;
  487. $current_user_status_label = $row['7'];
  488. if ($current_user_status_label == $statusname[ANONYMOUS]) {
  489. $user_is_anonymous = true;
  490. }
  491. $result = '';
  492. if (!$user_is_anonymous) {
  493. $icon = Display::return_icon(
  494. 'course.png',
  495. get_lang('Courses'),
  496. ['onmouseout' => 'clear_course_list (\'div_'.$user_id.'\')']
  497. );
  498. $result .= '<a href="javascript:void(0)" onclick="load_course_list(\'div_'.$user_id.'\','.$user_id.')" >
  499. '.$icon.'
  500. <div class="blackboard_hide" id="div_'.$user_id.'">&nbsp;&nbsp;</div>
  501. </a>';
  502. $icon = Display::return_icon(
  503. 'session.png',
  504. get_lang('Sessions'),
  505. ['onmouseout' => 'clear_session_list (\'div_s_'.$user_id.'\')']
  506. );
  507. $result .= '<a href="javascript:void(0)" onclick="load_session_list(\'div_s_'.$user_id.'\','.$user_id.')" >
  508. '.$icon.'
  509. <div class="blackboard_hide" id="div_s_'.$user_id.'">&nbsp;&nbsp;</div>
  510. </a>';
  511. } else {
  512. $result .= Display::return_icon('course_na.png', get_lang('Courses')).'&nbsp;&nbsp;';
  513. $result .= Display::return_icon('course_na.png', get_lang('Sessions')).'&nbsp;&nbsp;';
  514. }
  515. if (api_is_platform_admin()) {
  516. if (!$user_is_anonymous) {
  517. $result .= '<a href="user_information.php?user_id='.$user_id.'">'.
  518. Display::return_icon('info2.png', get_lang('Info')).'</a>&nbsp;&nbsp;';
  519. } else {
  520. $result .= Display::return_icon('info2_na.png', get_lang('Info')).'&nbsp;&nbsp;';
  521. }
  522. }
  523. // Only allow platform admins to login_as, or session admins only for students (not teachers nor other admins)
  524. $loginAsStatusForSessionAdmins = [$statusname[STUDENT]];
  525. // Except when allow_session_admin_login_as_teacher is enabled, then can login_as teachers also
  526. if (api_get_configuration_value('allow_session_admin_login_as_teacher')) {
  527. $loginAsStatusForSessionAdmins[] = $statusname[COURSEMANAGER];
  528. }
  529. $sessionAdminCanLoginAs = api_is_session_admin() &&
  530. in_array($current_user_status_label, $loginAsStatusForSessionAdmins);
  531. if (api_is_platform_admin() || $sessionAdminCanLoginAs) {
  532. if (!$user_is_anonymous) {
  533. if (api_global_admin_can_edit_admin($user_id, null, $sessionAdminCanLoginAs)) {
  534. $result .= '<a href="user_list.php?action=login_as&user_id='.$user_id.'&sec_token='.Security::getTokenFromSession().'">'.
  535. Display::return_icon('login_as.png', get_lang('LoginAs')).'</a>&nbsp;';
  536. } else {
  537. $result .= Display::return_icon('login_as_na.png', get_lang('LoginAs')).'&nbsp;';
  538. }
  539. } else {
  540. $result .= Display::return_icon('login_as_na.png', get_lang('LoginAs')).'&nbsp;';
  541. }
  542. } else {
  543. $result .= Display::return_icon('login_as_na.png', get_lang('LoginAs')).'&nbsp;';
  544. }
  545. if ($current_user_status_label != $statusname[STUDENT]) {
  546. $result .= Display::return_icon(
  547. 'statistics_na.png',
  548. get_lang('Reporting')
  549. ).'&nbsp;';
  550. } else {
  551. $result .= '<a href="../mySpace/myStudents.php?student='.$user_id.'">'.
  552. Display::return_icon('statistics.png', get_lang('Reporting')).
  553. '</a>&nbsp;';
  554. }
  555. if (api_is_platform_admin(true)) {
  556. $editProfileUrl = Display::getProfileEditionLink($user_id, true);
  557. if (!$user_is_anonymous &&
  558. api_global_admin_can_edit_admin($user_id, null, true)
  559. ) {
  560. $result .= '<a href="'.$editProfileUrl.'">'.
  561. Display::return_icon(
  562. 'edit.png',
  563. get_lang('Edit'),
  564. [],
  565. ICON_SIZE_SMALL
  566. ).
  567. '</a>&nbsp;';
  568. } else {
  569. $result .= Display::return_icon(
  570. 'edit_na.png',
  571. get_lang('Edit'),
  572. [],
  573. ICON_SIZE_SMALL
  574. ).'</a>&nbsp;';
  575. }
  576. }
  577. $allowAssignSkill = api_is_platform_admin(false, true);
  578. if ($allowAssignSkill) {
  579. $result .= Display::url(
  580. Display::return_icon(
  581. 'skill-badges.png',
  582. get_lang('AssignSkill'),
  583. null,
  584. ICON_SIZE_SMALL
  585. ),
  586. api_get_path(WEB_CODE_PATH).'badge/assign.php?'.http_build_query(['user' => $user_id])
  587. );
  588. }
  589. if ($is_admin) {
  590. $result .= Display::return_icon(
  591. 'admin_star.png',
  592. get_lang('IsAdministrator'),
  593. ['width' => ICON_SIZE_SMALL, 'heigth' => ICON_SIZE_SMALL]
  594. );
  595. } else {
  596. $result .= Display::return_icon(
  597. 'admin_star_na.png',
  598. get_lang('IsNotAdministrator')
  599. );
  600. }
  601. // actions for assigning sessions, courses or users
  602. if (!api_is_session_admin()) {
  603. if ($current_user_status_label == $statusname[SESSIONADMIN]) {
  604. $result .= Display::url(
  605. Display::return_icon(
  606. 'view_more_stats.gif',
  607. get_lang('AssignSessions')
  608. ),
  609. "dashboard_add_sessions_to_user.php?user={$user_id}"
  610. );
  611. } else {
  612. if ($current_user_status_label == $statusname[DRH] ||
  613. UserManager::is_admin($user_id) ||
  614. $current_user_status_label == $statusname[STUDENT_BOSS]
  615. ) {
  616. $result .= Display::url(
  617. Display::return_icon(
  618. 'user_subscribe_course.png',
  619. get_lang('AssignUsers'),
  620. '',
  621. ICON_SIZE_SMALL
  622. ),
  623. "dashboard_add_users_to_user.php?user={$user_id}"
  624. );
  625. }
  626. if ($current_user_status_label == $statusname[DRH] || UserManager::is_admin($user_id)) {
  627. $result .= Display::url(
  628. Display::return_icon(
  629. 'course_add.gif',
  630. get_lang('AssignCourses')
  631. ),
  632. "dashboard_add_courses_to_user.php?user={$user_id}"
  633. );
  634. $result .= Display::url(
  635. Display::return_icon(
  636. 'view_more_stats.gif',
  637. get_lang('AssignSessions')
  638. ),
  639. "dashboard_add_sessions_to_user.php?user={$user_id}"
  640. );
  641. }
  642. }
  643. }
  644. $allowDelete = api_get_configuration_value('allow_delete_user_for_session_admin');
  645. if (api_is_session_admin() && $allowDelete) {
  646. if ($user_id != api_get_user_id() &&
  647. !$user_is_anonymous &&
  648. api_global_admin_can_edit_admin($user_id, null, true)
  649. ) {
  650. // you cannot lock yourself out otherwise you could disable all the accounts including your own => everybody is locked out and nobody can change it anymore.
  651. $result .= ' <a href="user_list.php?action=delete_user&user_id='.$user_id.'&'.$url_params.'&sec_token='.Security::getTokenFromSession().'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice")))."'".')) return false;">'.
  652. Display::return_icon(
  653. 'delete.png',
  654. get_lang('Delete'),
  655. [],
  656. ICON_SIZE_SMALL
  657. ).
  658. '</a>';
  659. }
  660. }
  661. if (api_is_platform_admin()) {
  662. $result .= ' <a data-title="'.get_lang('FreeBusyCalendar').'" href="'.api_get_path(WEB_AJAX_PATH).'agenda.ajax.php?a=get_user_agenda&user_id='.$user_id.'&modal_size=lg" class="agenda_opener ajax">'.
  663. Display::return_icon(
  664. 'calendar.png',
  665. get_lang('FreeBusyCalendar'),
  666. [],
  667. ICON_SIZE_SMALL
  668. ).
  669. '</a>';
  670. if ($user_id != api_get_user_id() &&
  671. !$user_is_anonymous &&
  672. api_global_admin_can_edit_admin($user_id)
  673. ) {
  674. $result .= ' <a href="user_list.php?action=anonymize&user_id='.$user_id.'&'.$url_params.'&sec_token='.Security::getTokenFromSession().'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice")))."'".')) return false;">'.
  675. Display::return_icon(
  676. 'anonymous.png',
  677. get_lang('Anonymize'),
  678. [],
  679. ICON_SIZE_SMALL
  680. ).
  681. '</a>';
  682. }
  683. $deleteAllowed = !api_get_configuration_value('deny_delete_users');
  684. if ($deleteAllowed) {
  685. if ($user_id != api_get_user_id() &&
  686. !$user_is_anonymous &&
  687. api_global_admin_can_edit_admin($user_id)
  688. ) {
  689. // you cannot lock yourself out otherwise you could disable all the accounts
  690. // including your own => everybody is locked out and nobody can change it anymore.
  691. $result .= ' <a href="user_list.php?action=delete_user&user_id='.$user_id.'&'.$url_params.'&sec_token='.Security::getTokenFromSession().'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice")))."'".')) return false;">'.
  692. Display::return_icon(
  693. 'delete.png',
  694. get_lang('Delete'),
  695. [],
  696. ICON_SIZE_SMALL
  697. ).
  698. '</a>';
  699. } else {
  700. $result .= Display::return_icon(
  701. 'delete_na.png',
  702. get_lang('Delete'),
  703. [],
  704. ICON_SIZE_SMALL
  705. );
  706. }
  707. }
  708. }
  709. return $result;
  710. }
  711. /**
  712. * Build the active-column of the table to lock or unlock a certain user
  713. * lock = the user can no longer use this account.
  714. *
  715. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  716. *
  717. * @param int $active the current state of the account
  718. * @param string $params
  719. * @param array $row
  720. *
  721. * @return string Some HTML-code with the lock/unlock button
  722. */
  723. function active_filter($active, $params, $row)
  724. {
  725. $_user = api_get_user_info();
  726. if ($active == '1') {
  727. $action = 'Lock';
  728. $image = 'accept';
  729. } elseif ($active == '-1') {
  730. $action = 'edit';
  731. $image = 'warning';
  732. } elseif ($active == '0') {
  733. $action = 'Unlock';
  734. $image = 'error';
  735. }
  736. $result = '';
  737. if ($action === 'edit') {
  738. $result = Display::return_icon(
  739. $image.'.png',
  740. get_lang('AccountExpired'),
  741. [],
  742. 16
  743. );
  744. } elseif ($row['0'] != $_user['user_id']) {
  745. // you cannot lock yourself out otherwise you could disable all the
  746. // accounts including your own => everybody is locked out and nobody
  747. // can change it anymore.
  748. $result = Display::return_icon(
  749. $image.'.png',
  750. get_lang(ucfirst($action)),
  751. ['onclick' => 'active_user(this);', 'id' => 'img_'.$row['0']],
  752. 16
  753. );
  754. }
  755. return $result;
  756. }
  757. /**
  758. * Instead of displaying the integer of the status, we give a translation for the status.
  759. *
  760. * @param int $status
  761. *
  762. * @return string translation
  763. *
  764. * @version march 2008
  765. *
  766. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  767. */
  768. function status_filter($status)
  769. {
  770. $name = api_get_status_langvars();
  771. return $name[$status];
  772. }
  773. if (isset($_GET['keyword']) || isset($_GET['keyword_firstname'])) {
  774. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  775. $interbreadcrumb[] = ['url' => 'user_list.php', 'name' => get_lang('UserList')];
  776. $tool_name = get_lang('SearchUsers');
  777. } else {
  778. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  779. $tool_name = get_lang('UserList');
  780. }
  781. $message = '';
  782. if (!empty($action)) {
  783. $check = Security::check_token('get');
  784. if ($check) {
  785. switch ($action) {
  786. case 'add_user_to_my_url':
  787. $user_id = $_REQUEST['user_id'];
  788. $result = UrlManager::add_user_to_url($user_id, $urlId);
  789. if ($result) {
  790. $user_info = api_get_user_info($user_id);
  791. $message = get_lang('UserAdded').' '.$user_info['complete_name_with_username'];
  792. $message = Display::return_message($message, 'confirmation');
  793. }
  794. break;
  795. case 'delete_user':
  796. $message = UserManager::deleteUserWithVerification($_GET['user_id']);
  797. Display::addFlash($message);
  798. header('Location: '.api_get_self());
  799. exit;
  800. break;
  801. case 'delete':
  802. if (api_is_platform_admin() && !empty($_POST['id'])) {
  803. $number_of_selected_users = count($_POST['id']);
  804. $number_of_affected_users = 0;
  805. if (is_array($_POST['id'])) {
  806. foreach ($_POST['id'] as $index => $user_id) {
  807. if ($user_id != $_user['user_id']) {
  808. if (UserManager::delete_user($user_id)) {
  809. $number_of_affected_users++;
  810. }
  811. }
  812. }
  813. }
  814. if ($number_of_selected_users == $number_of_affected_users) {
  815. $message = Display::return_message(
  816. get_lang('SelectedUsersDeleted'),
  817. 'confirmation'
  818. );
  819. } else {
  820. $message = Display::return_message(
  821. get_lang('SomeUsersNotDeleted'),
  822. 'error'
  823. );
  824. }
  825. }
  826. break;
  827. case 'disable':
  828. if (api_is_platform_admin()) {
  829. $number_of_selected_users = count($_POST['id']);
  830. $number_of_affected_users = 0;
  831. if (is_array($_POST['id'])) {
  832. foreach ($_POST['id'] as $index => $user_id) {
  833. if ($user_id != $_user['user_id']) {
  834. if (UserManager::disable($user_id)) {
  835. $number_of_affected_users++;
  836. }
  837. }
  838. }
  839. }
  840. if ($number_of_selected_users == $number_of_affected_users) {
  841. $message = Display::return_message(
  842. get_lang('SelectedUsersDisabled'),
  843. 'confirmation'
  844. );
  845. } else {
  846. $message = Display::return_message(
  847. get_lang('SomeUsersNotDisabled'),
  848. 'error'
  849. );
  850. }
  851. }
  852. break;
  853. case 'enable':
  854. if (api_is_platform_admin()) {
  855. $number_of_selected_users = count($_POST['id']);
  856. $number_of_affected_users = 0;
  857. if (is_array($_POST['id'])) {
  858. foreach ($_POST['id'] as $index => $user_id) {
  859. if ($user_id != $_user['user_id']) {
  860. if (UserManager::enable($user_id)) {
  861. $number_of_affected_users++;
  862. }
  863. }
  864. }
  865. }
  866. if ($number_of_selected_users == $number_of_affected_users) {
  867. $message = Display::return_message(
  868. get_lang('SelectedUsersEnabled'),
  869. 'confirmation'
  870. );
  871. } else {
  872. $message = Display::return_message(
  873. get_lang('SomeUsersNotEnabled'),
  874. 'error'
  875. );
  876. }
  877. }
  878. break;
  879. case 'anonymize':
  880. $message = UserManager::anonymizeUserWithVerification($_GET['user_id']);
  881. Display::addFlash($message);
  882. header('Location: '.api_get_self());
  883. exit;
  884. break;
  885. }
  886. Security::clear_token();
  887. }
  888. }
  889. // Create a search-box
  890. $form = new FormValidator('search_simple', 'get', null, null, null, 'inline');
  891. $form->addText(
  892. 'keyword',
  893. get_lang('Search'),
  894. false,
  895. [
  896. 'aria-label' => get_lang('SearchUsers'),
  897. ]
  898. );
  899. $form->addButtonSearch(get_lang('Search'));
  900. $searchAdvanced = '
  901. <a id="advanced_params" href="javascript://"
  902. class="btn btn-default advanced_options" onclick="display_advanced_search_form();">
  903. <span id="img_plus_and_minus">&nbsp;
  904. '.Display::returnFontAwesomeIcon('arrow-right').' '.get_lang('AdvancedSearch').'
  905. </span>
  906. </a>';
  907. $actionsLeft = '';
  908. $actionsCenter = '';
  909. $actionsRight = '';
  910. if (api_is_platform_admin()) {
  911. $actionsRight .= '<a class="pull-right" href="'.api_get_path(WEB_CODE_PATH).'admin/user_add.php">'.
  912. Display::return_icon('new_user.png', get_lang('AddUsers'), '', ICON_SIZE_MEDIUM).'</a>';
  913. }
  914. $actionsLeft .= $form->returnForm();
  915. $actionsCenter .= $searchAdvanced;
  916. if (isset($_GET['keyword'])) {
  917. $parameters = ['keyword' => Security::remove_XSS($_GET['keyword'])];
  918. } elseif (isset($_GET['keyword_firstname'])) {
  919. $parameters['keyword_firstname'] = Security::remove_XSS($_GET['keyword_firstname']);
  920. $parameters['keyword_lastname'] = Security::remove_XSS($_GET['keyword_lastname']);
  921. $parameters['keyword_username'] = Security::remove_XSS($_GET['keyword_username']);
  922. $parameters['keyword_email'] = Security::remove_XSS($_GET['keyword_email']);
  923. $parameters['keyword_officialcode'] = Security::remove_XSS($_GET['keyword_officialcode']);
  924. $parameters['keyword_status'] = Security::remove_XSS($_GET['keyword_status']);
  925. $parameters['keyword_active'] = Security::remove_XSS($_GET['keyword_active']);
  926. $parameters['keyword_inactive'] = Security::remove_XSS($_GET['keyword_inactive']);
  927. }
  928. // Create a sortable table with user-data
  929. $parameters['sec_token'] = Security::get_token();
  930. $_admins_list = array_keys(UserManager::get_all_administrators());
  931. Session::write('admin_list', $_admins_list);
  932. // Display Advanced search form.
  933. $form = new FormValidator(
  934. 'advanced_search',
  935. 'get',
  936. '',
  937. '',
  938. [],
  939. FormValidator::LAYOUT_HORIZONTAL
  940. );
  941. $form->addElement('html', '<div id="advanced_search_form" style="display:none;">');
  942. $form->addElement('header', get_lang('AdvancedSearch'));
  943. $form->addText('keyword_firstname', get_lang('FirstName'), false);
  944. $form->addText('keyword_lastname', get_lang('LastName'), false);
  945. $form->addText('keyword_username', get_lang('LoginName'), false);
  946. $form->addText('keyword_email', get_lang('Email'), false);
  947. $form->addText('keyword_officialcode', get_lang('OfficialCode'), false);
  948. $status_options = [];
  949. $status_options['%'] = get_lang('All');
  950. $status_options[STUDENT] = get_lang('Student');
  951. $status_options[COURSEMANAGER] = get_lang('Teacher');
  952. $status_options[DRH] = get_lang('Drh');
  953. $status_options[SESSIONADMIN] = get_lang('SessionsAdmin');
  954. $status_options[PLATFORM_ADMIN] = get_lang('Administrator');
  955. $form->addElement(
  956. 'select',
  957. 'keyword_status',
  958. get_lang('Profile'),
  959. $status_options
  960. );
  961. $active_group = [];
  962. $active_group[] = $form->createElement('checkbox', 'keyword_active', '', get_lang('Active'));
  963. $active_group[] = $form->createElement('checkbox', 'keyword_inactive', '', get_lang('Inactive'));
  964. $form->addGroup($active_group, '', get_lang('ActiveAccount'), null, false);
  965. $form->addElement('checkbox', 'check_easy_passwords', null, get_lang('CheckEasyPasswords'));
  966. $data = $extraField->addElements($form, 0, [], true, false, $variablesToShow);
  967. $htmlHeadXtra[] = '
  968. <script>
  969. $(function () {
  970. '.$data['jquery_ready_content'].'
  971. })
  972. </script>
  973. ';
  974. $form->addButtonSearch(get_lang('SearchUsers'));
  975. $defaults = [];
  976. $defaults['keyword_active'] = 1;
  977. $defaults['keyword_inactive'] = 1;
  978. $form->setDefaults($defaults);
  979. $form->addElement('html', '</div>');
  980. $form = $form->returnForm();
  981. $table = new SortableTable(
  982. 'users',
  983. 'get_number_of_users',
  984. 'get_user_data',
  985. (api_is_western_name_order() xor api_sort_by_first_name()) ? 3 : 2
  986. );
  987. $table->set_additional_parameters($parameters);
  988. $table->set_header(0, '', false, 'width="18px"');
  989. $table->set_header(1, get_lang('Photo'), false);
  990. $table->set_header(2, get_lang('OfficialCode'));
  991. if (api_is_western_name_order()) {
  992. $table->set_header(3, get_lang('FirstName'));
  993. $table->set_header(4, get_lang('LastName'));
  994. } else {
  995. $table->set_header(3, get_lang('LastName'));
  996. $table->set_header(4, get_lang('FirstName'));
  997. }
  998. $table->set_header(5, get_lang('LoginName'));
  999. $table->set_header(6, get_lang('Email'));
  1000. $table->set_header(7, get_lang('Profile'));
  1001. $table->set_header(8, get_lang('Active'), true, 'width="15px"');
  1002. $table->set_header(9, get_lang('RegistrationDate'), true, 'width="90px"');
  1003. $table->set_header(10, get_lang('LatestLogin'), true, 'width="90px"');
  1004. $table->set_header(11, get_lang('Action'), false, 'width="220px"');
  1005. $table->set_column_filter(3, 'user_filter');
  1006. $table->set_column_filter(4, 'user_filter');
  1007. $table->set_column_filter(6, 'email_filter');
  1008. $table->set_column_filter(7, 'status_filter');
  1009. $table->set_column_filter(8, 'active_filter');
  1010. $table->set_column_filter(11, 'modify_filter');
  1011. // Hide email column if login is email, to avoid column with same data
  1012. if (api_get_setting('login_is_email') === 'true') {
  1013. $table->setHideColumn(6);
  1014. }
  1015. // Only show empty actions bar if delete users has been blocked
  1016. $actionsList = [];
  1017. if (api_is_platform_admin() &&
  1018. !api_get_configuration_value('deny_delete_users')
  1019. ) {
  1020. $actionsList['delete'] = get_lang('DeleteFromPlatform');
  1021. }
  1022. $actionsList['disable'] = get_lang('Disable');
  1023. $actionsList['enable'] = get_lang('Enable');
  1024. $table->set_form_actions($actionsList);
  1025. $table_result = $table->return_table();
  1026. $extra_search_options = '';
  1027. // Try to search the user everywhere
  1028. if ($table->get_total_number_of_items() == 0) {
  1029. if (api_get_multiple_access_url() && isset($_REQUEST['keyword'])) {
  1030. $keyword = Database::escape_string($_REQUEST['keyword']);
  1031. $conditions = ['username' => $keyword];
  1032. $user_list = UserManager::get_user_list(
  1033. $conditions,
  1034. [],
  1035. false,
  1036. ' OR '
  1037. );
  1038. if (!empty($user_list)) {
  1039. $extra_search_options = Display::page_subheader(get_lang('UsersFoundInOtherPortals'));
  1040. $table = new HTML_Table(['class' => 'data_table']);
  1041. $column = 0;
  1042. $row = 0;
  1043. $headers = [get_lang('User'), 'URL', get_lang('Actions')];
  1044. foreach ($headers as $header) {
  1045. $table->setHeaderContents($row, $column, $header);
  1046. $column++;
  1047. }
  1048. $row++;
  1049. foreach ($user_list as $user) {
  1050. $column = 0;
  1051. $access_info = UrlManager::get_access_url_from_user($user['id']);
  1052. $access_info_to_string = '';
  1053. $add_user = true;
  1054. if (!empty($access_info)) {
  1055. foreach ($access_info as $url_info) {
  1056. if ($urlId == $url_info['access_url_id']) {
  1057. $add_user = false;
  1058. }
  1059. $access_info_to_string .= $url_info['url'].'<br />';
  1060. }
  1061. }
  1062. if ($add_user) {
  1063. $row_table = [];
  1064. $row_table[] = api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].') ';
  1065. $row_table[] = $access_info_to_string;
  1066. $url = api_get_self().'?action=add_user_to_my_url&user_id='.$user['id'].'&sec_token='.Security::getTokenFromSession();
  1067. $row_table[] = Display::url(
  1068. get_lang('AddUserToMyURL'),
  1069. $url,
  1070. ['class' => 'btn']
  1071. );
  1072. foreach ($row_table as $cell) {
  1073. $table->setCellContents($row, $column, $cell);
  1074. $table->updateCellAttributes(
  1075. $row,
  1076. $column,
  1077. 'align="center"'
  1078. );
  1079. $column++;
  1080. }
  1081. $table->updateRowAttributes(
  1082. $row,
  1083. $row % 2 ? 'class="row_even"' : 'class="row_odd"',
  1084. true
  1085. );
  1086. $row++;
  1087. }
  1088. }
  1089. $extra_search_options .= $table->toHtml();
  1090. $table_result = '';
  1091. }
  1092. }
  1093. }
  1094. $toolbarActions = Display::toolbarAction(
  1095. 'toolbarUser',
  1096. [$actionsLeft, $actionsCenter, $actionsRight],
  1097. [4, 4, 4]
  1098. );
  1099. $tpl = new Template($tool_name);
  1100. $tpl->assign('actions', $toolbarActions);
  1101. $tpl->assign('message', $message);
  1102. $tpl->assign('content', $form.$table_result.$extra_search_options);
  1103. $tpl->display_one_col_template();