subscribe_user.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. if (!api_is_allowed_to_edit()) api_not_allowed(true);
  41. require_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  42. require_once (api_get_path(LIBRARY_PATH).'sortabletable.class.php');
  43. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  44. /*
  45. ==============================================================================
  46. MAIN CODE
  47. ==============================================================================
  48. */
  49. /*
  50. -----------------------------------------------------------
  51. Header
  52. -----------------------------------------------------------
  53. */
  54. $tool_name = get_lang("SubscribeUserToCourse");
  55. //extra entries in breadcrumb
  56. $interbreadcrumb[] = array ("url" => "user.php", "name" => get_lang("Users"));
  57. Display :: display_header($tool_name, "User");
  58. api_display_tool_title($tool_name);
  59. /*
  60. ==============================================================================
  61. MAIN SECTION
  62. ==============================================================================
  63. */
  64. if (isset ($_REQUEST['register']))
  65. {
  66. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher'){
  67. CourseManager :: subscribe_user($_REQUEST['user_id'], $_course['sysCode'],COURSEMANAGER);
  68. }
  69. else{
  70. CourseManager :: subscribe_user($_REQUEST['user_id'], $_course['sysCode']);
  71. }
  72. }
  73. if (isset ($_POST['action']))
  74. {
  75. switch ($_POST['action'])
  76. {
  77. case 'subscribe' :
  78. if (is_array($_POST['user']))
  79. {
  80. foreach ($_POST['user'] as $index => $user_id)
  81. {
  82. CourseManager :: subscribe_user($user_id, $_course['sysCode']);
  83. }
  84. }
  85. break;
  86. }
  87. }
  88. /*
  89. -----------------------------------------------------------
  90. SHOW LIST OF USERS
  91. -----------------------------------------------------------
  92. */
  93. /**
  94. * * Get the users to display on the current page.
  95. */
  96. function get_number_of_users()
  97. {
  98. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  99. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  100. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher'){
  101. $sql = "SELECT u.user_id
  102. FROM $user_table u
  103. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  104. WHERE cu.user_id IS NULL AND u.status='1'
  105. ";
  106. }
  107. else{
  108. $sql = "SELECT u.user_id
  109. FROM $user_table u
  110. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  111. WHERE cu.user_id IS NULL AND u.status='5'
  112. ";
  113. }
  114. if (isset ($_REQUEST['keyword']))
  115. {
  116. $keyword = mysql_real_escape_string($_REQUEST['keyword']);
  117. $sql .= " AND (firstname LIKE '%".$keyword."%' OR lastname LIKE '%".$keyword."%' OR username LIKE '%".$keyword."%' OR official_code LIKE '%".$keyword."%')";
  118. }
  119. $res = api_sql_query($sql, __FILE__, __LINE__);
  120. $result = mysql_num_rows($res);
  121. return $result;
  122. }
  123. /**
  124. * Get the users to display on the current page.
  125. */
  126. function get_user_data($from, $number_of_items, $column, $direction)
  127. {
  128. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  129. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  130. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher'){
  131. $sql = "SELECT
  132. u.user_id AS col0,
  133. u.official_code AS col1,
  134. u.lastname AS col2,
  135. u.firstname AS col3,
  136. u.email AS col4,
  137. u.user_id AS col5
  138. FROM $user_table u
  139. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  140. WHERE u.status='1' and cu.user_id IS NULL
  141. ";
  142. }
  143. else{
  144. $sql = "SELECT
  145. u.user_id AS col0,
  146. u.official_code AS col1,
  147. u.lastname AS col2,
  148. u.firstname AS col3,
  149. u.email AS col4,
  150. u.user_id AS col5
  151. FROM $user_table u
  152. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  153. WHERE u.status='5' and cu.user_id IS NULL
  154. ";
  155. }
  156. if (isset ($_REQUEST['keyword']))
  157. {
  158. $keyword = mysql_real_escape_string($_REQUEST['keyword']);
  159. $sql .= " AND (firstname LIKE '%".$keyword."%' OR lastname LIKE '%".$keyword."%' OR username LIKE '%".$keyword."%' OR official_code LIKE '%".$keyword."%')";
  160. }
  161. $sql .= " ORDER BY col$column $direction ";
  162. $sql .= " LIMIT $from,$number_of_items";
  163. $res = api_sql_query($sql, __FILE__, __LINE__);
  164. $users = array ();
  165. while ($user = mysql_fetch_row($res))
  166. {
  167. $users[] = $user;
  168. }
  169. return $users;
  170. }
  171. /**
  172. * Returns a mailto-link
  173. * @param string $email An email-address
  174. * @return string HTML-code with a mailto-link
  175. */
  176. function email_filter($email)
  177. {
  178. return Display :: encrypted_mailto_link($email, $email);
  179. }
  180. /**
  181. * Build the reg-column of the table
  182. * @param int $user_id The user id
  183. * @return string Some HTML-code
  184. */
  185. function reg_filter($user_id)
  186. {
  187. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher') $type='teacher'; else $type='student';
  188. $result = "<a href=\"".api_get_self()."?register=yes&amp;type=".$type."&amp;user_id=".$user_id."\">".get_lang("reg")."</a>";
  189. return $result;
  190. }
  191. // Build search-form
  192. $form = new FormValidator('search_user', 'POST',api_get_self().'?type='.$_REQUEST['type'],'',null,false);
  193. $renderer = & $form->defaultRenderer();
  194. $renderer->setElementTemplate('<span>{element}</span> ');
  195. $form->add_textfield('keyword', '', false);
  196. $form->addElement('submit', 'submit', get_lang('SearchButton'));
  197. // Build table
  198. $table = new SortableTable('users', 'get_number_of_users', 'get_user_data', 2);
  199. $parameters['keyword'] = $_REQUEST['keyword'];
  200. $parameters ['type'] = $_REQUEST['type'];
  201. $table->set_additional_parameters($parameters);
  202. $col = 0;
  203. $table->set_header($col ++, '', false);
  204. $table->set_header($col ++, get_lang('OfficialCode'));
  205. $table->set_header($col ++, get_lang('LastName'));
  206. $table->set_header($col ++, get_lang('FirstName'));
  207. $table->set_header($col ++, get_lang('Email'));
  208. $table->set_column_filter($col -1, 'email_filter');
  209. $table->set_header($col ++, get_lang('reg'), false);
  210. $table->set_column_filter($col -1, 'reg_filter');
  211. $table->set_form_actions(array ('subscribe' => get_lang('reg')), 'user');
  212. // Display form & table
  213. $form->display();
  214. echo '<br />';
  215. $table->display();
  216. /*
  217. ==============================================================================
  218. FOOTER
  219. ==============================================================================
  220. */
  221. Display :: display_footer();
  222. ?>