subscribe_user.php 31 KB

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