subscribe_user.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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, rue du Corbeau, 108, 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. $list_register_user='';
  65. $list_not_register_user='';
  66. if (isset ($_REQUEST['register']))
  67. {
  68. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher')
  69. {
  70. $result_simple_sub=CourseManager :: subscribe_user(Database::escape_string($_REQUEST['user_id']), $_course['sysCode'],COURSEMANAGER);
  71. }
  72. else
  73. {
  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. {
  79. for ($j=0; $j<count($user_id_temp);$j++)
  80. {
  81. if ($user_id_temp[$j]==$_GET['user_id'])
  82. {
  83. if ($result_simple_sub)
  84. {
  85. Display::display_confirmation_message($_SESSION['session_user_name'][$j].' '.get_lang('langAddedToCourse'));
  86. }
  87. else
  88. {
  89. Display::display_error_message($_SESSION['session_user_name'][$j].' '.get_lang('langNotAddedToCourse'));
  90. }
  91. }
  92. }
  93. unset($_SESSION['session_user_id']);
  94. unset($_SESSION['session_user_name']);
  95. }
  96. }
  97. if (isset ($_POST['action']))
  98. {
  99. switch ($_POST['action'])
  100. {
  101. case 'subscribe' :
  102. if (is_array($_POST['user']))
  103. {
  104. foreach ($_POST['user'] as $index => $user_id)
  105. {
  106. $user_id=Database::escape_string($user_id);
  107. $is_suscribe[]=CourseManager :: subscribe_user($user_id, $_course['sysCode']);
  108. $is_suscribe_user_id[]=$user_id;
  109. }
  110. }
  111. $user_id_temp=$_SESSION['session_user_id'];
  112. $user_name_temp=$_SESSION['session_user_name'];
  113. unset($_SESSION['session_user_id']);
  114. unset($_SESSION['session_user_name']);
  115. $counter=0;
  116. $$is_suscribe_counter=count($is_suscribe_user_id);
  117. $list_register_user='';
  118. if ($$is_suscribe_counter!=1)
  119. {
  120. for ($i=0; $i<$$is_suscribe_counter;$i++)
  121. {
  122. for ($j=0; $j<count($user_id_temp);$j++)
  123. {
  124. if ($is_suscribe_user_id[$i]==$user_id_temp[$j])
  125. {
  126. if ($is_suscribe[$i])
  127. {
  128. $list_register_user.=" - ".$user_name_temp[$j].'<br/>';
  129. $counter++;
  130. }
  131. else
  132. {
  133. $list_not_register_user.=" - ".$user_name_temp[$j].'<br/>';
  134. }
  135. }
  136. }
  137. }
  138. }
  139. else
  140. {
  141. $list_register_user=$user_name_temp[0]; // only 1 user register
  142. }
  143. if (!empty($list_register_user))
  144. {
  145. if ($$is_suscribe_counter==1)
  146. {
  147. $register_user_message=$list_register_user.' '.get_lang('langAddedToCourse');
  148. Display::display_confirmation_message($register_user_message,false);
  149. }
  150. else
  151. {
  152. $register_user_message='<br />'.get_lang('UsersRegistered').'<br/><br />'.$list_register_user;
  153. Display::display_confirmation_message($register_user_message,false);
  154. }
  155. }
  156. if (!empty($list_not_register_user))
  157. {
  158. $not_register_user_message='<br />'.get_lang('UsersNotRegistered').'<br/><br /><br />'.$list_not_register_user;
  159. Display::display_error_message($not_register_user_message,false);
  160. }
  161. break;
  162. }
  163. }
  164. if (!empty($_SESSION['session_user_id']))
  165. {
  166. unset($_SESSION['session_user_id']);
  167. }
  168. if (!empty($_SESSION['session_user_name']))
  169. {
  170. unset($_SESSION['session_user_name']);
  171. }
  172. /*
  173. -----------------------------------------------------------
  174. SHOW LIST OF USERS
  175. -----------------------------------------------------------
  176. */
  177. /**
  178. * * Get the users to display on the current page.
  179. */
  180. function get_number_of_users()
  181. {
  182. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  183. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  184. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher')
  185. {
  186. $sql = "SELECT u.user_id
  187. FROM $user_table u
  188. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  189. WHERE cu.user_id IS NULL AND u.status='1'
  190. ";
  191. }
  192. else
  193. {
  194. $sql = "SELECT u.user_id
  195. FROM $user_table u
  196. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  197. WHERE cu.user_id IS NULL AND u.status='5'
  198. ";
  199. }
  200. if (isset ($_REQUEST['keyword']))
  201. {
  202. $keyword = mysql_real_escape_string($_REQUEST['keyword']);
  203. $sql .= " AND (firstname LIKE '%".$keyword."%' OR lastname LIKE '%".$keyword."%' OR username LIKE '%".$keyword."%' OR official_code LIKE '%".$keyword."%')";
  204. }
  205. $res = api_sql_query($sql, __FILE__, __LINE__);
  206. $result = mysql_num_rows($res);
  207. return $result;
  208. }
  209. /**
  210. * Get the users to display on the current page.
  211. */
  212. function get_user_data($from, $number_of_items, $column, $direction)
  213. {
  214. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  215. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  216. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher')
  217. {
  218. $sql = "SELECT
  219. u.user_id AS col0,
  220. u.official_code AS col1,
  221. u.lastname AS col2,
  222. u.firstname AS col3,
  223. u.email AS col4,
  224. u.user_id AS col5
  225. FROM $user_table u
  226. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  227. WHERE u.status='1' and cu.user_id IS NULL
  228. ";
  229. }
  230. else
  231. {
  232. $sql = "SELECT
  233. u.user_id AS col0,
  234. u.official_code AS col1,
  235. u.lastname AS col2,
  236. u.firstname AS col3,
  237. u.email AS col4,
  238. u.user_id AS col5
  239. FROM $user_table u
  240. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  241. WHERE u.status='5' and cu.user_id IS NULL
  242. ";
  243. }
  244. if (isset ($_REQUEST['keyword']))
  245. {
  246. $keyword = mysql_real_escape_string($_REQUEST['keyword']);
  247. $sql .= " AND (firstname LIKE '%".$keyword."%' OR lastname LIKE '%".$keyword."%' OR username LIKE '%".$keyword."%' OR official_code LIKE '%".$keyword."%')";
  248. }
  249. $sql .= " ORDER BY col$column $direction ";
  250. $sql .= " LIMIT $from,$number_of_items";
  251. $res = api_sql_query($sql, __FILE__, __LINE__);
  252. $users = array ();
  253. while ($user = mysql_fetch_row($res))
  254. {
  255. $users[] = $user;
  256. $_SESSION['session_user_id'][]=$user[0];
  257. $_SESSION['session_user_name'][]=$user[3].' '.$user[2];
  258. }
  259. return $users;
  260. }
  261. /**
  262. * Returns a mailto-link
  263. * @param string $email An email-address
  264. * @return string HTML-code with a mailto-link
  265. */
  266. function email_filter($email)
  267. {
  268. return Display :: encrypted_mailto_link($email, $email);
  269. }
  270. /**
  271. * Build the reg-column of the table
  272. * @param int $user_id The user id
  273. * @return string Some HTML-code
  274. */
  275. function reg_filter($user_id)
  276. {
  277. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher') $type='teacher'; else $type='student';
  278. $result = "<a href=\"".api_get_self()."?register=yes&amp;type=".$type."&amp;user_id=".$user_id."\">".get_lang("reg")."</a>";
  279. return $result;
  280. }
  281. // Build search-form
  282. $form = new FormValidator('search_user', 'POST',api_get_self().'?type='.$_REQUEST['type'],'',null,false);
  283. $renderer = & $form->defaultRenderer();
  284. $renderer->setElementTemplate('<span>{element}</span> ');
  285. $form->add_textfield('keyword', '', false);
  286. $form->addElement('submit', 'submit', get_lang('SearchButton'));
  287. // Build table
  288. $table = new SortableTable('users', 'get_number_of_users', 'get_user_data', 2);
  289. $parameters['keyword'] = $_REQUEST['keyword'];
  290. $parameters ['type'] = $_REQUEST['type'];
  291. $table->set_additional_parameters($parameters);
  292. $col = 0;
  293. $table->set_header($col ++, '', false);
  294. $table->set_header($col ++, get_lang('OfficialCode'));
  295. $table->set_header($col ++, get_lang('LastName'));
  296. $table->set_header($col ++, get_lang('FirstName'));
  297. $table->set_header($col ++, get_lang('Email'));
  298. $table->set_column_filter($col -1, 'email_filter');
  299. $table->set_header($col ++, get_lang('reg'), false);
  300. $table->set_column_filter($col -1, 'reg_filter');
  301. $table->set_form_actions(array ('subscribe' => get_lang('reg')), 'user');
  302. // Display form & table
  303. $form->display();
  304. echo '<br />';
  305. $table->display();
  306. if ( !empty($_POST['keyword']))
  307. {
  308. $keyword_name=Security::remove_XSS($_POST['keyword']);
  309. echo '<br/>'.get_lang('SearchResultsFor').' <span style="font-style: italic ;"> '.$keyword_name.' </span><br>';
  310. }
  311. /*
  312. ==============================================================================
  313. FOOTER
  314. ==============================================================================
  315. */
  316. Display :: display_footer();
  317. ?>