user.php 39 KB

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