subscribe_user.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. include ("../inc/global.inc.php");
  33. $this_section = SECTION_COURSES;
  34. if (!api_is_allowed_to_edit()) {
  35. api_not_allowed(true);
  36. }
  37. require_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  38. require_once (api_get_path(LIBRARY_PATH).'sortabletable.class.php');
  39. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  40. /*
  41. ==============================================================================
  42. MAIN CODE
  43. ==============================================================================
  44. */
  45. /*
  46. -----------------------------------------------------------
  47. Header
  48. -----------------------------------------------------------
  49. */
  50. $tool_name = get_lang("SubscribeUserToCourse");
  51. if ($_REQUEST['type']=='teacher') {
  52. $tool_name = get_lang("SubscribeUserToCourseAsTeacher");
  53. }
  54. //extra entries in breadcrumb
  55. $interbreadcrumb[] = array ("url" => "user.php", "name" => get_lang("Users"));
  56. if ($_POST['keyword'])
  57. {
  58. $interbreadcrumb[] = array ("url" => "subscribe_user.php?type=".Security::remove_XSS($_GET['type']), "name" => $tool_name);
  59. $tool_name = get_lang('SearchResults');
  60. }
  61. Display :: display_header($tool_name, "User");
  62. // api_display_tool_title($tool_name);
  63. /*
  64. ==============================================================================
  65. MAIN SECTION
  66. ==============================================================================
  67. */
  68. $list_register_user='';
  69. $list_not_register_user='';
  70. if (isset ($_REQUEST['register'])) {
  71. if (isset($_REQUEST['type']) && $_REQUEST['type']=='teacher') {
  72. $result_simple_sub=CourseManager :: subscribe_user(Database::escape_string($_REQUEST['user_id']), $_course['sysCode'],COURSEMANAGER);
  73. } else {
  74. $result_simple_sub=CourseManager :: subscribe_user(Database::escape_string($_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('langAddedToCourse'));
  83. } else {
  84. Display::display_error_message($_SESSION['session_user_name'][$j].' '.get_lang('langNotAddedToCourse'));
  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=Database::escape_string($user_id);
  98. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher') {
  99. $is_suscribe[]=CourseManager :: subscribe_user($user_id, $_course['sysCode'],COURSEMANAGER);
  100. } else {
  101. $is_suscribe[]=CourseManager :: subscribe_user($user_id, $_course['sysCode']);
  102. }
  103. $is_suscribe_user_id[]=$user_id;
  104. }
  105. }
  106. $user_id_temp=$_SESSION['session_user_id'];
  107. $user_name_temp=$_SESSION['session_user_name'];
  108. unset($_SESSION['session_user_id']);
  109. unset($_SESSION['session_user_name']);
  110. $counter=0;
  111. $$is_suscribe_counter=count($is_suscribe_user_id);
  112. $list_register_user='';
  113. //if ($$is_suscribe_counter!=1) {
  114. for ($i=0; $i<$$is_suscribe_counter;$i++) {
  115. for ($j=0; $j<count($user_id_temp);$j++) {
  116. if ($is_suscribe_user_id[$i]==$user_id_temp[$j]) {
  117. if ($is_suscribe[$i]) {
  118. $list_register_user.=" - ".$user_name_temp[$j].'<br/>';
  119. $temp_unique_user=$user_name_temp[$j];
  120. $counter++;
  121. } else {
  122. $list_not_register_user.=" - ".$user_name_temp[$j].'<br/>';
  123. }
  124. }
  125. }
  126. }
  127. //} else {
  128. //$list_register_user=$temp_unique_user; // only 1 user register
  129. //}
  130. if (!empty($list_register_user)) {
  131. if ($$is_suscribe_counter==1) {
  132. $register_user_message=$temp_unique_user.' '.get_lang('langAddedToCourse');
  133. Display::display_confirmation_message($register_user_message,false);
  134. } else {
  135. $register_user_message='<br />'.get_lang('UsersRegistered').'<br/><br />'.$list_register_user;
  136. Display::display_confirmation_message($register_user_message,false);
  137. }
  138. }
  139. if (!empty($list_not_register_user)) {
  140. $not_register_user_message='<br />'.get_lang('UsersNotRegistered').'<br/><br /><br />'.$list_not_register_user;
  141. Display::display_error_message($not_register_user_message,false);
  142. }
  143. break;
  144. }
  145. }
  146. if (!empty($_SESSION['session_user_id'])) {
  147. unset($_SESSION['session_user_id']);
  148. }
  149. if (!empty($_SESSION['session_user_name'])) {
  150. unset($_SESSION['session_user_name']);
  151. }
  152. /*
  153. -----------------------------------------------------------
  154. SHOW LIST OF USERS
  155. -----------------------------------------------------------
  156. */
  157. /**
  158. * * Get the users to display on the current page.
  159. */
  160. function get_number_of_users() {
  161. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  162. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  163. if (isset($_REQUEST['type']) && $_REQUEST['type']=='teacher') {
  164. $sql = "SELECT u.user_id
  165. FROM $user_table u
  166. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  167. WHERE cu.user_id IS NULL";
  168. global $_configuration;
  169. if ($_configuration['multiple_access_urls']==true) {
  170. $url_access_id = api_get_current_access_url_id();
  171. if ($url_access_id !=-1) {
  172. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  173. $sql = "SELECT
  174. u.user_id
  175. FROM $user_table u
  176. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  177. INNER JOIN $tbl_url_rel_user as url_rel_user
  178. ON (url_rel_user.user_id = u.user_id)
  179. WHERE cu.user_id IS NULL AND access_url_id= $url_access_id ";
  180. }
  181. }
  182. } else {
  183. $sql = "SELECT u.user_id
  184. FROM $user_table u
  185. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  186. WHERE cu.user_id IS NULL";
  187. global $_configuration;
  188. if ($_configuration['multiple_access_urls']==true) {
  189. $url_access_id = api_get_current_access_url_id();
  190. if ($url_access_id !=-1) {
  191. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  192. $sql = "SELECT
  193. u.user_id
  194. FROM $user_table u
  195. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  196. INNER JOIN $tbl_url_rel_user as url_rel_user
  197. ON (url_rel_user.user_id = u.user_id)
  198. WHERE cu.user_id IS NULL AND access_url_id= $url_access_id ";
  199. }
  200. }
  201. }
  202. if (isset ($_REQUEST['keyword'])) {
  203. $keyword = Database::escape_string($_REQUEST['keyword']);
  204. $sql .= " AND (firstname LIKE '%".$keyword."%' OR lastname LIKE '%".$keyword."%' OR email LIKE '%".$keyword."%' OR username LIKE '%".$keyword."%' OR official_code LIKE '%".$keyword."%')";
  205. }
  206. $res = api_sql_query($sql, __FILE__, __LINE__);
  207. $result = Database::num_rows($res);
  208. return $result;
  209. }
  210. /**
  211. * Get the users to display on the current page.
  212. */
  213. function get_user_data($from, $number_of_items, $column, $direction) {
  214. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  215. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  216. $tbl_session_rel_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  217. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher') {
  218. if (!empty($_SESSION["id_session"])) {
  219. $sql = "SELECT
  220. u.user_id AS col0,
  221. u.official_code AS col1,
  222. u.lastname AS col2,
  223. u.firstname AS col3,
  224. u.email AS col4,
  225. u.active AS col5,
  226. u.user_id AS col6
  227. FROM $user_table u
  228. LEFT JOIN $tbl_session_rel_course_user cu on u.user_id = cu.id_user and course_code='".$_SESSION['_course']['id']."'
  229. WHERE cu.id_user IS NULL";
  230. } else {
  231. $sql = "SELECT
  232. u.user_id AS col0,
  233. u.official_code AS col1,
  234. u.lastname AS col2,
  235. u.firstname AS col3,
  236. u.email AS col4,
  237. u.active AS col5,
  238. u.user_id AS col6
  239. FROM $user_table u
  240. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  241. WHERE cu.user_id IS NULL";
  242. //showing only the courses of the current Dokeos access_url_id
  243. global $_configuration;
  244. if ($_configuration['multiple_access_urls']==true) {
  245. $url_access_id = api_get_current_access_url_id();
  246. if ($url_access_id !=-1) {
  247. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  248. $sql = "SELECT
  249. u.user_id AS col0,
  250. u.official_code AS col1,
  251. u.lastname AS col2,
  252. u.firstname AS col3,
  253. u.email AS col4,
  254. u.active AS col5,
  255. u.user_id AS col6
  256. FROM $user_table u
  257. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  258. INNER JOIN $tbl_url_rel_user as url_rel_user
  259. ON (url_rel_user.user_id = u.user_id)
  260. WHERE cu.user_id IS NULL AND access_url_id= $url_access_id ";
  261. }
  262. }
  263. }
  264. } else {
  265. if (!empty($_SESSION["id_session"])) {
  266. $sql = "SELECT
  267. u.user_id AS col0,
  268. u.official_code AS col1,
  269. u.lastname AS col2,
  270. u.firstname AS col3,
  271. u.email AS col4,
  272. u.active AS col5,
  273. u.user_id AS col6
  274. FROM $user_table u
  275. LEFT JOIN $tbl_session_rel_course_user cu on u.user_id = cu.id_user and course_code='".$_SESSION['_course']['id']."'
  276. WHERE cu.id_user IS NULL
  277. ";
  278. } else {
  279. $sql = "SELECT
  280. u.user_id AS col0,
  281. u.official_code AS col1,
  282. u.lastname AS col2,
  283. u.firstname AS col3,
  284. u.email AS col4,
  285. u.active AS col5,
  286. u.user_id AS col6
  287. FROM $user_table u
  288. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  289. WHERE cu.user_id IS NULL";
  290. //showing only the courses of the current Dokeos access_url_id
  291. global $_configuration;
  292. if ($_configuration['multiple_access_urls']==true) {
  293. $url_access_id = api_get_current_access_url_id();
  294. if ($url_access_id !=-1) {
  295. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  296. $sql = "SELECT
  297. u.user_id AS col0,
  298. u.official_code AS col1,
  299. u.lastname AS col2,
  300. u.firstname AS col3,
  301. u.email AS col4,
  302. u.active AS col5,
  303. u.user_id AS col6
  304. FROM $user_table u
  305. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  306. INNER JOIN $tbl_url_rel_user as url_rel_user
  307. ON (url_rel_user.user_id = u.user_id)
  308. WHERE cu.user_id IS NULL AND access_url_id= $url_access_id ";
  309. }
  310. }
  311. }
  312. }
  313. if (isset ($_REQUEST['keyword'])) {
  314. $keyword = Database::escape_string($_REQUEST['keyword']);
  315. $sql .= " AND (firstname LIKE '%".$keyword."%' OR lastname LIKE '%".$keyword."%' OR email LIKE '%".$keyword."%' OR username LIKE '%".$keyword."%' OR official_code LIKE '%".$keyword."%')";
  316. }
  317. $sql .= " ORDER BY col$column $direction ";
  318. $sql .= " LIMIT $from,$number_of_items";
  319. $res = api_sql_query($sql, __FILE__, __LINE__);
  320. $users = array ();
  321. while ($user = Database::fetch_row($res)) {
  322. $users[] = $user;
  323. $_SESSION['session_user_id'][]=$user[0];
  324. $_SESSION['session_user_name'][]=$user[3].' '.$user[2];
  325. }
  326. return $users;
  327. }
  328. /**
  329. * Returns a mailto-link
  330. * @param string $email An email-address
  331. * @return string HTML-code with a mailto-link
  332. */
  333. function email_filter($email) {
  334. return Display :: encrypted_mailto_link($email, $email);
  335. }
  336. /**
  337. * Build the reg-column of the table
  338. * @param int $user_id The user id
  339. * @return string Some HTML-code
  340. */
  341. function reg_filter($user_id) {
  342. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher') $type='teacher'; else $type='student';
  343. $result = "<a href=\"".api_get_self()."?register=yes&amp;type=".$type."&amp;user_id=".$user_id."\">".get_lang("reg")."</a>";
  344. return $result;
  345. }
  346. /**
  347. * Build the active-column of the table to lock or unlock a certain user
  348. * lock = the user can no longer use this account
  349. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  350. * @param int $active the current state of the account
  351. * @param int $user_id The user id
  352. * @param string $url_params
  353. * @return string Some HTML-code with the lock/unlock button
  354. */
  355. function active_filter($active, $url_params, $row) {
  356. global $_user;
  357. if ($active=='1') {
  358. $action='AccountActive';
  359. $image='right';
  360. }
  361. if ($active=='0') {
  362. $action='AccountInactive';
  363. $image='wrong';
  364. }
  365. 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.
  366. $result = '<center><img src="../img/'.$image.'.gif" border="0" style="vertical-align: middle;" alt="'.get_lang(ucfirst($action)).'" title="'.get_lang(ucfirst($action)).'"/></center>';
  367. }
  368. return $result;
  369. }
  370. // Build search-form
  371. echo '<div class="actions">';
  372. $actions .= '<a href="user.php">'.Display::return_icon('members.gif',get_lang('BackToUserList')).' '.get_lang('BackToUserList').'</a>';
  373. if ($_POST['keyword'])
  374. {
  375. $actions .= '<a href="subscribe_user.php?type='.Security::remove_XSS($_GET['type']).'">'.Display::return_icon('clean_group.gif').' '.get_lang('ClearSearchResults').'</a>';
  376. }
  377. $form = new FormValidator('search_user', 'POST',api_get_self().'?type='.$_REQUEST['type'],'',null,false);
  378. $renderer = & $form->defaultRenderer();
  379. $renderer->setElementTemplate('<span>{element}</span> ');
  380. $form->add_textfield('keyword', '', false);
  381. $form->addElement('style_submit_button', 'submit', get_lang('SearchButton'), 'class="search"');
  382. $form->addElement('static','additionalactions',null,$actions);
  383. $form->display();
  384. echo '</div>';
  385. // Build table
  386. $table = new SortableTable('users', 'get_number_of_users', 'get_user_data', 2);
  387. $parameters['keyword'] = $_REQUEST['keyword'];
  388. $parameters ['type'] = $_REQUEST['type'];
  389. $table->set_additional_parameters($parameters);
  390. $col = 0;
  391. $table->set_header($col ++, '', false);
  392. $table->set_header($col ++, get_lang('OfficialCode'));
  393. $table->set_header($col ++, get_lang('LastName'));
  394. $table->set_header($col ++, get_lang('FirstName'));
  395. $table->set_header($col ++, get_lang('Email'));
  396. $table->set_column_filter($col -1, 'email_filter');
  397. $table->set_header($col ++, get_lang('Active'),false);
  398. $table->set_column_filter($col -1, 'active_filter');
  399. $table->set_header($col ++, get_lang('reg'), false);
  400. $table->set_column_filter($col -1, 'reg_filter');
  401. $table->set_form_actions(array ('subscribe' => get_lang('reg')), 'user');
  402. if ( !empty($_POST['keyword'])) {
  403. $keyword_name=Security::remove_XSS($_POST['keyword']);
  404. echo '<br/>'.get_lang('SearchResultsFor').' <span style="font-style: italic ;"> '.$keyword_name.' </span><br>';
  405. }
  406. // Display table
  407. $table->display();
  408. /*
  409. ==============================================================================
  410. FOOTER
  411. ==============================================================================
  412. */
  413. Display :: display_footer();