subscribe_user.php 34 KB

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