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