user.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) various contributors
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  17. ==============================================================================
  18. */
  19. /**
  20. ==============================================================================
  21. * This script displays a list of the users of the current course.
  22. * Course admins can change user perimssions, subscribe and unsubscribe users...
  23. *
  24. * EXPERIMENTAL: support for virtual courses
  25. * - show users registered in virtual and real courses;
  26. * - only show the users of a virtual course if the current user;
  27. * is registered in that virtual course.
  28. *
  29. * Exceptions: platform admin and the course admin will see all virtual courses.
  30. * This is a new feature, there may be bugs.
  31. *
  32. * @todo possibility to edit user-course rights and view statistics for users in virtual courses
  33. * @todo convert normal table display to display function (refactor virtual course display function)
  34. * @todo display table functions need support for align and valign (e.g. to center text in cells) (this is now possible)
  35. * @author Roan Embrechts, refactoring + virtual courses support
  36. * @package dokeos.user
  37. ==============================================================================
  38. */
  39. /*
  40. ==============================================================================
  41. INIT SECTION
  42. ==============================================================================
  43. */
  44. // name of the language file that needs to be included
  45. $language_file = array('registration','admin','userInfo');
  46. require_once ("../inc/global.inc.php");
  47. $this_section = SECTION_COURSES;
  48. /*
  49. -----------------------------------------------------------
  50. Libraries
  51. -----------------------------------------------------------
  52. */
  53. require_once (api_get_path(LIBRARY_PATH)."debug.lib.inc.php");
  54. require_once (api_get_path(LIBRARY_PATH)."events.lib.inc.php");
  55. require_once (api_get_path(LIBRARY_PATH)."export.lib.inc.php");
  56. require_once (api_get_path(LIBRARY_PATH)."course.lib.php");
  57. require_once (api_get_path(LIBRARY_PATH).'sortabletable.class.php');
  58. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  59. require_once (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  60. require_once (api_get_path(LIBRARY_PATH).'groupmanager.lib.php');
  61. //CHECK KEYS
  62. if( !isset ($_cid))
  63. {
  64. header("location: ".$_configuration['root_web']);
  65. }
  66. /*
  67. -----------------------------------------------------------
  68. Constants and variables
  69. -----------------------------------------------------------
  70. */
  71. $currentCourseID = $_course['sysCode'];
  72. /*--------------------------------------
  73. Unregistering a user section
  74. --------------------------------------
  75. */
  76. if(api_is_allowed_to_edit())
  77. {
  78. if(isset($_POST['action']))
  79. {
  80. switch($_POST['action'])
  81. {
  82. case 'unsubscribe' :
  83. // Make sure we don't unsubscribe current user from the course
  84. $user_ids = array_diff($_POST['user'],array($_user['user_id']));
  85. if(count($user_ids) > 0)
  86. {
  87. CourseManager::unsubscribe_user($user_ids, $_SESSION['_course']['sysCode']);
  88. $message = get_lang('UsersUnsubscribed');
  89. }
  90. break;
  91. }
  92. }
  93. }
  94. if(api_is_allowed_to_edit())
  95. {
  96. if( isset ($_GET['action']))
  97. {
  98. switch ($_GET['action'])
  99. {
  100. case 'export' :
  101. if(api_get_setting('use_session_mode')!="true"){
  102. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  103. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  104. $course = api_get_course_info();
  105. $sql = "SELECT official_code,firstname,lastname,email FROM $table_user u, $table_course_user cu WHERE cu.user_id = u.user_id AND cu.course_code = '".$course['sysCode']."' ORDER BY lastname ASC";
  106. }
  107. else
  108. {
  109. $sql = "SELECT `user`.`user_id`, `user`.`lastname`, `user`.`firstname`,
  110. `user`.`email`, `user`.`official_code`, session.name
  111. FROM ".Database::get_main_table(TABLE_MAIN_USER)." `user`, ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)." cu, ".Database::get_main_table(TABLE_MAIN_SESSION)." session
  112. WHERE `user`.`user_id`= cu.`id_user`
  113. AND cu.`course_code`='".$currentCourseID."'
  114. AND session.id=cu.id_session
  115. AND session.id='".$_SESSION['id_session']."'";
  116. }
  117. $users = api_sql_query($sql, __FILE__, __LINE__);
  118. while ($user = mysql_fetch_array($users, MYSQL_ASSOC))
  119. {
  120. $data[] = $user;
  121. }
  122. switch ($_GET['type'])
  123. {
  124. case 'csv' :
  125. Export::export_table_csv($data);
  126. case 'xls' :
  127. Export::export_table_xls($data);
  128. }
  129. }
  130. }
  131. } // end if allowed to edit
  132. if(api_is_allowed_to_edit())
  133. {
  134. // Unregister user from course
  135. if($_GET['unregister'])
  136. {
  137. if(isset($_GET['user_id']) && is_numeric($_GET['user_id']) && $_GET['user_id'] != $_user['user_id'])
  138. {
  139. CourseManager::unsubscribe_user($_GET['user_id'],$_SESSION['_course']['sysCode']);
  140. $message = get_lang('UserUnsubscribed');
  141. }
  142. }
  143. } // end if allowed to edit
  144. /*
  145. ==============================================================================
  146. FUNCTIONS
  147. ==============================================================================
  148. */
  149. function display_user_search_form()
  150. {
  151. echo '<form method="get" action="user.php">';
  152. echo get_lang("SearchForUser") . "&nbsp;&nbsp;";
  153. echo '<input type="text" name="keyword" value="'.$_GET['keyword'].'"/>';
  154. echo '<input type="submit" value="'.get_lang('SearchButton').'"/>';
  155. echo '</form>';
  156. }
  157. /**
  158. * This function displays a list if users for each virtual course linked to the current
  159. * real course.
  160. *
  161. * defines globals
  162. *
  163. * @version 1.0
  164. * @author Roan Embrechts
  165. * @todo users from virtual courses always show "-" for the group related output. Edit and statistics columns are disabled * for these users, for now.
  166. */
  167. function show_users_in_virtual_courses()
  168. {
  169. global $_course, $_user;
  170. $real_course_code = $_course['sysCode'];
  171. $real_course_info = Database::get_course_info($real_course_code);
  172. $user_subscribed_virtual_course_list = CourseManager::get_list_of_virtual_courses_for_specific_user_and_real_course($_user['user_id'], $real_course_code);
  173. $number_of_virtual_courses = count($user_subscribed_virtual_course_list);
  174. $row = 0;
  175. $column_header[$row ++] = "ID";
  176. $column_header[$row ++] = get_lang("FullUserName");
  177. $column_header[$row ++] = get_lang("Role");
  178. $column_header[$row ++] = get_lang("Group");
  179. if( api_is_allowed_to_edit())
  180. {
  181. $column_header[$row ++] = get_lang("Tutor");
  182. }
  183. if( api_is_allowed_to_edit())
  184. {
  185. $column_header[$row ++] = get_lang("CourseManager");
  186. }
  187. //$column_header[$row++] = get_lang("Edit");
  188. //$column_header[$row++] = get_lang("Unreg");
  189. //$column_header[$row++] = get_lang("Tracking");
  190. if( !is_array($user_subscribed_virtual_course_list))
  191. return;
  192. foreach ($user_subscribed_virtual_course_list as $virtual_course)
  193. {
  194. $virtual_course_code = $virtual_course["code"];
  195. $virtual_course_user_list = CourseManager::get_user_list_from_course_code($virtual_course_code);
  196. $message = get_lang("RegisteredInVirtualCourse")." ".$virtual_course["title"]."&nbsp;&nbsp;(".$virtual_course["code"].")";
  197. echo "<br/>";
  198. echo "<h4>".$message."</h4>";
  199. $properties["width"] = "100%";
  200. $properties["cellspacing"] = "1";
  201. Display::display_complex_table_header($properties, $column_header);
  202. foreach ($virtual_course_user_list as $this_user)
  203. {
  204. $user_id = $this_user["user_id"];
  205. $loginname = $this_user["username"];
  206. $lastname = $this_user["lastname"];
  207. $firstname = $this_user["firstname"];
  208. $status = $this_user["status"];
  209. $role = $this_user["role"];
  210. if( $status == "1")
  211. $status = get_lang("CourseManager");
  212. else
  213. $status = " - ";
  214. //if(xxx['tutor'] == '0') $tutor = " - ";
  215. //else $tutor = get_lang("Tutor");
  216. $full_name = $lastname.", ".$firstname;
  217. if( $lastname == "" || $firstname == '')
  218. $full_name = $loginname;
  219. $user_info_hyperlink = "<a href=\"userInfo.php?".api_get_cidreq()."&origin=".$origin."&uInfo=".$user_id."&virtual_course=".$virtual_course["code"]."\">".$full_name."</a>";
  220. $row = 0;
  221. $table_row[$row ++] = $user_id;
  222. $table_row[$row ++] = $user_info_hyperlink; //Full name
  223. $table_row[$row ++] = $role; //Description
  224. $table_row[$row ++] = " - "; //Group, for the moment groups don't work for students in virtual courses
  225. if( api_is_allowed_to_edit())
  226. $table_row[$row ++] = " - "; //Tutor column
  227. if( api_is_allowed_to_edit())
  228. $table_row[$row ++] = $status; //Course Manager column
  229. Display::display_table_row($bgcolor, $table_row, true);
  230. }
  231. Display::display_table_footer();
  232. }
  233. }
  234. if(!$is_allowed_in_course){
  235. api_not_allowed(true);
  236. }
  237. /*
  238. -----------------------------------------------------------
  239. Header
  240. -----------------------------------------------------------
  241. */
  242. if( $origin != 'learnpath')
  243. {
  244. if (isset($_GET['keyword']))
  245. {
  246. $interbreadcrumb[] = array ("url" => "user.php", "name" => get_lang("Users"));
  247. $tool_name = get_lang('SearchResults');
  248. }
  249. else
  250. {
  251. $tool_name = get_lang('Users');
  252. }
  253. Display::display_header($tool_name, "User");
  254. }
  255. else
  256. {
  257. ?> <link rel="stylesheet" type="text/css" href="<?php echo api_get_path(WEB_CODE_PATH); ?>css/default.css" /> <?php
  258. }
  259. if( isset($message))
  260. {
  261. Display::display_normal_message($message);
  262. }
  263. /*
  264. ==============================================================================
  265. MAIN CODE
  266. ==============================================================================
  267. */
  268. //statistics
  269. event_access_tool(TOOL_USER);
  270. /*
  271. --------------------------------------
  272. Setting the permissions for this page
  273. --------------------------------------
  274. */
  275. $is_allowed_to_track = ($is_courseAdmin || $is_courseTutor) && $_configuration['tracking_enabled'];
  276. /*
  277. /*
  278. -----------------------------------------------------------
  279. Introduction section
  280. (editable by course admins)
  281. -----------------------------------------------------------
  282. */
  283. Display::display_introduction_section(TOOL_USER, $is_allowed);
  284. if( api_is_allowed_to_edit())
  285. {
  286. echo "<div align=\"right\">";
  287. echo '<a href="user.php?'.api_get_cidreq().'&action=export&amp;type=csv">'.Display::return_icon('excel.gif', get_lang('ExportAsCSV')).'&nbsp;'.get_lang('ExportAsCSV').'</a> | ';
  288. echo '<a href="subscribe_user.php?'.api_get_cidreq().'">'.Display::return_icon('add_user_big.gif',get_lang("SubscribeUserToCourse")).'&nbsp;'.get_lang("SubscribeUserToCourse").'</a> | ';
  289. echo "<a href=\"subscribe_user.php?".api_get_cidreq()."&type=teacher\">".Display::return_icon('add_user_big.gif', get_lang("SubscribeUserToCourseAsTeacher"))."&nbsp;".get_lang("SubscribeUserToCourseAsTeacher")."</a> | ";
  290. echo "<a href=\"../group/group.php?".api_get_cidreq()."\">".Display::return_icon('edit_group.gif', get_lang("GroupUserManagement"))."&nbsp;".get_lang("GroupUserManagement")."</a>";
  291. if(api_get_setting('use_session_mode')=='false')
  292. {
  293. echo ' | <a href="class.php?'.api_get_cidreq().'">'.get_lang('Classes').'</a>';
  294. }
  295. echo "</div>";
  296. }
  297. /*
  298. --------------------------------------
  299. DISPLAY USERS LIST
  300. --------------------------------------
  301. Also shows a "next page" button if there are
  302. more than 50 users.
  303. There's a bug in here somewhere - some users count as more than one if they are in more than one group
  304. --> code for > 50 users should take this into account
  305. (Roan, Feb 2004)
  306. */
  307. if( CourseManager::has_virtual_courses_from_code($course_id, $user_id))
  308. {
  309. $real_course_code = $_course['sysCode'];
  310. $real_course_info = Database::get_course_info($real_course_code);
  311. $message = get_lang("RegisteredInRealCourse")." ".$real_course_info["title"]."&nbsp;&nbsp;(".$real_course_info["official_code"].")";
  312. echo "<h4>".$message."</h4>";
  313. }
  314. /*
  315. ==============================================================================
  316. DISPLAY LIST OF USERS
  317. ==============================================================================
  318. */
  319. /**
  320. * * Get the users to display on the current page.
  321. */
  322. function get_number_of_users()
  323. {
  324. $counter=0;
  325. if(!empty($_SESSION["id_session"])){
  326. $a_course_users = CourseManager :: get_user_list_from_course_code($_SESSION['_course']['id'], true, $_SESSION['id_session']);
  327. }
  328. else{
  329. $a_course_users = CourseManager :: get_user_list_from_course_code($_SESSION['_course']['id'], true);
  330. }
  331. foreach($a_course_users as $user_id=>$o_course_user){
  332. if( (isset ($_GET['keyword']) && search_keyword($o_course_user['firstname'],$o_course_user['lastname'],$o_course_user['username'],$o_course_user['official_code'],$_GET['keyword'])) || !isset($_GET['keyword']) || empty($_GET['keyword'])){
  333. $counter++;
  334. }
  335. }
  336. return $counter;
  337. }
  338. function search_keyword($firstname,$lastname,$username,$official_code,$keyword){
  339. if(strripos($firstname,$keyword)!==false || strripos($lastname,$keyword)!==false || strripos($username,$keyword)!==false || strripos($official_code,$keyword)!==false){
  340. return true;
  341. }
  342. else{
  343. return false;
  344. }
  345. }
  346. function sort_users($a,$b){
  347. $a = trim(strtolower($a[$_GET['users_column']]));
  348. $b = trim(strtolower($b[$_GET['users_column']]));
  349. if($_GET['users_direction'] == 'DESC')
  350. return strcmp($b, $a);
  351. else
  352. return strcmp($a, $b);
  353. }
  354. /**
  355. * Get the users to display on the current page.
  356. */
  357. function get_user_data($from, $number_of_items, $column, $direction)
  358. {
  359. $a_users=array();
  360. if(!empty($_SESSION["id_session"])){
  361. $a_course_users = CourseManager :: get_user_list_from_course_code($_SESSION['_course']['id'], true, $_SESSION['id_session']);
  362. }
  363. else{
  364. $a_course_users = CourseManager :: get_user_list_from_course_code($_SESSION['_course']['id'], true);
  365. }
  366. foreach($a_course_users as $user_id=>$o_course_user){
  367. if( (isset ($_GET['keyword']) && search_keyword($o_course_user['firstname'],$o_course_user['lastname'],$o_course_user['username'],$o_course_user['official_code'],$_GET['keyword'])) || !isset($_GET['keyword']) || empty($_GET['keyword'])){
  368. $groups_name=GroupManager :: get_user_group_name($user_id);
  369. if(api_is_allowed_to_edit()){
  370. $temp=array();
  371. $temp[] = $user_id;
  372. $temp[] = $o_course_user['official_code'];
  373. $temp[] = $o_course_user['lastname'];
  374. $temp[] = $o_course_user['firstname'];
  375. $temp[] = $o_course_user['role'];
  376. $temp[] = implode(', ',$groups_name); //Group
  377. if(isset($o_course_user['tutor_id']) && $o_course_user['tutor_id']==1)
  378. $temp[] = get_lang('Tutor');
  379. else
  380. $temp[] = '-';
  381. if(isset($o_course_user['status']) && $o_course_user['status']==1)
  382. $temp[] = get_lang('CourseManager');
  383. else
  384. $temp[] = '-';
  385. $temp[] = $user_id;
  386. }
  387. else{
  388. $temp=array();
  389. $temp[] = $o_course_user['official_code'];
  390. $temp[] = $o_course_user['lastname'];
  391. $temp[] = $o_course_user['firstname'];
  392. $temp[] = $o_course_user['role'];
  393. $temp[] = implode(', ',$groups_name);//Group
  394. $temp[] = $user_id;
  395. }
  396. $a_users[$user_id] = $temp;
  397. }
  398. }
  399. usort($a_users, 'sort_users');
  400. return $a_users;
  401. }
  402. /**
  403. * Build the modify-column of the table
  404. * @param int $user_id The user id
  405. * @return string Some HTML-code
  406. */
  407. function modify_filter($user_id)
  408. {
  409. global $origin,$_user, $_course, $is_allowed_to_track;
  410. $result="<div style='text-align: center'>";
  411. // info
  412. $result .= '<a href="userInfo.php?'.api_get_cidreq().'&origin='.$origin.'&amp;uInfo='.$user_id.'"><img border="0" alt="'.get_lang('Info').'" src="../img/user_info.gif" /></a>&nbsp;';
  413. if($is_allowed_to_track)
  414. {
  415. $result .= '<a href="../mySpace/myStudents.php?'.api_get_cidreq().'&student='.$user_id.'&amp;details=true&amp;course='.$_course['id'].'&amp;origin=user_course"><img border="0" alt="'.get_lang('Tracking').'" src="../img/statistics.gif" /></a>&nbsp;';
  416. }
  417. if(api_is_allowed_to_edit())
  418. {
  419. // edit
  420. $result .= '<a href="userInfo.php?'.api_get_cidreq().'&origin='.$origin.'&amp;editMainUserInfo='.$user_id.'"><img border="0" alt="'.get_lang('Edit').'" src="../img/edit.gif" /></a>&nbsp;';
  421. // unregister
  422. if( $user_id != $_user['user_id'])
  423. {
  424. $result .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&unregister=yes&amp;user_id='.$user_id.'&amp;'.$sort_params.'" onclick="javascript:if(!confirm(\''.addslashes(htmlentities(get_lang('ConfirmYourChoice'))).'\')) return false;"><img border="0" alt="'.get_lang("Unreg").'" src="../img/delete.gif"/></a>';
  425. }
  426. }
  427. $result.="</div>";
  428. return $result;
  429. }
  430. $default_column = api_is_allowed_to_edit() ? 1 : 0;
  431. $table = new SortableTable('users', 'get_number_of_users', 'get_user_data',$default_column);
  432. $parameters['keyword'] = $_GET['keyword'];
  433. $table->set_additional_parameters($parameters);
  434. $header_nr = 0;
  435. if( api_is_allowed_to_edit())
  436. {
  437. $table->set_header($header_nr++, '', false);
  438. }
  439. $table->set_header($header_nr++, get_lang('OfficialCode'));
  440. $table->set_header($header_nr++, get_lang('LastName'));
  441. $table->set_header($header_nr++, get_lang('FirstName'));
  442. $table->set_header($header_nr++, get_lang('Description'));
  443. $table->set_header($header_nr++, get_lang('GroupSingle'),false);
  444. if( api_is_allowed_to_edit())
  445. {
  446. $table->set_header($header_nr++, get_lang('Tutor'));
  447. $table->set_header($header_nr++, get_lang('CourseManager'));
  448. }
  449. //actions column
  450. $table->set_header($header_nr++, '', false);
  451. $table->set_column_filter($header_nr-1,'modify_filter');
  452. if( api_is_allowed_to_edit())
  453. {
  454. $table->set_form_actions(array ('unsubscribe' => get_lang('Unreg')), 'user');
  455. }
  456. // Build search-form
  457. $form = new FormValidator('search_user', 'get','','',null,false);
  458. $renderer = & $form->defaultRenderer();
  459. $renderer->setElementTemplate('<span>{element}</span> ');
  460. $form->add_textfield('keyword', '', false);
  461. $form->addElement('submit', 'submit', get_lang('SearchButton'));
  462. $form->display();
  463. echo '<br />';
  464. $table->display();
  465. if( get_setting('allow_user_headings') == 'true' && $is_courseAdmin && api_is_allowed_to_edit() && $origin != 'learnpath') // only course administrators see this line
  466. {
  467. echo "<div align=\"right\">", "<form method=\"post\" action=\"userInfo.php\">", get_lang("CourseAdministratorOnly"), " : ", "<input type=\"submit\" name=\"viewDefList\" value=\"".get_lang("DefineHeadings")."\" />", "</form>", "</div>\n";
  468. }
  469. //User list of the virtual courses linked to this course.
  470. //show_users_in_virtual_courses($is_allowed_to_track);
  471. /*
  472. ==============================================================================
  473. FOOTER
  474. ==============================================================================
  475. */
  476. if( $origin != 'learnpath')
  477. {
  478. Display::display_footer();
  479. }
  480. ?>