user.php 19 KB

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