subscribe_user.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. <?php
  2. /* For licensing terms, see /license.txt*/
  3. use \ExtraField as ExtraFieldModel;
  4. use Chamilo\CoreBundle\Entity\ExtraField;
  5. /**
  6. * This script allows teachers to subscribe existing users
  7. * to their course.
  8. * @package chamilo.user
  9. */
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. $current_course_tool = TOOL_USER;
  12. // the section (for the tabs)
  13. $this_section = SECTION_COURSES;
  14. // notice for unauthorized people.
  15. api_protect_course_script(true);
  16. if (api_get_setting('allow_user_course_subscription_by_course_admin') === 'false') {
  17. if (!api_is_platform_admin()) {
  18. api_not_allowed(true);
  19. }
  20. }
  21. // Access restriction
  22. if (!api_is_allowed_to_edit()) {
  23. api_not_allowed(true);
  24. }
  25. $tool_name = get_lang("SubscribeUserToCourse");
  26. $type = isset($_REQUEST['type']) ? intval($_REQUEST['type']) : STUDENT;
  27. $keyword = isset($_REQUEST['keyword']) ? Security::remove_XSS($_REQUEST['keyword']) : null;
  28. $courseInfo = api_get_course_info();
  29. if ($type == COURSEMANAGER) {
  30. $tool_name = get_lang("SubscribeUserToCourseAsTeacher");
  31. }
  32. //extra entries in breadcrumb
  33. $interbreadcrumb[] = array(
  34. "url" => "user.php?".api_get_cidreq(),
  35. "name" => get_lang("ToolUser")
  36. );
  37. if ($keyword) {
  38. $interbreadcrumb[] = array(
  39. "url" => "subscribe_user.php?type=".$type.'&'.api_get_cidreq(),
  40. "name" => $tool_name
  41. );
  42. $tool_name = get_lang('SearchResults');
  43. }
  44. $sessionId = api_get_session_id();
  45. $list_register_user = '';
  46. $list_not_register_user = '';
  47. if (isset($_REQUEST['register'])) {
  48. $userInfo = api_get_user_info($_REQUEST['user_id']);
  49. $message = $userInfo['complete_name'].' '.get_lang('AddedToCourse');
  50. if ($type === COURSEMANAGER) {
  51. if (!empty($sessionId)) {
  52. $result_simple_sub = SessionManager::set_coach_to_course_session(
  53. $_REQUEST['user_id'],
  54. $sessionId,
  55. $courseInfo['real_id']
  56. );
  57. Display::addFlash(Display::return_message($message));
  58. } else {
  59. $result_simple_sub = CourseManager:: subscribe_user(
  60. $_REQUEST['user_id'],
  61. $courseInfo['code'],
  62. COURSEMANAGER
  63. );
  64. Display::addFlash(Display::return_message($message));
  65. }
  66. } else {
  67. $result_simple_sub = CourseManager:: subscribe_user(
  68. $_REQUEST['user_id'],
  69. $courseInfo['code']
  70. );
  71. Display::addFlash(Display::return_message($message));
  72. }
  73. header('Location:'.api_get_path(WEB_CODE_PATH).'user/user.php?'.api_get_cidreq().'&type='.$type);
  74. exit;
  75. }
  76. if (isset($_POST['action'])) {
  77. switch ($_POST['action']) {
  78. case 'subscribe':
  79. if (is_array($_POST['user'])) {
  80. foreach ($_POST['user'] as $index => $user_id) {
  81. $userInfo = api_get_user_info($user_id);
  82. if ($type === COURSEMANAGER) {
  83. if (!empty($sessionId)) {
  84. $is_suscribe[] = SessionManager::set_coach_to_course_session(
  85. $user_id,
  86. $sessionId,
  87. $courseInfo['real_id']
  88. );
  89. } else {
  90. $is_suscribe[] = CourseManager::subscribe_user(
  91. $user_id,
  92. $courseInfo['code'],
  93. COURSEMANAGER
  94. );
  95. }
  96. } else {
  97. $is_suscribe[] = CourseManager::subscribe_user(
  98. $user_id,
  99. $courseInfo['code']
  100. );
  101. }
  102. $message = $userInfo['complete_name'].' '.get_lang('AddedToCourse');
  103. Display::addFlash(Display::return_message($message));
  104. }
  105. }
  106. header('Location:'.api_get_path(WEB_CODE_PATH).'user/user.php?'.api_get_cidreq().'&type='.$type);
  107. exit;
  108. break;
  109. }
  110. }
  111. $is_western_name_order = api_is_western_name_order();
  112. $sort_by_first_name = api_sort_by_first_name();
  113. // Build table
  114. $table = new SortableTable(
  115. 'subscribe_users',
  116. 'get_number_of_users',
  117. 'get_user_data',
  118. ($is_western_name_order xor $sort_by_first_name) ? 3 : 2
  119. );
  120. $parameters['keyword'] = $keyword;
  121. $parameters['type'] = $type;
  122. $table->set_additional_parameters($parameters);
  123. $col = 0;
  124. $table->set_header($col++, '', false);
  125. $table->set_header($col++, get_lang('OfficialCode'));
  126. if (api_is_western_name_order()) {
  127. $table->set_header($col++, get_lang('FirstName'));
  128. $table->set_header($col++, get_lang('LastName'));
  129. } else {
  130. $table->set_header($col++, get_lang('LastName'));
  131. $table->set_header($col++, get_lang('FirstName'));
  132. }
  133. if (api_get_setting('show_email_addresses') == 'true') {
  134. $table->set_header($col++, get_lang('Email'));
  135. $table->set_column_filter($col - 1, 'email_filter');
  136. }
  137. $table->set_header($col++, get_lang('Active'), false);
  138. $table->set_column_filter($col - 1, 'active_filter');
  139. $table->set_header($col++, get_lang('Actions'), false);
  140. $table->set_column_filter($col - 1, 'reg_filter');
  141. $table->set_form_actions(array('subscribe' => get_lang('reg')), 'user');
  142. if (!empty($_POST['keyword'])) {
  143. $keyword_name = Security::remove_XSS($_POST['keyword']);
  144. echo '<br/>'.get_lang('SearchResultsFor').' <span style="font-style: italic ;"> '.$keyword_name.' </span><br>';
  145. }
  146. Display :: display_header($tool_name, "User");
  147. // Build search-form
  148. switch ($type) {
  149. case STUDENT:
  150. $url = api_get_path(WEB_CODE_PATH).'user/user.php?'.api_get_cidreq().'';
  151. break;
  152. case COURSEMANAGER:
  153. $url = api_get_path(WEB_CODE_PATH).'user/user.php?'.api_get_cidreq().'&type='.COURSEMANAGER;
  154. break;
  155. }
  156. $actionsLeft = '';
  157. $actionsLeft = Display::url(
  158. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM),
  159. $url
  160. );
  161. if (isset($_GET['subscribe_user_filter_value']) && !empty($_GET['subscribe_user_filter_value'])) {
  162. $actionsLeft .= '<a href="subscribe_user.php?type='.$type.'">'.
  163. Display::return_icon('clean_group.gif').' '.get_lang('ClearFilterResults').'</a>';
  164. }
  165. $extraForm = '';
  166. if (api_get_setting('ProfilingFilterAddingUsers') === 'true') {
  167. $extraForm = display_extra_profile_fields_filter();
  168. }
  169. // Build search-form
  170. $form = new FormValidator(
  171. 'search_user',
  172. 'get',
  173. api_get_self().'?'.api_get_cidreq(),
  174. '',
  175. null,
  176. FormValidator::LAYOUT_INLINE
  177. );
  178. $form->addText('keyword', '', false);
  179. $form->addElement('hidden', 'type', $type);
  180. $form->addElement('hidden', 'cidReq', api_get_course_id());
  181. $form->addButtonSearch(get_lang('Search'));
  182. echo Display::toolbarAction('toolbar-subscriber', [$actionsLeft, $extraForm, $form->returnForm()], [4, 4, 4]);
  183. $option = $type == COURSEMANAGER ? 2 : 1;
  184. echo UserManager::getUserSubscriptionTab($option);
  185. // Display table
  186. $table->display();
  187. Display::display_footer();
  188. /* SHOW LIST OF USERS */
  189. /**
  190. ** Get the users to display on the current page.
  191. */
  192. function get_number_of_users()
  193. {
  194. // Database table definition
  195. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  196. $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  197. $tbl_session_rel_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  198. $table_user_field_values = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  199. $courseCode = api_get_course_id();
  200. $sessionId = api_get_session_id();
  201. if (isset($_REQUEST['type']) && $_REQUEST['type'] === 'teacher') {
  202. if (api_get_session_id() != 0) {
  203. $sql = "SELECT COUNT(u.id)
  204. FROM $user_table u
  205. LEFT JOIN $tbl_session_rel_course_user cu
  206. ON
  207. u.user_id = cu.user_id AND
  208. c_id = '".api_get_course_int_id()."' AND
  209. session_id ='".$sessionId."'
  210. WHERE
  211. cu.user_id IS NULL AND
  212. u.status = 1 AND
  213. (u.official_code <> 'ADMIN' OR u.official_code IS NULL) ";
  214. if (api_is_multiple_url_enabled()) {
  215. $url_access_id = api_get_current_access_url_id();
  216. if ($url_access_id != -1) {
  217. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  218. $sql = "SELECT COUNT(u.id)
  219. FROM $user_table u
  220. LEFT JOIN $tbl_session_rel_course_user cu
  221. ON
  222. u.user_id = cu.user_id AND cu.c_id = '".api_get_course_int_id()."' AND
  223. session_id ='".$sessionId."'
  224. INNER JOIN $tbl_url_rel_user as url_rel_user
  225. ON (url_rel_user.user_id = u.user_id)
  226. WHERE
  227. cu.user_id IS NULL AND
  228. access_url_id= $url_access_id AND
  229. u.status = 1 AND
  230. (u.official_code <> 'ADMIN' OR u.official_code IS NULL)
  231. ";
  232. }
  233. }
  234. } else {
  235. $sql = "SELECT COUNT(u.id)
  236. FROM $user_table u
  237. LEFT JOIN $course_user_table cu
  238. ON u.user_id = cu.user_id and c_id='".api_get_course_int_id()."'
  239. WHERE cu.user_id IS NULL AND u.status<>".DRH." ";
  240. if (api_is_multiple_url_enabled()) {
  241. $url_access_id = api_get_current_access_url_id();
  242. if ($url_access_id != -1) {
  243. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  244. $sql = "SELECT COUNT(u.id)
  245. FROM $user_table u
  246. LEFT JOIN $course_user_table cu
  247. ON u.user_id = cu.user_id AND c_id='".api_get_course_int_id()."'
  248. INNER JOIN $tbl_url_rel_user as url_rel_user
  249. ON (url_rel_user.user_id = u.user_id)
  250. WHERE cu.user_id IS NULL AND u.status<>".DRH." AND access_url_id= $url_access_id ";
  251. }
  252. }
  253. }
  254. } else {
  255. // students
  256. if ($sessionId != 0) {
  257. $sql = "SELECT COUNT(u.id)
  258. FROM $user_table u
  259. LEFT JOIN $tbl_session_rel_course_user cu
  260. ON
  261. u.user_id = cu.user_id AND
  262. c_id='".api_get_course_int_id()."' AND
  263. session_id ='".$sessionId."'
  264. WHERE
  265. cu.user_id IS NULL AND
  266. u.status<>".DRH." AND
  267. (u.official_code <> 'ADMIN' OR u.official_code IS NULL) ";
  268. if (api_is_multiple_url_enabled()) {
  269. $url_access_id = api_get_current_access_url_id();
  270. if ($url_access_id != -1) {
  271. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  272. $sql = "SELECT COUNT(u.id)
  273. FROM $user_table u
  274. LEFT JOIN $tbl_session_rel_course_user cu
  275. ON
  276. u.user_id = cu.user_id AND
  277. c_id='".api_get_course_int_id()."' AND
  278. session_id ='".$sessionId."'
  279. INNER JOIN $tbl_url_rel_user as url_rel_user
  280. ON (url_rel_user.user_id = u.id)
  281. WHERE
  282. cu.user_id IS NULL AND
  283. u.status<>".DRH." AND
  284. access_url_id= $url_access_id AND
  285. (u.official_code <> 'ADMIN' OR u.official_code IS NULL) ";
  286. }
  287. }
  288. } else {
  289. $sql = "SELECT COUNT(u.id)
  290. FROM $user_table u
  291. LEFT JOIN $course_user_table cu
  292. ON u.user_id = cu.user_id AND c_id='".api_get_course_int_id()."'";
  293. // we change the SQL when we have a filter
  294. if (isset($_GET['subscribe_user_filter_value']) &&
  295. !empty($_GET['subscribe_user_filter_value']) &&
  296. api_get_setting('ProfilingFilterAddingUsers') === 'true'
  297. ) {
  298. $field_identification = explode('*', $_GET['subscribe_user_filter_value']);
  299. $sql .= "
  300. LEFT JOIN $table_user_field_values field_values
  301. ON field_values.item_id = u.user_id
  302. WHERE
  303. cu.user_id IS NULL AND
  304. u.status <> ".DRH." AND
  305. field_values.field_id = '".intval($field_identification[0])."' AND
  306. field_values.value = '".Database::escape_string($field_identification[1])."'
  307. ";
  308. } else {
  309. $sql .= "WHERE cu.user_id IS NULL AND u.status <> ".DRH." ";
  310. }
  311. if (api_is_multiple_url_enabled()) {
  312. $url_access_id = api_get_current_access_url_id();
  313. if ($url_access_id != -1) {
  314. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  315. $sql = "SELECT COUNT(u.id)
  316. FROM $user_table u
  317. LEFT JOIN $course_user_table cu
  318. ON u.user_id = cu.user_id AND c_id='".api_get_course_int_id()."'
  319. INNER JOIN $tbl_url_rel_user as url_rel_user
  320. ON (url_rel_user.user_id = u.id)
  321. WHERE cu.user_id IS NULL AND access_url_id= $url_access_id AND u.status <> ".DRH." ";
  322. }
  323. }
  324. }
  325. }
  326. // when there is a keyword then we are searching and we have to change the SQL statement
  327. if (isset($_GET['keyword']) && !empty($_GET['keyword'])) {
  328. $keyword = Database::escape_string(trim($_REQUEST['keyword']));
  329. $sql .= " AND (
  330. firstname LIKE '%".$keyword."%' OR
  331. lastname LIKE '%".$keyword."%' OR
  332. email LIKE '%".$keyword."%' OR
  333. username LIKE '%".$keyword."%' OR
  334. official_code LIKE '%".$keyword."%'
  335. )";
  336. // we also want to search for users who have something in their profile fields that matches the keyword
  337. if (api_get_setting('ProfilingFilterAddingUsers') === 'true') {
  338. $additional_users = search_additional_profile_fields($keyword);
  339. }
  340. // getting all the users of the course (to make sure that we do not display users that are already in the course)
  341. if (!empty($sessionId)) {
  342. $a_course_users = CourseManager:: get_user_list_from_course_code(
  343. $courseCode,
  344. $sessionId
  345. );
  346. } else {
  347. $a_course_users = CourseManager:: get_user_list_from_course_code(
  348. $courseCode,
  349. 0
  350. );
  351. }
  352. foreach ($a_course_users as $user_id => $course_user) {
  353. $users_of_course[] = $course_user['user_id'];
  354. }
  355. }
  356. $sql .= " AND u.status <> ".ANONYMOUS." ";
  357. $res = Database::query($sql);
  358. $count_user = 0;
  359. if ($res) {
  360. $row = Database::fetch_row($res);
  361. $count_user = $row[0];
  362. }
  363. return $count_user;
  364. }
  365. /**
  366. * Get the users to display on the current page.
  367. */
  368. function get_user_data($from, $number_of_items, $column, $direction)
  369. {
  370. $url_access_id = api_get_current_access_url_id();
  371. $course_code = api_get_course_id();
  372. $sessionId = api_get_session_id();
  373. $courseId = api_get_course_int_id();
  374. // Database table definitions
  375. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  376. $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  377. $tbl_session_rel_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  378. $table_user_field_values = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  379. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  380. // adding teachers
  381. $is_western_name_order = api_is_western_name_order();
  382. if (api_get_setting('show_email_addresses') === 'true') {
  383. $select_fields = "u.id AS col0,
  384. u.official_code AS col1,
  385. ".($is_western_name_order
  386. ? "u.firstname AS col2,
  387. u.lastname AS col3,"
  388. : "u.lastname AS col2,
  389. u.firstname AS col3,")."
  390. u.email AS col4,
  391. u.active AS col5,
  392. u.user_id AS col6";
  393. } else {
  394. $select_fields = "u.user_id AS col0,
  395. u.official_code AS col1,
  396. ".($is_western_name_order
  397. ? "u.firstname AS col2,
  398. u.lastname AS col3,"
  399. : "u.lastname AS col2,
  400. u.firstname AS col3,")."
  401. u.active AS col4,
  402. u.user_id AS col5";
  403. }
  404. if (isset($_REQUEST['type']) && $_REQUEST['type'] == COURSEMANAGER) {
  405. // adding a teacher through a session
  406. if (!empty($sessionId)) {
  407. $sql = "SELECT $select_fields
  408. FROM $user_table u
  409. LEFT JOIN $tbl_session_rel_course_user cu
  410. ON
  411. u.user_id = cu.user_id AND
  412. c_id ='".$courseId."' AND
  413. session_id ='".$sessionId."'
  414. INNER JOIN $tbl_url_rel_user as url_rel_user
  415. ON (url_rel_user.user_id = u.user_id) ";
  416. // applying the filter of the additional user profile fields
  417. if (isset($_GET['subscribe_user_filter_value']) &&
  418. !empty($_GET['subscribe_user_filter_value']) &&
  419. api_get_setting('ProfilingFilterAddingUsers') == 'true'
  420. ) {
  421. $field_identification = explode('*', $_GET['subscribe_user_filter_value']);
  422. $sql .= "
  423. LEFT JOIN $table_user_field_values field_values
  424. ON field_values.item_id = u.user_id
  425. WHERE
  426. cu.user_id IS NULL AND
  427. u.status = 1 AND
  428. (u.official_code <> 'ADMIN' OR u.official_code IS NULL) AND
  429. field_values.field_id = '".intval($field_identification[0])."' AND
  430. field_values.value = '".Database::escape_string($field_identification[1])."'";
  431. } else {
  432. $sql .= "WHERE cu.user_id IS NULL AND u.status=1 AND (u.official_code <> 'ADMIN' OR u.official_code IS NULL) ";
  433. }
  434. $sql .= " AND access_url_id = $url_access_id";
  435. } else {
  436. // adding a teacher NOT through a session
  437. $sql = "SELECT $select_fields
  438. FROM $user_table u
  439. LEFT JOIN $course_user_table cu
  440. ON u.user_id = cu.user_id AND c_id = '".$courseId."'";
  441. // applying the filter of the additional user profile fields
  442. if (isset($_GET['subscribe_user_filter_value']) &&
  443. !empty($_GET['subscribe_user_filter_value']) &&
  444. api_get_setting('ProfilingFilterAddingUsers') == 'true'
  445. ) {
  446. $field_identification = explode('*', $_GET['subscribe_user_filter_value']);
  447. $sql .= "
  448. LEFT JOIN $table_user_field_values field_values
  449. ON field_values.item_id = u.user_id
  450. WHERE
  451. cu.user_id IS NULL AND u.status<>".DRH." AND
  452. field_values.field_id = '".intval($field_identification[0])."' AND
  453. field_values.value = '".Database::escape_string($field_identification[1])."'";
  454. } else {
  455. $sql .= "WHERE cu.user_id IS NULL AND u.status<>".DRH." ";
  456. }
  457. // adding a teacher NOT trough a session on a portal with multiple URLs
  458. if (api_is_multiple_url_enabled()) {
  459. if ($url_access_id != -1) {
  460. $sql = "SELECT $select_fields
  461. FROM $user_table u
  462. LEFT JOIN $course_user_table cu
  463. ON u.user_id = cu.user_id and c_id='".$courseId."'
  464. INNER JOIN $tbl_url_rel_user as url_rel_user
  465. ON (url_rel_user.user_id = u.user_id) ";
  466. // applying the filter of the additional user profile fields
  467. if (isset($_GET['subscribe_user_filter_value']) &&
  468. !empty($_GET['subscribe_user_filter_value']) &&
  469. api_get_setting('ProfilingFilterAddingUsers') == 'true'
  470. ) {
  471. $field_identification = explode('*', $_GET['subscribe_user_filter_value']);
  472. $sql .= "
  473. LEFT JOIN $table_user_field_values field_values
  474. ON field_values.item_id = u.user_id
  475. WHERE
  476. cu.user_id IS NULL AND
  477. u.status<>".DRH." AND
  478. field_values.field_id = '".intval($field_identification[0])."' AND
  479. field_values.value = '".Database::escape_string($field_identification[1])."'";
  480. } else {
  481. $sql .= "WHERE cu.user_id IS NULL AND u.status<>".DRH." AND access_url_id= $url_access_id ";
  482. }
  483. }
  484. }
  485. }
  486. } else {
  487. // adding a student
  488. if (!empty($sessionId)) {
  489. $sql = "SELECT $select_fields
  490. FROM $user_table u
  491. LEFT JOIN $tbl_session_rel_course_user cu
  492. ON
  493. u.user_id = cu.user_id AND
  494. c_id ='".$courseId."' AND
  495. session_id ='".$sessionId."' ";
  496. if (api_is_multiple_url_enabled()) {
  497. $sql .= " INNER JOIN $tbl_url_rel_user as url_rel_user ON (url_rel_user.user_id = u.user_id) ";
  498. }
  499. // applying the filter of the additional user profile fields
  500. if (isset($_GET['subscribe_user_filter_value']) &&
  501. !empty($_GET['subscribe_user_filter_value'])
  502. ) {
  503. $field_identification = explode('*', $_GET['subscribe_user_filter_value']);
  504. $sql .= "
  505. LEFT JOIN $table_user_field_values field_values
  506. ON field_values.item_id = u.user_id
  507. WHERE
  508. cu.user_id IS NULL AND
  509. u.status<>".DRH." AND
  510. (u.official_code <> 'ADMIN' OR u.official_code IS NULL) AND
  511. field_values.field_id = '".intval($field_identification[0])."' AND
  512. field_values.value = '".Database::escape_string($field_identification[1])."'";
  513. } else {
  514. $sql .= "WHERE
  515. cu.user_id IS NULL AND
  516. u.status<>".DRH." AND
  517. (u.official_code <> 'ADMIN' OR u.official_code IS NULL) ";
  518. }
  519. if (api_is_multiple_url_enabled()) {
  520. $sql .= "AND access_url_id = $url_access_id";
  521. }
  522. } else {
  523. $sql = "SELECT $select_fields
  524. FROM $user_table u
  525. LEFT JOIN $course_user_table cu
  526. ON
  527. u.user_id = cu.user_id AND
  528. c_id ='".$courseId."'";
  529. // applying the filter of the additional user profile fields
  530. if (isset($_GET['subscribe_user_filter_value']) && !empty($_GET['subscribe_user_filter_value'])) {
  531. $field_identification = explode('*', $_GET['subscribe_user_filter_value']);
  532. $sql .= "
  533. LEFT JOIN $table_user_field_values field_values
  534. ON field_values.item_id = u.user_id
  535. WHERE
  536. cu.user_id IS NULL AND
  537. u.status<>".DRH." AND
  538. field_values.field_id = '".intval($field_identification[0])."' AND
  539. field_values.value = '".Database::escape_string($field_identification[1])."'";
  540. } else {
  541. $sql .= "WHERE cu.user_id IS NULL AND u.status<>".DRH." ";
  542. }
  543. //showing only the courses of the current Chamilo access_url_id
  544. if (api_is_multiple_url_enabled()) {
  545. if ($url_access_id != -1) {
  546. $sql = "SELECT $select_fields
  547. FROM $user_table u
  548. LEFT JOIN $course_user_table cu
  549. ON u.user_id = cu.user_id AND c_id='".$courseId."'
  550. INNER JOIN $tbl_url_rel_user as url_rel_user
  551. ON (url_rel_user.user_id = u.user_id) ";
  552. // applying the filter of the additional user profile fields
  553. if (isset($_GET['subscribe_user_filter_value']) &&
  554. !empty($_GET['subscribe_user_filter_value']) &&
  555. api_get_setting('ProfilingFilterAddingUsers') == 'true'
  556. ) {
  557. $field_identification = explode('*', $_GET['subscribe_user_filter_value']);
  558. $sql .= "
  559. LEFT JOIN $table_user_field_values field_values
  560. ON field_values.item_id = u.user_id
  561. WHERE
  562. cu.user_id IS NULL AND
  563. u.status<>".DRH." AND
  564. field_values.field_id = '".intval($field_identification[0])."' AND
  565. field_values.value = '".Database::escape_string($field_identification[1])."' AND
  566. access_url_id = $url_access_id
  567. ";
  568. } else {
  569. $sql .= "WHERE cu.user_id IS NULL AND u.status<>".DRH." AND access_url_id= $url_access_id ";
  570. }
  571. }
  572. }
  573. }
  574. }
  575. // adding additional WHERE statements to the SQL for the search functionality
  576. if (isset($_REQUEST['keyword'])) {
  577. $keyword = Database::escape_string(trim($_REQUEST['keyword']));
  578. $sql .= " AND (
  579. firstname LIKE '%".$keyword."%' OR
  580. lastname LIKE '%".$keyword."%' OR
  581. email LIKE '%".$keyword."%' OR
  582. username LIKE '%".$keyword."%' OR
  583. official_code LIKE '%".$keyword."%'
  584. )
  585. ";
  586. if (api_get_setting('ProfilingFilterAddingUsers') === 'true') {
  587. // we also want to search for users who have something in
  588. // their profile fields that matches the keyword
  589. $additional_users = search_additional_profile_fields($keyword);
  590. }
  591. // getting all the users of the course (to make sure that we do not
  592. // display users that are already in the course)
  593. if (!empty($sessionId)) {
  594. $a_course_users = CourseManager :: get_user_list_from_course_code($course_code, $sessionId);
  595. } else {
  596. $a_course_users = CourseManager :: get_user_list_from_course_code($course_code, 0);
  597. }
  598. foreach ($a_course_users as $user_id => $course_user) {
  599. $users_of_course[] = $course_user['user_id'];
  600. }
  601. }
  602. $sql .= " AND u.status != ".ANONYMOUS." ";
  603. // Sorting and pagination (used by the sortable table)
  604. $sql .= " ORDER BY col$column $direction ";
  605. $from = (int) $from;
  606. $number_of_items = (int) $number_of_items;
  607. $sql .= " LIMIT $from, $number_of_items";
  608. $res = Database::query($sql);
  609. $users = array();
  610. while ($user = Database::fetch_row($res)) {
  611. $users[] = $user;
  612. }
  613. return $users;
  614. }
  615. /**
  616. * Returns a mailto-link
  617. * @param string $email An email-address
  618. * @return string HTML-code with a mailto-link
  619. */
  620. function email_filter($email)
  621. {
  622. return Display :: encrypted_mailto_link($email, $email);
  623. }
  624. /**
  625. * Build the reg-column of the table
  626. * @param int $user_id The user id
  627. * @return string Some HTML-code
  628. */
  629. function reg_filter($user_id)
  630. {
  631. if (isset($_REQUEST['type']) && $_REQUEST['type'] == COURSEMANAGER) {
  632. $type = COURSEMANAGER;
  633. } else {
  634. $type = STUDENT;
  635. }
  636. $user_id = (int) $user_id;
  637. $result = '<a class="btn btn-small btn-primary" href="'.api_get_self().'?'.api_get_cidreq().'&register=yes&type='.$type.'&user_id='.$user_id.'">'.
  638. get_lang("reg").'</a>';
  639. return $result;
  640. }
  641. /**
  642. * Build the active-column of the table to lock or unlock a certain user
  643. * lock = the user can no longer use this account
  644. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  645. * @param int $active the current state of the account
  646. * @param string $url_params
  647. * @return string Some HTML-code with the lock/unlock button
  648. */
  649. function active_filter($active, $url_params, $row)
  650. {
  651. $_user = api_get_user_info();
  652. if ($active == '1') {
  653. $action = 'AccountActive';
  654. $image = 'accept';
  655. }
  656. if ($active == '0') {
  657. $action = 'AccountInactive';
  658. $image = 'error';
  659. }
  660. $result = '';
  661. if ($row['0'] <> $_user['user_id']) {
  662. // you cannot lock yourself out otherwise you could disable all the accounts
  663. // including your own => everybody is locked out and nobody can change it anymore.
  664. $result = Display::return_icon(
  665. $image.'.png',
  666. get_lang(ucfirst($action)),
  667. array(),
  668. ICON_SIZE_TINY
  669. );
  670. }
  671. return $result;
  672. }
  673. /**
  674. * Search the additional user profile fields defined by the platform administrator in
  675. * platform administration > profiling for a given keyword.
  676. * We not only search in the predefined options but also in the input fields wherer
  677. * the user can enter some text.
  678. *
  679. * For this we get the additional profile field options that match the (search) keyword,
  680. * then we find all the users who have entered the (search)keyword in a input field of the
  681. * additional profile fields or have chosen one of the matching predefined options
  682. *
  683. * @param string $keyword a keyword we are looking for in the additional profile fields
  684. * @return array $additional_users an array with the users who have an additional profile field that matches the keyword
  685. */
  686. function search_additional_profile_fields($keyword)
  687. {
  688. // database table definitions
  689. $table_user_field_options = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
  690. $table_user_field_values = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  691. $tableExtraField = Database::get_main_table(TABLE_EXTRA_FIELD);
  692. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  693. $keyword = Database::escape_string($keyword);
  694. // getting the field option text that match this keyword (for radio buttons and checkboxes)
  695. $sql = "SELECT * FROM $table_user_field_options
  696. WHERE display_text LIKE '%".$keyword."%'";
  697. $result_profiling = Database::query($sql);
  698. while ($profiling_field_options = Database::fetch_array($result_profiling)) {
  699. $profiling_field_options_exact_values[] = $profiling_field_options;
  700. }
  701. $profiling_field_options_exact_values_sql = '';
  702. foreach ($profiling_field_options_exact_values as $profilingkey => $profilingvalue) {
  703. $profiling_field_options_exact_values_sql .= " OR (field_id = '".$profilingvalue['field_id']."' AND value='".$profilingvalue['option_value']."') ";
  704. }
  705. $extraFieldType = ExtraField::USER_FIELD_TYPE;
  706. // getting all the user ids of the users who have chosen on of the predefined fields that contain the keyword
  707. // or all the users who have entered the keyword in a free-form field
  708. $sql = "SELECT
  709. user.user_id as col0,
  710. user.official_code as col1,
  711. user.lastname as col2,
  712. user.firstname as col3,
  713. user.email as col4,
  714. user.active as col5,
  715. user.user_id as col6
  716. FROM $table_user user, $table_user_field_values user_values, $tableExtraField e
  717. WHERE
  718. user.user_id = user_values.item_id AND
  719. user_values.field_id = e.id AND
  720. e.extra_field_type = $extraFieldType AND
  721. (value LIKE '%".$keyword."%'".$profiling_field_options_exact_values_sql.")";
  722. $result = Database::query($sql);
  723. $additional_users = array();
  724. while ($profiled_users = Database::fetch_array($result)) {
  725. $additional_users[$profiled_users['col0']] = $profiled_users;
  726. }
  727. return $additional_users;
  728. }
  729. /**
  730. * This function displays a dropdown list with all the additional user
  731. * profile fields defined by the platform administrator in
  732. * platform administration > profiling.
  733. * Only the fields that have predefined fields are usefull for such a filter.
  734. *
  735. */
  736. function display_extra_profile_fields_filter()
  737. {
  738. // getting all the additional user profile fields
  739. $extra = UserManager::get_extra_fields(0, 50, 5, 'ASC');
  740. $return = '<option value="">'.get_lang('SelectFilter').'</option>';
  741. // looping through the additional user profile fields
  742. foreach ($extra as $id => $field_details) {
  743. // $field_details[2] contains the type of the additional user profile field
  744. switch ($field_details[2]) {
  745. // text fields cannot be used as a filter
  746. case ExtraFieldModel::FIELD_TYPE_TEXT:
  747. break;
  748. // text area fields cannot be used as a filter
  749. case ExtraFieldModel::FIELD_TYPE_TEXTAREA:
  750. break;
  751. case ExtraFieldModel::FIELD_TYPE_RADIO:
  752. case ExtraFieldModel::FIELD_TYPE_SELECT:
  753. case ExtraFieldModel::FIELD_TYPE_SELECT_MULTIPLE:
  754. $return .= '<optgroup label="'.$field_details[3].'">';
  755. foreach ($field_details[9] as $option_id => $option_details) {
  756. if (isset($_GET['subscribe_user_filter_value']) &&
  757. $_GET['subscribe_user_filter_value'] == $field_details[0].'*'.$option_details[1]
  758. ) {
  759. $selected = 'selected="selected"';
  760. } else {
  761. $selected = false;
  762. }
  763. $return .= '<option value="'.$field_details[0].'*'.$option_details[1].'" '.$selected.'>'.$option_details[2].'</option>';
  764. }
  765. $return .= '</optgroup>';
  766. break;
  767. }
  768. }
  769. $html = '<form class="form-inline" id="subscribe_user_filter" name="subscribe_user_filter" method="get" action="'.api_get_self().'?'.api_get_cidreq().'">';
  770. $html .= '<input type="hidden" name="type" id="type" value="'.Security::remove_XSS($_REQUEST['type']).'" />';
  771. $html .= '<select name="subscribe_user_filter_value" id="subscribe_user_filter_value">'.$return.'</select>';
  772. $html .= '<button type="submit" name="submit_filter" id="submit_filter" value="" class="search">'.get_lang('Filter').'</button>';
  773. $html .= '</form>';
  774. return $html;
  775. }