file ongoing.html was added on branch MOODLE_15_STABLE on 2005-07-07 16:14:36 +0000
[moodle.git] / user / index.php
blob5f1478d35e9e0a67e46a176a5d978fa72cc76d15
1 <?PHP // $Id$
3 // Lists all the users within a given course
5 require_once('../config.php');
6 require_once($CFG->libdir.'/tablelib.php');
8 define('USER_SMALL_CLASS', 20); // Below this is considered small
9 define('USER_LARGE_CLASS', 200); // Above this is considered large
10 define('DEFAULT_PAGE_SIZE', 20);
12 $id = required_param('id', PARAM_INT); // Course id
13 $group = optional_param('group', -1, PARAM_INT); // Group to show
14 $page = optional_param('page', 0, PARAM_INT); // which page to show
15 $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // how many per page
16 $mode = optional_param('mode', NULL); // '0' for less details, '1' for more
17 $showteachers = optional_param('teachers', 1, PARAM_INT); // do we want to see the teacher list?
19 if (! $course = get_record('course', 'id', $id)) {
20 error("Course ID is incorrect");
23 require_login($course->id);
25 if (!$course->category) {
26 if (!$CFG->showsiteparticipantslist and !isteacher(SITEID)) {
27 notice(get_string('sitepartlist0'));
29 if ($CFG->showsiteparticipantslist < 2 and !isteacherinanycourse()) {
30 notice(get_string('sitepartlist1'));
34 add_to_log($course->id, 'user', 'view all', 'index.php?id='.$course->id, '');
36 $isteacher = isteacher($course->id);
38 $countries = get_list_of_countries();
40 $strnever = get_string('never');
42 $datestring->day = get_string('day');
43 $datestring->days = get_string('days');
44 $datestring->hour = get_string('hour');
45 $datestring->hours = get_string('hours');
46 $datestring->min = get_string('min');
47 $datestring->mins = get_string('mins');
48 $datestring->sec = get_string('sec');
49 $datestring->secs = get_string('secs');
51 if ($mode !== NULL) {
52 $SESSION->userindexmode = $fullmode = ($mode == 1);
53 } else if (isset($SESSION->userindexmode)) {
54 $fullmode = $SESSION->userindexmode;
55 } else {
56 $fullmode = false;
59 /// Check to see if groups are being used in this forum
60 /// and if so, set $currentgroup to reflect the current group
62 $groupmode = groupmode($course); // Groups are being used
63 $currentgroup = get_and_set_current_group($course, $groupmode, $group);
65 if (!$currentgroup) { // To make some other functions work better later
66 $currentgroup = NULL;
69 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and $course->groupmodeforce and
70 !isteacheredit($course->id));
72 if ($isseparategroups and (!$currentgroup) ) { //XXX
73 print_heading(get_string("notingroup", "forum"));
74 print_footer($course);
75 exit;
78 // Should use this variable so that we don't break stuff every time a variable is added or changed.
79 $baseurl = $CFG->wwwroot.'/user/index.php?id='.$course->id.'&amp;group='.$currentgroup.'&amp;perpage='.$perpage.'&amp;teachers='.$showteachers;
81 /// Print headers
83 if ($course->category) {
84 print_header("$course->shortname: ".get_string('participants'), $course->fullname,
85 "<a href=\"../course/view.php?id=$course->id\">$course->shortname</a> -> ".
86 get_string('participants'), "", "", true, "&nbsp;", navmenu($course));
87 } else {
88 print_header("$course->shortname: ".get_string('participants'), $course->fullname,
89 get_string('participants'), "", "", true, "&nbsp;", navmenu($course));
92 /// Print settings and things in a table across the top
94 echo '<table class="controls" cellspacing="0"><tr>';
96 if ($groupmode == VISIBLEGROUPS or ($groupmode and isteacheredit($course->id))) {
97 if ($groups = get_records_menu("groups", "courseid", $course->id, "name ASC", "id,name")) {
98 echo '<td class="left">';
99 print_group_menu($groups, $groupmode, $currentgroup, $baseurl);
100 echo '</td>';
104 echo '<td class="right">';
105 echo get_string('userlist').': ';
106 $formatmenu = array( '0' => get_string('detailedless'),
107 '1' => get_string('detailedmore'));
108 echo popup_form($baseurl.'&amp;mode=', $formatmenu, 'formatmenu', $fullmode, '', '', '', true);
109 echo '</td></tr></table>';
111 if ($currentgroup and (!$isseparategroups or isteacheredit($course->id))) { /// Display info about the group
112 if ($group = get_record('groups', 'id', $currentgroup)) {
113 if (!empty($group->description) or (!empty($group->picture) and empty($group->hidepicture))) {
114 echo '<table class="groupinfobox"><tr><td class="left side picture">';
115 print_group_picture($group, $course->id, true, false, false);
116 echo '</td><td class="content">';
117 echo '<h3>'.$group->name;
118 if (isteacheredit($course->id)) {
119 echo '&nbsp;<a title="'.get_string('editgroupprofile').'" href="../course/groups.php?id='.$course->id.'&amp;group='.$group->id.'">';
120 echo '<img src="'.$CFG->pixpath.'/t/edit.gif" alt="" border="0">';
121 echo '</a>';
123 echo '</h3>';
124 echo format_text($group->description);
125 echo '</td></tr></table>';
131 $exceptions = array(); // This will be an array of userids that are shown as teachers and thus
132 // do not have to be shown as users as well. Only relevant on site course.
134 if($showteachers) {
136 $tablecolumns = array('picture', 'fullname', 'city', 'country', 'lastaccess');
137 $tableheaders = array('', get_string('fullname'), get_string('city'), get_string('country'), get_string('lastaccess'));
139 $table = new flexible_table('user-index-teachers');
141 $table->define_columns($tablecolumns);
142 $table->define_headers($tableheaders);
143 $table->define_baseurl($baseurl);
145 $table->sortable(true, 'lastaccess', SORT_DESC);
147 $table->set_attribute('cellspacing', '0');
148 $table->set_attribute('id', 'teachers');
149 $table->set_attribute('class', 'generaltable generalbox');
151 $table->setup();
153 if($whereclause = $table->get_sql_where()) {
154 $whereclause .= ' AND ';
157 $teachersql = "SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, u.maildigest,
158 u.email, u.maildisplay, u.city, u.country, u.lastlogin, u.picture, u.lang, u.timezone,
159 u.emailstop, t.authority,t.role,t.editall,t.timeaccess as lastaccess, m.groupid
160 FROM {$CFG->prefix}user u
161 LEFT JOIN {$CFG->prefix}user_teachers t ON t.userid = u.id
162 LEFT JOIN {$CFG->prefix}groups_members m ON m.userid = u.id ";
164 if($isseparategroups) {
165 $whereclause .= '(t.editall OR groupid = '.$currentgroup.') AND ';
167 else if ($currentgroup) { // Displaying a group by choice
168 $whereclause .= 'groupid = '.$currentgroup.' AND ';
171 $teachersql .= 'WHERE '.$whereclause.' t.course = '.$course->id.' AND u.deleted = 0 AND u.confirmed = 1 AND t.authority > 0';
173 if($sortclause = $table->get_sql_sort()) {
174 $teachersql .= ' ORDER BY '.$sortclause;
177 $teachers = get_records_sql($teachersql);
179 if(!empty($teachers)) {
181 echo '<h2>'.$course->teachers;
182 echo ' <a href="'.$baseurl.'&amp;teachers=0">';
183 echo '<img src="'.$CFG->pixpath.'/i/hide.gif" height="16" width="16" alt="'.get_string('hide').'" /></a>';
184 if (isadmin() or ($course->category and (iscreator() or (isteacheredit($course->id) and !empty($CFG->teacherassignteachers))))) {
185 echo ' <a href="'.$CFG->wwwroot.'/course/teacher.php?id='.$course->id.'">';
186 echo '<img src="'.$CFG->pixpath.'/i/edit.gif" height="16" width="16" alt="'.get_string('edit').'" /></a>';
188 echo '</h2>';
190 $exceptions = array_keys($teachers);
192 if ($fullmode) {
193 foreach ($teachers as $key => $teacher) {
194 print_user($teacher, $course);
196 } else {
197 $countrysort = (strpos($sortclause, 'country') !== false);
198 foreach ($teachers as $teacher) {
200 if ($teacher->lastaccess) {
201 $lastaccess = format_time(time() - $teacher->lastaccess, $datestring);
202 } else {
203 $lastaccess = $strnever;
206 if (empty($teacher->country)) {
207 $country = '';
209 else {
210 if($countrysort) {
211 $country = '('.$teacher->country.') '.$countries[$teacher->country];
213 else {
214 $country = $countries[$teacher->country];
218 $table->add_data(array (
219 //'<input type="checkbox" name="userid[]" value="'.$teacher->id.'" />',
220 print_user_picture($teacher->id, $course->id, $teacher->picture, false, true),
221 '<strong><a href="'.$CFG->wwwroot.'/user/view.php?id='.$teacher->id.'&amp;course='.$course->id.'">'.fullname($teacher, $isteacher).'</a></strong>',
222 $teacher->city,
223 $country,
224 $lastaccess));
227 $table->print_html();
231 else {
232 // Don't show teachers
233 echo '<h2>'.$course->teachers;
234 echo ' <a href="'.$baseurl.'&amp;teachers=1">';
235 echo '<img src="'.$CFG->pixpath.'/i/show.gif" height="16" width="16" alt="'.get_string('show').'" /></a>';
236 if (isadmin() or ($course->category and (iscreator() or (isteacheredit($course->id) and !empty($CFG->teacherassignteachers))))) {
237 echo ' <a href="'.$CFG->wwwroot.'/course/teacher.php?id='.$course->id.'">';
238 echo '<img src="'.$CFG->pixpath.'/i/edit.gif" height="16" width="16" alt="'.get_string('edit').'" /></a>';
240 echo '</h2>';
243 $guest = get_guest();
244 $exceptions[] = $guest->id;
246 $tablecolumns = array('picture', 'fullname', 'city', 'country', 'lastaccess');
247 $tableheaders = array('', get_string('fullname'), get_string('city'), get_string('country'), get_string('lastaccess'));
249 $table = new flexible_table('user-index-students');
251 $table->define_columns($tablecolumns);
252 $table->define_headers($tableheaders);
253 $table->define_baseurl($baseurl);
255 $table->sortable(true, 'lastaccess', SORT_DESC);
257 $table->set_attribute('cellspacing', '0');
258 $table->set_attribute('id', 'students');
259 $table->set_attribute('class', 'generaltable generalbox');
261 $table->set_control_variables(array(
262 TABLE_VAR_SORT => 'ssort',
263 TABLE_VAR_HIDE => 'shide',
264 TABLE_VAR_SHOW => 'sshow',
265 TABLE_VAR_IFIRST => 'sifirst',
266 TABLE_VAR_ILAST => 'silast',
267 TABLE_VAR_PAGE => 'spage'
269 $table->setup();
271 if(SITEID == $course->id) {
272 $select = 'SELECT u.id, u.username, u.firstname, u.lastname, u.email, u.city, u.country,
273 u.picture, u.lang, u.timezone, u.emailstop, u.maildisplay, u.lastaccess ';
274 $from = 'FROM '.$CFG->prefix.'user u ';
275 $where = 'WHERE confirmed = 1 AND u.deleted = 0 ';
277 else {
278 $select = 'SELECT u.id, u.username, u.firstname, u.lastname, u.email, u.city, u.country,
279 u.picture, u.lang, u.timezone, u.emailstop, u.maildisplay, s.timeaccess AS lastaccess ';
280 $from = 'FROM '.$CFG->prefix.'user u LEFT JOIN '.$CFG->prefix.'user_students s ON s.userid = u.id ';
281 $where = 'WHERE s.course = '.$course->id.' AND u.deleted = 0 ';
284 if ($currentgroup) { // Displaying a group by choice
285 // FIX: TODO: This will not work if $currentgroup == 0, i.e. "those not in a group"
286 $from .= 'LEFT JOIN '.$CFG->prefix.'groups_members gm ON u.id = gm.userid ';
287 $where .= ' AND gm.groupid = '.$currentgroup;
290 if($course->id == SITEID) {
291 $where .= ' AND u.id NOT IN ('.implode(',', $exceptions).')';
294 $totalcount = count_records_sql('SELECT COUNT(*) '.$from.$where);
296 if($table->get_sql_where()) {
297 $where .= ' AND '.$table->get_sql_where();
300 if($table->get_sql_sort()) {
301 $sort = ' ORDER BY '.$table->get_sql_sort();
303 else {
304 $sort = '';
307 if($table->get_sql_sort()) {
308 $sort = ' ORDER BY '.$table->get_sql_sort();
310 else {
311 $sort = '';
314 $matchcount = count_records_sql('SELECT COUNT(*) '.$from.$where);
316 $table->initialbars($totalcount > $perpage);
317 $table->pagesize($perpage, $matchcount);
319 if($table->get_page_start() !== '' && $table->get_page_size() !== '') {
320 $limit = ' '.sql_paging_limit($table->get_page_start(), $table->get_page_size());
322 else {
323 $limit = '';
326 $students = get_records_sql($select.$from.$where.$sort.$limit);
328 $a->count = $totalcount;
329 $a->items = $totalcount == 1 ? $course->student : $course->students;
330 echo '<h2>'.get_string('counteditems', '', $a);
331 if (isteacheredit($course->id)) {
332 echo ' <a href="../course/student.php?id='.$course->id.'">';
333 echo '<img src="'.$CFG->pixpath.'/i/edit.gif" height="16" width="16" alt="" /></a>';
335 echo '</h2>';
337 if ($CFG->longtimenosee > 0 && $CFG->longtimenosee < 1000 && $totalcount > 0) {
338 echo '<p id="longtimenosee">('.get_string('unusedaccounts', '', $CFG->longtimenosee).')</p>';
341 if ($fullmode) { // Print simple listing
342 if ($totalcount < 1) {
343 print_heading(get_string("nostudentsfound", "", $course->students));
345 else {
347 if($totalcount > $perpage) {
349 $firstinitial = $table->get_initial_first();
350 $lastinitial = $table->get_initial_last();
351 $strall = get_string('all');
352 $alpha = explode(',', get_string('alphabet'));
354 // Bar of first initials
356 echo '<div class="initialbar firstinitial">'.get_string('firstname').' : ';
357 if(!empty($firstinitial)) {
358 echo '<a href="'.$baseurl.'&amp;sifirst=">'.$strall.'</a>';
359 } else {
360 echo '<strong>'.$strall.'</strong>';
362 foreach ($alpha as $letter) {
363 if ($letter == $firstinitial) {
364 echo ' <strong>'.$letter.'</strong>';
365 } else {
366 echo ' <a href="'.$baseurl.'&amp;sifirst='.$letter.'">'.$letter.'</a>';
369 echo '</div>';
371 // Bar of last initials
373 echo '<div class="initialbar lastinitial">'.get_string('lastname').' : ';
374 if(!empty($lastinitial)) {
375 echo '<a href="'.$baseurl.'&amp;silast=">'.$strall.'</a>';
376 } else {
377 echo '<strong>'.$strall.'</strong>';
379 foreach ($alpha as $letter) {
380 if ($letter == $lastinitial) {
381 echo ' <strong>'.$letter.'</strong>';
382 } else {
383 echo ' <a href="'.$baseurl.'&amp;silast='.$letter.'">'.$letter.'</a>';
386 echo '</div>';
388 print_paging_bar($matchcount, intval($table->get_page_start() / $perpage), $perpage, $baseurl.'&amp;', 'spage');
392 if($matchcount > 0) {
393 foreach ($students as $student) {
394 print_user($student, $course);
397 else {
398 print_heading(get_string('nothingtodisplay'));
402 else {
403 $countrysort = (strpos($sort, 'country') !== false);
404 if (!empty($students)) {
405 foreach ($students as $student) {
406 if ($student->lastaccess) {
407 $lastaccess = format_time(time() - $student->lastaccess, $datestring);
408 } else {
409 $lastaccess = $strnever;
412 if (empty($student->country)) {
413 $country = '';
415 else {
416 if($countrysort) {
417 $country = '('.$student->country.') '.$countries[$student->country];
419 else {
420 $country = $countries[$student->country];
424 $table->add_data(array (
425 //'<input type="checkbox" name="userid[]" value="'.$teacher->id.'" />',
426 print_user_picture($student->id, $course->id, $student->picture, false, true),
427 '<strong><a href="'.$CFG->wwwroot.'/user/view.php?id='.$student->id.'&amp;course='.$course->id.'">'.fullname($student).'</a></strong>',
428 $student->city,
429 $country,
430 $lastaccess));
434 $table->print_html();
438 if ($perpage == 99999) {
439 echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage='.DEFAULT_PAGE_SIZE.'">'.get_string('showperpage', '', DEFAULT_PAGE_SIZE).'</a></div>';
441 else if ($matchcount > 0 && $perpage < $matchcount) {
442 echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage=99999">'.get_string('showall', '', $matchcount).'</a></div>';
445 print_footer($course);