subscribe_user.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. /**
  9. * Code
  10. */
  11. // name of the language file that needs to be included
  12. $language_file = array('registration','admin');
  13. // including the global Dokeos file
  14. require_once '../inc/global.inc.php';
  15. // the section (for the tabs)
  16. $this_section = SECTION_COURSES;
  17. // access restriction
  18. if (!api_is_allowed_to_edit()) {
  19. api_not_allowed(true);
  20. }
  21. $tool_name = get_lang("SubscribeUserToCourse");
  22. if ($_REQUEST['type']=='teacher') {
  23. $tool_name = get_lang("SubscribeUserToCourseAsTeacher");
  24. }
  25. //extra entries in breadcrumb
  26. $interbreadcrumb[] = array ("url" => "user.php", "name" => get_lang("ToolUser"));
  27. if ($_GET['keyword'])
  28. {
  29. $interbreadcrumb[] = array ("url" => "subscribe_user.php?type=".Security::remove_XSS($_GET['type']), "name" => $tool_name);
  30. $tool_name = get_lang('SearchResults');
  31. }
  32. Display :: display_header($tool_name, "User");
  33. // api_display_tool_title($tool_name);
  34. /*
  35. MAIN SECTION
  36. */
  37. $current_session_id = api_get_session_id();
  38. $list_register_user='';
  39. $list_not_register_user='';
  40. if (isset ($_REQUEST['register'])) {
  41. if (isset($_REQUEST['type']) && $_REQUEST['type']=='teacher') {
  42. if (!empty($current_session_id)) {
  43. $result_simple_sub = SessionManager::set_coach_to_course_session(intval($_REQUEST['user_id']), $current_session_id, $_course['sysCode']);
  44. } else {
  45. $result_simple_sub = CourseManager :: subscribe_user(intval($_REQUEST['user_id']), $_course['sysCode'],COURSEMANAGER);
  46. }
  47. } else {
  48. $result_simple_sub=CourseManager :: subscribe_user(intval($_REQUEST['user_id']), $_course['sysCode']);
  49. }
  50. $user_id_temp=$_SESSION['session_user_id'];
  51. if (is_array($user_id_temp)) {
  52. $counter = count($user_id_temp);
  53. for ($j=0; $j<$counter;$j++) {
  54. if ($user_id_temp[$j]==$_GET['user_id']) {
  55. if ($result_simple_sub) {
  56. Display::display_confirmation_message($_SESSION['session_user_name'][$j].' '.get_lang('langAddedToCourse'));
  57. } else {
  58. Display::display_error_message($_SESSION['session_user_name'][$j].' '.get_lang('NotAddedToCourse'));
  59. }
  60. }
  61. }
  62. unset($_SESSION['session_user_id']);
  63. unset($_SESSION['session_user_name']);
  64. }
  65. }
  66. if (isset ($_POST['action'])) {
  67. switch ($_POST['action']) {
  68. case 'subscribe' :
  69. if (is_array($_POST['user'])) {
  70. foreach ($_POST['user'] as $index => $user_id) {
  71. $user_id=intval($user_id);
  72. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher') {
  73. if (!empty($current_session_id)) {
  74. $is_suscribe[] = SessionManager::set_coach_to_course_session($user_id, $current_session_id, $_course['sysCode']);
  75. } else {
  76. $is_suscribe[]=CourseManager :: subscribe_user($user_id, $_course['sysCode'],COURSEMANAGER);
  77. }
  78. } else {
  79. $is_suscribe[]=CourseManager :: subscribe_user($user_id, $_course['sysCode']);
  80. }
  81. $is_suscribe_user_id[]=$user_id;
  82. }
  83. }
  84. $user_id_temp=$_SESSION['session_user_id'];
  85. $user_name_temp=$_SESSION['session_user_name'];
  86. unset($_SESSION['session_user_id']);
  87. unset($_SESSION['session_user_name']);
  88. $counter=0;
  89. $is_suscribe_counter=count($is_suscribe_user_id);
  90. $list_register_user='';
  91. //if ($$is_suscribe_counter!=1) {
  92. for ($i=0; $i<$is_suscribe_counter;$i++) {
  93. for ($j=0; $j<count($user_id_temp);$j++) {
  94. if ($is_suscribe_user_id[$i]==$user_id_temp[$j]) {
  95. if ($is_suscribe[$i]) {
  96. $list_register_user.=" - ".$user_name_temp[$j].'<br/>';
  97. $temp_unique_user=$user_name_temp[$j];
  98. $counter++;
  99. } else {
  100. $list_not_register_user.=" - ".$user_name_temp[$j].'<br/>';
  101. }
  102. }
  103. }
  104. }
  105. //} else {
  106. //$list_register_user=$temp_unique_user; // only 1 user register
  107. //}
  108. if (!empty($list_register_user)) {
  109. if ($is_suscribe_counter==1) {
  110. $register_user_message=$temp_unique_user.' '.get_lang('langAddedToCourse');
  111. Display::display_confirmation_message($register_user_message,false);
  112. } else {
  113. $register_user_message='<br />'.get_lang('UsersRegistered').'<br/><br />'.$list_register_user;
  114. Display::display_confirmation_message($register_user_message,false);
  115. }
  116. }
  117. if (!empty($list_not_register_user)) {
  118. $not_register_user_message='<br />'.get_lang('UsersNotRegistered').'<br/><br /><br />'.$list_not_register_user;
  119. Display::display_error_message($not_register_user_message,false);
  120. }
  121. break;
  122. }
  123. }
  124. if (!empty($_SESSION['session_user_id'])) {
  125. unset($_SESSION['session_user_id']);
  126. }
  127. if (!empty($_SESSION['session_user_name'])) {
  128. unset($_SESSION['session_user_name']);
  129. }
  130. /* SHOW LIST OF USERS */
  131. /**
  132. * * Get the users to display on the current page.
  133. */
  134. function get_number_of_users() {
  135. global $_configuration;
  136. // Database table definition
  137. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  138. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  139. $tbl_session_rel_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  140. $table_user_field_values = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  141. if (isset($_REQUEST['type']) && $_REQUEST['type']=='teacher') {
  142. if (api_get_session_id() != 0) {
  143. $sql = "SELECT u.user_id
  144. FROM $user_table u
  145. LEFT JOIN $tbl_session_rel_course_user cu on u.user_id = cu.id_user and course_code='".api_get_course_id()."' AND id_session ='".api_get_session_id()."'
  146. WHERE cu.id_user IS NULL AND u.status=1 AND (u.official_code <> 'ADMIN' OR u.official_code IS NULL) ";
  147. if ($_configuration['multiple_access_urls']) {
  148. $url_access_id = api_get_current_access_url_id();
  149. if ($url_access_id !=-1) {
  150. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  151. $sql = "SELECT u.user_id FROM $user_table u
  152. LEFT JOIN $tbl_session_rel_course_user cu on u.user_id = cu.id_user and course_code='".api_get_course_id()."' AND id_session ='".api_get_session_id()."'
  153. INNER JOIN $tbl_url_rel_user as url_rel_user
  154. ON (url_rel_user.user_id = u.user_id)
  155. WHERE cu.id_user IS NULL AND access_url_id= $url_access_id AND u.status=1 AND (u.official_code <> 'ADMIN' OR u.official_code IS NULL) ";
  156. }
  157. }
  158. } else {
  159. $sql = "SELECT u.user_id FROM $user_table u LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".api_get_course_id()."'
  160. WHERE cu.user_id IS NULL AND u.status<>".DRH." ";
  161. if ($_configuration['multiple_access_urls']) {
  162. $url_access_id = api_get_current_access_url_id();
  163. if ($url_access_id !=-1) {
  164. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  165. $sql = "SELECT u.user_id FROM $user_table u
  166. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id AND course_code='".api_get_course_id()."'
  167. INNER JOIN $tbl_url_rel_user as url_rel_user
  168. ON (url_rel_user.user_id = u.user_id)
  169. WHERE cu.user_id IS NULL AND u.status<>".DRH." AND access_url_id= $url_access_id ";
  170. }
  171. }
  172. }
  173. } else {
  174. // students
  175. if (api_get_session_id() != 0) {
  176. $sql = "SELECT u.user_id
  177. FROM $user_table u
  178. LEFT JOIN $tbl_session_rel_course_user cu on u.user_id = cu.id_user and course_code='".api_get_course_id()."' AND id_session ='".api_get_session_id()."'
  179. WHERE cu.id_user IS NULL AND u.status<>".DRH." AND (u.official_code <> 'ADMIN' OR u.official_code IS NULL) ";
  180. if ($_configuration['multiple_access_urls']) {
  181. $url_access_id = api_get_current_access_url_id();
  182. if ($url_access_id !=-1) {
  183. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  184. $sql = "SELECT u.user_id
  185. FROM $user_table u
  186. LEFT JOIN $tbl_session_rel_course_user cu on u.user_id = cu.id_user and course_code='".api_get_course_id()."' AND id_session ='".api_get_session_id()."'
  187. INNER JOIN $tbl_url_rel_user as url_rel_user
  188. ON (url_rel_user.user_id = u.user_id)
  189. WHERE cu.id_user IS NULL AND u.status<>".DRH." AND access_url_id= $url_access_id AND (u.official_code <> 'ADMIN' OR u.official_code IS NULL) ";
  190. }
  191. }
  192. } else {
  193. $sql = "SELECT u.user_id
  194. FROM $user_table u
  195. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'";
  196. // we change the SQL when we have a filter
  197. if (isset($_GET['subscribe_user_filter_value']) AND !empty($_GET['subscribe_user_filter_value']) AND api_get_setting('ProfilingFilterAddingUsers') == 'true'){
  198. $field_identification = explode('*',$_GET['subscribe_user_filter_value']);
  199. $sql .= "
  200. LEFT JOIN $table_user_field_values field_values
  201. ON field_values.user_id = u.user_id
  202. WHERE cu.user_id IS NULL AND u.status<>".DRH."
  203. AND field_values.field_id = '".Database::escape_string($field_identification[0])."'
  204. AND field_values.field_value = '".Database::escape_string($field_identification[1])."'";
  205. } else {
  206. $sql .= "WHERE cu.user_id IS NULL AND u.status<>".DRH." ";
  207. }
  208. if (isset($_configuration['multiple_access_urls']) && $_configuration['multiple_access_urls']) {
  209. $url_access_id = api_get_current_access_url_id();
  210. if ($url_access_id !=-1) {
  211. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  212. $sql = "SELECT u.user_id
  213. FROM $user_table u
  214. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  215. INNER JOIN $tbl_url_rel_user as url_rel_user
  216. ON (url_rel_user.user_id = u.user_id)
  217. WHERE cu.user_id IS NULL AND access_url_id= $url_access_id AND u.status<>".DRH." ";
  218. }
  219. }
  220. }
  221. }
  222. // when there is a keyword then we are searching and we have to change the SQL statement
  223. if (isset ($_GET['keyword']) AND !empty($_GET['keyword'])) {
  224. $keyword = Database::escape_string(trim($_REQUEST['keyword']));
  225. $sql .= " AND (firstname LIKE '%".$keyword."%' OR lastname LIKE '%".$keyword."%' OR email LIKE '%".$keyword."%' OR username LIKE '%".$keyword."%' OR official_code LIKE '%".$keyword."%')";
  226. // we also want to search for users who have something in their profile fields that matches the keyword
  227. if (api_get_setting('ProfilingFilterAddingUsers') == 'true') {
  228. $additional_users = search_additional_profile_fields($keyword);
  229. }
  230. // getting all the users of the course (to make sure that we do not display users that are already in the course)
  231. if (!empty($_SESSION["id_session"])) {
  232. $a_course_users = CourseManager :: get_user_list_from_course_code($_SESSION['_course']['id'], true, $_SESSION['id_session']);
  233. } else {
  234. $a_course_users = CourseManager :: get_user_list_from_course_code($_SESSION['_course']['id'], true);
  235. }
  236. foreach ($a_course_users as $user_id=>$course_user) {
  237. $users_of_course[] = $course_user['user_id'];
  238. }
  239. }
  240. $sql .=" AND u.status <> ".ANONYMOUS." ";
  241. $res = Database::query($sql);
  242. while ($user = Database::fetch_row($res)) {
  243. $users[] = $user[0];
  244. }
  245. $result = Database::num_rows($res);
  246. // we add 1 for every additional user (a user where the keyword matches one of the additional profile fields)
  247. // that is not yet in the course and not yet in the search result
  248. if (isset ($_REQUEST['keyword']) AND api_get_setting('ProfilingFilterAddingUsers') == 'true') {
  249. foreach($additional_users as $additional_user_key=>$additional_user_value){
  250. if (!in_array($additional_user_key,$users) AND !in_array($additional_user_key,$users_of_course)){
  251. $result++;
  252. }
  253. }
  254. }
  255. return $result;
  256. }
  257. /**
  258. * Get the users to display on the current page.
  259. */
  260. function get_user_data($from, $number_of_items, $column, $direction) {
  261. global $_course, $_configuration;;
  262. // Database table definitions
  263. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  264. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  265. $tbl_session_rel_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  266. $table_user_field_values = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  267. // adding teachers
  268. $is_western_name_order = api_is_western_name_order();
  269. if (isset($_REQUEST['type']) && $_REQUEST['type']=='teacher') {
  270. // adding a teacher through a session
  271. if (!empty($_SESSION["id_session"])) {
  272. $sql = "SELECT
  273. u.user_id AS col0,
  274. u.official_code AS col1,
  275. ".($is_western_name_order
  276. ? "u.firstname AS col2,
  277. u.lastname AS col3,"
  278. : "u.lastname AS col2,
  279. u.firstname AS col3,")."
  280. u.email AS col4,
  281. u.active AS col5,
  282. u.user_id AS col6
  283. FROM $user_table u
  284. LEFT JOIN $tbl_session_rel_course_user cu on u.user_id = cu.id_user AND course_code='".$_SESSION['_course']['id']."' AND id_session ='".$_SESSION["id_session"]."' ";
  285. // applying the filter of the additional user profile fields
  286. if (isset($_GET['subscribe_user_filter_value']) AND !empty($_GET['subscribe_user_filter_value']) AND api_get_setting('ProfilingFilterAddingUsers') == 'true'){
  287. $field_identification = explode('*',$_GET['subscribe_user_filter_value']);
  288. $sql .= "
  289. LEFT JOIN $table_user_field_values field_values
  290. ON field_values.user_id = u.user_id
  291. WHERE cu.id_user IS NULL AND u.status=1 AND (u.official_code <> 'ADMIN' OR u.official_code IS NULL)
  292. AND field_values.field_id = '".Database::escape_string($field_identification[0])."'
  293. AND field_values.field_value = '".Database::escape_string($field_identification[1])."'";
  294. } else {
  295. $sql .= "WHERE cu.id_user IS NULL AND u.status=1 AND (u.official_code <> 'ADMIN' OR u.official_code IS NULL) ";
  296. }
  297. } else {
  298. // adding a teacher NOT through a session
  299. $sql = "SELECT
  300. u.user_id AS col0,
  301. u.official_code AS col1,
  302. ".($is_western_name_order
  303. ? "u.firstname AS col2,
  304. u.lastname AS col3,"
  305. : "u.lastname AS col2,
  306. u.firstname AS col3,")."
  307. u.email AS col4,
  308. u.active AS col5,
  309. u.user_id AS col6
  310. FROM $user_table u
  311. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'";
  312. // applying the filter of the additional user profile fields
  313. if (isset($_GET['subscribe_user_filter_value']) AND !empty($_GET['subscribe_user_filter_value']) AND api_get_setting('ProfilingFilterAddingUsers') == 'true'){
  314. $field_identification = explode('*',$_GET['subscribe_user_filter_value']);
  315. $sql .= "
  316. LEFT JOIN $table_user_field_values field_values
  317. ON field_values.user_id = u.user_id
  318. WHERE cu.user_id IS NULL AND u.status<>".DRH."
  319. AND field_values.field_id = '".Database::escape_string($field_identification[0])."'
  320. AND field_values.field_value = '".Database::escape_string($field_identification[1])."'";
  321. } else {
  322. $sql .= "WHERE cu.user_id IS NULL AND u.status<>".DRH." ";
  323. }
  324. // adding a teacher NOT trough a session on a portal with multiple URLs
  325. if ($_configuration['multiple_access_urls']) {
  326. $url_access_id = api_get_current_access_url_id();
  327. if ($url_access_id !=-1) {
  328. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  329. $sql = "SELECT
  330. u.user_id AS col0,
  331. u.official_code AS col1,
  332. ".($is_western_name_order
  333. ? "u.firstname AS col2,
  334. u.lastname AS col3,"
  335. : "u.lastname AS col2,
  336. u.firstname AS col3,")."
  337. u.email AS col4,
  338. u.active AS col5,
  339. u.user_id AS col6
  340. FROM $user_table u
  341. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  342. INNER JOIN $tbl_url_rel_user as url_rel_user ON (url_rel_user.user_id = u.user_id) ";
  343. // applying the filter of the additional user profile fields
  344. if (isset($_GET['subscribe_user_filter_value']) AND !empty($_GET['subscribe_user_filter_value']) AND api_get_setting('ProfilingFilterAddingUsers') == 'true'){
  345. $field_identification = explode('*',$_GET['subscribe_user_filter_value']);
  346. $sql .= "
  347. LEFT JOIN $table_user_field_values field_values
  348. ON field_values.user_id = u.user_id
  349. WHERE cu.user_id IS NULL AND u.status<>".DRH."
  350. AND field_values.field_id = '".Database::escape_string($field_identification[0])."'
  351. AND field_values.field_value = '".Database::escape_string($field_identification[1])."'";
  352. } else {
  353. $sql .= "WHERE cu.user_id IS NULL AND u.status<>".DRH." AND access_url_id= $url_access_id ";
  354. }
  355. }
  356. }
  357. }
  358. } else {
  359. // adding a student
  360. if (!empty($_SESSION["id_session"])) {
  361. $sql = "SELECT
  362. u.user_id AS col0,
  363. u.official_code AS col1,
  364. ".($is_western_name_order
  365. ? "u.firstname AS col2,
  366. u.lastname AS col3,"
  367. : "u.lastname AS col2,
  368. u.firstname AS col3,")."
  369. u.email AS col4,
  370. u.active AS col5,
  371. u.user_id AS col6
  372. FROM $user_table u
  373. LEFT JOIN $tbl_session_rel_course_user cu on u.user_id = cu.id_user and course_code='".$_SESSION['_course']['id']."' AND id_session ='".$_SESSION["id_session"]."' ";
  374. // applying the filter of the additional user profile fields
  375. if (isset($_GET['subscribe_user_filter_value']) AND !empty($_GET['subscribe_user_filter_value'])){
  376. $field_identification = explode('*',$_GET['subscribe_user_filter_value']);
  377. $sql .= "
  378. LEFT JOIN $table_user_field_values field_values
  379. ON field_values.user_id = u.user_id
  380. WHERE cu.id_user IS NULL AND u.status<>".DRH." AND (u.official_code <> 'ADMIN' OR u.official_code IS NULL)
  381. AND field_values.field_id = '".Database::escape_string($field_identification[0])."'
  382. AND field_values.field_value = '".Database::escape_string($field_identification[1])."'";
  383. } else {
  384. $sql .= "WHERE cu.id_user IS NULL AND u.status<>".DRH." AND (u.official_code <> 'ADMIN' OR u.official_code IS NULL) ";
  385. }
  386. } else {
  387. $sql = "SELECT
  388. u.user_id AS col0,
  389. u.official_code AS col1,
  390. ".($is_western_name_order
  391. ? "u.firstname AS col2,
  392. u.lastname AS col3,"
  393. : "u.lastname AS col2,
  394. u.firstname AS col3,")."
  395. u.email AS col4,
  396. u.active AS col5,
  397. u.user_id AS col6
  398. FROM $user_table u
  399. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'";
  400. // applying the filter of the additional user profile fields
  401. if (isset($_GET['subscribe_user_filter_value']) AND !empty($_GET['subscribe_user_filter_value'])){
  402. $field_identification = explode('*',$_GET['subscribe_user_filter_value']);
  403. $sql .= "
  404. LEFT JOIN $table_user_field_values field_values
  405. ON field_values.user_id = u.user_id
  406. WHERE cu.user_id IS NULL AND u.status<>".DRH."
  407. AND field_values.field_id = '".Database::escape_string($field_identification[0])."'
  408. AND field_values.field_value = '".Database::escape_string($field_identification[1])."'";
  409. } else {
  410. $sql .= "WHERE cu.user_id IS NULL AND u.status<>".DRH." ";
  411. }
  412. //showing only the courses of the current Dokeos access_url_id
  413. global $_configuration;
  414. if (isset($_configuration['multiple_access_urls']) && $_configuration['multiple_access_urls']) {
  415. $url_access_id = api_get_current_access_url_id();
  416. if ($url_access_id !=-1) {
  417. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  418. $sql = "SELECT
  419. u.user_id AS col0,
  420. u.official_code AS col1,
  421. ".($is_western_name_order
  422. ? "u.firstname AS col2,
  423. u.lastname AS col3,"
  424. : "u.lastname AS col2,
  425. u.firstname AS col3,")."
  426. u.email AS col4,
  427. u.active AS col5,
  428. u.user_id AS col6
  429. FROM $user_table u
  430. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  431. INNER JOIN $tbl_url_rel_user as url_rel_user
  432. ON (url_rel_user.user_id = u.user_id) ";
  433. // applying the filter of the additional user profile fields
  434. if (isset($_GET['subscribe_user_filter_value']) AND !empty($_GET['subscribe_user_filter_value']) AND api_get_setting('ProfilingFilterAddingUsers') == 'true'){
  435. $field_identification = explode('*',$_GET['subscribe_user_filter_value']);
  436. $sql .= "
  437. LEFT JOIN $table_user_field_values field_values
  438. ON field_values.user_id = u.user_id
  439. WHERE cu.user_id IS NULL AND u.status<>".DRH."
  440. AND field_values.field_id = '".Database::escape_string($field_identification[0])."'
  441. AND field_values.field_value = '".Database::escape_string($field_identification[1])."' AND access_url_id= $url_access_id ";
  442. } else {
  443. $sql .= "WHERE cu.user_id IS NULL AND u.status<>".DRH." AND access_url_id= $url_access_id ";
  444. }
  445. }
  446. }
  447. }
  448. }
  449. // adding additional WHERE statements to the SQL for the search functionality
  450. if (isset ($_REQUEST['keyword'])) {
  451. $keyword = Database::escape_string(trim($_REQUEST['keyword']));
  452. $sql .= " AND (firstname LIKE '%".$keyword."%' OR lastname LIKE '%".$keyword."%' OR email LIKE '%".$keyword."%' OR username LIKE '%".$keyword."%' OR official_code LIKE '%".$keyword."%')";
  453. if (api_get_setting('ProfilingFilterAddingUsers') == 'true') {
  454. // we also want to search for users who have something in their profile fields that matches the keyword
  455. $additional_users = search_additional_profile_fields($keyword);
  456. }
  457. // getting all the users of the course (to make sure that we do not display users that are already in the course)
  458. if (!empty($_SESSION["id_session"])) {
  459. $a_course_users = CourseManager :: get_user_list_from_course_code($_SESSION['_course']['id'], true, $_SESSION['id_session']);
  460. } else {
  461. $a_course_users = CourseManager :: get_user_list_from_course_code($_SESSION['_course']['id'], true);
  462. }
  463. foreach ($a_course_users as $user_id=>$course_user) {
  464. $users_of_course[] = $course_user['user_id'];
  465. }
  466. }
  467. $sql .=" AND u.status != ".ANONYMOUS." ";
  468. // Sorting and pagination (used by the sortable table)
  469. $sql .= " ORDER BY col$column $direction ";
  470. $sql .= " LIMIT $from,$number_of_items";
  471. $res = Database::query($sql);
  472. $users = array ();
  473. while ($user = Database::fetch_row($res)) {
  474. $users[] = $user;
  475. $_SESSION['session_user_id'][] = $user[0];
  476. if ($is_western_name_order) {
  477. $_SESSION['session_user_name'][] = api_get_person_name($user[2], $user[3]);
  478. } else {
  479. $_SESSION['session_user_name'][] = api_get_person_name($user[3], $user[2]);
  480. }
  481. }
  482. // adding additional users based on the search on the additional profile fields
  483. if (isset ($_REQUEST['keyword'])){
  484. if (is_array($additional_users)) {
  485. foreach($additional_users as $additional_user_key=>$additional_user_value){
  486. if (!in_array($additional_user_key,$_SESSION['session_user_id']) AND !in_array($additional_user_key,$users_of_course)){
  487. $users[]= array($additional_user_value['col0'],$additional_user_value['col1'],$additional_user_value['col2'].'*',$additional_user_value['col3'].'*',$additional_user_value['col4'],$additional_user_value['col5'], $additional_user_value['col6']);
  488. }
  489. }
  490. }
  491. }
  492. return $users;
  493. }
  494. /**
  495. * Returns a mailto-link
  496. * @param string $email An email-address
  497. * @return string HTML-code with a mailto-link
  498. */
  499. function email_filter($email) {
  500. return Display :: encrypted_mailto_link($email, $email);
  501. }
  502. /**
  503. * Build the reg-column of the table
  504. * @param int $user_id The user id
  505. * @return string Some HTML-code
  506. */
  507. function reg_filter($user_id) {
  508. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher') $type='teacher'; else $type='student';
  509. $result = "<a href=\"".api_get_self()."?register=yes&amp;type=".$type."&amp;user_id=".$user_id."\">".get_lang("reg")."</a>";
  510. return $result;
  511. }
  512. /**
  513. * Build the active-column of the table to lock or unlock a certain user
  514. * lock = the user can no longer use this account
  515. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  516. * @param int $active the current state of the account
  517. * @param int $user_id The user id
  518. * @param string $url_params
  519. * @return string Some HTML-code with the lock/unlock button
  520. */
  521. function active_filter($active, $url_params, $row) {
  522. global $_user;
  523. if ($active=='1') {
  524. $action='AccountActive';
  525. $image='accept';
  526. }
  527. if ($active=='0') {
  528. $action='AccountInactive';
  529. $image='error';
  530. }
  531. if ($row['0']<>$_user['user_id']) { // you cannot lock yourself out otherwise you could disable all the accounts including your own => everybody is locked out and nobody can change it anymore.
  532. $result = '<center>'.Display::return_icon($image.'.png', get_lang(ucfirst($action)), array('border'=>'0', 'style'=>'vertical-align: middle;') , 16).'</center>';
  533. }
  534. return $result;
  535. }
  536. $is_western_name_order = api_is_western_name_order();
  537. $sort_by_first_name = api_sort_by_first_name();
  538. // Build search-form
  539. echo '<div class="actions">';
  540. if (isset($_GET['keyword'])) {
  541. $actions .= '<a href="subscribe_user.php?type='.Security::remove_XSS($_REQUEST['type']).'">'.Display::return_icon('clean_group.gif').' '.get_lang('ClearSearchResults').'</a>';
  542. }
  543. if (isset($_GET['subscribe_user_filter_value']) AND !empty($_GET['subscribe_user_filter_value'])) {
  544. $actions .= '<a href="subscribe_user.php?type='.Security::remove_XSS($_REQUEST['type']).'">'.Display::return_icon('clean_group.gif').' '.get_lang('ClearFilterResults').'</a>';
  545. }
  546. if (api_get_setting('ProfilingFilterAddingUsers') == 'true') {
  547. display_extra_profile_fields_filter();
  548. }
  549. // Build search-form
  550. $form = new FormValidator('search_user', 'get', '', '', null, false);
  551. $renderer = & $form->defaultRenderer();
  552. $renderer->setElementTemplate('<span>{element}</span> ');
  553. $form->add_textfield('keyword', '', false);
  554. $form->addElement('hidden', 'type', Security::remove_XSS($_REQUEST['type']));
  555. $form->addElement('style_submit_button', 'submit', get_lang('SearchButton'), 'class="search"');
  556. $form->addElement('static', 'additionalactions', null, $actions);
  557. $form->display();
  558. echo '</div>';
  559. // Build table
  560. $table = new SortableTable('subscribe_users', 'get_number_of_users', 'get_user_data', ($is_western_name_order xor $sort_by_first_name) ? 3 : 2);
  561. $parameters['keyword'] = Security::remove_XSS($_REQUEST['keyword']);
  562. $parameters ['type'] = Security::remove_XSS($_REQUEST['type']);
  563. $table->set_additional_parameters($parameters);
  564. $col = 0;
  565. $table->set_header($col ++, '', false);
  566. $table->set_header($col ++, get_lang('OfficialCode'));
  567. if (api_is_western_name_order()) {
  568. $table->set_header($col ++, get_lang('FirstName'));
  569. $table->set_header($col ++, get_lang('LastName'));
  570. } else {
  571. $table->set_header($col ++, get_lang('LastName'));
  572. $table->set_header($col ++, get_lang('FirstName'));
  573. }
  574. $table->set_header($col ++, get_lang('Email'));
  575. $table->set_column_filter($col -1, 'email_filter');
  576. $table->set_header($col ++, get_lang('Active'),false);
  577. $table->set_column_filter($col -1, 'active_filter');
  578. $table->set_header($col ++, get_lang('reg'), false);
  579. $table->set_column_filter($col -1, 'reg_filter');
  580. $table->set_form_actions(array ('subscribe' => get_lang('reg')), 'user');
  581. if (!empty($_POST['keyword'])) {
  582. $keyword_name=Security::remove_XSS($_POST['keyword']);
  583. echo '<br/>'.get_lang('SearchResultsFor').' <span style="font-style: italic ;"> '.$keyword_name.' </span><br>';
  584. }
  585. // Display table
  586. $table->display();
  587. // footer
  588. Display :: display_footer();
  589. /**
  590. * Search the additional user profile fields defined by the platform administrator in
  591. * platform administration > profiling for a given keyword.
  592. * We not only search in the predefined options but also in the input fields wherer
  593. * the user can enter some text.
  594. *
  595. * For this we get the additional profile field options that match the (search) keyword,
  596. * then we find all the users who have entered the (search)keyword in a input field of the
  597. * additional profile fields or have chosen one of the matching predefined options
  598. *
  599. * @param string $keyword a keyword we are looking for in the additional profile fields
  600. * @return array $additional_users an array with the users who have an additional profile field that matches the keyword
  601. */
  602. function search_additional_profile_fields($keyword)
  603. {
  604. // database table definitions
  605. $table_user_field_options = Database :: get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  606. $table_user_field_values = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  607. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  608. $table_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  609. $table_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  610. // getting the field option text that match this keyword (for radio buttons and checkboxes)
  611. $sql_profiling = "SELECT * FROM $table_user_field_options WHERE option_display_text LIKE '%".$keyword."%'";
  612. $result_profiling = Database::query($sql_profiling);
  613. while ($profiling_field_options = Database::fetch_array($result_profiling)) {
  614. $profiling_field_options_exact_values[] = $profiling_field_options;
  615. }
  616. foreach ($profiling_field_options_exact_values as $profilingkey=>$profilingvalue){
  617. $profiling_field_options_exact_values_sql .= "OR (field_id = '".$profilingvalue['field_id']."' AND field_value='".$profilingvalue['option_value']."') ";
  618. }
  619. // getting all the user ids of the users who have chosen on of the predefined fields that contain the keyword
  620. // or all the users who have entered the keyword in a free-form field
  621. $sql_profiling_values = "SELECT user.user_id as col0, user.official_code as col1, user.lastname as col2, user.firstname as col3, user.email as col4, user.active as col5, user.user_id as col6
  622. FROM $table_user user, $table_user_field_values user_values
  623. WHERE user.user_id = user_values.user_id
  624. AND ( field_value LIKE '%".$keyword."%'
  625. ".$profiling_field_options_exact_values_sql.")";
  626. $result_profiling_values = Database::query($sql_profiling_values);
  627. while ($profiled_users = Database::fetch_array($result_profiling_values)) {
  628. $additional_users[$profiled_users['col0']] = $profiled_users;
  629. }
  630. return $additional_users;
  631. }
  632. /**
  633. * This function displays a dropdown list with all the additional user profile fields defined by the platform administrator in
  634. * platform administration > profiling. Only the fields that have predefined fields are usefull for such a filter.
  635. *
  636. */
  637. function display_extra_profile_fields_filter()
  638. {
  639. // getting all the additional user profile fields
  640. $extra = UserManager::get_extra_fields(0,50,5,'ASC');
  641. $return='<option value="">'.get_lang('SelectFilter').'</option>';
  642. // looping through the additional user profile fields
  643. foreach($extra as $id => $field_details) {
  644. // $field_details[2] contains the type of the additional user profile field
  645. switch($field_details[2]) {
  646. // text fields cannot be used as a filter
  647. case USER_FIELD_TYPE_TEXT:
  648. break;
  649. // text area fields cannot be used as a filter
  650. case USER_FIELD_TYPE_TEXTAREA:
  651. break;
  652. case USER_FIELD_TYPE_RADIO:
  653. case USER_FIELD_TYPE_SELECT:
  654. case USER_FIELD_TYPE_SELECT_MULTIPLE:
  655. $return .= '<optgroup label="'.$field_details[3].'">';
  656. foreach($field_details[9] as $option_id => $option_details) {
  657. if ($_GET['subscribe_user_filter_value'] == $field_details[0].'*'.$option_details[1]) {
  658. $selected = 'selected="selected"';
  659. } else {
  660. $selected = false;
  661. }
  662. $return .= '<option value="'.$field_details[0].'*'.$option_details[1].'" '.$selected.'>'.$option_details[2].'</option>';
  663. }
  664. $return .= '</optgroup>';
  665. break;
  666. }
  667. }
  668. echo '<form id="subscribe_user_filter" name="subscribe_user_filter" method="get" action="'.api_get_self().'?api_get_cidreq" style="float:left;">';
  669. echo ' <input type="hidden" name="type" id="type" value="'.Security::remove_XSS($_REQUEST['type']).'" />';
  670. echo '<select name="subscribe_user_filter_value" id="subscribe_user_filter_value">'.$return.'</select>';
  671. echo '<button type="submit" name="submit_filter" id="submit_filter" value="" class="search">'.get_lang('Filter').'</button>';
  672. echo '</form>';
  673. }