subscribe_user.php 32 KB

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