user.php 40 KB

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