subscribe_user.php 28 KB

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