subscribe_user.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php // $Id: subscribe_user.php 16739 2008-11-13 15:36:40Z pcool $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos SPRL
  6. Copyright (c) 2003 Ghent University (UGent)
  7. For a full list of contributors, see "credits.txt".
  8. The full license can be read in "license.txt".
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. See the GNU General Public License for more details.
  14. Contact: Dokeos, rue Notre Dame, 152, B-1140 Evere, Belgium, info@dokeos.com
  15. ==============================================================================
  16. */
  17. /**
  18. ==============================================================================
  19. * This script allows teachers to subscribe existing users
  20. * to their course.
  21. *
  22. * @package dokeos.user
  23. ==============================================================================
  24. */
  25. /*
  26. ==============================================================================
  27. INIT SECTION
  28. ==============================================================================
  29. */
  30. // name of the language file that needs to be included
  31. $language_file = array('registration','admin');
  32. include ("../inc/global.inc.php");
  33. $this_section = SECTION_COURSES;
  34. if (!api_is_allowed_to_edit()) api_not_allowed(true);
  35. require_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  36. require_once (api_get_path(LIBRARY_PATH).'sortabletable.class.php');
  37. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  38. /*
  39. ==============================================================================
  40. MAIN CODE
  41. ==============================================================================
  42. */
  43. /*
  44. -----------------------------------------------------------
  45. Header
  46. -----------------------------------------------------------
  47. */
  48. $tool_name = get_lang("SubscribeUserToCourse");
  49. //extra entries in breadcrumb
  50. $interbreadcrumb[] = array ("url" => "user.php", "name" => get_lang("Users"));
  51. Display :: display_header($tool_name, "User");
  52. api_display_tool_title($tool_name);
  53. /*
  54. ==============================================================================
  55. MAIN SECTION
  56. ==============================================================================
  57. */
  58. $list_register_user='';
  59. $list_not_register_user='';
  60. if (isset ($_REQUEST['register']))
  61. {
  62. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher')
  63. {
  64. $result_simple_sub=CourseManager :: subscribe_user(Database::escape_string($_REQUEST['user_id']), $_course['sysCode'],COURSEMANAGER);
  65. }
  66. else
  67. {
  68. $result_simple_sub=CourseManager :: subscribe_user(Database::escape_string($_REQUEST['user_id']), $_course['sysCode']);
  69. }
  70. $user_id_temp=$_SESSION['session_user_id'];
  71. if (is_array($user_id_temp))
  72. {
  73. $counter = count($user_id_temp);
  74. for ($j=0; $j<$counter;$j++)
  75. {
  76. if ($user_id_temp[$j]==$_GET['user_id'])
  77. {
  78. if ($result_simple_sub)
  79. {
  80. Display::display_confirmation_message($_SESSION['session_user_name'][$j].' '.get_lang('langAddedToCourse'));
  81. }
  82. else
  83. {
  84. Display::display_error_message($_SESSION['session_user_name'][$j].' '.get_lang('langNotAddedToCourse'));
  85. }
  86. }
  87. }
  88. unset($_SESSION['session_user_id']);
  89. unset($_SESSION['session_user_name']);
  90. }
  91. }
  92. if (isset ($_POST['action']))
  93. {
  94. switch ($_POST['action'])
  95. {
  96. case 'subscribe' :
  97. if (is_array($_POST['user']))
  98. {
  99. foreach ($_POST['user'] as $index => $user_id)
  100. {
  101. $user_id=Database::escape_string($user_id);
  102. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher')
  103. {
  104. $is_suscribe[]=CourseManager :: subscribe_user($user_id, $_course['sysCode'],COURSEMANAGER);
  105. } else {
  106. $is_suscribe[]=CourseManager :: subscribe_user($user_id, $_course['sysCode']);
  107. }
  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
  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
  198. ";
  199. }
  200. if (isset ($_REQUEST['keyword']))
  201. {
  202. $keyword = Database::escape_string($_REQUEST['keyword']);
  203. $sql .= " AND (firstname LIKE '%".$keyword."%' OR lastname LIKE '%".$keyword."%' OR email LIKE '%".$keyword."%' OR username LIKE '%".$keyword."%' OR official_code LIKE '%".$keyword."%')";
  204. }
  205. $res = api_sql_query($sql, __FILE__, __LINE__);
  206. $result = Database::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.active AS col5,
  225. u.user_id AS col6
  226. FROM $user_table u
  227. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  228. WHERE cu.user_id IS NULL
  229. ";
  230. }
  231. else
  232. {
  233. $sql = "SELECT
  234. u.user_id AS col0,
  235. u.official_code AS col1,
  236. u.lastname AS col2,
  237. u.firstname AS col3,
  238. u.email AS col4,
  239. u.active AS col5,
  240. u.user_id AS col6
  241. FROM $user_table u
  242. LEFT JOIN $course_user_table cu on u.user_id = cu.user_id and course_code='".$_SESSION['_course']['id']."'
  243. WHERE cu.user_id IS NULL
  244. ";
  245. }
  246. if (isset ($_REQUEST['keyword']))
  247. {
  248. $keyword = Database::escape_string($_REQUEST['keyword']);
  249. $sql .= " AND (firstname LIKE '%".$keyword."%' OR lastname LIKE '%".$keyword."%' OR email LIKE '%".$keyword."%' OR username LIKE '%".$keyword."%' OR official_code LIKE '%".$keyword."%')";
  250. }
  251. $sql .= " ORDER BY col$column $direction ";
  252. $sql .= " LIMIT $from,$number_of_items";
  253. $res = api_sql_query($sql, __FILE__, __LINE__);
  254. $users = array ();
  255. while ($user = Database::fetch_row($res))
  256. {
  257. $users[] = $user;
  258. $_SESSION['session_user_id'][]=$user[0];
  259. $_SESSION['session_user_name'][]=$user[3].' '.$user[2];
  260. }
  261. return $users;
  262. }
  263. /**
  264. * Returns a mailto-link
  265. * @param string $email An email-address
  266. * @return string HTML-code with a mailto-link
  267. */
  268. function email_filter($email)
  269. {
  270. return Display :: encrypted_mailto_link($email, $email);
  271. }
  272. /**
  273. * Build the reg-column of the table
  274. * @param int $user_id The user id
  275. * @return string Some HTML-code
  276. */
  277. function reg_filter($user_id)
  278. {
  279. if(isset($_REQUEST['type']) && $_REQUEST['type']=='teacher') $type='teacher'; else $type='student';
  280. $result = "<a href=\"".api_get_self()."?register=yes&amp;type=".$type."&amp;user_id=".$user_id."\">".get_lang("reg")."</a>";
  281. return $result;
  282. }
  283. /**
  284. * Build the active-column of the table to lock or unlock a certain user
  285. * lock = the user can no longer use this account
  286. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  287. * @param int $active the current state of the account
  288. * @param int $user_id The user id
  289. * @param string $url_params
  290. * @return string Some HTML-code with the lock/unlock button
  291. */
  292. function active_filter($active, $url_params, $row)
  293. {
  294. global $_user;
  295. if ($active=='1')
  296. {
  297. $action='AccountActive';
  298. $image='right';
  299. }
  300. if ($active=='0')
  301. {
  302. $action='AccountInactive';
  303. $image='wrong';
  304. }
  305. 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.
  306. {
  307. $result = '<center><img src="../img/'.$image.'.gif" border="0" style="vertical-align: middle;" alt="'.get_lang(ucfirst($action)).'" title="'.get_lang(ucfirst($action)).'"/></center>';
  308. }
  309. return $result;
  310. }
  311. // Build search-form
  312. echo '<div class="actions">';
  313. $form = new FormValidator('search_user', 'POST',api_get_self().'?type='.$_REQUEST['type'],'',null,false);
  314. $renderer = & $form->defaultRenderer();
  315. $renderer->setElementTemplate('<span>{element}</span> ');
  316. $form->add_textfield('keyword', '', false);
  317. $form->addElement('submit', 'submit', get_lang('SearchButton'));
  318. $form->display();
  319. echo '</div>';
  320. // Build table
  321. $table = new SortableTable('users', 'get_number_of_users', 'get_user_data', 2);
  322. $parameters['keyword'] = $_REQUEST['keyword'];
  323. $parameters ['type'] = $_REQUEST['type'];
  324. $table->set_additional_parameters($parameters);
  325. $col = 0;
  326. $table->set_header($col ++, '', false);
  327. $table->set_header($col ++, get_lang('OfficialCode'));
  328. $table->set_header($col ++, get_lang('LastName'));
  329. $table->set_header($col ++, get_lang('FirstName'));
  330. $table->set_header($col ++, get_lang('Email'));
  331. $table->set_column_filter($col -1, 'email_filter');
  332. $table->set_header($col ++, get_lang('Active'),false);
  333. $table->set_column_filter($col -1, 'active_filter');
  334. $table->set_header($col ++, get_lang('reg'), false);
  335. $table->set_column_filter($col -1, 'reg_filter');
  336. $table->set_form_actions(array ('subscribe' => get_lang('reg')), 'user');
  337. // Display table
  338. $table->display();
  339. if ( !empty($_POST['keyword']))
  340. {
  341. $keyword_name=Security::remove_XSS($_POST['keyword']);
  342. echo '<br/>'.get_lang('SearchResultsFor').' <span style="font-style: italic ;"> '.$keyword_name.' </span><br>';
  343. }
  344. /*
  345. ==============================================================================
  346. FOOTER
  347. ==============================================================================
  348. */
  349. Display :: display_footer();
  350. ?>