user.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script displays a list of the users of the current course.
  5. * Course admins can change user permissions, subscribe and unsubscribe users...
  6. *
  7. * show users registered in courses
  8. *
  9. * @author Roan Embrechts
  10. * @author Julio Montoya, Several fixes
  11. *
  12. * @package chamilo.user
  13. */
  14. $use_anonymous = true;
  15. require_once __DIR__.'/../inc/global.inc.php';
  16. $current_course_tool = TOOL_USER;
  17. $this_section = SECTION_COURSES;
  18. // notice for unauthorized people.
  19. api_protect_course_script(true);
  20. if (!api_is_platform_admin(true)) {
  21. if (!api_is_course_admin() && !api_is_coach()) {
  22. if (api_get_course_setting('allow_user_view_user_list') == 0) {
  23. api_not_allowed(true);
  24. }
  25. }
  26. }
  27. /* Constants and variables */
  28. $course_code = api_get_course_id();
  29. $sessionId = api_get_session_id();
  30. $is_western_name_order = api_is_western_name_order();
  31. $sort_by_first_name = api_sort_by_first_name();
  32. $course_info = api_get_course_info();
  33. $user_id = api_get_user_id();
  34. $_user = api_get_user_info();
  35. $courseCode = $course_info['code'];
  36. $courseId = $course_info['real_id'];
  37. $type = isset($_REQUEST['type']) ? intval($_REQUEST['type']) : STUDENT;
  38. $canEditUsers = api_get_setting('allow_user_course_subscription_by_course_admin') == 'true' || api_is_platform_admin();
  39. //Can't auto unregister from a session
  40. if (!empty($sessionId)) {
  41. $course_info['unsubscribe'] = 0;
  42. }
  43. /* Un registering a user section */
  44. if (api_is_allowed_to_edit(null, true)) {
  45. if (isset($_POST['action'])) {
  46. switch ($_POST['action']) {
  47. case 'unsubscribe':
  48. // Make sure we don't unsubscribe current user from the course
  49. if (is_array($_POST['user'])) {
  50. $user_ids = array_diff($_POST['user'], [$user_id]);
  51. if (count($user_ids) > 0) {
  52. CourseManager::unsubscribe_user($user_ids, $courseCode);
  53. Display::addFlash(
  54. Display::return_message(get_lang('UsersUnsubscribed'))
  55. );
  56. }
  57. }
  58. }
  59. }
  60. }
  61. // Getting extra fields that have the filter option "on"
  62. $extraField = new ExtraField('user');
  63. $extraFields = $extraField->get_all(['filter = ?' => 1]);
  64. $user_image_pdf_size = 80;
  65. $canEdit = api_is_allowed_to_edit(null, true);
  66. $canRead = api_is_allowed_to_edit(null, true) || api_is_coach();
  67. if (isset($_GET['action'])) {
  68. switch ($_GET['action']) {
  69. case 'set_tutor':
  70. if (!$canEdit) {
  71. api_not_allowed();
  72. }
  73. $userId = isset($_GET['user_id']) ? (int) $_GET['user_id'] : null;
  74. $isTutor = isset($_GET['is_tutor']) ? (int) $_GET['is_tutor'] : 0;
  75. $userInfo = api_get_user_info($userId);
  76. if (!empty($userId)) {
  77. if (!$sessionId) {
  78. if ($userInfo['status'] != INVITEE) {
  79. CourseManager::updateUserCourseTutor(
  80. $userId,
  81. $courseId,
  82. $isTutor
  83. );
  84. Display::addFlash(
  85. Display::return_message(get_lang('Updated'))
  86. );
  87. } else {
  88. Display::addFlash(
  89. Display::return_message(
  90. get_lang('InviteesCantBeTutors'),
  91. 'error'
  92. )
  93. );
  94. }
  95. }
  96. }
  97. break;
  98. case 'export':
  99. if (!$canRead) {
  100. api_not_allowed();
  101. }
  102. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  103. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  104. $is_western_name_order = api_is_western_name_order();
  105. $data = [];
  106. $a_users = [];
  107. $current_access_url_id = api_get_current_access_url_id();
  108. $extra_fields = UserManager::get_extra_user_data(
  109. api_get_user_id(),
  110. false,
  111. false,
  112. false,
  113. true
  114. );
  115. $extra_fields = array_keys($extra_fields);
  116. $select_email_condition = '';
  117. if (api_get_setting('show_email_addresses') === 'true') {
  118. $select_email_condition = ' user.email, ';
  119. if ($sort_by_first_name) {
  120. $a_users[0] = [
  121. 'id',
  122. get_lang('FirstName'),
  123. get_lang('LastName'),
  124. get_lang('Username'),
  125. get_lang('Email'),
  126. get_lang('Phone'),
  127. get_lang('OfficialCode'),
  128. get_lang('Active'),
  129. ];
  130. } else {
  131. $a_users[0] = [
  132. 'id',
  133. get_lang('LastName'),
  134. get_lang('FirstName'),
  135. get_lang('Username'),
  136. get_lang('Email'),
  137. get_lang('Phone'),
  138. get_lang('OfficialCode'),
  139. get_lang('Active'),
  140. ];
  141. }
  142. } else {
  143. if ($sort_by_first_name) {
  144. $a_users[0] = [
  145. 'id',
  146. get_lang('FirstName'),
  147. get_lang('LastName'),
  148. get_lang('Username'),
  149. get_lang('Phone'),
  150. get_lang('OfficialCode'),
  151. get_lang('Active'),
  152. ];
  153. } else {
  154. $a_users[0] = [
  155. 'id',
  156. get_lang('LastName'),
  157. get_lang('FirstName'),
  158. get_lang('Username'),
  159. get_lang('Phone'),
  160. get_lang('OfficialCode'),
  161. get_lang('Active'),
  162. ];
  163. }
  164. }
  165. $legal = '';
  166. if (isset($course_info['activate_legal']) && $course_info['activate_legal'] == 1) {
  167. $legal = ', legal_agreement';
  168. $a_users[0][] = get_lang('LegalAgreementAccepted');
  169. }
  170. if ($_GET['format'] === 'pdf') {
  171. $select_email_condition = ' user.email, ';
  172. if ($is_western_name_order) {
  173. $a_users[0] = [
  174. '#',
  175. get_lang('UserPicture'),
  176. get_lang('OfficialCode'),
  177. get_lang('FirstName').', '.get_lang('LastName'),
  178. get_lang('Email'),
  179. get_lang('Phone'),
  180. ];
  181. } else {
  182. $a_users[0] = [
  183. '#',
  184. get_lang('UserPicture'),
  185. get_lang('OfficialCode'),
  186. get_lang('LastName').', '.get_lang('FirstName'),
  187. get_lang('Email'),
  188. get_lang('Phone'),
  189. ];
  190. }
  191. }
  192. $a_users[0] = array_merge($a_users[0], $extra_fields);
  193. // users subscribed to the course through a session.
  194. if (api_get_session_id()) {
  195. $table_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  196. $sql = "SELECT DISTINCT
  197. user.user_id, ".($is_western_name_order ? "user.firstname, user.lastname" : "user.lastname, user.firstname").",
  198. user.username,
  199. $select_email_condition
  200. phone,
  201. user.official_code,
  202. active
  203. $legal
  204. FROM $table_session_course_user as session_course_user,
  205. $table_users as user ";
  206. if (api_is_multiple_url_enabled()) {
  207. $sql .= ' , '.Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER).' au ';
  208. }
  209. $sql .= "
  210. WHERE c_id = $courseId
  211. AND session_course_user.user_id = user.user_id
  212. AND session_id = $sessionId
  213. ";
  214. if (api_is_multiple_url_enabled()) {
  215. $sql .= " AND user.user_id = au.user_id AND access_url_id = $current_access_url_id ";
  216. }
  217. // only users no coaches/teachers
  218. if ($type == COURSEMANAGER) {
  219. $sql .= " AND session_course_user.status = 2 ";
  220. } else {
  221. $sql .= " AND session_course_user.status = 0 ";
  222. }
  223. $sql .= $sort_by_first_name ? ' ORDER BY user.firstname, user.lastname' : ' ORDER BY user.lastname, user.firstname';
  224. $rs = Database::query($sql);
  225. $counter = 1;
  226. while ($user = Database:: fetch_array($rs, 'ASSOC')) {
  227. if (isset($user['legal_agreement'])) {
  228. if ($user['legal_agreement'] == 1) {
  229. $user['legal_agreement'] = get_lang('Yes');
  230. } else {
  231. $user['legal_agreement'] = get_lang('No');
  232. }
  233. }
  234. $extra_fields = UserManager::get_extra_user_data(
  235. $user['user_id'],
  236. false,
  237. false,
  238. false,
  239. true
  240. );
  241. if (!empty($extra_fields)) {
  242. foreach ($extra_fields as $key => $extra_value) {
  243. $user[$key] = $extra_value;
  244. }
  245. }
  246. $data[] = $user;
  247. if ($_GET['format'] === 'pdf') {
  248. $user_info = api_get_user_info($user['user_id']);
  249. $user_image = '<img src="'.$user_info['avatar'].'" width ="'.$user_image_pdf_size.'px" />';
  250. if ($is_western_name_order) {
  251. $user_pdf = [
  252. $counter,
  253. $user_image,
  254. $user['official_code'],
  255. $user['firstname'].', '.$user['lastname'],
  256. $user['email'],
  257. $user['phone'],
  258. ];
  259. } else {
  260. $user_pdf = [
  261. $counter,
  262. $user_image,
  263. $user['official_code'],
  264. $user['lastname'].', '.$user['firstname'],
  265. $user['email'],
  266. $user['phone'],
  267. ];
  268. }
  269. $a_users[] = $user_pdf;
  270. } else {
  271. $a_users[] = $user;
  272. }
  273. $counter++;
  274. }
  275. }
  276. if ($sessionId == 0) {
  277. // users directly subscribed to the course
  278. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  279. $sql = "SELECT DISTINCT
  280. user.user_id, ".($is_western_name_order ? "user.firstname, user.lastname" : "user.lastname, user.firstname").",
  281. user.username,
  282. $select_email_condition
  283. phone,
  284. user.official_code,
  285. active $legal
  286. FROM $table_course_user as course_user, $table_users as user ";
  287. if (api_is_multiple_url_enabled()) {
  288. $sql .= ' , '.Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER).' au ';
  289. }
  290. $sql .= " WHERE
  291. c_id = '$courseId' AND
  292. course_user.relation_type <> ".COURSE_RELATION_TYPE_RRHH." AND
  293. course_user.user_id = user.user_id ";
  294. if (api_is_multiple_url_enabled()) {
  295. $sql .= " AND user.user_id = au.user_id AND access_url_id = $current_access_url_id ";
  296. }
  297. // only users no teachers/coaches
  298. if ($type == COURSEMANAGER) {
  299. $sql .= " AND course_user.status = 1 ";
  300. } else {
  301. $sql .= " AND course_user.status = 5 ";
  302. }
  303. $sql .= ($sort_by_first_name ? " ORDER BY user.firstname, user.lastname" : " ORDER BY user.lastname, user.firstname");
  304. $rs = Database::query($sql);
  305. $counter = 1;
  306. while ($user = Database::fetch_array($rs, 'ASSOC')) {
  307. if (isset($user['legal_agreement'])) {
  308. if ($user['legal_agreement'] == 1) {
  309. $user['legal_agreement'] = get_lang('Yes');
  310. } else {
  311. $user['legal_agreement'] = get_lang('No');
  312. }
  313. }
  314. $extra_fields = UserManager::get_extra_user_data(
  315. $user['user_id'],
  316. false,
  317. false,
  318. false,
  319. true
  320. );
  321. if (!empty($extra_fields)) {
  322. foreach ($extra_fields as $key => $extra_value) {
  323. $user[$key] = $extra_value;
  324. }
  325. }
  326. if ($_GET['format'] == 'pdf') {
  327. $user_info = api_get_user_info($user['user_id']);
  328. $user_image = '<img src="'.$user_info['avatar'].'" width ="'.$user_image_pdf_size.'px" />';
  329. if ($is_western_name_order) {
  330. $user_pdf = [
  331. $counter,
  332. $user_image,
  333. $user['official_code'],
  334. $user['firstname'].', '.$user['lastname'],
  335. $user['email'],
  336. $user['phone'],
  337. ];
  338. } else {
  339. $user_pdf = [
  340. $counter,
  341. $user_image,
  342. $user['official_code'],
  343. $user['lastname'].', '.$user['firstname'],
  344. $user['email'],
  345. $user['phone'],
  346. ];
  347. }
  348. $a_users[] = $user_pdf;
  349. } else {
  350. $a_users[] = $user;
  351. }
  352. $data[] = $user;
  353. $counter++;
  354. }
  355. }
  356. $fileName = get_lang('StudentList');
  357. $pdfTitle = get_lang('StudentList');
  358. if ($type == COURSEMANAGER) {
  359. $fileName = get_lang('Teachers');
  360. $pdfTitle = get_lang('Teachers');
  361. }
  362. switch ($_GET['format']) {
  363. case 'csv':
  364. Export::arrayToCsv($a_users, $fileName);
  365. exit;
  366. case 'xls':
  367. Export::arrayToXls($a_users, $fileName);
  368. exit;
  369. case 'pdf':
  370. $header_attributes = [
  371. ['style' => 'width:10px'],
  372. ['style' => 'width:30px'],
  373. ['style' => 'width:50px'],
  374. ['style' => 'width:500px'],
  375. ];
  376. $params = [
  377. 'filename' => $fileName,
  378. 'pdf_title' => $pdfTitle,
  379. 'header_attributes' => $header_attributes,
  380. ];
  381. Export::export_table_pdf($a_users, $params);
  382. exit;
  383. }
  384. }
  385. }
  386. if (api_is_allowed_to_edit(null, true)) {
  387. // Unregister user from course
  388. if (isset($_REQUEST['unregister']) && $_REQUEST['unregister']) {
  389. if (isset($_GET['user_id']) && is_numeric($_GET['user_id']) &&
  390. ($_GET['user_id'] != $_user['user_id'] || api_is_platform_admin())
  391. ) {
  392. $user_id = intval($_GET['user_id']);
  393. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  394. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  395. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  396. $sql = 'SELECT user.user_id
  397. FROM '.$tbl_user.' user
  398. INNER JOIN '.$tbl_session_rel_user.' reluser
  399. ON user.user_id = reluser.user_id AND reluser.relation_type<>'.SESSION_RELATION_TYPE_RRHH.'
  400. INNER JOIN '.$tbl_session_rel_course.' rel_course
  401. ON rel_course.session_id = reluser.session_id
  402. WHERE
  403. user.user_id = "'.$user_id.'" AND
  404. rel_course.c_id = "'.$courseId.'"';
  405. $result = Database::query($sql);
  406. $row = Database::fetch_array($result, 'ASSOC');
  407. if ($row['user_id'] == $user_id || $row['user_id'] == "") {
  408. CourseManager::unsubscribe_user($_GET['user_id'], $courseCode);
  409. Display::addFlash(
  410. Display::return_message(get_lang('UserUnsubscribed'))
  411. );
  412. } else {
  413. Display::addFlash(
  414. Display::return_message(
  415. get_lang('ThisStudentIsSubscribeThroughASession')
  416. )
  417. );
  418. }
  419. }
  420. }
  421. } else {
  422. // If student can unsubscribe
  423. if (isset($_REQUEST['unregister']) && $_REQUEST['unregister'] == 'yes') {
  424. if ($course_info['unsubscribe'] == 1) {
  425. $user_id = api_get_user_id();
  426. CourseManager::unsubscribe_user($user_id, $course_info['code']);
  427. header('Location: '.api_get_path(WEB_PATH).'user_portal.php');
  428. exit;
  429. }
  430. }
  431. }
  432. // $is_allowed_in_course is first defined in local.inc.php
  433. if (!api_is_allowed_in_course()) {
  434. api_not_allowed(true);
  435. }
  436. // Statistics
  437. Event::event_access_tool(TOOL_USER);
  438. $default_column = 3;
  439. $tableLabel = $type === STUDENT ? 'student' : 'teacher';
  440. $table = new SortableTable(
  441. $tableLabel.'_list',
  442. 'get_number_of_users',
  443. 'get_user_data',
  444. $default_column
  445. );
  446. $parameters['keyword'] = isset($_GET['keyword']) ? Security::remove_XSS($_GET['keyword']) : null;
  447. $parameters['sec_token'] = Security::get_token();
  448. $parameters['id_session'] = api_get_session_id();
  449. $parameters['type'] = $type;
  450. $table->set_additional_parameters($parameters);
  451. $header_nr = 0;
  452. $indexList = [];
  453. $table->set_header($header_nr++, '', false);
  454. $indexList['photo'] = $header_nr;
  455. $table->set_header($header_nr++, get_lang('Photo'), false);
  456. $indexList['official_code'] = $header_nr;
  457. $table->set_header($header_nr++, get_lang('OfficialCode'));
  458. if ($is_western_name_order) {
  459. $indexList['firstname'] = $header_nr;
  460. $table->set_header($header_nr++, get_lang('FirstName'));
  461. $indexList['lastname'] = $header_nr;
  462. $table->set_header($header_nr++, get_lang('LastName'));
  463. } else {
  464. $indexList['lastname'] = $header_nr;
  465. $table->set_header($header_nr++, get_lang('LastName'));
  466. $indexList['firstname'] = $header_nr;
  467. $table->set_header($header_nr++, get_lang('FirstName'));
  468. }
  469. $indexList['username'] = $header_nr;
  470. $table->set_header($header_nr++, get_lang('LoginName'));
  471. $indexList['groups'] = $header_nr;
  472. $table->set_header($header_nr++, get_lang('GroupSingle'), false);
  473. $hideFields = api_get_configuration_value('hide_user_field_from_list');
  474. if (!empty($hideFields)) {
  475. $hideFields = $hideFields['fields'];
  476. foreach ($hideFields as $fieldToHide) {
  477. if (isset($indexList[$fieldToHide])) {
  478. $table->setHideColumn($indexList[$fieldToHide]);
  479. }
  480. }
  481. }
  482. $table->setHideColumn('is_tutor');
  483. $table->setHideColumn('user_status_in_course');
  484. if (api_is_allowed_to_edit(null, true)) {
  485. $table->set_header($header_nr++, get_lang('Status'), false);
  486. $table->set_header($header_nr++, get_lang('Active'), false);
  487. if ($canEditUsers) {
  488. $table->set_column_filter(8, 'active_filter');
  489. } else {
  490. $table->set_column_filter(8, 'active_filter');
  491. }
  492. foreach ($extraFields as $extraField) {
  493. $table->set_header($header_nr++, $extraField['display_text'], false);
  494. }
  495. // Actions column
  496. $table->set_header($header_nr++, get_lang('Action'), false);
  497. $table->set_column_filter($header_nr - 1, 'modify_filter');
  498. if ($canEditUsers) {
  499. $table->set_form_actions(['unsubscribe' => get_lang('Unreg')], 'user');
  500. }
  501. } else {
  502. if ($course_info['unsubscribe'] == 1) {
  503. $table->set_header($header_nr++, get_lang('Action'), false);
  504. $table->set_column_filter($header_nr - 1, 'modify_filter');
  505. }
  506. }
  507. /* Header */
  508. if (isset($origin) && $origin == 'learnpath') {
  509. Display::display_reduced_header();
  510. } else {
  511. if (isset($_GET['keyword']) && !empty($_GET['keyword'])) {
  512. $interbreadcrumb[] = [
  513. 'url' => 'user.php?'.api_get_cidreq(),
  514. 'name' => get_lang('Users'),
  515. ];
  516. $tool_name = get_lang('SearchResults');
  517. } else {
  518. $tool_name = get_lang('Users');
  519. $origin = 'users';
  520. }
  521. Display::display_header($tool_name, 'User');
  522. }
  523. // Tool introduction
  524. Display::display_introduction_section(TOOL_USER, 'left');
  525. $actions = '';
  526. $selectedTab = 1;
  527. if ($canRead) {
  528. echo '<div class="actions">';
  529. switch ($type) {
  530. case STUDENT:
  531. $selectedTab = 1;
  532. $url = api_get_path(WEB_CODE_PATH).'user/subscribe_user.php?'.api_get_cidreq().'&type='.STUDENT;
  533. $icon = Display::url(
  534. Display::return_icon('add-user.png', get_lang('Add'), [], ICON_SIZE_MEDIUM),
  535. $url
  536. );
  537. break;
  538. case COURSEMANAGER:
  539. $selectedTab = 2;
  540. $url = api_get_path(WEB_CODE_PATH).'user/subscribe_user.php?'.api_get_cidreq().'&type='.COURSEMANAGER;
  541. $icon = Display::url(
  542. Display::return_icon('add-teacher.png', get_lang('Add'), [], ICON_SIZE_MEDIUM),
  543. $url
  544. );
  545. break;
  546. }
  547. echo '<div class="row">';
  548. echo '<div class="col-md-6">';
  549. if ($canEdit) {
  550. echo $icon;
  551. }
  552. if ($canRead) {
  553. $actions .= '<a href="user.php?'.api_get_cidreq().'&action=export&format=csv&type='.$type.'">'.
  554. Display::return_icon('export_csv.png', get_lang('ExportAsCSV'), [], ICON_SIZE_MEDIUM).'</a> ';
  555. $actions .= '<a href="user.php?'.api_get_cidreq().'&action=export&format=xls&type='.$type.'">'.
  556. Display::return_icon('export_excel.png', get_lang('ExportAsXLS'), [], ICON_SIZE_MEDIUM).'</a> ';
  557. }
  558. if ($canEditUsers && $canEdit) {
  559. $actions .= '<a href="user_import.php?'.api_get_cidreq().'&action=import&type='.$type.'">'.
  560. Display::return_icon('import_csv.png', get_lang('ImportUsersToACourse'), [], ICON_SIZE_MEDIUM).'</a> ';
  561. }
  562. if ($canRead) {
  563. $actions .= '<a href="user.php?'.api_get_cidreq().'&action=export&format=pdf&type='.$type.'">'.
  564. Display::return_icon('pdf.png', get_lang('ExportToPDF'), [], ICON_SIZE_MEDIUM).'</a> ';
  565. }
  566. echo $actions;
  567. echo '</div>';
  568. echo '<div class="col-md-6">';
  569. echo '<div class="pull-right">';
  570. // Build search-form
  571. $form = new FormValidator(
  572. 'search_user',
  573. 'get',
  574. api_get_self().'?type='.$type,
  575. '',
  576. null,
  577. FormValidator::LAYOUT_INLINE
  578. );
  579. $form->addHidden('type', $type);
  580. $form->addText('keyword', '', false);
  581. $form->addElement('hidden', 'cidReq', api_get_course_id());
  582. $form->addButtonSearch(get_lang('SearchButton'));
  583. $form->display();
  584. echo '</div>';
  585. echo '</div>';
  586. echo '</div>';
  587. $allowTutors = api_get_setting('allow_tutors_to_assign_students_to_session');
  588. if (api_is_allowed_to_edit() && $allowTutors === 'true') {
  589. $actions .= ' <a class="btn btn-default" href="session_list.php?'.api_get_cidreq().'">'.
  590. get_lang('Sessions').'</a>';
  591. }
  592. echo '</div>';
  593. }
  594. echo UserManager::getUserSubscriptionTab($selectedTab);
  595. $table->display();
  596. if (!empty($_GET['keyword']) && !empty($_GET['submit'])) {
  597. $keyword_name = Security::remove_XSS($_GET['keyword']);
  598. echo '<br/>'.get_lang('SearchResultsFor').' <span style="font-style: italic ;"> '.$keyword_name.' </span><br>';
  599. }
  600. if (!isset($origin) || $origin != 'learnpath') {
  601. Display::display_footer();
  602. }
  603. /* Helper functions for the users lists in course */
  604. /**
  605. * Get the users to display on the current page.
  606. */
  607. function get_number_of_users()
  608. {
  609. $counter = 0;
  610. $sessionId = api_get_session_id();
  611. $courseCode = api_get_course_id();
  612. $active = isset($_GET['active']) ? $_GET['active'] : null;
  613. $type = isset($_REQUEST['type']) ? intval($_REQUEST['type']) : STUDENT;
  614. if (empty($sessionId)) {
  615. $status = $type;
  616. } else {
  617. if ($type == COURSEMANAGER) {
  618. $status = 2;
  619. } else {
  620. $status = 0;
  621. }
  622. }
  623. if (!empty($sessionId)) {
  624. $a_course_users = CourseManager::get_user_list_from_course_code(
  625. $courseCode,
  626. $sessionId,
  627. null,
  628. null,
  629. $status,
  630. null,
  631. false,
  632. false,
  633. null,
  634. null,
  635. null,
  636. $active
  637. );
  638. } else {
  639. $a_course_users = CourseManager::get_user_list_from_course_code(
  640. $courseCode,
  641. 0,
  642. null,
  643. null,
  644. $status,
  645. null,
  646. false,
  647. false,
  648. null,
  649. null,
  650. null,
  651. $active
  652. );
  653. }
  654. foreach ($a_course_users as $o_course_user) {
  655. if ((
  656. isset($_GET['keyword']) &&
  657. searchUserKeyword(
  658. $o_course_user['firstname'],
  659. $o_course_user['lastname'],
  660. $o_course_user['username'],
  661. $o_course_user['official_code'],
  662. $_GET['keyword']
  663. )
  664. ) || !isset($_GET['keyword']) || empty($_GET['keyword'])
  665. ) {
  666. $counter++;
  667. }
  668. }
  669. return $counter;
  670. }
  671. /**
  672. * @param string $firstname
  673. * @param string $lastname
  674. * @param string $username
  675. * @param string $official_code
  676. * @param $keyword
  677. *
  678. * @return bool
  679. */
  680. function searchUserKeyword($firstname, $lastname, $username, $official_code, $keyword)
  681. {
  682. if (api_strripos($firstname, $keyword) !== false ||
  683. api_strripos($lastname, $keyword) !== false ||
  684. api_strripos($username, $keyword) !== false ||
  685. api_strripos($official_code, $keyword) !== false
  686. ) {
  687. return true;
  688. } else {
  689. return false;
  690. }
  691. }
  692. /**
  693. * Get the users to display on the current page.
  694. *
  695. * @param int $from Offset
  696. * @param int $number_of_items
  697. * @param int $column The column on which to sort
  698. * @param string $direction ASC or DESC, for the sort order of the query results
  699. *
  700. * @return array
  701. */
  702. function get_user_data($from, $number_of_items, $column, $direction)
  703. {
  704. global $is_western_name_order;
  705. global $extraFields;
  706. $canEditUsers = api_get_setting('allow_user_course_subscription_by_course_admin') == 'true' || api_is_platform_admin();
  707. $type = isset($_REQUEST['type']) ? intval($_REQUEST['type']) : STUDENT;
  708. $course_info = api_get_course_info();
  709. $sessionId = api_get_session_id();
  710. $course_code = $course_info['code'];
  711. $a_users = [];
  712. $limit = null;
  713. // limit
  714. if (!isset($_GET['keyword']) || empty($_GET['keyword'])) {
  715. $limit = 'LIMIT '.intval($from).','.intval($number_of_items);
  716. }
  717. if (!in_array($direction, ['ASC', 'DESC'])) {
  718. $direction = 'ASC';
  719. }
  720. switch ($column) {
  721. case 2: //official code
  722. $order_by = 'ORDER BY user.official_code '.$direction;
  723. break;
  724. case 3:
  725. if ($is_western_name_order) {
  726. $order_by = 'ORDER BY user.firstname '.$direction.', user.lastname '.$direction;
  727. } else {
  728. $order_by = 'ORDER BY user.lastname '.$direction.', user.firstname '.$direction;
  729. }
  730. break;
  731. case 4:
  732. if ($is_western_name_order) {
  733. $order_by = 'ORDER BY user.lastname '.$direction.', user.firstname '.$direction;
  734. } else {
  735. $order_by = 'ORDER BY user.firstname '.$direction.', user.lastname '.$direction;
  736. }
  737. break;
  738. case 5: //username
  739. $order_by = 'ORDER BY user.username '.$direction;
  740. break;
  741. default:
  742. if ($is_western_name_order) {
  743. $order_by = 'ORDER BY user.lastname '.$direction.', user.firstname '.$direction;
  744. } else {
  745. $order_by = 'ORDER BY user.firstname '.$direction.', user.lastname '.$direction;
  746. }
  747. break;
  748. }
  749. $active = isset($_GET['active']) ? $_GET['active'] : null;
  750. if (empty($sessionId)) {
  751. $status = $type;
  752. } else {
  753. if ($type == COURSEMANAGER) {
  754. $status = 2;
  755. } else {
  756. $status = 0;
  757. }
  758. }
  759. $a_course_users = CourseManager :: get_user_list_from_course_code(
  760. $course_code,
  761. $sessionId,
  762. $limit,
  763. $order_by,
  764. $status,
  765. null,
  766. false,
  767. false,
  768. null,
  769. [],
  770. [],
  771. $active
  772. );
  773. foreach ($a_course_users as $user_id => $o_course_user) {
  774. if ((
  775. isset($_GET['keyword']) &&
  776. searchUserKeyword(
  777. $o_course_user['firstname'],
  778. $o_course_user['lastname'],
  779. $o_course_user['username'],
  780. $o_course_user['official_code'],
  781. $_GET['keyword']
  782. )
  783. ) || !isset($_GET['keyword']) || empty($_GET['keyword'])
  784. ) {
  785. $groupsNameList = GroupManager::getAllGroupPerUserSubscription($user_id);
  786. $groupsNameListParsed = [];
  787. if (!empty($groupsNameList)) {
  788. $groupsNameListParsed = array_column($groupsNameList, 'name');
  789. }
  790. $temp = [];
  791. if (api_is_allowed_to_edit(null, true)) {
  792. $userInfo = api_get_user_info($user_id);
  793. $photo = Display::img($userInfo['avatar_small'], $userInfo['complete_name'], [], false);
  794. $temp[] = $user_id;
  795. $temp[] = $photo;
  796. $temp[] = $o_course_user['official_code'];
  797. if ($is_western_name_order) {
  798. $temp[] = $o_course_user['firstname'];
  799. $temp[] = $o_course_user['lastname'];
  800. } else {
  801. $temp[] = $o_course_user['lastname'];
  802. $temp[] = $o_course_user['firstname'];
  803. }
  804. $temp[] = $o_course_user['username'];
  805. // Groups.
  806. $temp[] = implode(', ', $groupsNameListParsed);
  807. // Status
  808. $default_status = get_lang('Student');
  809. if ((isset($o_course_user['status_rel']) && $o_course_user['status_rel'] == 1) ||
  810. (isset($o_course_user['status_session']) && $o_course_user['status_session'] == 2)
  811. ) {
  812. $default_status = get_lang('CourseManager');
  813. } elseif (isset($o_course_user['is_tutor']) && $o_course_user['is_tutor'] == 1) {
  814. $default_status = get_lang('Tutor');
  815. }
  816. $temp[] = $default_status;
  817. // Active
  818. $temp[] = $o_course_user['active'];
  819. $extraFieldOption = new ExtraFieldOption('user');
  820. $extraFieldValue = new ExtraFieldValue('user');
  821. if (!empty($extraFields)) {
  822. foreach ($extraFields as $extraField) {
  823. $data = $extraFieldValue->get_values_by_handler_and_field_id(
  824. $user_id,
  825. $extraField['id']
  826. );
  827. $optionList = $extraFieldOption->get_field_option_by_field_and_option(
  828. $extraField['id'],
  829. $data['value']
  830. );
  831. if (!empty($optionList)) {
  832. $options = implode(', ', array_column($optionList, 'display_text'));
  833. $temp[] = $options;
  834. } else {
  835. $temp[] = $data['value'];
  836. }
  837. }
  838. }
  839. // User id for actions
  840. $temp[] = $user_id;
  841. $temp['is_tutor'] = isset($o_course_user['is_tutor']) ? $o_course_user['is_tutor'] : '';
  842. $temp['user_status_in_course'] = isset($o_course_user['status_rel']) ? $o_course_user['status_rel'] : '';
  843. } else {
  844. $userInfo = api_get_user_info($user_id);
  845. $userPicture = $userInfo['avatar'];
  846. $photo = '<img src="'.$userPicture.'" alt="'.$userInfo['complete_name'].'" width="22" height="22" title="'.$userInfo['complete_name'].'" />';
  847. $temp[] = '';
  848. $temp[] = $photo;
  849. $temp[] = $o_course_user['official_code'];
  850. if ($is_western_name_order) {
  851. $temp[] = $o_course_user['firstname'];
  852. $temp[] = $o_course_user['lastname'];
  853. } else {
  854. $temp[] = $o_course_user['lastname'];
  855. $temp[] = $o_course_user['firstname'];
  856. }
  857. $temp[] = $o_course_user['username'];
  858. // Group.
  859. $temp[] = implode(', ', $groupsNameListParsed);
  860. if ($course_info['unsubscribe'] == 1) {
  861. //User id for actions
  862. $temp[] = $user_id;
  863. }
  864. }
  865. $a_users[$user_id] = $temp;
  866. }
  867. }
  868. return $a_users;
  869. }
  870. /**
  871. * Build the active-column of the table to lock or unlock a certain user
  872. * lock = the user can no longer use this account.
  873. *
  874. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  875. *
  876. * @param int $active the current state of the account
  877. * @param string $urlParams
  878. *
  879. * @return string Some HTML-code with the lock/unlock button
  880. */
  881. function active_filter($active, $urlParams, $row)
  882. {
  883. $userId = api_get_user_id();
  884. $action = '';
  885. $image = '';
  886. if ($active == '1') {
  887. $action = 'AccountActive';
  888. $image = 'accept';
  889. }
  890. if ($active == '0') {
  891. $action = 'AccountInactive';
  892. $image = 'error';
  893. }
  894. $result = '';
  895. /* you cannot lock yourself out otherwise you could disable all the accounts including your own => everybody is
  896. locked out and nobody can change it anymore.*/
  897. if ($row[0] != $userId) {
  898. $result = '<center><img src="'.Display::returnIconPath($image.'.png', 16).'" border="0" alt="'.get_lang(ucfirst($action)).'" title="'.get_lang(ucfirst($action)).'"/></center>';
  899. }
  900. return $result;
  901. }
  902. /**
  903. * Build the modify-column of the table.
  904. *
  905. * @param int $user_id The user id
  906. *
  907. * @return string Some HTML-code
  908. */
  909. function modify_filter($user_id, $row, $data)
  910. {
  911. global $charset;
  912. $canEditUsers = api_get_setting('allow_user_course_subscription_by_course_admin') == 'true' || api_is_platform_admin();
  913. $is_allowed_to_track = api_is_allowed_to_edit(true, true);
  914. $user_id = $data[0];
  915. $userInfo = api_get_user_info($user_id);
  916. $isInvitee = $userInfo['status'] == INVITEE ? true : false;
  917. $course_info = $_course = api_get_course_info();
  918. $current_user_id = api_get_user_id();
  919. $sessionId = api_get_session_id();
  920. $type = isset($_REQUEST['type']) ? intval($_REQUEST['type']) : STUDENT;
  921. $result = '';
  922. if ($is_allowed_to_track) {
  923. $result .= '<a href="../mySpace/myStudents.php?'.api_get_cidreq().'&student='.$user_id.'&details=true&course='.$_course['id'].'&origin=user_course&id_session='.api_get_session_id().'" title="'.get_lang('Tracking').'">
  924. '.Display::return_icon('statistics.png', get_lang('Tracking')).'
  925. </a>';
  926. }
  927. // If platform admin, show the login_as icon (this drastically shortens
  928. // time taken by support to test things out)
  929. if (api_is_platform_admin()) {
  930. $result .= ' <a href="'.api_get_path(WEB_CODE_PATH).'admin/user_list.php?action=login_as&user_id='.$user_id.'&sec_token='.Security::getTokenFromSession().'">'.
  931. Display::return_icon('login_as.gif', get_lang('LoginAs')).'</a>&nbsp;&nbsp;';
  932. }
  933. if (api_is_allowed_to_edit(null, true)) {
  934. if (empty($sessionId)) {
  935. $isTutor = isset($data['is_tutor']) ? intval($data['is_tutor']) : 0;
  936. $isTutor = empty($isTutor) ? 1 : 0;
  937. $text = get_lang('RemoveTutorStatus');
  938. if ($isTutor) {
  939. $text = get_lang('SetTutor');
  940. }
  941. if ($isInvitee) {
  942. $disabled = 'disabled';
  943. } else {
  944. $disabled = '';
  945. }
  946. $allow = api_get_configuration_value('extra');
  947. if ($allow) {
  948. $result .= '<a href="'.
  949. api_get_path(WEB_CODE_PATH).'extra/userInfo.php?'.api_get_cidreq().'&editMainUserInfo='.$user_id.'" title="'.get_lang('Edit').'" >'.
  950. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).
  951. '</a>&nbsp;';
  952. }
  953. if ($data['user_status_in_course'] == STUDENT) {
  954. $result .= Display::url(
  955. $text,
  956. 'user.php?'.api_get_cidreq().'&action=set_tutor&is_tutor='.$isTutor.'&user_id='.$user_id.'&type='.$type,
  957. ['class' => 'btn btn-default '.$disabled]
  958. ).'&nbsp;';
  959. }
  960. }
  961. // edit
  962. if ($canEditUsers) {
  963. // unregister
  964. if ($user_id != $current_user_id || api_is_platform_admin()) {
  965. $result .= '<a class="btn btn-small btn-danger" href="'.api_get_self().'?'.api_get_cidreq().'&type='.$type.'&unregister=yes&user_id='.$user_id.'" title="'.get_lang('Unreg').' " onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset)).'\')) return false;">'.
  966. get_lang('Unreg').'</a>&nbsp;';
  967. }
  968. }
  969. } else {
  970. // Show buttons for unsubscribe
  971. if ($course_info['unsubscribe'] == 1) {
  972. if ($user_id == $current_user_id) {
  973. $result .= '<a class="btn btn-small btn-danger" href="'.api_get_self().'?'.api_get_cidreq().'&type='.$type.'&unregister=yes&user_id='.$user_id.'" title="'.get_lang('Unreg').' " onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset)).'\')) return false;">'.
  974. get_lang('Unreg').'</a>&nbsp;';
  975. }
  976. }
  977. }
  978. return $result;
  979. }