subscribe_user.php 6.6 KB

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