user_list.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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. $(document).ready(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.id AS col9,
  209. u.registration_date AS col10,
  210. u.expiration_date AS exp,
  211. u.password
  212. FROM $user_table u";
  213. }
  214. // adding the filter to see the user's only of the current access_url
  215. if ((api_is_platform_admin() || api_is_session_admin()) && api_get_multiple_access_url()) {
  216. $access_url_rel_user_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  217. $sql .= " INNER JOIN $access_url_rel_user_table url_rel_user
  218. ON (u.id=url_rel_user.user_id)";
  219. }
  220. $keywordList = [
  221. 'keyword_firstname',
  222. 'keyword_lastname',
  223. 'keyword_username',
  224. 'keyword_email',
  225. 'keyword_officialcode',
  226. 'keyword_status',
  227. 'keyword_active',
  228. 'keyword_inactive',
  229. 'check_easy_passwords',
  230. ];
  231. $keywordListValues = [];
  232. $atLeastOne = false;
  233. foreach ($keywordList as $keyword) {
  234. $keywordListValues[$keyword] = null;
  235. if (isset($_GET[$keyword]) && !empty($_GET[$keyword])) {
  236. $keywordListValues[$keyword] = $_GET[$keyword];
  237. $atLeastOne = true;
  238. }
  239. }
  240. if ($atLeastOne == false) {
  241. $keywordListValues = [];
  242. }
  243. /*
  244. // This block is never executed because $keyword_extra_data never exists
  245. if (isset($keyword_extra_data) && !empty($keyword_extra_data)) {
  246. $extra_info = UserManager::get_extra_field_information_by_name($keyword_extra_data);
  247. $field_id = $extra_info['id'];
  248. $sql.= " INNER JOIN user_field_values ufv ON u.id=ufv.user_id AND ufv.field_id=$field_id ";
  249. }
  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 = intval($column);
  403. $from = intval($from);
  404. $number_of_items = intval($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_row($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[10])) {
  420. // check expiration date
  421. $expiration_time = convert_sql_date($user[10]);
  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],
  430. $photo,
  431. $user[1],
  432. $user[2],
  433. $user[3],
  434. $user[4],
  435. $user[5],
  436. $user[6],
  437. $user[7],
  438. api_get_local_time($user[9]),
  439. $user[0],
  440. ];
  441. }
  442. return $users;
  443. }
  444. /**
  445. * Returns a mailto-link.
  446. *
  447. * @param string $email An email-address
  448. *
  449. * @return string HTML-code with a mailto-link
  450. */
  451. function email_filter($email)
  452. {
  453. return Display::encrypted_mailto_link($email, $email);
  454. }
  455. /**
  456. * Returns a mailto-link.
  457. *
  458. * @param string $email An email-address
  459. * @param array $params Deprecated
  460. * @param array $row
  461. *
  462. * @return string HTML-code with a mailto-link
  463. */
  464. function user_filter($name, $params, $row)
  465. {
  466. return '<a href="'.api_get_path(WEB_CODE_PATH).'admin/user_information.php?user_id='.$row[0].'">'.$name.'</a>';
  467. }
  468. /**
  469. * Build the modify-column of the table.
  470. *
  471. * @param int The user id
  472. * @param string URL params to add to table links
  473. * @param array Row of elements to alter
  474. *
  475. * @throws Exception
  476. *
  477. * @return string Some HTML-code with modify-buttons
  478. */
  479. function modify_filter($user_id, $url_params, $row)
  480. {
  481. $_admins_list = Session::read('admin_list', []);
  482. $is_admin = in_array($user_id, $_admins_list);
  483. $statusname = api_get_status_langvars();
  484. $user_is_anonymous = false;
  485. $current_user_status_label = $row['7'];
  486. if ($current_user_status_label == $statusname[ANONYMOUS]) {
  487. $user_is_anonymous = true;
  488. }
  489. $result = '';
  490. if (!$user_is_anonymous) {
  491. $icon = Display::return_icon(
  492. 'course.png',
  493. get_lang('Courses'),
  494. ['onmouseout' => 'clear_course_list (\'div_'.$user_id.'\')']
  495. );
  496. $result .= '<a href="javascript:void(0)" onclick="load_course_list(\'div_'.$user_id.'\','.$user_id.')" >
  497. '.$icon.'
  498. <div class="blackboard_hide" id="div_'.$user_id.'">&nbsp;&nbsp;</div>
  499. </a>';
  500. $icon = Display::return_icon(
  501. 'session.png',
  502. get_lang('Sessions'),
  503. ['onmouseout' => 'clear_session_list (\'div_s_'.$user_id.'\')']
  504. );
  505. $result .= '<a href="javascript:void(0)" onclick="load_session_list(\'div_s_'.$user_id.'\','.$user_id.')" >
  506. '.$icon.'
  507. <div class="blackboard_hide" id="div_s_'.$user_id.'">&nbsp;&nbsp;</div>
  508. </a>';
  509. } else {
  510. $result .= Display::return_icon('course_na.png', get_lang('Courses')).'&nbsp;&nbsp;';
  511. $result .= Display::return_icon('course_na.png', get_lang('Sessions')).'&nbsp;&nbsp;';
  512. }
  513. if (api_is_platform_admin()) {
  514. if (!$user_is_anonymous) {
  515. $result .= '<a href="user_information.php?user_id='.$user_id.'">'.
  516. Display::return_icon('info2.png', get_lang('Info')).'</a>&nbsp;&nbsp;';
  517. } else {
  518. $result .= Display::return_icon('info2_na.png', get_lang('Info')).'&nbsp;&nbsp;';
  519. }
  520. }
  521. //only allow platform admins to login_as, or session admins only for students (not teachers nor other admins)
  522. $loginAsStatusForSessionAdmins = [$statusname[STUDENT]];
  523. //except when allow_session_admin_login_as_teacher is enabled, then can login_as teachers also
  524. if (api_get_configuration_value('allow_session_admin_login_as_teacher')) {
  525. $loginAsStatusForSessionAdmins[] = $statusname[COURSEMANAGER];
  526. }
  527. $sessionAdminCanLoginAs = api_is_session_admin() &&
  528. in_array($current_user_status_label, $loginAsStatusForSessionAdmins);
  529. if (api_is_platform_admin() || $sessionAdminCanLoginAs) {
  530. if (!$user_is_anonymous) {
  531. if (api_global_admin_can_edit_admin($user_id, null, $sessionAdminCanLoginAs)) {
  532. $result .= '<a href="user_list.php?action=login_as&user_id='.$user_id.'&sec_token='.Security::getTokenFromSession().'">'.
  533. Display::return_icon('login_as.png', get_lang('LoginAs')).'</a>&nbsp;';
  534. } else {
  535. $result .= Display::return_icon('login_as_na.png', get_lang('LoginAs')).'&nbsp;';
  536. }
  537. } else {
  538. $result .= Display::return_icon('login_as_na.png', get_lang('LoginAs')).'&nbsp;';
  539. }
  540. } else {
  541. $result .= Display::return_icon('login_as_na.png', get_lang('LoginAs')).'&nbsp;';
  542. }
  543. if ($current_user_status_label != $statusname[STUDENT]) {
  544. $result .= Display::return_icon(
  545. 'statistics_na.png',
  546. get_lang('Reporting')
  547. ).'&nbsp;';
  548. } else {
  549. $result .= '<a href="../mySpace/myStudents.php?student='.$user_id.'">'.
  550. Display::return_icon('statistics.png', get_lang('Reporting')).
  551. '</a>&nbsp;';
  552. }
  553. if (api_is_platform_admin(true)) {
  554. $editProfileUrl = Display::getProfileEditionLink($user_id, true);
  555. if (!$user_is_anonymous &&
  556. api_global_admin_can_edit_admin($user_id, null, true)
  557. ) {
  558. $result .= '<a href="'.$editProfileUrl.'">'.
  559. Display::return_icon(
  560. 'edit.png',
  561. get_lang('Edit'),
  562. [],
  563. ICON_SIZE_SMALL
  564. ).
  565. '</a>&nbsp;';
  566. } else {
  567. $result .= Display::return_icon(
  568. 'edit_na.png',
  569. get_lang('Edit'),
  570. [],
  571. ICON_SIZE_SMALL
  572. ).'</a>&nbsp;';
  573. }
  574. }
  575. $allowAssignSkill = api_is_platform_admin(false, true);
  576. if ($allowAssignSkill) {
  577. $result .= Display::url(
  578. Display::return_icon(
  579. 'skill-badges.png',
  580. get_lang('AssignSkill'),
  581. null,
  582. ICON_SIZE_SMALL
  583. ),
  584. api_get_path(WEB_CODE_PATH).'badge/assign.php?'.http_build_query(['user' => $user_id])
  585. );
  586. }
  587. if ($is_admin) {
  588. $result .= Display::return_icon(
  589. 'admin_star.png',
  590. get_lang('IsAdministrator'),
  591. ['width' => ICON_SIZE_SMALL, 'heigth' => ICON_SIZE_SMALL]
  592. );
  593. } else {
  594. $result .= Display::return_icon(
  595. 'admin_star_na.png',
  596. get_lang('IsNotAdministrator')
  597. );
  598. }
  599. // actions for assigning sessions, courses or users
  600. if (!api_is_session_admin()) {
  601. if ($current_user_status_label == $statusname[SESSIONADMIN]) {
  602. $result .= Display::url(
  603. Display::return_icon(
  604. 'view_more_stats.gif',
  605. get_lang('AssignSessions')
  606. ),
  607. "dashboard_add_sessions_to_user.php?user={$user_id}"
  608. );
  609. } else {
  610. if ($current_user_status_label == $statusname[DRH] ||
  611. UserManager::is_admin($user_id) ||
  612. $current_user_status_label == $statusname[STUDENT_BOSS]
  613. ) {
  614. $result .= Display::url(
  615. Display::return_icon(
  616. 'user_subscribe_course.png',
  617. get_lang('AssignUsers'),
  618. '',
  619. ICON_SIZE_SMALL
  620. ),
  621. "dashboard_add_users_to_user.php?user={$user_id}"
  622. );
  623. }
  624. if ($current_user_status_label == $statusname[DRH] || UserManager::is_admin($user_id)) {
  625. $result .= Display::url(
  626. Display::return_icon(
  627. 'course_add.gif',
  628. get_lang('AssignCourses')
  629. ),
  630. "dashboard_add_courses_to_user.php?user={$user_id}"
  631. );
  632. $result .= Display::url(
  633. Display::return_icon(
  634. 'view_more_stats.gif',
  635. get_lang('AssignSessions')
  636. ),
  637. "dashboard_add_sessions_to_user.php?user={$user_id}"
  638. );
  639. }
  640. }
  641. }
  642. $allowDelete = api_get_configuration_value('allow_delete_user_for_session_admin');
  643. if (api_is_session_admin() && $allowDelete) {
  644. if ($user_id != api_get_user_id() &&
  645. !$user_is_anonymous &&
  646. api_global_admin_can_edit_admin($user_id, null, true)
  647. ) {
  648. // 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.
  649. $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;">'.
  650. Display::return_icon(
  651. 'delete.png',
  652. get_lang('Delete'),
  653. [],
  654. ICON_SIZE_SMALL
  655. ).
  656. '</a>';
  657. }
  658. }
  659. if (api_is_platform_admin()) {
  660. $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">'.
  661. Display::return_icon(
  662. 'calendar.png',
  663. get_lang('FreeBusyCalendar'),
  664. [],
  665. ICON_SIZE_SMALL
  666. ).
  667. '</a>';
  668. if ($user_id != api_get_user_id() &&
  669. !$user_is_anonymous &&
  670. api_global_admin_can_edit_admin($user_id)
  671. ) {
  672. $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;">'.
  673. Display::return_icon(
  674. 'anonymous.png',
  675. get_lang('Anonymize'),
  676. [],
  677. ICON_SIZE_SMALL
  678. ).
  679. '</a>';
  680. }
  681. $deleteAllowed = !api_get_configuration_value('deny_delete_users');
  682. if ($deleteAllowed) {
  683. if ($user_id != api_get_user_id() &&
  684. !$user_is_anonymous &&
  685. api_global_admin_can_edit_admin($user_id)
  686. ) {
  687. // you cannot lock yourself out otherwise you could disable all the accounts
  688. // including your own => everybody is locked out and nobody can change it anymore.
  689. $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;">'.
  690. Display::return_icon(
  691. 'delete.png',
  692. get_lang('Delete'),
  693. [],
  694. ICON_SIZE_SMALL
  695. ).
  696. '</a>';
  697. } else {
  698. $result .= Display::return_icon(
  699. 'delete_na.png',
  700. get_lang('Delete'),
  701. [],
  702. ICON_SIZE_SMALL
  703. );
  704. }
  705. }
  706. }
  707. return $result;
  708. }
  709. /**
  710. * Build the active-column of the table to lock or unlock a certain user
  711. * lock = the user can no longer use this account.
  712. *
  713. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  714. *
  715. * @param int $active the current state of the account
  716. * @param string $params
  717. * @param array $row
  718. *
  719. * @return string Some HTML-code with the lock/unlock button
  720. */
  721. function active_filter($active, $params, $row)
  722. {
  723. $_user = api_get_user_info();
  724. if ($active == '1') {
  725. $action = 'Lock';
  726. $image = 'accept';
  727. } elseif ($active == '-1') {
  728. $action = 'edit';
  729. $image = 'warning';
  730. } elseif ($active == '0') {
  731. $action = 'Unlock';
  732. $image = 'error';
  733. }
  734. $result = '';
  735. if ($action === 'edit') {
  736. $result = Display::return_icon(
  737. $image.'.png',
  738. get_lang('AccountExpired'),
  739. [],
  740. 16
  741. );
  742. } elseif ($row['0'] != $_user['user_id']) {
  743. // you cannot lock yourself out otherwise you could disable all the
  744. // accounts including your own => everybody is locked out and nobody
  745. // can change it anymore.
  746. $result = Display::return_icon(
  747. $image.'.png',
  748. get_lang(ucfirst($action)),
  749. ['onclick' => 'active_user(this);', 'id' => 'img_'.$row['0']],
  750. 16
  751. );
  752. }
  753. return $result;
  754. }
  755. /**
  756. * Instead of displaying the integer of the status, we give a translation for the status.
  757. *
  758. * @param int $status
  759. *
  760. * @return string translation
  761. *
  762. * @version march 2008
  763. *
  764. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  765. */
  766. function status_filter($status)
  767. {
  768. $statusname = api_get_status_langvars();
  769. return $statusname[$status];
  770. }
  771. if (isset($_GET['keyword']) || isset($_GET['keyword_firstname'])) {
  772. $interbreadcrumb[] = ["url" => 'index.php', "name" => get_lang('PlatformAdmin')];
  773. $interbreadcrumb[] = ["url" => 'user_list.php', "name" => get_lang('UserList')];
  774. $tool_name = get_lang('SearchUsers');
  775. } else {
  776. $interbreadcrumb[] = ["url" => 'index.php', "name" => get_lang('PlatformAdmin')];
  777. $tool_name = get_lang('UserList');
  778. }
  779. $message = '';
  780. if (!empty($action)) {
  781. $check = Security::check_token('get');
  782. if ($check) {
  783. switch ($action) {
  784. case 'add_user_to_my_url':
  785. $user_id = $_REQUEST['user_id'];
  786. $result = UrlManager::add_user_to_url($user_id, $urlId);
  787. if ($result) {
  788. $user_info = api_get_user_info($user_id);
  789. $message = get_lang('UserAdded').' '.$user_info['complete_name_with_username'];
  790. $message = Display::return_message($message, 'confirmation');
  791. }
  792. break;
  793. case 'delete_user':
  794. $message = UserManager::deleteUserWithVerification($_GET['user_id']);
  795. Display::addFlash($message);
  796. header('Location: '.api_get_self());
  797. exit;
  798. break;
  799. case 'delete':
  800. if (api_is_platform_admin() && !empty($_POST['id'])) {
  801. $number_of_selected_users = count($_POST['id']);
  802. $number_of_affected_users = 0;
  803. if (is_array($_POST['id'])) {
  804. foreach ($_POST['id'] as $index => $user_id) {
  805. if ($user_id != $_user['user_id']) {
  806. if (UserManager::delete_user($user_id)) {
  807. $number_of_affected_users++;
  808. }
  809. }
  810. }
  811. }
  812. if ($number_of_selected_users == $number_of_affected_users) {
  813. $message = Display::return_message(
  814. get_lang('SelectedUsersDeleted'),
  815. 'confirmation'
  816. );
  817. } else {
  818. $message = Display::return_message(
  819. get_lang('SomeUsersNotDeleted'),
  820. 'error'
  821. );
  822. }
  823. }
  824. break;
  825. case 'disable':
  826. if (api_is_platform_admin()) {
  827. $number_of_selected_users = count($_POST['id']);
  828. $number_of_affected_users = 0;
  829. if (is_array($_POST['id'])) {
  830. foreach ($_POST['id'] as $index => $user_id) {
  831. if ($user_id != $_user['user_id']) {
  832. if (UserManager::disable($user_id)) {
  833. $number_of_affected_users++;
  834. }
  835. }
  836. }
  837. }
  838. if ($number_of_selected_users == $number_of_affected_users) {
  839. $message = Display::return_message(
  840. get_lang('SelectedUsersDisabled'),
  841. 'confirmation'
  842. );
  843. } else {
  844. $message = Display::return_message(
  845. get_lang('SomeUsersNotDisabled'),
  846. 'error'
  847. );
  848. }
  849. }
  850. break;
  851. case 'enable':
  852. if (api_is_platform_admin()) {
  853. $number_of_selected_users = count($_POST['id']);
  854. $number_of_affected_users = 0;
  855. if (is_array($_POST['id'])) {
  856. foreach ($_POST['id'] as $index => $user_id) {
  857. if ($user_id != $_user['user_id']) {
  858. if (UserManager::enable($user_id)) {
  859. $number_of_affected_users++;
  860. }
  861. }
  862. }
  863. }
  864. if ($number_of_selected_users == $number_of_affected_users) {
  865. $message = Display::return_message(
  866. get_lang('SelectedUsersEnabled'),
  867. 'confirmation'
  868. );
  869. } else {
  870. $message = Display::return_message(
  871. get_lang('SomeUsersNotEnabled'),
  872. 'error'
  873. );
  874. }
  875. }
  876. break;
  877. case 'anonymize':
  878. $message = UserManager::anonymizeUserWithVerification($_GET['user_id']);
  879. Display::addFlash($message);
  880. header('Location: '.api_get_self());
  881. exit;
  882. break;
  883. }
  884. Security::clear_token();
  885. }
  886. }
  887. // Create a search-box
  888. $form = new FormValidator('search_simple', 'get', null, null, null, 'inline');
  889. $form->addText(
  890. 'keyword',
  891. get_lang('Search'),
  892. false,
  893. [
  894. 'aria-label' => get_lang("SearchUsers"),
  895. ]
  896. );
  897. $form->addButtonSearch(get_lang('Search'));
  898. $searchAdvanced = '
  899. <a id="advanced_params" href="javascript://"
  900. class="btn btn-default advanced_options" onclick="display_advanced_search_form();">
  901. <span id="img_plus_and_minus">&nbsp;
  902. '.Display::returnFontAwesomeIcon('arrow-right').' '.get_lang('AdvancedSearch').'
  903. </span>
  904. </a>';
  905. $actionsLeft = '';
  906. $actionsCenter = '';
  907. $actionsRight = '';
  908. if (api_is_platform_admin()) {
  909. $actionsRight .= '<a class="pull-right" href="'.api_get_path(WEB_CODE_PATH).'admin/user_add.php">'.
  910. Display::return_icon('new_user.png', get_lang('AddUsers'), '', ICON_SIZE_MEDIUM).'</a>';
  911. }
  912. $actionsLeft .= $form->returnForm();
  913. $actionsCenter .= $searchAdvanced;
  914. if (isset($_GET['keyword'])) {
  915. $parameters = ['keyword' => Security::remove_XSS($_GET['keyword'])];
  916. } elseif (isset($_GET['keyword_firstname'])) {
  917. $parameters['keyword_firstname'] = Security::remove_XSS($_GET['keyword_firstname']);
  918. $parameters['keyword_lastname'] = Security::remove_XSS($_GET['keyword_lastname']);
  919. $parameters['keyword_username'] = Security::remove_XSS($_GET['keyword_username']);
  920. $parameters['keyword_email'] = Security::remove_XSS($_GET['keyword_email']);
  921. $parameters['keyword_officialcode'] = Security::remove_XSS($_GET['keyword_officialcode']);
  922. $parameters['keyword_status'] = Security::remove_XSS($_GET['keyword_status']);
  923. $parameters['keyword_active'] = Security::remove_XSS($_GET['keyword_active']);
  924. $parameters['keyword_inactive'] = Security::remove_XSS($_GET['keyword_inactive']);
  925. }
  926. // Create a sortable table with user-data
  927. $parameters['sec_token'] = Security::get_token();
  928. $_admins_list = array_keys(UserManager::get_all_administrators());
  929. Session::write('admin_list', $_admins_list);
  930. // Display Advanced search form.
  931. $form = new FormValidator(
  932. 'advanced_search',
  933. 'get',
  934. '',
  935. '',
  936. [],
  937. FormValidator::LAYOUT_HORIZONTAL
  938. );
  939. $form->addElement('html', '<div id="advanced_search_form" style="display:none;">');
  940. $form->addElement('header', get_lang('AdvancedSearch'));
  941. $form->addText('keyword_firstname', get_lang('FirstName'), false);
  942. $form->addText('keyword_lastname', get_lang('LastName'), false);
  943. $form->addText('keyword_username', get_lang('LoginName'), false);
  944. $form->addText('keyword_email', get_lang('Email'), false);
  945. $form->addText('keyword_officialcode', get_lang('OfficialCode'), false);
  946. $status_options = [];
  947. $status_options['%'] = get_lang('All');
  948. $status_options[STUDENT] = get_lang('Student');
  949. $status_options[COURSEMANAGER] = get_lang('Teacher');
  950. $status_options[DRH] = get_lang('Drh');
  951. $status_options[SESSIONADMIN] = get_lang('SessionsAdmin');
  952. $status_options[PLATFORM_ADMIN] = get_lang('Administrator');
  953. $form->addElement(
  954. 'select',
  955. 'keyword_status',
  956. get_lang('Profile'),
  957. $status_options
  958. );
  959. $active_group = [];
  960. $active_group[] = $form->createElement('checkbox', 'keyword_active', '', get_lang('Active'));
  961. $active_group[] = $form->createElement('checkbox', 'keyword_inactive', '', get_lang('Inactive'));
  962. $form->addGroup($active_group, '', get_lang('ActiveAccount'), null, false);
  963. $form->addElement('checkbox', 'check_easy_passwords', null, get_lang('CheckEasyPasswords'));
  964. $data = $extraField->addElements($form, 0, [], true, false, $variablesToShow);
  965. $htmlHeadXtra[] = '
  966. <script>
  967. $(document).ready(function() {
  968. '.$data['jquery_ready_content'].'
  969. })
  970. </script>
  971. ';
  972. $form->addButtonSearch(get_lang('SearchUsers'));
  973. $defaults = [];
  974. $defaults['keyword_active'] = 1;
  975. $defaults['keyword_inactive'] = 1;
  976. $form->setDefaults($defaults);
  977. $form->addElement('html', '</div>');
  978. $form = $form->returnForm();
  979. $table = new SortableTable(
  980. 'users',
  981. 'get_number_of_users',
  982. 'get_user_data',
  983. (api_is_western_name_order() xor api_sort_by_first_name()) ? 3 : 2
  984. );
  985. $table->set_additional_parameters($parameters);
  986. $table->set_header(0, '', false, 'width="18px"');
  987. $table->set_header(1, get_lang('Photo'), false);
  988. $table->set_header(2, get_lang('OfficialCode'));
  989. if (api_is_western_name_order()) {
  990. $table->set_header(3, get_lang('FirstName'));
  991. $table->set_header(4, get_lang('LastName'));
  992. } else {
  993. $table->set_header(3, get_lang('LastName'));
  994. $table->set_header(4, get_lang('FirstName'));
  995. }
  996. $table->set_header(5, get_lang('LoginName'));
  997. $table->set_header(6, get_lang('Email'));
  998. $table->set_header(7, get_lang('Profile'));
  999. $table->set_header(8, get_lang('Active'), true, 'width="15px"');
  1000. $table->set_header(9, get_lang('RegistrationDate'), true, 'width="90px"');
  1001. $table->set_header(10, get_lang('Action'), false, 'width="220px"');
  1002. $table->set_column_filter(3, 'user_filter');
  1003. $table->set_column_filter(4, 'user_filter');
  1004. $table->set_column_filter(6, 'email_filter');
  1005. $table->set_column_filter(7, 'status_filter');
  1006. $table->set_column_filter(8, 'active_filter');
  1007. $table->set_column_filter(10, 'modify_filter');
  1008. // Hide email column if login is email, to avoid column with same data
  1009. if (api_get_setting('login_is_email') === 'true') {
  1010. $table->setHideColumn(6);
  1011. }
  1012. // Only show empty actions bar if delete users has been blocked
  1013. $actionsList = [];
  1014. if (api_is_platform_admin() &&
  1015. !api_get_configuration_value('deny_delete_users')
  1016. ) {
  1017. $actionsList['delete'] = get_lang('DeleteFromPlatform');
  1018. }
  1019. $actionsList['disable'] = get_lang('Disable');
  1020. $actionsList['enable'] = get_lang('Enable');
  1021. $table->set_form_actions($actionsList);
  1022. $table_result = $table->return_table();
  1023. $extra_search_options = '';
  1024. //Try to search the user everywhere
  1025. if ($table->get_total_number_of_items() == 0) {
  1026. if (api_get_multiple_access_url() && isset($_REQUEST['keyword'])) {
  1027. $keyword = Database::escape_string($_REQUEST['keyword']);
  1028. $conditions = ['username' => $keyword];
  1029. $user_list = UserManager::get_user_list(
  1030. $conditions,
  1031. [],
  1032. false,
  1033. ' OR '
  1034. );
  1035. if (!empty($user_list)) {
  1036. $extra_search_options = Display::page_subheader(get_lang('UsersFoundInOtherPortals'));
  1037. $table = new HTML_Table(['class' => 'data_table']);
  1038. $column = 0;
  1039. $row = 0;
  1040. $headers = [get_lang('User'), 'URL', get_lang('Actions')];
  1041. foreach ($headers as $header) {
  1042. $table->setHeaderContents($row, $column, $header);
  1043. $column++;
  1044. }
  1045. $row++;
  1046. foreach ($user_list as $user) {
  1047. $column = 0;
  1048. $access_info = UrlManager::get_access_url_from_user($user['id']);
  1049. $access_info_to_string = '';
  1050. $add_user = true;
  1051. if (!empty($access_info)) {
  1052. foreach ($access_info as $url_info) {
  1053. if ($urlId == $url_info['access_url_id']) {
  1054. $add_user = false;
  1055. }
  1056. $access_info_to_string .= $url_info['url'].'<br />';
  1057. }
  1058. }
  1059. if ($add_user) {
  1060. $row_table = [];
  1061. $row_table[] = api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].') ';
  1062. $row_table[] = $access_info_to_string;
  1063. $url = api_get_self().'?action=add_user_to_my_url&user_id='.$user['id'].'&sec_token='.Security::getTokenFromSession();
  1064. $row_table[] = Display::url(
  1065. get_lang('AddUserToMyURL'),
  1066. $url,
  1067. ['class' => 'btn']
  1068. );
  1069. foreach ($row_table as $cell) {
  1070. $table->setCellContents($row, $column, $cell);
  1071. $table->updateCellAttributes(
  1072. $row,
  1073. $column,
  1074. 'align="center"'
  1075. );
  1076. $column++;
  1077. }
  1078. $table->updateRowAttributes(
  1079. $row,
  1080. $row % 2 ? 'class="row_even"' : 'class="row_odd"',
  1081. true
  1082. );
  1083. $row++;
  1084. }
  1085. }
  1086. $extra_search_options .= $table->toHtml();
  1087. $table_result = '';
  1088. }
  1089. }
  1090. }
  1091. $toolbarActions = Display::toolbarAction(
  1092. 'toolbarUser',
  1093. [$actionsLeft, $actionsCenter, $actionsRight],
  1094. [4, 4, 4]
  1095. );
  1096. $tpl = new Template($tool_name);
  1097. $tpl->assign('actions', $toolbarActions);
  1098. $tpl->assign('message', $message);
  1099. $tpl->assign('content', $form.$table_result.$extra_search_options);
  1100. $tpl->display_one_col_template();