subscribe_user.php 31 KB

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