Removed a block of redundant code in participants list (Merged from MOODLE_15_STABLE)
[moodle.git] / user / index.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
3// Lists all the users within a given course
4
cc038b47 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
cc038b47 10 define('DEFAULT_PAGE_SIZE', 20);
f9903ed0 11
cc038b47 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?
18
19 if (! $course = get_record('course', 'id', $id)) {
f9903ed0 20 error("Course ID is incorrect");
21 }
22
23 require_login($course->id);
24
4bc162b7 25 if (!$course->category) {
26 if (!$CFG->showsiteparticipantslist and !isteacher(SITEID)) {
27 notice(get_string('sitepartlist0'));
28 }
9407d456 29 if ($CFG->showsiteparticipantslist < 2 and !isteacherinanycourse()) {
4bc162b7 30 notice(get_string('sitepartlist1'));
31 }
32 }
33
cc038b47 34 add_to_log($course->id, 'user', 'view all', 'index.php?id='.$course->id, '');
f9903ed0 35
951b22a8 36 $isteacher = isteacher($course->id);
4969ad74 37
68b38e8d 38 $countries = get_list_of_countries();
39
cc038b47 40 $strnever = get_string('never');
68b38e8d 41
cc038b47 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');
d578afc8 50
ff3caf30 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;
57 }
58
ff3caf30 59/// Check to see if groups are being used in this forum
60/// and if so, set $currentgroup to reflect the current group
61
ff3caf30 62 $groupmode = groupmode($course); // Groups are being used
cc038b47 63 $currentgroup = get_and_set_current_group($course, $groupmode, $group);
ff3caf30 64
cc038b47 65 if (!$currentgroup) { // To make some other functions work better later
66 $currentgroup = NULL;
67 }
ff3caf30 68
4bc162b7 69 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and $course->groupmodeforce and
2cb2ce61 70 !isteacheredit($course->id));
71
ff3caf30 72 if ($isseparategroups and (!$currentgroup) ) { //XXX
73 print_heading(get_string("notingroup", "forum"));
74 print_footer($course);
75 exit;
76 }
77
cc038b47 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;
ff3caf30 80
81/// Print headers
2cb2ce61 82
4969ad74 83 if ($course->category) {
cc038b47 84 print_header("$course->shortname: ".get_string('participants'), $course->fullname,
031c49fa 85 "<a href=\"../course/view.php?id=$course->id\">$course->shortname</a> -> ".
cc038b47 86 get_string('participants'), "", "", true, "&nbsp;", navmenu($course));
dc0dc7d5 87 } else {
cc038b47 88 print_header("$course->shortname: ".get_string('participants'), $course->fullname,
89 get_string('participants'), "", "", true, "&nbsp;", navmenu($course));
4969ad74 90 }
91
ff3caf30 92/// Print settings and things in a table across the top
93
cc038b47 94 echo '<table class="controls" cellspacing="0"><tr>';
ff3caf30 95
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">';
cc038b47 99 print_group_menu($groups, $groupmode, $currentgroup, $baseurl);
ff3caf30 100 echo '</td>';
101 }
102 }
103
cc038b47 104 echo '<td class="right">';
4bc162b7 105 echo get_string('userlist').': ';
ff3caf30 106 $formatmenu = array( '0' => get_string('detailedless'),
107 '1' => get_string('detailedmore'));
cc038b47 108 echo popup_form($baseurl.'&amp;mode=', $formatmenu, 'formatmenu', $fullmode, '', '', '', true);
ff3caf30 109 echo '</td></tr></table>';
110
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;
2c27cd19 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>';
122 }
ff3caf30 123 echo '</h3>';
124 echo format_text($group->description);
125 echo '</td></tr></table>';
126 }
127 }
128 }
129
4bc162b7 130
cc038b47 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.
68b38e8d 133
134 if($showteachers) {
135
68b38e8d 136 $tablecolumns = array('picture', 'fullname', 'city', 'country', 'lastaccess');
137 $tableheaders = array('', get_string('fullname'), get_string('city'), get_string('country'), get_string('lastaccess'));
138
cc038b47 139 $table = new flexible_table('user-index-teachers');
68b38e8d 140
141 $table->define_columns($tablecolumns);
142 $table->define_headers($tableheaders);
cc038b47 143 $table->define_baseurl($baseurl);
68b38e8d 144
a5a3f48a 145 $table->sortable(true, 'lastaccess', SORT_DESC);
68b38e8d 146
147 $table->set_attribute('cellspacing', '0');
148 $table->set_attribute('id', 'teachers');
149 $table->set_attribute('class', 'generaltable generalbox');
150
151 $table->setup();
152
153 if($whereclause = $table->get_sql_where()) {
154 $whereclause .= ' AND ';
155 }
156
157 $teachersql = "SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, u.maildigest,
cc038b47 158 u.email, u.maildisplay, u.city, u.country, u.lastlogin, u.picture, u.lang, u.timezone,
68b38e8d 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 ";
163
164 if($isseparategroups) {
165 $whereclause .= '(t.editall OR groupid = '.$currentgroup.') AND ';
166 }
167 else if ($currentgroup) { // Displaying a group by choice
168 $whereclause .= 'groupid = '.$currentgroup.' AND ';
169 }
170
171 $teachersql .= 'WHERE '.$whereclause.' t.course = '.$course->id.' AND u.deleted = 0 AND u.confirmed = 1 AND t.authority > 0';
172
173 if($sortclause = $table->get_sql_sort()) {
174 $teachersql .= ' ORDER BY '.$sortclause;
175 }
176
177 $teachers = get_records_sql($teachersql);
178
179 if(!empty($teachers)) {
180
cc038b47 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>';
ec99b88e 184 if (isadmin() or ($course->category and (iscreator() or (isteacheredit($course->id) and !empty($CFG->teacherassignteachers))))) {
cc038b47 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>';
03f191ad 187 }
188 echo '</h2>';
480dda61 189
cc038b47 190 $exceptions = array_keys($teachers);
68b38e8d 191
ff3caf30 192 if ($fullmode) {
193 foreach ($teachers as $key => $teacher) {
194 print_user($teacher, $course);
4969ad74 195 }
ff3caf30 196 } else {
bf845f2b 197 $countrysort = (strpos($sortclause, 'country') !== false);
68b38e8d 198 foreach ($teachers as $teacher) {
199
200 if ($teacher->lastaccess) {
201 $lastaccess = format_time(time() - $teacher->lastaccess, $datestring);
202 } else {
203 $lastaccess = $strnever;
204 }
bf845f2b 205
206 if (empty($teacher->country)) {
207 $country = '';
208 }
209 else {
210 if($countrysort) {
211 $country = '('.$teacher->country.') '.$countries[$teacher->country];
212 }
213 else {
214 $country = $countries[$teacher->country];
215 }
216 }
68b38e8d 217
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,
bf845f2b 223 $country,
68b38e8d 224 $lastaccess));
225 }
cc038b47 226
227 $table->print_html();
4969ad74 228 }
229 }
dc0dc7d5 230 }
cc038b47 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>';
62b80756 239 }
cc038b47 240 echo '</h2>';
2cb2ce61 241 }
a328425d 242
cc038b47 243 $guest = get_guest();
244 $exceptions[] = $guest->id;
245
d7aba64b 246 $tablecolumns = array('picture', 'fullname', 'city', 'country', 'lastaccess','');
f19570d0 247 $tableheaders = array('', get_string('fullname'), get_string('city'), get_string('country'), get_string('lastaccess'), ($isteacher) ? get_string('select') : '');
cc038b47 248
249 $table = new flexible_table('user-index-students');
250
251 $table->define_columns($tablecolumns);
252 $table->define_headers($tableheaders);
253 $table->define_baseurl($baseurl);
254
a5a3f48a 255 $table->sortable(true, 'lastaccess', SORT_DESC);
cc038b47 256
257 $table->set_attribute('cellspacing', '0');
258 $table->set_attribute('id', 'students');
259 $table->set_attribute('class', 'generaltable generalbox');
260
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'
268 ));
269 $table->setup();
270
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 ';
03f191ad 276 }
cc038b47 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 ';
69ec0a8b 282 }
283
cc038b47 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;
288 }
4969ad74 289
cc038b47 290 if($course->id == SITEID) {
291 $where .= ' AND u.id NOT IN ('.implode(',', $exceptions).')';
292 }
a328425d 293
cc038b47 294 $totalcount = count_records_sql('SELECT COUNT(*) '.$from.$where);
a328425d 295
cc038b47 296 if($table->get_sql_where()) {
297 $where .= ' AND '.$table->get_sql_where();
a328425d 298 }
299
cc038b47 300 if($table->get_sql_sort()) {
cc038b47 301 $sort = ' ORDER BY '.$table->get_sql_sort();
302 }
303 else {
304 $sort = '';
305 }
a328425d 306
cc038b47 307 $matchcount = count_records_sql('SELECT COUNT(*) '.$from.$where);
ff3caf30 308
cc038b47 309 $table->initialbars($totalcount > $perpage);
310 $table->pagesize($perpage, $matchcount);
4bc162b7 311
cc038b47 312 if($table->get_page_start() !== '' && $table->get_page_size() !== '') {
313 $limit = ' '.sql_paging_limit($table->get_page_start(), $table->get_page_size());
4bc162b7 314 }
cc038b47 315 else {
316 $limit = '';
317 }
318
319 $students = get_records_sql($select.$from.$where.$sort.$limit);
cbb0fee0 320
cc038b47 321 $a->count = $totalcount;
322 $a->items = $totalcount == 1 ? $course->student : $course->students;
323 echo '<h2>'.get_string('counteditems', '', $a);
324 if (isteacheredit($course->id)) {
325 echo ' <a href="../course/student.php?id='.$course->id.'">';
326 echo '<img src="'.$CFG->pixpath.'/i/edit.gif" height="16" width="16" alt="" /></a>';
327 }
328 echo '</h2>';
ff3caf30 329
cc038b47 330 if ($CFG->longtimenosee > 0 && $CFG->longtimenosee < 1000 && $totalcount > 0) {
331 echo '<p id="longtimenosee">('.get_string('unusedaccounts', '', $CFG->longtimenosee).')</p>';
332 }
4bc162b7 333
f19570d0 334 if ($isteacher) {
335 echo '<form action="messageselect.php" method="post" name="messageselect">';
336 echo '<input type="hidden" name="id" value="'.$id.'" />';
337 echo '<input type="hidden" name="returnto" value="'.$_SERVER['REQUEST_URI'].'" />';
338 }
339
cc038b47 340 if ($fullmode) { // Print simple listing
341 if ($totalcount < 1) {
342 print_heading(get_string("nostudentsfound", "", $course->students));
68b38e8d 343 }
cc038b47 344 else {
345
346 if($totalcount > $perpage) {
347
348 $firstinitial = $table->get_initial_first();
349 $lastinitial = $table->get_initial_last();
350 $strall = get_string('all');
351 $alpha = explode(',', get_string('alphabet'));
352
353 // Bar of first initials
354
355 echo '<div class="initialbar firstinitial">'.get_string('firstname').' : ';
356 if(!empty($firstinitial)) {
357 echo '<a href="'.$baseurl.'&amp;sifirst=">'.$strall.'</a>';
d578afc8 358 } else {
cc038b47 359 echo '<strong>'.$strall.'</strong>';
d578afc8 360 }
cc038b47 361 foreach ($alpha as $letter) {
362 if ($letter == $firstinitial) {
363 echo ' <strong>'.$letter.'</strong>';
364 } else {
365 echo ' <a href="'.$baseurl.'&amp;sifirst='.$letter.'">'.$letter.'</a>';
366 }
367 }
368 echo '</div>';
369
370 // Bar of last initials
371
372 echo '<div class="initialbar lastinitial">'.get_string('lastname').' : ';
373 if(!empty($lastinitial)) {
374 echo '<a href="'.$baseurl.'&amp;silast=">'.$strall.'</a>';
d578afc8 375 } else {
cc038b47 376 echo '<strong>'.$strall.'</strong>';
377 }
378 foreach ($alpha as $letter) {
379 if ($letter == $lastinitial) {
380 echo ' <strong>'.$letter.'</strong>';
381 } else {
382 echo ' <a href="'.$baseurl.'&amp;silast='.$letter.'">'.$letter.'</a>';
383 }
d578afc8 384 }
cc038b47 385 echo '</div>';
386
387 print_paging_bar($matchcount, intval($table->get_page_start() / $perpage), $perpage, $baseurl.'&amp;', 'spage');
388
32b4b974 389 }
d578afc8 390
cc038b47 391 if($matchcount > 0) {
392 foreach ($students as $student) {
f19570d0 393 print_user($student, $course, true);
cc038b47 394 }
adcb3ac5 395 }
cc038b47 396 else {
397 print_heading(get_string('nothingtodisplay'));
adcb3ac5 398 }
4969ad74 399 }
cc038b47 400 }
401 else {
402 $countrysort = (strpos($sort, 'country') !== false);
403 if (!empty($students)) {
404 foreach ($students as $student) {
405 if ($student->lastaccess) {
406 $lastaccess = format_time(time() - $student->lastaccess, $datestring);
407 } else {
408 $lastaccess = $strnever;
409 }
410
411 if (empty($student->country)) {
412 $country = '';
413 }
414 else {
415 if($countrysort) {
416 $country = '('.$student->country.') '.$countries[$student->country];
417 }
418 else {
419 $country = $countries[$student->country];
420 }
421 }
adcb3ac5 422
cc038b47 423 $table->add_data(array (
424 //'<input type="checkbox" name="userid[]" value="'.$teacher->id.'" />',
425 print_user_picture($student->id, $course->id, $student->picture, false, true),
426 '<strong><a href="'.$CFG->wwwroot.'/user/view.php?id='.$student->id.'&amp;course='.$course->id.'">'.fullname($student).'</a></strong>',
427 $student->city,
428 $country,
f19570d0 429 $lastaccess,
430 ($isteacher ? '<input type="checkbox" name="email'.$student->id.'" />' : '')
431 ));
432
4969ad74 433 }
cc038b47 434 }
dc0dc7d5 435
cc038b47 436 $table->print_html();
6b281f1f 437
cc038b47 438 }
eca1c454 439
f19570d0 440 if ($isteacher) {
441 echo '<center>';
442 echo '<input type="button" onclick="checkall()" value="'.get_string('checkall').'" />';
443 echo '<input type="button" onclick="checknone()" value="'.get_string('checknone').'" />';
444 $displaylist[1] = get_string('messageselectadd');
445 choose_from_menu ($displaylist, "messageselect", "", get_string("withselectedusers"), "javascript:document.messageselect.submit()");
446 echo '</center></form>';
447 }
448
cc038b47 449 if ($perpage == 99999) {
450 echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage='.DEFAULT_PAGE_SIZE.'">'.get_string('showperpage', '', DEFAULT_PAGE_SIZE).'</a></div>';
451 }
452 else if ($matchcount > 0 && $perpage < $matchcount) {
453 echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage=99999">'.get_string('showall', '', $matchcount).'</a></div>';
454 }
455
456 print_footer($course);
f9903ed0 457
f9903ed0 458?>