subscribe_user.php 31 KB

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