subscribe_user.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2005 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Elie harfouche (elie at harfdesign dot com)
  9. Copyright (c) Roan Embrechts, Vrije Universiteit Brussel
  10. Copyright (c) Wolfgang Schneider
  11. Copyright (c) Bart Mollet, Hogeschool Gent
  12. For a full list of contributors, see "credits.txt".
  13. The full license can be read in "license.txt".
  14. This program is free software; you can redistribute it and/or
  15. modify it under the terms of the GNU General Public License
  16. as published by the Free Software Foundation; either version 2
  17. of the License, or (at your option) any later version.
  18. See the GNU General Public License for more details.
  19. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  20. Mail: info@dokeos.com
  21. ==============================================================================
  22. */
  23. /**
  24. ==============================================================================
  25. * This script allows teachers to subscribe existing users
  26. * to their course.
  27. *
  28. * @package dokeos.user
  29. ==============================================================================
  30. */
  31. /*
  32. ==============================================================================
  33. INIT SECTION
  34. ==============================================================================
  35. */
  36. // name of the language file that needs to be included
  37. $language_file = 'registration';
  38. include ("../inc/global.inc.php");
  39. $this_section = SECTION_COURSES;
  40. require_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  41. require_once (api_get_path(LIBRARY_PATH).'sortabletable.class.php');
  42. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  43. /*
  44. ==============================================================================
  45. MAIN CODE
  46. ==============================================================================
  47. */
  48. /*
  49. -----------------------------------------------------------
  50. Header
  51. -----------------------------------------------------------
  52. */
  53. $tool_name = get_lang("SubscribeUserToCourse");
  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. if (isset ($_GET['register']))
  64. {
  65. CourseManager :: subscribe_user($_GET['user_id'], $_course['sysCode']);
  66. }
  67. if (isset ($_POST['action']))
  68. {
  69. switch ($_POST['action'])
  70. {
  71. case 'subscribe' :
  72. if (is_array($_POST['user']))
  73. {
  74. foreach ($_POST['user'] as $index => $user_id)
  75. {
  76. CourseManager :: subscribe_user($user_id, $_course['sysCode']);
  77. }
  78. }
  79. break;
  80. }
  81. }
  82. /*
  83. -----------------------------------------------------------
  84. SHOW LIST OF USERS
  85. -----------------------------------------------------------
  86. */
  87. /**
  88. * * Get the users to display on the current page.
  89. */
  90. function get_number_of_users()
  91. {
  92. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  93. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  94. $sql = "SELECT u.user_id
  95. FROM $user_table u
  96. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  97. WHERE cu.user_id IS NULL
  98. ";
  99. if (isset ($_GET['keyword']))
  100. {
  101. $keyword = mysql_real_escape_string($_GET['keyword']);
  102. $sql .= " AND (firstname LIKE '%".$keyword."%' OR lastname LIKE '%".$keyword."%' OR username LIKE '%".$keyword."%' OR official_code LIKE '%".$keyword."%')";
  103. }
  104. $res = api_sql_query($sql, __FILE__, __LINE__);
  105. $result = mysql_num_rows($res);
  106. return $result;
  107. }
  108. /**
  109. * Get the users to display on the current page.
  110. */
  111. function get_user_data($from, $number_of_items, $column, $direction)
  112. {
  113. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  114. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  115. $sql = "SELECT
  116. u.user_id AS col0,
  117. u.official_code AS col1,
  118. u.lastname AS col2,
  119. u.firstname AS col3,
  120. u.email AS col4,
  121. u.user_id AS col5
  122. FROM $user_table u
  123. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  124. WHERE cu.user_id IS NULL
  125. ";
  126. if (isset ($_GET['keyword']))
  127. {
  128. $keyword = mysql_real_escape_string($_GET['keyword']);
  129. $sql .= " AND (firstname LIKE '%".$keyword."%' OR lastname LIKE '%".$keyword."%' OR username LIKE '%".$keyword."%' OR official_code LIKE '%".$keyword."%')";
  130. }
  131. $sql .= " ORDER BY col$column $direction ";
  132. $sql .= " LIMIT $from,$number_of_items";
  133. $res = api_sql_query($sql, __FILE__, __LINE__);
  134. $users = array ();
  135. while ($user = mysql_fetch_row($res))
  136. {
  137. $users[] = $user;
  138. }
  139. return $users;
  140. }
  141. /**
  142. * Returns a mailto-link
  143. * @param string $email An email-address
  144. * @return string HTML-code with a mailto-link
  145. */
  146. function email_filter($email)
  147. {
  148. return Display :: encrypted_mailto_link($email, $email);
  149. }
  150. /**
  151. * Build the reg-column of the table
  152. * @param int $user_id The user id
  153. * @return string Some HTML-code
  154. */
  155. function reg_filter($user_id)
  156. {
  157. $result = "<a href=\"".$_SERVER['PHP_SELF']."?register=yes&amp;user_id=".$user_id."\">".get_lang("reg")."</a>";
  158. return $result;
  159. }
  160. // Build search-form
  161. $form = new FormValidator('search_user', 'get','','',null,false);
  162. $renderer = & $form->defaultRenderer();
  163. $renderer->setElementTemplate('<span>{element}</span> ');
  164. $form->add_textfield('keyword', '', false);
  165. $form->addElement('submit', 'submit', get_lang('SearchButton'));
  166. // Build table
  167. $table = new SortableTable('users', 'get_number_of_users', 'get_user_data', 2);
  168. $parameters['keyword'] = $_GET['keyword'];
  169. $table->set_additional_parameters($parameters);
  170. $col = 0;
  171. $table->set_header($col ++, '', false);
  172. $table->set_header($col ++, get_lang('OfficialCode'));
  173. $table->set_header($col ++, get_lang('Lastname'));
  174. $table->set_header($col ++, get_lang('Firstname'));
  175. $table->set_header($col ++, get_lang('Email'));
  176. $table->set_column_filter($col -1, 'email_filter');
  177. $table->set_header($col ++, get_lang('reg'), false);
  178. $table->set_column_filter($col -1, 'reg_filter');
  179. $table->set_form_actions(array ('subscribe' => get_lang('reg')), 'user');
  180. // Display form & table
  181. $form->display();
  182. echo '<br />';
  183. $table->display();
  184. /*
  185. ==============================================================================
  186. FOOTER
  187. ==============================================================================
  188. */
  189. Display :: display_footer();
  190. ?>