Revert to previous version
[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");
951b22a8 6
7 define('USER_SMALL_CLASS', 20); // Below this is considered small
8 define('USER_LARGE_CLASS', 200); // Above this is considered large
f9903ed0 9
10 require_variable($id); //course
d578afc8 11 optional_variable($sort, "lastaccess"); //how to sort students
4969ad74 12 optional_variable($dir,"desc"); //how to sort students
13 optional_variable($page, "0"); // which page to show
a328425d 14 optional_variable($lastinitial, ""); // only show students with this last initial
15 optional_variable($firstinitial, ""); // only show students with this first initial
4969ad74 16 optional_variable($perpage, "20"); // how many per page
4bc162b7 17 optional_variable($_GET['format'], 'brief'); // what kind of view
f9903ed0 18
4bc162b7 19 $compactmode = ($_GET['format'] == 'brief');
55e4b5f9 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 }
31 if ($CFG->showsiteparticipantslist < 2 and !isteacher()) {
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
41 if ($showteachers) {
42 $participantslink = get_string("participants");
d578afc8 43 } else {
4969ad74 44 $participantslink = "<a href=\"index.php?id=$course->id\">".get_string("participants")."</a>";
d578afc8 45 }
46
4bc162b7 47 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and $course->groupmodeforce and
2cb2ce61 48 !isteacheredit($course->id));
49
50 $currentgroup = $isseparategroups ? get_current_group($course->id) : NULL;
51
4969ad74 52 if ($course->category) {
53 print_header("$course->shortname: ".get_string("participants"), "$course->fullname",
54 "<A HREF=../course/view.php?id=$course->id>$course->shortname</A> -> ".
951b22a8 55 "$participantslink", "", "", true, "&nbsp;", navmenu($course));
dc0dc7d5 56 } else {
4bc162b7 57 print_header("$course->shortname: ".get_string("participants"), "$course->fullname",
951b22a8 58 "$participantslink", "", "", true, "&nbsp;", navmenu($course));
4969ad74 59 }
60
4bc162b7 61 echo '<div style="text-align: right;">';
62 echo get_string('userlist').': ';
63 $formatmenu = array(
64 '' => get_string('detailedmore'),
65 'brief' => get_string('detailedless'),
66 );
67 // [pj] Oh, the things I do to put it in one line... :P
68 echo str_replace('<form', '<form style="display: inline;"', popup_form ("index.php?id=$id&amp;format=", $formatmenu, 'formatmenu', $_GET['format'], '', '', '', true));
69 echo '</div>';
70
62b80756 71 $exceptions = ''; // This will be a list of userids that are shown as teachers and thus
72 // do not have to be shown as users as well. Only relevant on site course.
4969ad74 73 if ($showteachers) {
2cb2ce61 74 if ($teachers = get_course_teachers($course->id)) {
75 echo "<h2 align=\"center\">$course->teachers</h2>";
4bc162b7 76 if($compactmode) {
77 $exceptions .= implode(',', array_keys($teachers));
78 print_user_table($teachers, $isteacher);
79 }
80 else {
81 foreach ($teachers as $teacher) {
82 if ($isseparategroups) {
83 if ($teacher->editall or ismember($currentgroup, $teacher->id)) {
84 print_user($teacher, $course);
85 $exceptions .= "$teacher->id,";
86 }
87 } else if ($teacher->authority > 0) { // Don't print teachers with no authority
2cb2ce61 88 print_user($teacher, $course);
62b80756 89 $exceptions .= "$teacher->id,";
2cb2ce61 90 }
4969ad74 91 }
92 }
93 }
dc0dc7d5 94 }
62b80756 95 $guest = get_guest();
96 $exceptions .= $guest->id;
4bc162b7 97
222ac91b 98 if ($course->id == SITEID) { // Show all site users (even unconfirmed)
62b80756 99 $students = get_users(true, '', true, $exceptions, $sort.' '.$dir, $firstinitial, $lastinitial, $page*$perpage, $perpage);
100 $totalcount = get_users(false, '', true, '', '', '', '') - 1; // -1 to not count guest user
101 if ($firstinitial or $lastinitial) {
102 $matchcount = get_users(false, '', true, '', '', $firstinitial, $lastinitial) - 1;
103 } else {
104 $matchcount = $totalcount;
105 }
4d744a22 106 } else {
62b80756 107 if ($sort == "lastaccess") {
108 $dsort = "s.timeaccess";
109 } else {
110 $dsort = "u.$sort";
111 }
4bc162b7 112 $students = get_course_students($course->id, $dsort, $dir, $page*$perpage,
2cb2ce61 113 $perpage, $firstinitial, $lastinitial, $currentgroup);
62b80756 114 $totalcount = count_course_students($course, "", "", "", $currentgroup);
115 if ($firstinitial or $lastinitial) {
116 $matchcount = count_course_students($course, "", $firstinitial, $lastinitial, $currentgroup);
117 } else {
118 $matchcount = $totalcount;
119 }
2cb2ce61 120 }
a328425d 121
4969ad74 122 echo "<h2 align=center>$totalcount $course->students</h2>";
dc95f4a1 123
a328425d 124 if (($CFG->longtimenosee < 500) and (!$page) and ($sort == "lastaccess")) {
69ec0a8b 125 echo "<center><p><font size=1>(";
126 print_string("unusedaccounts","",$CFG->longtimenosee);
127 echo ")</font></p></center>";
128 }
129
a328425d 130 /// Print paging bars if necessary
4969ad74 131
a328425d 132 if ($totalcount > $perpage) {
133 $alphabet = explode(',', get_string('alphabet'));
951b22a8 134 $strall = get_string("all");
135
a328425d 136
137 /// Bar of first initials
138
139 echo "<center><p align=\"center\">";
140 echo get_string("firstname")." : ";
141 if ($firstinitial) {
142 echo " <a href=\"index.php?id=$course->id&sort=firstname&dir=ASC&".
951b22a8 143 "perpage=$perpage&lastinitial=$lastinitial\">$strall</a> ";
a328425d 144 } else {
951b22a8 145 echo " <b>$strall</b> ";
a328425d 146 }
147 foreach ($alphabet as $letter) {
148 if ($letter == $firstinitial) {
149 echo " <b>$letter</b> ";
150 } else {
151 echo " <a href=\"index.php?id=$course->id&sort=firstname&dir=ASC&".
152 "perpage=$perpage&lastinitial=$lastinitial&firstinitial=$letter\">$letter</a> ";
30bacddc 153 }
4969ad74 154 }
a328425d 155 echo "<br />";
4969ad74 156
a328425d 157 /// Bar of last initials
4969ad74 158
a328425d 159 echo get_string("lastname")." : ";
160 if ($lastinitial) {
161 echo " <a href=\"index.php?id=$course->id&sort=lastname&dir=ASC&".
951b22a8 162 "perpage=$perpage&firstinitial=$firstinitial\">$strall</a> ";
a328425d 163 } else {
951b22a8 164 echo " <b>$strall</b> ";
a328425d 165 }
166 foreach ($alphabet as $letter) {
167 if ($letter == $lastinitial) {
168 echo " <b>$letter</b> ";
169 } else {
170 echo " <a href=\"index.php?id=$course->id&sort=lastname&dir=ASC&".
171 "perpage=$perpage&firstinitial=$firstinitial&lastinitial=$letter\">$letter</a> ";
172 }
173 }
174 echo "</p>";
175 echo "</center>";
176
4bc162b7 177 print_paging_bar($matchcount, $page, $perpage,
a328425d 178 "index.php?id=$course->id&sort=$sort&dir=$dir&perpage=$perpage&firstinitial=$firstinitial&lastinitial=$lastinitial&");
179
180 }
181
62b80756 182 if ($matchcount < 1) {
a328425d 183 print_heading(get_string("nostudentsfound", "", $course->students));
184
4bc162b7 185 } if (!$compactmode && (0 < $matchcount and $matchcount < USER_SMALL_CLASS)) { // Print simple listing
a328425d 186 foreach ($students as $student) {
951b22a8 187 print_user($student, $course);
a328425d 188 }
189
190 } else if ($matchcount > 0) {
4bc162b7 191 print_user_table($students, $isteacher);
192 print_paging_bar($matchcount, $page, $perpage,
193 "index.php?id=$course->id&sort=$sort&dir=$dir&perpage=$perpage&firstinitial=$firstinitial&lastinitial=$lastinitial&");
194
195 if ($perpage < $totalcount) {
196 echo "<center><p>";
197 echo "<a href=\"index.php?id=$course->id&sort=$sort&dir=$dir&perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
198 echo "</p></center>";
199 }
200 }
cbb0fee0 201
4bc162b7 202 print_footer($course);
203
204function print_user_table($users, $isteacher) {
4969ad74 205 // Print one big table with abbreviated info
4bc162b7 206 global $sort, $course, $dir, $CFG;
207
4969ad74 208 $columns = array("firstname", "lastname", "city", "country", "lastaccess");
d578afc8 209
951b22a8 210 $countries = get_list_of_countries();
211
212 $strnever = get_string("never");
213
214 $datestring->day = get_string("day");
215 $datestring->days = get_string("days");
216 $datestring->hour = get_string("hour");
217 $datestring->hours = get_string("hours");
218 $datestring->min = get_string("min");
219 $datestring->mins = get_string("mins");
220 $datestring->sec = get_string("sec");
221 $datestring->secs = get_string("secs");
222
4969ad74 223 foreach ($columns as $column) {
224 $colname[$column] = get_string($column);
225 if ($sort != $column) {
226 $columnicon = "";
d578afc8 227 if ($column == "lastaccess") {
4969ad74 228 $columndir = "desc";
d578afc8 229 } else {
4969ad74 230 $columndir = "asc";
d578afc8 231 }
4969ad74 232 } else {
233 $columndir = $dir == "asc" ? "desc":"asc";
234 if ($column == "lastaccess") {
235 $columnicon = $dir == "asc" ? "up":"down";
d578afc8 236 } else {
4969ad74 237 $columnicon = $dir == "asc" ? "down":"up";
d578afc8 238 }
4969ad74 239 $columnicon = " <img src=\"$CFG->pixpath/t/$columnicon.gif\" />";
32b4b974 240 }
4969ad74 241 $$column = "<a href=\"index.php?id=$course->id&sort=$column&dir=$columndir\">".$colname["$column"]."</a>$columnicon";
242 }
d578afc8 243
4bc162b7 244 foreach ($users as $key => $user) {
245 $users[$key]->country = ($user->country) ? $countries[$user->country] : '';
4969ad74 246 }
247 if ($sort == "country") { // Need to re-sort by full country name, not code
4bc162b7 248 foreach ($users as $user) {
249 $sstudents[$user->id] = $user->country;
adcb3ac5 250 }
4969ad74 251 asort($sstudents);
252 foreach ($sstudents as $key => $value) {
4bc162b7 253 $nstudents[] = $users[$key];
adcb3ac5 254 }
4bc162b7 255 $users = $nstudents;
4969ad74 256 }
adcb3ac5 257
a328425d 258
4969ad74 259 $table->head = array ("&nbsp;", "$firstname / $lastname", $city, $country, $lastaccess);
260 $table->align = array ("LEFT", "LEFT", "LEFT", "LEFT", "LEFT");
261 $table->size = array ("10", "*", "*", "*", "*");
262 $table->size = array ("10", "*", "*", "*", "*");
1b6a4b1d 263 $table->cellpadding = 4;
4969ad74 264 $table->cellspacing = 0;
4969ad74 265
4bc162b7 266 foreach ($users as $user) {
267
268 if ($user->lastaccess) {
269 $lastaccess = format_time(time() - $user->lastaccess, $datestring);
4969ad74 270 } else {
951b22a8 271 $lastaccess = $strnever;
4969ad74 272 }
dc0dc7d5 273
4bc162b7 274 $picture = print_user_picture($user->id, $course->id, $user->picture, false, true);
6b281f1f 275
4bc162b7 276 $fullname = fullname($user, $isteacher);
eca1c454 277
4969ad74 278 $table->data[] = array ($picture,
4bc162b7 279 "<b><a href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">$fullname</a></b>",
280 "<font size=2>$user->city</font>",
281 "<font size=2>$user->country</font>",
4969ad74 282 "<font size=2>$lastaccess</font>");
283 }
284 print_table($table);
4bc162b7 285}
f9903ed0 286
f9903ed0 287?>