subscribe_user.php 32 KB

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