subscribe_user.php 31 KB

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