user.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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. $current_course_tool = TOOL_USER;
  15. $this_section = SECTION_COURSES;
  16. // notice for unauthorized people.
  17. api_protect_course_script(true);
  18. if (!api_is_platform_admin(true)) {
  19. if (!api_is_course_admin() && !api_is_coach()) {
  20. if (api_get_course_setting('allow_user_view_user_list') == 0) {
  21. api_not_allowed(true);
  22. }
  23. }
  24. }
  25. $is_allowed_in_course = api_is_allowed_in_course();
  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. $_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('display.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. /**
  415. * Get the users to display on the current page.
  416. */
  417. function get_number_of_users()
  418. {
  419. $counter = 0;
  420. $sessionId = api_get_session_id();
  421. $courseCode = api_get_course_id();
  422. $active = isset($_GET['active']) ? $_GET['active'] : null;
  423. if (!empty($sessionId)) {
  424. $a_course_users = CourseManager::get_user_list_from_course_code(
  425. $courseCode,
  426. $sessionId,
  427. null,
  428. null,
  429. null,
  430. null,
  431. false,
  432. false,
  433. null,
  434. null,
  435. null,
  436. $active
  437. );
  438. } else {
  439. $a_course_users = CourseManager::get_user_list_from_course_code(
  440. $courseCode,
  441. 0,
  442. null,
  443. null,
  444. null,
  445. null,
  446. false,
  447. false,
  448. null,
  449. null,
  450. null,
  451. $active
  452. );
  453. }
  454. foreach ($a_course_users as $o_course_user) {
  455. if ((isset($_GET['keyword']) &&
  456. searchUserKeyword(
  457. $o_course_user['firstname'],
  458. $o_course_user['lastname'],
  459. $o_course_user['username'],
  460. $o_course_user['official_code'],
  461. $_GET['keyword']
  462. )
  463. ) || !isset($_GET['keyword']) || empty($_GET['keyword'])
  464. ) {
  465. $counter++;
  466. }
  467. }
  468. return $counter;
  469. }
  470. /**
  471. * @param string $firstname
  472. * @param string $lastname
  473. * @param string $username
  474. * @param string $official_code
  475. * @param $keyword
  476. * @return bool
  477. */
  478. function searchUserKeyword($firstname, $lastname, $username, $official_code, $keyword) {
  479. if (
  480. api_strripos($firstname, $keyword) !== false ||
  481. api_strripos($lastname, $keyword) !== false ||
  482. api_strripos($username, $keyword) !== false ||
  483. api_strripos($official_code, $keyword) !== false
  484. ) {
  485. return true;
  486. } else {
  487. return false;
  488. }
  489. }
  490. /**
  491. * Get the users to display on the current page.
  492. */
  493. function get_user_data($from, $number_of_items, $column, $direction)
  494. {
  495. global $is_western_name_order;
  496. global $extraFields;
  497. $type = isset($_REQUEST['type']) ? intval($_REQUEST['type']) : STUDENT;
  498. $course_info = api_get_course_info();
  499. $sessionId = api_get_session_id();
  500. $course_code = $course_info['code'];
  501. $a_users = array();
  502. $limit = null;
  503. // limit
  504. if (!isset($_GET['keyword']) || empty($_GET['keyword'])) {
  505. $limit = 'LIMIT '.intval($from).','.intval($number_of_items);
  506. }
  507. if (!in_array($direction, array('ASC', 'DESC'))) {
  508. $direction = 'ASC';
  509. }
  510. switch ($column) {
  511. case 2: //official code
  512. $order_by = 'ORDER BY user.official_code '.$direction;
  513. break;
  514. case 3:
  515. if ($is_western_name_order) {
  516. $order_by = 'ORDER BY user.firstname '.$direction.', user.lastname '.$direction;
  517. } else {
  518. $order_by = 'ORDER BY user.lastname '.$direction.', user.firstname '.$direction;
  519. }
  520. break;
  521. case 4:
  522. if ($is_western_name_order) {
  523. $order_by = 'ORDER BY user.lastname '.$direction.', user.firstname '.$direction;
  524. } else {
  525. $order_by = 'ORDER BY user.firstname '.$direction.', user.lastname '.$direction;
  526. }
  527. break;
  528. case 5: //username
  529. $order_by = 'ORDER BY user.username '.$direction;
  530. break;
  531. default:
  532. if ($is_western_name_order) {
  533. $order_by = 'ORDER BY user.lastname '.$direction.', user.firstname '.$direction;
  534. } else {
  535. $order_by = 'ORDER BY user.firstname '.$direction.', user.lastname '.$direction;
  536. }
  537. break;
  538. }
  539. $active = isset($_GET['active']) ? $_GET['active'] : null;
  540. if (empty($sessionId)) {
  541. $status = $type;
  542. } else {
  543. if ($type == COURSEMANAGER) {
  544. $status = 2;
  545. } else {
  546. $status = 0;
  547. }
  548. }
  549. $a_course_users = CourseManager :: get_user_list_from_course_code(
  550. $course_code,
  551. $sessionId,
  552. $limit,
  553. $order_by,
  554. $status,
  555. null,
  556. false,
  557. false,
  558. null,
  559. array(),
  560. array(),
  561. $active
  562. );
  563. foreach ($a_course_users as $user_id => $o_course_user) {
  564. if ((
  565. isset($_GET['keyword']) &&
  566. searchUserKeyword(
  567. $o_course_user['firstname'],
  568. $o_course_user['lastname'],
  569. $o_course_user['username'],
  570. $o_course_user['official_code'],
  571. $_GET['keyword'])
  572. ) || !isset($_GET['keyword']) || empty($_GET['keyword'])
  573. ) {
  574. $groupsNameList = GroupManager::getAllGroupPerUserSubscription($user_id);
  575. $groupsNameListParsed = [];
  576. if (!empty($groupsNameList)) {
  577. $groupsNameListParsed = array_column($groupsNameList, 'name');
  578. }
  579. $temp = array();
  580. if (api_is_allowed_to_edit(null, true)) {
  581. $userInfo = api_get_user_info($user_id);
  582. $photo = Display::img($userInfo['avatar_small'], $userInfo['complete_name'], [], false);
  583. $temp[] = $user_id;
  584. $temp[] = $photo;
  585. $temp[] = $o_course_user['official_code'];
  586. if ($is_western_name_order) {
  587. $temp[] = $o_course_user['firstname'];
  588. $temp[] = $o_course_user['lastname'];
  589. } else {
  590. $temp[] = $o_course_user['lastname'];
  591. $temp[] = $o_course_user['firstname'];
  592. }
  593. $temp[] = $o_course_user['username'];
  594. // Groups.
  595. $temp[] = implode(', ', $groupsNameListParsed);
  596. // Status
  597. $default_status = get_lang('Student');
  598. if ((isset($o_course_user['status_rel']) && $o_course_user['status_rel'] == 1) ||
  599. (isset($o_course_user['status_session']) && $o_course_user['status_session'] == 2)
  600. ) {
  601. $default_status = get_lang('CourseManager');
  602. } elseif (isset($o_course_user['is_tutor']) && $o_course_user['is_tutor'] == 1) {
  603. $default_status = get_lang('Tutor');
  604. }
  605. $temp[] = $default_status;
  606. // Active
  607. $temp[] = $o_course_user['active'];
  608. if (!empty($extraFields)) {
  609. foreach ($extraFields as $extraField) {
  610. $extraFieldValue = new ExtraFieldValue('user');
  611. $data = $extraFieldValue->get_values_by_handler_and_field_id(
  612. $user_id,
  613. $extraField['id']
  614. );
  615. $temp[] = $data['value'];
  616. }
  617. }
  618. // User id for actions
  619. $temp[] = $user_id;
  620. $temp['is_tutor'] = isset($o_course_user['is_tutor']) ? $o_course_user['is_tutor'] : '';
  621. $temp['user_status_in_course'] = isset($o_course_user['status_rel']) ? $o_course_user['status_rel'] : '';
  622. } else {
  623. $userInfo = api_get_user_info($user_id);
  624. $userPicture = $userInfo['avatar'];
  625. $photo= '<img src="'.$userPicture.'" alt="'.$userInfo['complete_name'].'" width="22" height="22" title="'.$userInfo['complete_name'].'" />';
  626. $temp[] = $user_id;
  627. $temp[] = $photo;
  628. $temp[] = $o_course_user['official_code'];
  629. if ($is_western_name_order) {
  630. $temp[] = $o_course_user['firstname'];
  631. $temp[] = $o_course_user['lastname'];
  632. } else {
  633. $temp[] = $o_course_user['lastname'];
  634. $temp[] = $o_course_user['firstname'];
  635. }
  636. $temp[] = $o_course_user['username'];
  637. // Group.
  638. $temp[] = implode(', ', $groupsNameListParsed);
  639. if ($course_info['unsubscribe'] == 1) {
  640. //User id for actions
  641. $temp[] = $user_id;
  642. }
  643. }
  644. $a_users[$user_id] = $temp;
  645. }
  646. }
  647. return $a_users;
  648. }
  649. /**
  650. * Build the active-column of the table to lock or unlock a certain user
  651. * lock = the user can no longer use this account
  652. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  653. * @param int $active the current state of the account
  654. * @param int $user_id The user id
  655. * @param string $urlParams
  656. *
  657. * @return string Some HTML-code with the lock/unlock button
  658. */
  659. function active_filter($active, $urlParams, $row)
  660. {
  661. $userId = api_get_user_id();
  662. $action = '';
  663. $image = '';
  664. if ($active == '1') {
  665. $action = 'AccountActive';
  666. $image = 'accept';
  667. }
  668. if ($active == '0') {
  669. $action = 'AccountInactive';
  670. $image = 'error';
  671. }
  672. $result = '';
  673. /* you cannot lock yourself out otherwise you could disable all the accounts including your own => everybody is
  674. locked out and nobody can change it anymore.*/
  675. if ($row[0] <> $userId) {
  676. $title = get_lang(ucfirst($action));
  677. $result = '<center>'.Display::return_icon($image.'.png', $title).'</center>';
  678. }
  679. return $result;
  680. }
  681. /**
  682. * Build the modify-column of the table
  683. * @param int $user_id The user id
  684. * @return string Some HTML-code
  685. */
  686. function modify_filter($user_id, $row, $data)
  687. {
  688. global $is_allowed_to_track, $charset;
  689. $user_id = $data[0];
  690. $userInfo = api_get_user_info($user_id);
  691. $isInvitee = $userInfo['status'] == INVITEE ? true : false;
  692. $course_info = $_course = api_get_course_info();
  693. $current_user_id = api_get_user_id();
  694. $sessionId = api_get_session_id();
  695. $type = isset($_REQUEST['type']) ? intval($_REQUEST['type']) : STUDENT;
  696. $result = "";
  697. if ($is_allowed_to_track) {
  698. $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').'">
  699. '.Display::return_icon('stats.png', get_lang('Tracking')).'
  700. </a>';
  701. }
  702. // If platform admin, show the login_as icon (this drastically shortens
  703. // time taken by support to test things out)
  704. if (api_is_platform_admin()) {
  705. $result .= ' <a href="'.api_get_path(
  706. WEB_CODE_PATH
  707. ).'admin/user_list.php?action=login_as&user_id='.$user_id.'">'.
  708. Display::return_icon('login_as.gif', get_lang('LoginAs')).'</a>&nbsp;&nbsp;';
  709. }
  710. if (api_is_allowed_to_edit(null, true)) {
  711. if (empty($sessionId)) {
  712. $isTutor = isset($data['is_tutor']) ? intval($data['is_tutor']) : 0;
  713. $isTutor = empty($isTutor) ? 1 : 0;
  714. $text = get_lang('RemoveTutorStatus');
  715. if ($isTutor) {
  716. $text = get_lang('SetTutor');
  717. }
  718. if ($isInvitee) {
  719. $disabled = 'disabled';
  720. } else {
  721. $disabled = '';
  722. }
  723. if ($data['user_status_in_course'] == STUDENT) {
  724. $result .= Display::url(
  725. $text,
  726. 'user.php?'.api_get_cidreq(
  727. ).'&action=set_tutor&is_tutor='.$isTutor.'&user_id='.$user_id.'&type='.$type,
  728. array('class' => 'btn btn-default '.$disabled)
  729. ).'&nbsp;';
  730. }
  731. }
  732. // edit
  733. if (api_get_setting(
  734. 'course.allow_user_course_subscription_by_course_admin'
  735. ) == 'true' or api_is_platform_admin()
  736. ) {
  737. // unregister
  738. if ($user_id != $current_user_id || api_is_platform_admin()) {
  739. $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;">'.
  740. get_lang('Unreg').'</a>&nbsp;';
  741. } else {
  742. //$result .= Display::return_icon('unsubscribe_course_na.png', get_lang('Unreg'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  743. }
  744. }
  745. } else {
  746. // Show buttons for unsubscribe
  747. if ($course_info['unsubscribe'] == 1) {
  748. if ($user_id == $current_user_id) {
  749. $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;">'.
  750. get_lang('Unreg').'</a>&nbsp;';
  751. }
  752. }
  753. }
  754. return $result;
  755. }
  756. function hide_field()
  757. {
  758. return null;
  759. }
  760. $default_column = 3;
  761. $table = new SortableTable('user_list', 'get_number_of_users', 'get_user_data', $default_column);
  762. $parameters['keyword'] = isset($_GET['keyword']) ? Security::remove_XSS($_GET['keyword']) : null;
  763. $parameters['sec_token'] = Security::get_token();
  764. $table->set_additional_parameters($parameters);
  765. $header_nr = 0;
  766. $indexList = array();
  767. $table->set_header($header_nr++, '', false);
  768. $indexList['photo'] = $header_nr;
  769. $table->set_header($header_nr++, get_lang('Photo'), false);
  770. $table->set_header($header_nr++, get_lang('OfficialCode'));
  771. $indexList['official_code'] = $header_nr;
  772. if ($is_western_name_order) {
  773. $indexList['firstname'] = $header_nr;
  774. $table->set_header($header_nr++, get_lang('FirstName'));
  775. $indexList['lastname'] = $header_nr;
  776. $table->set_header($header_nr++, get_lang('LastName'));
  777. } else {
  778. $indexList['lastname'] = $header_nr;
  779. $table->set_header($header_nr++, get_lang('LastName'));
  780. $indexList['firstname'] = $header_nr;
  781. $table->set_header($header_nr++, get_lang('FirstName'));
  782. }
  783. $indexList['username'] = $header_nr;
  784. $table->set_header($header_nr++, get_lang('LoginName'));
  785. $indexList['groups'] = $header_nr;
  786. $table->set_header($header_nr++, get_lang('GroupSingle'), false);
  787. if (api_is_allowed_to_edit(null, true) && api_get_setting(
  788. 'course.allow_user_course_subscription_by_course_admin'
  789. ) == 'true'
  790. ) {
  791. } else {
  792. $table->set_column_filter(0, 'hide_field');
  793. }
  794. $hideFields = api_get_configuration_value('hide_user_field_from_list');
  795. if (!empty($hideFields)) {
  796. foreach ($hideFields as $fieldToHide) {
  797. if (isset($indexList[$fieldToHide])) {
  798. $table->setHideColumn($indexList[$fieldToHide]);
  799. }
  800. }
  801. }
  802. if (api_is_allowed_to_edit(null, true)) {
  803. $table->set_header($header_nr++, get_lang('Status'), false);
  804. $table->set_header($header_nr++, get_lang('Active'), false);
  805. if (api_get_setting(
  806. 'course.allow_user_course_subscription_by_course_admin'
  807. ) == 'true'
  808. ) {
  809. $table->set_column_filter(8, 'active_filter');
  810. } else {
  811. $table->set_column_filter(8, 'active_filter');
  812. }
  813. foreach ($extraFields as $extraField) {
  814. $table->set_header($header_nr++, $extraField['display_text'], false);
  815. }
  816. // Actions column
  817. $table->set_header($header_nr++, get_lang('Action'), false);
  818. $table->set_column_filter($header_nr-1, 'modify_filter');
  819. if (api_get_setting(
  820. 'course.allow_user_course_subscription_by_course_admin'
  821. ) == 'true'
  822. ) {
  823. $table->set_form_actions(array('unsubscribe' => get_lang('Unreg')), 'user');
  824. }
  825. } else {
  826. if ($course_info['unsubscribe'] == 1) {
  827. $table->set_header($header_nr++, get_lang('Action'), false);
  828. $table->set_column_filter($header_nr-1, 'modify_filter');
  829. }
  830. }
  831. /* Header */
  832. if (isset($origin) && $origin == 'learnpath') {
  833. Display::display_reduced_header();
  834. } else {
  835. if (isset($_GET['keyword']) && !empty($_GET['keyword'])) {
  836. $interbreadcrumb[] = array(
  837. "url" => "user.php?".api_get_cidreq(),
  838. "name" => get_lang("Users"),
  839. );
  840. $tool_name = get_lang('SearchResults');
  841. } else {
  842. $tool_name = get_lang('Users');
  843. $origin = 'users';
  844. }
  845. Display::display_header($tool_name, "User");
  846. }
  847. /* Setting the permissions for this page */
  848. $is_allowed_to_track = ($is_courseAdmin || $is_courseTutor);
  849. // Tool introduction
  850. Display::display_introduction_section(TOOL_USER, 'left');
  851. $actions = '';
  852. $selectedTab = 1;
  853. if (api_is_allowed_to_edit(null, true)) {
  854. echo '<div class="actions">';
  855. switch ($type) {
  856. case STUDENT:
  857. $selectedTab = 1;
  858. $url = api_get_path(WEB_CODE_PATH).'user/subscribe_user.php?'.api_get_cidreq().'&type='.STUDENT;
  859. $icon = Display::url(
  860. Display::return_icon('add-user.png', get_lang('Add'), '', ICON_SIZE_MEDIUM),
  861. $url
  862. );
  863. break;
  864. case COURSEMANAGER:
  865. $selectedTab = 2;
  866. $url = api_get_path(WEB_CODE_PATH).'user/subscribe_user.php?'.api_get_cidreq().'&type='.COURSEMANAGER;
  867. $icon = Display::url(
  868. Display::return_icon('add-teacher.png', get_lang('Add'), '', ICON_SIZE_MEDIUM),
  869. $url
  870. );
  871. break;
  872. }
  873. echo '<div class="row">';
  874. echo '<div class="col-md-6">';
  875. echo $icon;
  876. $actions .= '<a href="user.php?'.api_get_cidreq().'&action=export&format=csv&type='.$type.'">'.
  877. Display::return_icon('export_csv.png', get_lang('ExportAsCSV'),'',ICON_SIZE_MEDIUM).'</a> ';
  878. $actions .= '<a href="user.php?'.api_get_cidreq().'&action=export&format=xls&type='.$type.'">'.
  879. Display::return_icon('export_excel.png', get_lang('ExportAsXLS'),'',ICON_SIZE_MEDIUM).'</a> ';
  880. if (api_get_setting(
  881. 'course.allow_user_course_subscription_by_course_admin'
  882. ) == 'true' ||
  883. api_is_platform_admin()
  884. ) {
  885. $actions .= '<a href="user_import.php?'.api_get_cidreq().'&action=import">'.
  886. Display::return_icon('import_csv.png', get_lang('ImportUsersToACourse'),'',ICON_SIZE_MEDIUM).'</a> ';
  887. }
  888. $actions .= '<a href="user.php?'.api_get_cidreq().'&action=export&format=pdf&type='.$type.'">'.
  889. Display::return_icon('pdf.png', get_lang('ExportToPDF'),'',ICON_SIZE_MEDIUM).'</a> ';
  890. echo $actions;
  891. echo '</div>';
  892. echo '<div class="col-md-6">';
  893. echo '<div class="pull-right">';
  894. // Build search-form
  895. $form = new FormValidator(
  896. 'search_user',
  897. 'get',
  898. api_get_self().'?'.api_get_cidreq(),
  899. '',
  900. null,
  901. FormValidator::LAYOUT_INLINE
  902. );
  903. $form->addHidden('cidReq', api_get_course_id());
  904. $form->addHidden('id_session', api_get_session_id());
  905. $form->addText('keyword', '', false);
  906. $form->addButtonSearch(get_lang('SearchButton'));
  907. $form->display();
  908. echo '</div>';
  909. echo '</div>';
  910. echo '</div>';
  911. $allowTutors = api_get_setting('session.allow_tutors_to_assign_students_to_session');
  912. if (api_is_allowed_to_edit() && $allowTutors == 'true') {
  913. $actions .= ' <a class="btn btn-default" href="session_list.php?'.api_get_cidreq().'">'.
  914. get_lang('Sessions').'</a>';
  915. }
  916. echo '</div>';
  917. }
  918. echo UserManager::getUserSubscriptionTab($selectedTab);
  919. $table->display();
  920. if (!empty($_GET['keyword']) && !empty($_GET['submit'])) {
  921. $keyword_name = Security::remove_XSS($_GET['keyword']);
  922. echo '<br/>'.get_lang('SearchResultsFor').' <span style="font-style: italic ;"> '.$keyword_name.' </span><br>';
  923. }
  924. if (!isset($origin) || $origin != 'learnpath') {
  925. Display::display_footer();
  926. }