Merged fix to allow referrals from the home page
[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
f9903ed0 17
55e4b5f9 18
f9903ed0 19 if (! $course = get_record("course", "id", $id)) {
20 error("Course ID is incorrect");
21 }
22
23 require_login($course->id);
24
da3a08d7 25 add_to_log($course->id, "user", "view all", "index.php?id=$course->id", "");
f9903ed0 26
951b22a8 27 $isteacher = isteacher($course->id);
4969ad74 28 $showteachers = ($page == 0 and $sort == "lastaccess" and $dir == "desc");
29
30 if ($showteachers) {
31 $participantslink = get_string("participants");
d578afc8 32 } else {
4969ad74 33 $participantslink = "<a href=\"index.php?id=$course->id\">".get_string("participants")."</a>";
d578afc8 34 }
35
2cb2ce61 36 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and $course->groupmodeforce and
37 !isteacheredit($course->id));
38
39 $currentgroup = $isseparategroups ? get_current_group($course->id) : NULL;
40
4969ad74 41 if ($course->category) {
42 print_header("$course->shortname: ".get_string("participants"), "$course->fullname",
43 "<A HREF=../course/view.php?id=$course->id>$course->shortname</A> -> ".
951b22a8 44 "$participantslink", "", "", true, "&nbsp;", navmenu($course));
dc0dc7d5 45 } else {
4969ad74 46 print_header("$course->shortname: ".get_string("participants"), "$course->fullname",
951b22a8 47 "$participantslink", "", "", true, "&nbsp;", navmenu($course));
4969ad74 48 }
49
62b80756 50 $exceptions = ''; // This will be a list of userids that are shown as teachers and thus
51 // do not have to be shown as users as well. Only relevant on site course.
4969ad74 52 if ($showteachers) {
2cb2ce61 53 if ($teachers = get_course_teachers($course->id)) {
54 echo "<h2 align=\"center\">$course->teachers</h2>";
4969ad74 55 foreach ($teachers as $teacher) {
2cb2ce61 56 if ($isseparategroups) {
57 if ($teacher->editall or ismember($currentgroup, $teacher->id)) {
58 print_user($teacher, $course);
62b80756 59 $exceptions .= "$teacher->id,";
2cb2ce61 60 }
61 } else if ($teacher->authority > 0) { // Don't print teachers with no authority
951b22a8 62 print_user($teacher, $course);
62b80756 63 $exceptions .= "$teacher->id,";
4969ad74 64 }
65 }
66 }
dc0dc7d5 67 }
62b80756 68 $guest = get_guest();
69 $exceptions .= $guest->id;
70
222ac91b 71 if ($course->id == SITEID) { // Show all site users (even unconfirmed)
62b80756 72 $students = get_users(true, '', true, $exceptions, $sort.' '.$dir, $firstinitial, $lastinitial, $page*$perpage, $perpage);
73 $totalcount = get_users(false, '', true, '', '', '', '') - 1; // -1 to not count guest user
74 if ($firstinitial or $lastinitial) {
75 $matchcount = get_users(false, '', true, '', '', $firstinitial, $lastinitial) - 1;
76 } else {
77 $matchcount = $totalcount;
78 }
4d744a22 79 } else {
62b80756 80 if ($sort == "lastaccess") {
81 $dsort = "s.timeaccess";
82 } else {
83 $dsort = "u.$sort";
84 }
85 $students = get_course_students($course->id, $dsort, $dir, $page*$perpage,
2cb2ce61 86 $perpage, $firstinitial, $lastinitial, $currentgroup);
62b80756 87 $totalcount = count_course_students($course, "", "", "", $currentgroup);
88 if ($firstinitial or $lastinitial) {
89 $matchcount = count_course_students($course, "", $firstinitial, $lastinitial, $currentgroup);
90 } else {
91 $matchcount = $totalcount;
92 }
2cb2ce61 93 }
a328425d 94
4969ad74 95 echo "<h2 align=center>$totalcount $course->students</h2>";
dc95f4a1 96
a328425d 97 if (($CFG->longtimenosee < 500) and (!$page) and ($sort == "lastaccess")) {
69ec0a8b 98 echo "<center><p><font size=1>(";
99 print_string("unusedaccounts","",$CFG->longtimenosee);
100 echo ")</font></p></center>";
101 }
102
a328425d 103 /// Print paging bars if necessary
4969ad74 104
a328425d 105 if ($totalcount > $perpage) {
106 $alphabet = explode(',', get_string('alphabet'));
951b22a8 107 $strall = get_string("all");
108
a328425d 109
110 /// Bar of first initials
111
112 echo "<center><p align=\"center\">";
113 echo get_string("firstname")." : ";
114 if ($firstinitial) {
115 echo " <a href=\"index.php?id=$course->id&sort=firstname&dir=ASC&".
951b22a8 116 "perpage=$perpage&lastinitial=$lastinitial\">$strall</a> ";
a328425d 117 } else {
951b22a8 118 echo " <b>$strall</b> ";
a328425d 119 }
120 foreach ($alphabet as $letter) {
121 if ($letter == $firstinitial) {
122 echo " <b>$letter</b> ";
123 } else {
124 echo " <a href=\"index.php?id=$course->id&sort=firstname&dir=ASC&".
125 "perpage=$perpage&lastinitial=$lastinitial&firstinitial=$letter\">$letter</a> ";
30bacddc 126 }
4969ad74 127 }
a328425d 128 echo "<br />";
4969ad74 129
a328425d 130 /// Bar of last initials
4969ad74 131
a328425d 132 echo get_string("lastname")." : ";
133 if ($lastinitial) {
134 echo " <a href=\"index.php?id=$course->id&sort=lastname&dir=ASC&".
951b22a8 135 "perpage=$perpage&firstinitial=$firstinitial\">$strall</a> ";
a328425d 136 } else {
951b22a8 137 echo " <b>$strall</b> ";
a328425d 138 }
139 foreach ($alphabet as $letter) {
140 if ($letter == $lastinitial) {
141 echo " <b>$letter</b> ";
142 } else {
143 echo " <a href=\"index.php?id=$course->id&sort=lastname&dir=ASC&".
144 "perpage=$perpage&firstinitial=$firstinitial&lastinitial=$letter\">$letter</a> ";
145 }
146 }
147 echo "</p>";
148 echo "</center>";
149
a328425d 150 print_paging_bar($matchcount, $page, $perpage,
151 "index.php?id=$course->id&sort=$sort&dir=$dir&perpage=$perpage&firstinitial=$firstinitial&lastinitial=$lastinitial&");
152
153 }
154
62b80756 155 if ($matchcount < 1) {
a328425d 156 print_heading(get_string("nostudentsfound", "", $course->students));
157
158 } if (0 < $matchcount and $matchcount < USER_SMALL_CLASS) { // Print simple listing
159 foreach ($students as $student) {
951b22a8 160 print_user($student, $course);
a328425d 161 }
162
163 } else if ($matchcount > 0) {
cbb0fee0 164
4969ad74 165 // Print one big table with abbreviated info
166 $columns = array("firstname", "lastname", "city", "country", "lastaccess");
d578afc8 167
951b22a8 168 $countries = get_list_of_countries();
169
170 $strnever = get_string("never");
171
172 $datestring->day = get_string("day");
173 $datestring->days = get_string("days");
174 $datestring->hour = get_string("hour");
175 $datestring->hours = get_string("hours");
176 $datestring->min = get_string("min");
177 $datestring->mins = get_string("mins");
178 $datestring->sec = get_string("sec");
179 $datestring->secs = get_string("secs");
180
4969ad74 181 foreach ($columns as $column) {
182 $colname[$column] = get_string($column);
183 if ($sort != $column) {
184 $columnicon = "";
d578afc8 185 if ($column == "lastaccess") {
4969ad74 186 $columndir = "desc";
d578afc8 187 } else {
4969ad74 188 $columndir = "asc";
d578afc8 189 }
4969ad74 190 } else {
191 $columndir = $dir == "asc" ? "desc":"asc";
192 if ($column == "lastaccess") {
193 $columnicon = $dir == "asc" ? "up":"down";
d578afc8 194 } else {
4969ad74 195 $columnicon = $dir == "asc" ? "down":"up";
d578afc8 196 }
4969ad74 197 $columnicon = " <img src=\"$CFG->pixpath/t/$columnicon.gif\" />";
32b4b974 198 }
4969ad74 199 $$column = "<a href=\"index.php?id=$course->id&sort=$column&dir=$columndir\">".$colname["$column"]."</a>$columnicon";
200 }
d578afc8 201
4969ad74 202 foreach ($students as $key => $student) {
a5db6f8a 203 $students[$key]->country = ($student->country) ? $countries[$student->country] : '';
4969ad74 204 }
205 if ($sort == "country") { // Need to re-sort by full country name, not code
206 foreach ($students as $student) {
207 $sstudents[$student->id] = $student->country;
adcb3ac5 208 }
4969ad74 209 asort($sstudents);
210 foreach ($sstudents as $key => $value) {
211 $nstudents[] = $students[$key];
adcb3ac5 212 }
4969ad74 213 $students = $nstudents;
214 }
adcb3ac5 215
a328425d 216
4969ad74 217 $table->head = array ("&nbsp;", "$firstname / $lastname", $city, $country, $lastaccess);
218 $table->align = array ("LEFT", "LEFT", "LEFT", "LEFT", "LEFT");
219 $table->size = array ("10", "*", "*", "*", "*");
220 $table->size = array ("10", "*", "*", "*", "*");
1b6a4b1d 221 $table->cellpadding = 4;
4969ad74 222 $table->cellspacing = 0;
223
224 foreach ($students as $student) {
225
226 if ($student->lastaccess) {
951b22a8 227 $lastaccess = format_time(time() - $student->lastaccess, $datestring);
4969ad74 228 } else {
951b22a8 229 $lastaccess = $strnever;
4969ad74 230 }
dc0dc7d5 231
f8f63ecd 232 $picture = print_user_picture($student->id, $course->id, $student->picture, false, true);
6b281f1f 233
eca1c454 234 $fullname = fullname($student, $isteacher);
235
4969ad74 236 $table->data[] = array ($picture,
eca1c454 237 "<b><a href=\"$CFG->wwwroot/user/view.php?id=$student->id&course=$course->id\">$fullname</a></b>",
4969ad74 238 "<font size=2>$student->city</font>",
239 "<font size=2>$student->country</font>",
240 "<font size=2>$lastaccess</font>");
241 }
242 print_table($table);
c69ce08b 243
a328425d 244 print_paging_bar($matchcount, $page, $perpage,
959e11bf 245 "index.php?id=$course->id&sort=$sort&dir=$dir&perpage=$perpage&firstinitial=$firstinitial&lastinitial=$lastinitial&");
15c71521 246
4969ad74 247 if ($perpage != 99999) {
248 echo "<center><p>";
249 echo "<a href=\"index.php?id=$course->id&sort=$sort&dir=$dir&perpage=99999\">".get_string("showall", "", $totalcount)."</a>";
250 echo "</p></center>";
9ae472a3 251 }
a328425d 252 }
f9903ed0 253
254 print_footer($course);
255
f9903ed0 256?>