user_list.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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('Attempting to login as %s %s (id %s)'),
  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('Login successful. Go to %s'), Display::url($url, $url));
  31. Display::display_header(get_lang('User list'));
  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('Action not allowed').'");
  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('Advanced search').'\');
  139. } else {
  140. $("#advanced_search_form").css("display","none");
  141. $("#img_plus_and_minus").html(\'&nbsp;'.Display::returnFontAwesomeIcon('arrow-right').' '.get_lang('Advanced search').'\');
  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('Course 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('Course 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('Informationrmation')).'</a>&nbsp;&nbsp;';
  519. } else {
  520. $result .= Display::return_icon('info2_na.png', get_lang('Informationrmation')).'&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 session.allow_session_admin_login_as_teacher is enabled, then can login_as teachers also
  526. if (api_get_configuration_value('session.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('Login as')).'</a>&nbsp;';
  536. } else {
  537. $result .= Display::return_icon('login_as_na.png', get_lang('Login as')).'&nbsp;';
  538. }
  539. } else {
  540. $result .= Display::return_icon('login_as_na.png', get_lang('Login as')).'&nbsp;';
  541. }
  542. } else {
  543. $result .= Display::return_icon('login_as_na.png', get_lang('Login as')).'&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('Assign skill'),
  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('Is administrator'),
  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('Is not administrator')
  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('AssignCourse sessions')
  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('Assign users'),
  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. 'add.png',
  630. get_lang('Assign courses')
  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('AssignCourse sessions')
  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().'" title="'.addslashes(api_htmlentities(get_lang('Please confirm your choice'))).'" class="delete-swal">'.
  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('Free/Busy calendar').'" 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('Free/Busy calendar'),
  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().'" class="delete-swal" title="'.addslashes(api_htmlentities(get_lang("Please confirm your choice"))).'" >'.
  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().'" title="'.addslashes(api_htmlentities(get_lang("Please confirm your choice"))).'" class="delete-swal">'.
  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('Account expired'),
  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('Administration')];
  775. $interbreadcrumb[] = ['url' => 'user_list.php', 'name' => get_lang('User list')];
  776. $tool_name = get_lang('Search users');
  777. } else {
  778. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
  779. $tool_name = get_lang('User list');
  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('The user has been added').' '.$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('Selected users deleted'),
  817. 'confirmation'
  818. );
  819. } else {
  820. $message = Display::return_message(
  821. get_lang('Some of the selected users have not been deleted. We recommend you confirm which, by using the advanced search.'),
  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('The selected users have all been disabled'),
  843. 'confirmation'
  844. );
  845. } else {
  846. $message = Display::return_message(
  847. get_lang('Some of the selected users have not been disabled. We recommend you confirm which, by using the advanced search.'),
  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('The selected users were all enabled.'),
  869. 'confirmation'
  870. );
  871. } else {
  872. $message = Display::return_message(
  873. get_lang('Some of the selected users have not been enabled. We recommend you confirm which, by using the advanced search.'),
  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('Search users'),
  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('Advanced search').'
  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('Add a user'), '', 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('Advanced search'));
  943. $form->addText('keyword_firstname', get_lang('First name'), false);
  944. $form->addText('keyword_lastname', get_lang('Last name'), false);
  945. $form->addText('keyword_username', get_lang('Login'), false);
  946. $form->addText('keyword_email', get_lang('e-mail'), false);
  947. $form->addText('keyword_officialcode', get_lang('Code'), false);
  948. $status_options = [];
  949. $status_options['%'] = get_lang('All');
  950. $status_options[STUDENT] = get_lang('Learner');
  951. $status_options[COURSEMANAGER] = get_lang('Trainer');
  952. $status_options[DRH] = get_lang('Human Resources Manager');
  953. $status_options[SESSIONADMIN] = get_lang('Course 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('Check passwords too easy to guess'));
  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('Search users'));
  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('Code'));
  991. if (api_is_western_name_order()) {
  992. $table->set_header(3, get_lang('First name'));
  993. $table->set_header(4, get_lang('Last name'));
  994. } else {
  995. $table->set_header(3, get_lang('Last name'));
  996. $table->set_header(4, get_lang('First name'));
  997. }
  998. $table->set_header(5, get_lang('Login'));
  999. $table->set_header(6, get_lang('e-mail'));
  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('Registration date'), true, 'width="90px"');
  1003. $table->set_header(10, get_lang('Latest login'), 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('Remove from portal');
  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('Users found in other portals'));
  1040. $table = new HTML_Table(['class' => 'data_table']);
  1041. $column = 0;
  1042. $row = 0;
  1043. $headers = [get_lang('User'), 'URL', get_lang('Detail')];
  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('Add user to my portal'),
  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();