subscribe_user.php 16 KB

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