user.php 39 KB

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