user.php 38 KB

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