Merging from STABLE:
[moodle.git] / user / index.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
3// Lists all the users within a given course
4
b0e3a925 5 require_once("../config.php");
68b38e8d 6 require_once($CFG->libdir.'/tablelib.php');
951b22a8 7
8 define('USER_SMALL_CLASS', 20); // Below this is considered small
9 define('USER_LARGE_CLASS', 200); // Above this is considered large
f9903ed0 10
11 require_variable($id); //course
d578afc8 12 optional_variable($sort, "lastaccess"); //how to sort students
4969ad74 13 optional_variable($dir,"desc"); //how to sort students
14 optional_variable($page, "0"); // which page to show
a328425d 15 optional_variable($lastinitial, ""); // only show students with this last initial
16 optional_variable($firstinitial, ""); // only show students with this first initial
4969ad74 17 optional_variable($perpage, "20"); // how many per page
ff3caf30 18 optional_variable($group, "-1"); // Group to show
19 $mode = optional_param('mode', NULL); // '0' for less details, '1' for more
20
f9903ed0 21 if (! $course = get_record("course", "id", $id)) {
22 error("Course ID is incorrect");
23 }
24
25 require_login($course->id);
26
4bc162b7 27 if (!$course->category) {
28 if (!$CFG->showsiteparticipantslist and !isteacher(SITEID)) {
29 notice(get_string('sitepartlist0'));
30 }
9407d456 31 if ($CFG->showsiteparticipantslist < 2 and !isteacherinanycourse()) {
4bc162b7 32 notice(get_string('sitepartlist1'));
33 }
34 }
35
da3a08d7 36 add_to_log($course->id, "user", "view all", "index.php?id=$course->id", "");
f9903ed0 37
951b22a8 38 $isteacher = isteacher($course->id);
4969ad74 39 $showteachers = ($page == 0 and $sort == "lastaccess" and $dir == "desc");
40
68b38e8d 41 $countries = get_list_of_countries();
42
43 $strnever = get_string("never");
44
45 $datestring->day = get_string("day");
46 $datestring->days = get_string("days");
47 $datestring->hour = get_string("hour");
48 $datestring->hours = get_string("hours");
49 $datestring->min = get_string("min");
50 $datestring->mins = get_string("mins");
51 $datestring->sec = get_string("sec");
52 $datestring->secs = get_string("secs");
53
4969ad74 54 if ($showteachers) {
55 $participantslink = get_string("participants");
d578afc8 56 } else {
4969ad74 57 $participantslink = "<a href=\"index.php?id=$course->id\">".get_string("participants")."</a>";
d578afc8 58 }
59
ff3caf30 60 if ($mode !== NULL) {
61 $SESSION->userindexmode = $fullmode = ($mode == 1);
62 } else if (isset($SESSION->userindexmode)) {
63 $fullmode = $SESSION->userindexmode;
64 } else {
65 $fullmode = false;
66 }
67
68
69
70/// Check to see if groups are being used in this forum
71/// and if so, set $currentgroup to reflect the current group
72
73 $changegroup = isset($_GET['group']) ? $_GET['group'] : -1; // Group change requested?
74 $groupmode = groupmode($course); // Groups are being used
75 $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
76
77
4bc162b7 78 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and $course->groupmodeforce and
2cb2ce61 79 !isteacheredit($course->id));
80
ff3caf30 81 if ($isseparategroups and (!$currentgroup) ) { //XXX
82 print_heading(get_string("notingroup", "forum"));
83 print_footer($course);
84 exit;
85 }
86
87 if (!$currentgroup) { // To make some other functions work better later
88 $currentgroup = NULL;
89 }
90
91
92/// Print headers
2cb2ce61 93
4969ad74 94 if ($course->category) {
95 print_header("$course->shortname: ".get_string("participants"), "$course->fullname",
031c49fa 96 "<a href=\"../course/view.php?id=$course->id\">$course->shortname</a> -> ".
951b22a8 97 "$participantslink", "", "", true, "&nbsp;", navmenu($course));
dc0dc7d5 98 } else {
4bc162b7 99 print_header("$course->shortname: ".get_string("participants"), "$course->fullname",
951b22a8 100 "$participantslink", "", "", true, "&nbsp;", navmenu($course));
4969ad74 101 }
102
ff3caf30 103/// Print settings and things in a table across the top
104
105 echo '<table width="100%" border="0" cellpadding="3" cellspacing="0"><tr valign="top">';
106
107 if ($groupmode == VISIBLEGROUPS or ($groupmode and isteacheredit($course->id))) {
108 if ($groups = get_records_menu("groups", "courseid", $course->id, "name ASC", "id,name")) {
109 echo '<td class="left">';
110 print_group_menu($groups, $groupmode, $currentgroup, "index.php?id=$course->id");
111 echo '</td>';
112 }
113 }
114
115 echo '<td class="right" align="right">';
4bc162b7 116 echo get_string('userlist').': ';
ff3caf30 117 $formatmenu = array( '0' => get_string('detailedless'),
118 '1' => get_string('detailedmore'));
119 echo popup_form("index.php?id=$id&amp;sort=$sort&amp;dir=$dir&amp;perpage=$perpage&amp;lastinitial=$lastinitial&amp;mode=", $formatmenu, 'formatmenu', $fullmode, '', '', '', true);
120 echo '</td></tr></table>';
121
122 if ($currentgroup and (!$isseparategroups or isteacheredit($course->id))) { /// Display info about the group
123 if ($group = get_record('groups', 'id', $currentgroup)) {
124 if (!empty($group->description) or (!empty($group->picture) and empty($group->hidepicture))) {
125 echo '<table class="groupinfobox"><tr><td class="left side picture">';
126 print_group_picture($group, $course->id, true, false, false);
127 echo '</td><td class="content">';
128 echo '<h3>'.$group->name;
2c27cd19 129 if (isteacheredit($course->id)) {
130 echo '&nbsp;<a title="'.get_string('editgroupprofile').'" href="../course/groups.php?id='.$course->id.'&amp;group='.$group->id.'">';
131 echo '<img src="'.$CFG->pixpath.'/t/edit.gif" alt="" border="0">';
132 echo '</a>';
133 }
ff3caf30 134 echo '</h3>';
135 echo format_text($group->description);
136 echo '</td></tr></table>';
137 }
138 }
139 }
140
4bc162b7 141
62b80756 142 $exceptions = ''; // This will be a list of userids that are shown as teachers and thus
143 // do not have to be shown as users as well. Only relevant on site course.
68b38e8d 144
145 if($showteachers) {
146
147echo '<style type="text/css"> body#user-index table#teachers { margin: auto; width: 80%; } body#user-index table#teachers td, body#user-index table#teachers th {vertical-align: middle; padding: 4px;}</style>';
148
149 $tablecolumns = array('picture', 'fullname', 'city', 'country', 'lastaccess');
150 $tableheaders = array('', get_string('fullname'), get_string('city'), get_string('country'), get_string('lastaccess'));
151
152 $table = new flexible_table('user-index-teacher');
153
154 $table->define_columns($tablecolumns);
155 $table->define_headers($tableheaders);
156 $table->define_baseurl($CFG->wwwroot.'/user/index.php?id='.$course->id);
157
158 $table->sortable(true);
159
160 $table->set_attribute('cellspacing', '0');
161 $table->set_attribute('id', 'teachers');
162 $table->set_attribute('class', 'generaltable generalbox');
163
164 $table->setup();
165
166 if($whereclause = $table->get_sql_where()) {
167 $whereclause .= ' AND ';
168 }
169
170 $teachersql = "SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, u.maildigest,
171 u.email, u.city, u.country, u.lastlogin, u.picture, u.lang, u.timezone,
172 u.emailstop, t.authority,t.role,t.editall,t.timeaccess as lastaccess, m.groupid
173 FROM {$CFG->prefix}user u
174 LEFT JOIN {$CFG->prefix}user_teachers t ON t.userid = u.id
175 LEFT JOIN {$CFG->prefix}groups_members m ON m.userid = u.id ";
176
177 if($isseparategroups) {
178 $whereclause .= '(t.editall OR groupid = '.$currentgroup.') AND ';
179 }
180 else if ($currentgroup) { // Displaying a group by choice
181 $whereclause .= 'groupid = '.$currentgroup.' AND ';
182 }
183
184 $teachersql .= 'WHERE '.$whereclause.' t.course = '.$course->id.' AND u.deleted = 0 AND u.confirmed = 1 AND t.authority > 0';
185
186 if($sortclause = $table->get_sql_sort()) {
187 $teachersql .= ' ORDER BY '.$sortclause;
188 }
189
190 $teachers = get_records_sql($teachersql);
191
192 if(!empty($teachers)) {
193
03f191ad 194 echo "<h2 align=\"center\">$course->teachers";
ec99b88e 195 if (isadmin() or ($course->category and (iscreator() or (isteacheredit($course->id) and !empty($CFG->teacherassignteachers))))) {
03f191ad 196 echo ' <a href="../course/teacher.php?id='.$course->id.'">';
9a58f7cb 197 echo '<img src="'.$CFG->pixpath.'/i/edit.gif" height="16" width="16" alt="" /></a>';
03f191ad 198 }
199 echo '</h2>';
480dda61 200
ff3caf30 201 $exceptions .= implode(',', array_keys($teachers));
68b38e8d 202
ff3caf30 203 if ($fullmode) {
204 foreach ($teachers as $key => $teacher) {
205 print_user($teacher, $course);
4969ad74 206 }
ff3caf30 207 } else {
68b38e8d 208 foreach ($teachers as $teacher) {
209
210 if ($teacher->lastaccess) {
211 $lastaccess = format_time(time() - $teacher->lastaccess, $datestring);
212 } else {
213 $lastaccess = $strnever;
214 }
215
216 $table->add_data(array (
217 //'<input type="checkbox" name="userid[]" value="'.$teacher->id.'" />',
218 print_user_picture($teacher->id, $course->id, $teacher->picture, false, true),
219 '<strong><a href="'.$CFG->wwwroot.'/user/view.php?id='.$teacher->id.'&amp;course='.$course->id.'">'.fullname($teacher, $isteacher).'</a></strong>',
220 $teacher->city,
221 $teacher->country ? $countries[$teacher->country] : '',
222 $lastaccess));
223 }
4969ad74 224 }
68b38e8d 225
226 $table->print_html();
4969ad74 227 }
dc0dc7d5 228 }
ff3caf30 229
62b80756 230 $guest = get_guest();
231 $exceptions .= $guest->id;
4bc162b7 232
222ac91b 233 if ($course->id == SITEID) { // Show all site users (even unconfirmed)
ff3caf30 234 $students = get_users(true, '', true, $exceptions, $sort.' '.$dir,
235 $firstinitial, $lastinitial, $page*$perpage, $perpage);
62b80756 236 $totalcount = get_users(false, '', true, '', '', '', '') - 1; // -1 to not count guest user
237 if ($firstinitial or $lastinitial) {
238 $matchcount = get_users(false, '', true, '', '', $firstinitial, $lastinitial) - 1;
239 } else {
240 $matchcount = $totalcount;
241 }
4d744a22 242 } else {
62b80756 243 if ($sort == "lastaccess") {
244 $dsort = "s.timeaccess";
245 } else {
246 $dsort = "u.$sort";
247 }
ff3caf30 248 $students = get_course_students($course->id, $dsort, $dir, $page*$perpage, $perpage,
249 $firstinitial, $lastinitial, $currentgroup);
62b80756 250 $totalcount = count_course_students($course, "", "", "", $currentgroup);
251 if ($firstinitial or $lastinitial) {
252 $matchcount = count_course_students($course, "", $firstinitial, $lastinitial, $currentgroup);
253 } else {
254 $matchcount = $totalcount;
255 }
2cb2ce61 256 }
a328425d 257
eb58929b 258 $a->count = $totalcount;
259 $a->items = $course->students;
260 echo '<h2 align="center">'.get_string('counteditems', '', $a);
03f191ad 261 if (isteacheredit($course->id)) {
262 echo ' <a href="../course/student.php?id='.$course->id.'">';
031c49fa 263 echo '<img src="'.$CFG->pixpath.'/i/edit.gif" height="16" width="16" alt="" /></a>';
03f191ad 264 }
265 echo '</h2>';
dc95f4a1 266
333f44da 267 if (($CFG->longtimenosee > 0) and ($CFG->longtimenosee < 1000) and (!$page) and ($sort == "lastaccess")) {
2107c1f7 268 echo '<p id="longtimenosee">('.get_string('unusedaccounts', '', $CFG->longtimenosee).')</p>';
69ec0a8b 269 }
270
a328425d 271 /// Print paging bars if necessary
4969ad74 272
a328425d 273 if ($totalcount > $perpage) {
274 $alphabet = explode(',', get_string('alphabet'));
951b22a8 275 $strall = get_string("all");
276
a328425d 277
278 /// Bar of first initials
279
280 echo "<center><p align=\"center\">";
281 echo get_string("firstname")." : ";
282 if ($firstinitial) {
63c14189 283 echo " <a href=\"index.php?id=$course->id&amp;sort=firstname&amp;dir=ASC&amp;group=$currentgroup&amp;".
839f2456 284 "perpage=$perpage&amp;lastinitial=$lastinitial\">$strall</a> ";
a328425d 285 } else {
951b22a8 286 echo " <b>$strall</b> ";
a328425d 287 }
288 foreach ($alphabet as $letter) {
289 if ($letter == $firstinitial) {
290 echo " <b>$letter</b> ";
291 } else {
63c14189 292 echo " <a href=\"index.php?id=$course->id&amp;sort=firstname&amp;dir=ASC&amp;group=$currentgroup&amp;".
839f2456 293 "perpage=$perpage&amp;lastinitial=$lastinitial&amp;firstinitial=$letter\">$letter</a> ";
30bacddc 294 }
4969ad74 295 }
a328425d 296 echo "<br />";
4969ad74 297
a328425d 298 /// Bar of last initials
4969ad74 299
a328425d 300 echo get_string("lastname")." : ";
301 if ($lastinitial) {
63c14189 302 echo " <a href=\"index.php?id=$course->id&amp;sort=lastname&amp;dir=ASC&amp;group=$currentgroup&amp;".
839f2456 303 "perpage=$perpage&amp;firstinitial=$firstinitial\">$strall</a> ";
a328425d 304 } else {
951b22a8 305 echo " <b>$strall</b> ";
a328425d 306 }
307 foreach ($alphabet as $letter) {
308 if ($letter == $lastinitial) {
309 echo " <b>$letter</b> ";
310 } else {
63c14189 311 echo " <a href=\"index.php?id=$course->id&amp;sort=lastname&amp;dir=ASC&amp;group=$currentgroup&amp;".
839f2456 312 "perpage=$perpage&amp;firstinitial=$firstinitial&amp;lastinitial=$letter\">$letter</a> ";
a328425d 313 }
314 }
315 echo "</p>";
316 echo "</center>";
317
4bc162b7 318 print_paging_bar($matchcount, $page, $perpage,
63c14189 319 "index.php?id=$course->id&amp;sort=$sort&amp;dir=$dir&amp;group=$currentgroup&amp;perpage=$perpage&amp;firstinitial=$firstinitial&amp;lastinitial=$lastinitial&amp;");
a328425d 320
321 }
322
62b80756 323 if ($matchcount < 1) {
a328425d 324 print_heading(get_string("nostudentsfound", "", $course->students));
325
ff3caf30 326 } else if ($fullmode) { // Print simple listing
a328425d 327 foreach ($students as $student) {
951b22a8 328 print_user($student, $course);
a328425d 329 }
330
331 } else if ($matchcount > 0) {
4bc162b7 332 print_user_table($students, $isteacher);
ff3caf30 333
4bc162b7 334 print_paging_bar($matchcount, $page, $perpage,
63c14189 335 "index.php?id=$course->id&amp;sort=$sort&amp;dir=$dir&amp;group=$currentgroup&amp;perpage=$perpage&amp;firstinitial=$firstinitial&amp;lastinitial=$lastinitial&amp;");
4bc162b7 336
337 if ($perpage < $totalcount) {
338 echo "<center><p>";
63c14189 339 echo "<a href=\"index.php?id=$course->id&amp;sort=$sort&amp;dir=$dir&amp;group=$currentgroup&amp;perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
4bc162b7 340 echo "</p></center>";
341 }
342 }
cbb0fee0 343
4bc162b7 344 print_footer($course);
ff3caf30 345 exit;
346
347
348
4bc162b7 349
350function print_user_table($users, $isteacher) {
4969ad74 351 // Print one big table with abbreviated info
ff3caf30 352 global $mode, $sort, $course, $dir, $CFG;
cf9f97c2 353 if (isset($_GET['group'])) {
354 $group_param = "&amp;group=".$_GET['group'];
355 } else {
68b38e8d 356 $group_param = "";
357 }
4969ad74 358 $columns = array("firstname", "lastname", "city", "country", "lastaccess");
d578afc8 359
951b22a8 360 $countries = get_list_of_countries();
361
362 $strnever = get_string("never");
363
364 $datestring->day = get_string("day");
365 $datestring->days = get_string("days");
366 $datestring->hour = get_string("hour");
367 $datestring->hours = get_string("hours");
368 $datestring->min = get_string("min");
369 $datestring->mins = get_string("mins");
370 $datestring->sec = get_string("sec");
371 $datestring->secs = get_string("secs");
372
4969ad74 373 foreach ($columns as $column) {
374 $colname[$column] = get_string($column);
375 if ($sort != $column) {
376 $columnicon = "";
d578afc8 377 if ($column == "lastaccess") {
4969ad74 378 $columndir = "desc";
d578afc8 379 } else {
4969ad74 380 $columndir = "asc";
d578afc8 381 }
4969ad74 382 } else {
383 $columndir = $dir == "asc" ? "desc":"asc";
384 if ($column == "lastaccess") {
385 $columnicon = $dir == "asc" ? "up":"down";
d578afc8 386 } else {
4969ad74 387 $columnicon = $dir == "asc" ? "down":"up";
d578afc8 388 }
839f2456 389 $columnicon = " <img src=\"$CFG->pixpath/t/$columnicon.gif\" alt=\"\"/>";
32b4b974 390 }
cf9f97c2 391 $$column = "<a href=\"index.php?id=$course->id&amp;sort=$column&amp;dir=$columndir$group_param\">".$colname["$column"]."</a>$columnicon";
4969ad74 392 }
d578afc8 393
4bc162b7 394 foreach ($users as $key => $user) {
395 $users[$key]->country = ($user->country) ? $countries[$user->country] : '';
4969ad74 396 }
397 if ($sort == "country") { // Need to re-sort by full country name, not code
4bc162b7 398 foreach ($users as $user) {
399 $sstudents[$user->id] = $user->country;
adcb3ac5 400 }
4969ad74 401 asort($sstudents);
402 foreach ($sstudents as $key => $value) {
4bc162b7 403 $nstudents[] = $users[$key];
adcb3ac5 404 }
4bc162b7 405 $users = $nstudents;
4969ad74 406 }
adcb3ac5 407
a328425d 408
4969ad74 409 $table->head = array ("&nbsp;", "$firstname / $lastname", $city, $country, $lastaccess);
031c49fa 410 $table->align = array ("left", "left", "left", "left", "left");
4969ad74 411 $table->size = array ("10", "*", "*", "*", "*");
412 $table->size = array ("10", "*", "*", "*", "*");
1b6a4b1d 413 $table->cellpadding = 4;
4969ad74 414 $table->cellspacing = 0;
4969ad74 415
4bc162b7 416 foreach ($users as $user) {
417
418 if ($user->lastaccess) {
419 $lastaccess = format_time(time() - $user->lastaccess, $datestring);
4969ad74 420 } else {
951b22a8 421 $lastaccess = $strnever;
4969ad74 422 }
dc0dc7d5 423
4bc162b7 424 $picture = print_user_picture($user->id, $course->id, $user->picture, false, true);
6b281f1f 425
4bc162b7 426 $fullname = fullname($user, $isteacher);
eca1c454 427
4969ad74 428 $table->data[] = array ($picture,
839f2456 429 "<b><a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">$fullname</a></b>",
40d4f736 430 $user->city,
431 $user->country,
432 $lastaccess);
4969ad74 433 }
434 print_table($table);
4bc162b7 435}
f9903ed0 436
f9903ed0 437?>