subscribe_user.php 34 KB

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