subscribe_user.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2005 Dokeos SPRL
  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 = array('registration','admin');
  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. $counter = count($user_id_temp);
  80. for ($j=0; $j<$counter;$j++)
  81. {
  82. if ($user_id_temp[$j]==$_GET['user_id'])
  83. {
  84. if ($result_simple_sub)
  85. {
  86. Display::display_confirmation_message($_SESSION['session_user_name'][$j].' '.get_lang('langAddedToCourse'));
  87. }
  88. else
  89. {
  90. Display::display_error_message($_SESSION['session_user_name'][$j].' '.get_lang('langNotAddedToCourse'));
  91. }
  92. }
  93. }
  94. unset($_SESSION['session_user_id']);
  95. unset($_SESSION['session_user_name']);
  96. }
  97. }
  98. if (isset ($_POST['action']))
  99. {
  100. switch ($_POST['action'])
  101. {
  102. case 'subscribe' :
  103. if (is_array($_POST['user']))
  104. {
  105. foreach ($_POST['user'] as $index => $user_id)
  106. {
  107. $user_id=Database::escape_string($user_id);
  108. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher')
  109. {
  110. $is_suscribe[]=CourseManager :: subscribe_user($user_id, $_course['sysCode'],COURSEMANAGER);
  111. } else {
  112. $is_suscribe[]=CourseManager :: subscribe_user($user_id, $_course['sysCode']);
  113. }
  114. $is_suscribe_user_id[]=$user_id;
  115. }
  116. }
  117. $user_id_temp=$_SESSION['session_user_id'];
  118. $user_name_temp=$_SESSION['session_user_name'];
  119. unset($_SESSION['session_user_id']);
  120. unset($_SESSION['session_user_name']);
  121. $counter=0;
  122. $$is_suscribe_counter=count($is_suscribe_user_id);
  123. $list_register_user='';
  124. if ($$is_suscribe_counter!=1)
  125. {
  126. for ($i=0; $i<$$is_suscribe_counter;$i++)
  127. {
  128. for ($j=0; $j<count($user_id_temp);$j++)
  129. {
  130. if ($is_suscribe_user_id[$i]==$user_id_temp[$j])
  131. {
  132. if ($is_suscribe[$i])
  133. {
  134. $list_register_user.=" - ".$user_name_temp[$j].'<br/>';
  135. $counter++;
  136. }
  137. else
  138. {
  139. $list_not_register_user.=" - ".$user_name_temp[$j].'<br/>';
  140. }
  141. }
  142. }
  143. }
  144. }
  145. else
  146. {
  147. $list_register_user=$user_name_temp[0]; // only 1 user register
  148. }
  149. if (!empty($list_register_user))
  150. {
  151. if ($$is_suscribe_counter==1)
  152. {
  153. $register_user_message=$list_register_user.' '.get_lang('langAddedToCourse');
  154. Display::display_confirmation_message($register_user_message,false);
  155. }
  156. else
  157. {
  158. $register_user_message='<br />'.get_lang('UsersRegistered').'<br/><br />'.$list_register_user;
  159. Display::display_confirmation_message($register_user_message,false);
  160. }
  161. }
  162. if (!empty($list_not_register_user))
  163. {
  164. $not_register_user_message='<br />'.get_lang('UsersNotRegistered').'<br/><br /><br />'.$list_not_register_user;
  165. Display::display_error_message($not_register_user_message,false);
  166. }
  167. break;
  168. }
  169. }
  170. if (!empty($_SESSION['session_user_id']))
  171. {
  172. unset($_SESSION['session_user_id']);
  173. }
  174. if (!empty($_SESSION['session_user_name']))
  175. {
  176. unset($_SESSION['session_user_name']);
  177. }
  178. /*
  179. -----------------------------------------------------------
  180. SHOW LIST OF USERS
  181. -----------------------------------------------------------
  182. */
  183. /**
  184. * * Get the users to display on the current page.
  185. */
  186. function get_number_of_users()
  187. {
  188. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  189. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  190. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher')
  191. {
  192. $sql = "SELECT u.user_id
  193. FROM $user_table u
  194. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  195. WHERE cu.user_id IS NULL
  196. ";
  197. }
  198. else
  199. {
  200. $sql = "SELECT u.user_id
  201. FROM $user_table u
  202. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  203. WHERE cu.user_id IS NULL
  204. ";
  205. }
  206. if (isset ($_REQUEST['keyword']))
  207. {
  208. $keyword = Database::escape_string($_REQUEST['keyword']);
  209. $sql .= " AND (firstname LIKE '%".$keyword."%' OR lastname LIKE '%".$keyword."%' OR email LIKE '%".$keyword."%' OR username LIKE '%".$keyword."%' OR official_code LIKE '%".$keyword."%')";
  210. }
  211. $res = api_sql_query($sql, __FILE__, __LINE__);
  212. $result = Database::num_rows($res);
  213. return $result;
  214. }
  215. /**
  216. * Get the users to display on the current page.
  217. */
  218. function get_user_data($from, $number_of_items, $column, $direction)
  219. {
  220. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  221. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  222. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher')
  223. {
  224. $sql = "SELECT
  225. u.user_id AS col0,
  226. u.official_code AS col1,
  227. u.lastname AS col2,
  228. u.firstname AS col3,
  229. u.email AS col4,
  230. u.active AS col5,
  231. u.user_id AS col6
  232. FROM $user_table u
  233. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  234. WHERE cu.user_id IS NULL
  235. ";
  236. }
  237. else
  238. {
  239. $sql = "SELECT
  240. u.user_id AS col0,
  241. u.official_code AS col1,
  242. u.lastname AS col2,
  243. u.firstname AS col3,
  244. u.email AS col4,
  245. u.active AS col5,
  246. u.user_id AS col6
  247. FROM $user_table u
  248. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  249. WHERE cu.user_id IS NULL
  250. ";
  251. }
  252. if (isset ($_REQUEST['keyword']))
  253. {
  254. $keyword = Database::escape_string($_REQUEST['keyword']);
  255. $sql .= " AND (firstname LIKE '%".$keyword."%' OR lastname LIKE '%".$keyword."%' OR email LIKE '%".$keyword."%' OR username LIKE '%".$keyword."%' OR official_code LIKE '%".$keyword."%')";
  256. }
  257. $sql .= " ORDER BY col$column $direction ";
  258. $sql .= " LIMIT $from,$number_of_items";
  259. $res = api_sql_query($sql, __FILE__, __LINE__);
  260. $users = array ();
  261. while ($user = Database::fetch_row($res))
  262. {
  263. $users[] = $user;
  264. $_SESSION['session_user_id'][]=$user[0];
  265. $_SESSION['session_user_name'][]=$user[3].' '.$user[2];
  266. }
  267. return $users;
  268. }
  269. /**
  270. * Returns a mailto-link
  271. * @param string $email An email-address
  272. * @return string HTML-code with a mailto-link
  273. */
  274. function email_filter($email)
  275. {
  276. return Display :: encrypted_mailto_link($email, $email);
  277. }
  278. /**
  279. * Build the reg-column of the table
  280. * @param int $user_id The user id
  281. * @return string Some HTML-code
  282. */
  283. function reg_filter($user_id)
  284. {
  285. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher') $type='teacher'; else $type='student';
  286. $result = "<a href=\"".api_get_self()."?register=yes&amp;type=".$type."&amp;user_id=".$user_id."\">".get_lang("reg")."</a>";
  287. return $result;
  288. }
  289. /**
  290. * Build the active-column of the table to lock or unlock a certain user
  291. * lock = the user can no longer use this account
  292. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  293. * @param int $active the current state of the account
  294. * @param int $user_id The user id
  295. * @param string $url_params
  296. * @return string Some HTML-code with the lock/unlock button
  297. */
  298. function active_filter($active, $url_params, $row)
  299. {
  300. global $_user;
  301. if ($active=='1')
  302. {
  303. $action='AccountActive';
  304. $image='right';
  305. }
  306. if ($active=='0')
  307. {
  308. $action='AccountInactive';
  309. $image='wrong';
  310. }
  311. 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.
  312. {
  313. $result = '<center><img src="../img/'.$image.'.gif" border="0" style="vertical-align: middle;" alt="'.get_lang(ucfirst($action)).'" title="'.get_lang(ucfirst($action)).'"/></center>';
  314. }
  315. return $result;
  316. }
  317. // Build search-form
  318. $form = new FormValidator('search_user', 'POST',api_get_self().'?type='.$_REQUEST['type'],'',null,false);
  319. $renderer = & $form->defaultRenderer();
  320. $renderer->setElementTemplate('<span>{element}</span> ');
  321. $form->add_textfield('keyword', '', false);
  322. $form->addElement('submit', 'submit', get_lang('SearchButton'));
  323. // Build table
  324. $table = new SortableTable('users', 'get_number_of_users', 'get_user_data', 2);
  325. $parameters['keyword'] = $_REQUEST['keyword'];
  326. $parameters ['type'] = $_REQUEST['type'];
  327. $table->set_additional_parameters($parameters);
  328. $col = 0;
  329. $table->set_header($col ++, '', false);
  330. $table->set_header($col ++, get_lang('OfficialCode'));
  331. $table->set_header($col ++, get_lang('LastName'));
  332. $table->set_header($col ++, get_lang('FirstName'));
  333. $table->set_header($col ++, get_lang('Email'));
  334. $table->set_column_filter($col -1, 'email_filter');
  335. $table->set_header($col ++, get_lang('Active'),false);
  336. $table->set_column_filter($col -1, 'active_filter');
  337. $table->set_header($col ++, get_lang('reg'), false);
  338. $table->set_column_filter($col -1, 'reg_filter');
  339. $table->set_form_actions(array ('subscribe' => get_lang('reg')), 'user');
  340. // Display form & table
  341. $form->display();
  342. echo '<br />';
  343. $table->display();
  344. if ( !empty($_POST['keyword']))
  345. {
  346. $keyword_name=Security::remove_XSS($_POST['keyword']);
  347. echo '<br/>'.get_lang('SearchResultsFor').' <span style="font-style: italic ;"> '.$keyword_name.' </span><br>';
  348. }
  349. /*
  350. ==============================================================================
  351. FOOTER
  352. ==============================================================================
  353. */
  354. Display :: display_footer();
  355. ?>