subscribe_user.php 32 KB

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