user_list.php 43 KB

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