subscribe_user.php 31 KB

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