user.php 39 KB

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