subscribe_user.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 = Database::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. $is_western_name_order = api_is_western_name_order();
  218. if (isset($_REQUEST['type']) && $_REQUEST['type']=='teacher') {
  219. if (!empty($_SESSION["id_session"])) {
  220. $sql = "SELECT
  221. u.user_id AS col0,
  222. u.official_code AS col1,
  223. ".($is_western_name_order
  224. ? "u.firstname AS col2,
  225. u.lastname AS col3,"
  226. : "u.lastname AS col2,
  227. u.firstname AS col3,")."
  228. u.email AS col4,
  229. u.active AS col5,
  230. u.user_id AS col6
  231. FROM $user_table u
  232. LEFT JOIN $tbl_session_rel_course_user cu on u.user_id = cu.id_user and course_code='".$_SESSION['_course']['id']."'
  233. WHERE cu.id_user IS NULL";
  234. } else {
  235. $sql = "SELECT
  236. u.user_id AS col0,
  237. u.official_code AS col1,
  238. ".($is_western_name_order
  239. ? "u.firstname AS col2,
  240. u.lastname AS col3,"
  241. : "u.lastname AS col2,
  242. u.firstname AS col3,")."
  243. u.email AS col4,
  244. u.active AS col5,
  245. u.user_id AS col6
  246. FROM $user_table u
  247. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  248. WHERE cu.user_id IS NULL";
  249. //showing only the courses of the current Dokeos access_url_id
  250. global $_configuration;
  251. if ($_configuration['multiple_access_urls']==true) {
  252. $url_access_id = api_get_current_access_url_id();
  253. if ($url_access_id !=-1) {
  254. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  255. $sql = "SELECT
  256. u.user_id AS col0,
  257. u.official_code AS col1,
  258. ".($is_western_name_order
  259. ? "u.firstname AS col2,
  260. u.lastname AS col3,"
  261. : "u.lastname AS col2,
  262. u.firstname AS col3,")."
  263. u.email AS col4,
  264. u.active AS col5,
  265. u.user_id AS col6
  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 ";
  271. }
  272. }
  273. }
  274. } else {
  275. if (!empty($_SESSION["id_session"])) {
  276. $sql = "SELECT
  277. u.user_id AS col0,
  278. u.official_code AS col1,
  279. ".($is_western_name_order
  280. ? "u.firstname AS col2,
  281. u.lastname AS col3,"
  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 $tbl_session_rel_course_user cu on u.user_id = cu.id_user and course_code='".$_SESSION['_course']['id']."'
  289. WHERE cu.id_user IS NULL
  290. ";
  291. } else {
  292. $sql = "SELECT
  293. u.user_id AS col0,
  294. u.official_code AS col1,
  295. ".($is_western_name_order
  296. ? "u.firstname AS col2,
  297. u.lastname AS col3,"
  298. : "u.lastname AS col2,
  299. u.firstname AS col3,")."
  300. u.email AS col4,
  301. u.active AS col5,
  302. u.user_id AS col6
  303. FROM $user_table u
  304. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  305. WHERE cu.user_id IS NULL";
  306. //showing only the courses of the current Dokeos access_url_id
  307. global $_configuration;
  308. if ($_configuration['multiple_access_urls']==true) {
  309. $url_access_id = api_get_current_access_url_id();
  310. if ($url_access_id !=-1) {
  311. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  312. $sql = "SELECT
  313. u.user_id AS col0,
  314. u.official_code AS col1,
  315. ".($is_western_name_order
  316. ? "u.firstname AS col2,
  317. u.lastname AS col3,"
  318. : "u.lastname AS col2,
  319. u.firstname AS col3,")."
  320. u.email AS col4,
  321. u.active AS col5,
  322. u.user_id AS col6
  323. FROM $user_table u
  324. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  325. INNER JOIN $tbl_url_rel_user as url_rel_user
  326. ON (url_rel_user.user_id = u.user_id)
  327. WHERE cu.user_id IS NULL AND access_url_id= $url_access_id ";
  328. }
  329. }
  330. }
  331. }
  332. if (isset ($_REQUEST['keyword'])) {
  333. $keyword = Database::escape_string($_REQUEST['keyword']);
  334. $sql .= " AND (firstname LIKE '%".$keyword."%' OR lastname LIKE '%".$keyword."%' OR email LIKE '%".$keyword."%' OR username LIKE '%".$keyword."%' OR official_code LIKE '%".$keyword."%')";
  335. }
  336. $sql .= " ORDER BY col$column $direction ";
  337. $sql .= " LIMIT $from,$number_of_items";
  338. $res = Database::query($sql, __FILE__, __LINE__);
  339. $users = array ();
  340. while ($user = Database::fetch_row($res)) {
  341. $users[] = $user;
  342. $_SESSION['session_user_id'][] = $user[0];
  343. if ($is_western_name_order) {
  344. $_SESSION['session_user_name'][] = api_get_person_name($user[2], $user[3]);
  345. } else {
  346. $_SESSION['session_user_name'][] = api_get_person_name($user[3], $user[2]);
  347. }
  348. }
  349. return $users;
  350. }
  351. /**
  352. * Returns a mailto-link
  353. * @param string $email An email-address
  354. * @return string HTML-code with a mailto-link
  355. */
  356. function email_filter($email) {
  357. return Display :: encrypted_mailto_link($email, $email);
  358. }
  359. /**
  360. * Build the reg-column of the table
  361. * @param int $user_id The user id
  362. * @return string Some HTML-code
  363. */
  364. function reg_filter($user_id) {
  365. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher') $type='teacher'; else $type='student';
  366. $result = "<a href=\"".api_get_self()."?register=yes&amp;type=".$type."&amp;user_id=".$user_id."\">".get_lang("reg")."</a>";
  367. return $result;
  368. }
  369. /**
  370. * Build the active-column of the table to lock or unlock a certain user
  371. * lock = the user can no longer use this account
  372. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  373. * @param int $active the current state of the account
  374. * @param int $user_id The user id
  375. * @param string $url_params
  376. * @return string Some HTML-code with the lock/unlock button
  377. */
  378. function active_filter($active, $url_params, $row) {
  379. global $_user;
  380. if ($active=='1') {
  381. $action='AccountActive';
  382. $image='right';
  383. }
  384. if ($active=='0') {
  385. $action='AccountInactive';
  386. $image='wrong';
  387. }
  388. 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.
  389. $result = '<center><img src="../img/'.$image.'.gif" border="0" style="vertical-align: middle;" alt="'.get_lang(ucfirst($action)).'" title="'.get_lang(ucfirst($action)).'"/></center>';
  390. }
  391. return $result;
  392. }
  393. $is_western_name_order = api_is_western_name_order();
  394. $sort_by_first_name = api_sort_by_first_name();
  395. // Build search-form
  396. echo '<div class="actions">';
  397. $actions .= '<a href="user.php">'.Display::return_icon('members.gif',get_lang('BackToUserList')).' '.get_lang('BackToUserList').'</a>';
  398. if ($_POST['keyword'])
  399. {
  400. $actions .= '<a href="subscribe_user.php?type='.Security::remove_XSS($_GET['type']).'">'.Display::return_icon('clean_group.gif').' '.get_lang('ClearSearchResults').'</a>';
  401. }
  402. $form = new FormValidator('search_user', 'POST',api_get_self().'?type='.$_REQUEST['type'],'',null,false);
  403. $renderer = & $form->defaultRenderer();
  404. $renderer->setElementTemplate('<span>{element}</span> ');
  405. $form->add_textfield('keyword', '', false);
  406. $form->addElement('style_submit_button', 'submit', get_lang('SearchButton'), 'class="search"');
  407. $form->addElement('static','additionalactions',null,$actions);
  408. $form->display();
  409. echo '</div>';
  410. // Build table
  411. $table = new SortableTable('users', 'get_number_of_users', 'get_user_data', ($is_western_name_order xor $sort_by_first_name) ? 3 : 2);
  412. $parameters['keyword'] = $_REQUEST['keyword'];
  413. $parameters ['type'] = $_REQUEST['type'];
  414. $table->set_additional_parameters($parameters);
  415. $col = 0;
  416. $table->set_header($col ++, '', false);
  417. $table->set_header($col ++, get_lang('OfficialCode'));
  418. if (api_is_western_name_order()) {
  419. $table->set_header($col ++, get_lang('FirstName'));
  420. $table->set_header($col ++, get_lang('LastName'));
  421. } else {
  422. $table->set_header($col ++, get_lang('LastName'));
  423. $table->set_header($col ++, get_lang('FirstName'));
  424. }
  425. $table->set_header($col ++, get_lang('Email'));
  426. $table->set_column_filter($col -1, 'email_filter');
  427. $table->set_header($col ++, get_lang('Active'),false);
  428. $table->set_column_filter($col -1, 'active_filter');
  429. $table->set_header($col ++, get_lang('reg'), false);
  430. $table->set_column_filter($col -1, 'reg_filter');
  431. $table->set_form_actions(array ('subscribe' => get_lang('reg')), 'user');
  432. if (!empty($_POST['keyword'])) {
  433. $keyword_name=Security::remove_XSS($_POST['keyword']);
  434. echo '<br/>'.get_lang('SearchResultsFor').' <span style="font-style: italic ;"> '.$keyword_name.' </span><br>';
  435. }
  436. // Display table
  437. $table->display();
  438. /*
  439. ==============================================================================
  440. FOOTER
  441. ==============================================================================
  442. */
  443. Display :: display_footer();