Merged from MOODLE_15_STABLE: Fixing misnamed variable in forgot password link in...
[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
e5ada99f 139 $table = new flexible_table('user-index-teachers-'.$course->id);
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
6746bb8a 246 $tablecolumns = array('picture', 'fullname', 'city', 'country', 'lastaccess');
247 $tableheaders = array('', get_string('fullname'), get_string('city'), get_string('country'), get_string('lastaccess'));
248
249 if ($course->enrolperiod) {
250 $tablecolumns[] = 'timeend';
251 $tableheaders[] = get_string('enrolmentend');
252 }
253
254 if ($isteacher) {
255 $tablecolumns[] = '';
256 $tableheaders[] = get_string('select');
257 }
cc038b47 258
e5ada99f 259 $table = new flexible_table('user-index-students-'.$course->id);
cc038b47 260
261 $table->define_columns($tablecolumns);
262 $table->define_headers($tableheaders);
263 $table->define_baseurl($baseurl);
264
a5a3f48a 265 $table->sortable(true, 'lastaccess', SORT_DESC);
cc038b47 266
267 $table->set_attribute('cellspacing', '0');
268 $table->set_attribute('id', 'students');
269 $table->set_attribute('class', 'generaltable generalbox');
270
271 $table->set_control_variables(array(
272 TABLE_VAR_SORT => 'ssort',
273 TABLE_VAR_HIDE => 'shide',
274 TABLE_VAR_SHOW => 'sshow',
275 TABLE_VAR_IFIRST => 'sifirst',
276 TABLE_VAR_ILAST => 'silast',
277 TABLE_VAR_PAGE => 'spage'
278 ));
279 $table->setup();
280
281 if(SITEID == $course->id) {
282 $select = 'SELECT u.id, u.username, u.firstname, u.lastname, u.email, u.city, u.country,
283 u.picture, u.lang, u.timezone, u.emailstop, u.maildisplay, u.lastaccess ';
284 $from = 'FROM '.$CFG->prefix.'user u ';
285 $where = 'WHERE confirmed = 1 AND u.deleted = 0 ';
03f191ad 286 }
cc038b47 287 else {
288 $select = 'SELECT u.id, u.username, u.firstname, u.lastname, u.email, u.city, u.country,
289 u.picture, u.lang, u.timezone, u.emailstop, u.maildisplay, s.timeaccess AS lastaccess ';
6746bb8a 290 $select .= $course->enrolperiod?', s.timeend ':'';
cc038b47 291 $from = 'FROM '.$CFG->prefix.'user u LEFT JOIN '.$CFG->prefix.'user_students s ON s.userid = u.id ';
292 $where = 'WHERE s.course = '.$course->id.' AND u.deleted = 0 ';
69ec0a8b 293 }
294
cc038b47 295 if ($currentgroup) { // Displaying a group by choice
296 // FIX: TODO: This will not work if $currentgroup == 0, i.e. "those not in a group"
297 $from .= 'LEFT JOIN '.$CFG->prefix.'groups_members gm ON u.id = gm.userid ';
298 $where .= ' AND gm.groupid = '.$currentgroup;
299 }
4969ad74 300
cc038b47 301 if($course->id == SITEID) {
302 $where .= ' AND u.id NOT IN ('.implode(',', $exceptions).')';
303 }
a328425d 304
cc038b47 305 $totalcount = count_records_sql('SELECT COUNT(*) '.$from.$where);
a328425d 306
cc038b47 307 if($table->get_sql_where()) {
308 $where .= ' AND '.$table->get_sql_where();
a328425d 309 }
310
cc038b47 311 if($table->get_sql_sort()) {
cc038b47 312 $sort = ' ORDER BY '.$table->get_sql_sort();
313 }
314 else {
315 $sort = '';
316 }
a328425d 317
cc038b47 318 $matchcount = count_records_sql('SELECT COUNT(*) '.$from.$where);
ff3caf30 319
cc038b47 320 $table->initialbars($totalcount > $perpage);
321 $table->pagesize($perpage, $matchcount);
4bc162b7 322
cc038b47 323 if($table->get_page_start() !== '' && $table->get_page_size() !== '') {
324 $limit = ' '.sql_paging_limit($table->get_page_start(), $table->get_page_size());
4bc162b7 325 }
cc038b47 326 else {
327 $limit = '';
328 }
329
330 $students = get_records_sql($select.$from.$where.$sort.$limit);
cbb0fee0 331
cc038b47 332 $a->count = $totalcount;
333 $a->items = $totalcount == 1 ? $course->student : $course->students;
334 echo '<h2>'.get_string('counteditems', '', $a);
335 if (isteacheredit($course->id)) {
336 echo ' <a href="../course/student.php?id='.$course->id.'">';
337 echo '<img src="'.$CFG->pixpath.'/i/edit.gif" height="16" width="16" alt="" /></a>';
338 }
339 echo '</h2>';
ff3caf30 340
cc038b47 341 if ($CFG->longtimenosee > 0 && $CFG->longtimenosee < 1000 && $totalcount > 0) {
342 echo '<p id="longtimenosee">('.get_string('unusedaccounts', '', $CFG->longtimenosee).')</p>';
343 }
4bc162b7 344
f19570d0 345 if ($isteacher) {
6746bb8a 346 echo '
347<script Language="JavaScript">
348<!--
349function checksubmit(form) {
350 var destination = form.formaction.options[form.formaction.selectedIndex].value;
351 if (destination == "" || !checkchecked(form)) {
352 form.formaction.selectedIndex = 0;
353 return false;
354 } else {
355 return true;
356 }
357}
358
359function checkchecked(form) {
360 var inputs = document.getElementsByTagName(\'INPUT\');
361 var checked = false;
362 inputs = filterByParent(inputs, function() {return form;});
363 for(var i = 0; i < inputs.length; ++i) {
364 if(inputs[i].type == \'checkbox\' && inputs[i].checked) {
365 checked = true;
366 }
367 }
368 return checked;
369}
370//-->
371</script>
372';
db0a8174 373 echo '<form action="action_redir.php" method="post" name="studentsform" onSubmit="return checksubmit(this);">';
f19570d0 374 echo '<input type="hidden" name="id" value="'.$id.'" />';
375 echo '<input type="hidden" name="returnto" value="'.$_SERVER['REQUEST_URI'].'" />';
6746bb8a 376 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
f19570d0 377 }
378
cc038b47 379 if ($fullmode) { // Print simple listing
380 if ($totalcount < 1) {
381 print_heading(get_string("nostudentsfound", "", $course->students));
68b38e8d 382 }
cc038b47 383 else {
384
385 if($totalcount > $perpage) {
386
387 $firstinitial = $table->get_initial_first();
388 $lastinitial = $table->get_initial_last();
389 $strall = get_string('all');
390 $alpha = explode(',', get_string('alphabet'));
391
392 // Bar of first initials
393
394 echo '<div class="initialbar firstinitial">'.get_string('firstname').' : ';
395 if(!empty($firstinitial)) {
396 echo '<a href="'.$baseurl.'&amp;sifirst=">'.$strall.'</a>';
d578afc8 397 } else {
cc038b47 398 echo '<strong>'.$strall.'</strong>';
d578afc8 399 }
cc038b47 400 foreach ($alpha as $letter) {
401 if ($letter == $firstinitial) {
402 echo ' <strong>'.$letter.'</strong>';
403 } else {
404 echo ' <a href="'.$baseurl.'&amp;sifirst='.$letter.'">'.$letter.'</a>';
405 }
406 }
407 echo '</div>';
408
409 // Bar of last initials
410
411 echo '<div class="initialbar lastinitial">'.get_string('lastname').' : ';
412 if(!empty($lastinitial)) {
413 echo '<a href="'.$baseurl.'&amp;silast=">'.$strall.'</a>';
d578afc8 414 } else {
cc038b47 415 echo '<strong>'.$strall.'</strong>';
416 }
417 foreach ($alpha as $letter) {
418 if ($letter == $lastinitial) {
419 echo ' <strong>'.$letter.'</strong>';
420 } else {
421 echo ' <a href="'.$baseurl.'&amp;silast='.$letter.'">'.$letter.'</a>';
422 }
d578afc8 423 }
cc038b47 424 echo '</div>';
425
426 print_paging_bar($matchcount, intval($table->get_page_start() / $perpage), $perpage, $baseurl.'&amp;', 'spage');
427
32b4b974 428 }
d578afc8 429
cc038b47 430 if($matchcount > 0) {
431 foreach ($students as $student) {
f19570d0 432 print_user($student, $course, true);
cc038b47 433 }
adcb3ac5 434 }
cc038b47 435 else {
436 print_heading(get_string('nothingtodisplay'));
adcb3ac5 437 }
4969ad74 438 }
cc038b47 439 }
440 else {
441 $countrysort = (strpos($sort, 'country') !== false);
6746bb8a 442 $timeformat = get_string('strftimedate');
cc038b47 443 if (!empty($students)) {
444 foreach ($students as $student) {
445 if ($student->lastaccess) {
446 $lastaccess = format_time(time() - $student->lastaccess, $datestring);
447 } else {
448 $lastaccess = $strnever;
449 }
450
451 if (empty($student->country)) {
452 $country = '';
453 }
454 else {
455 if($countrysort) {
456 $country = '('.$student->country.') '.$countries[$student->country];
457 }
458 else {
459 $country = $countries[$student->country];
460 }
461 }
adcb3ac5 462
6746bb8a 463 $data = array (
cc038b47 464 //'<input type="checkbox" name="userid[]" value="'.$teacher->id.'" />',
465 print_user_picture($student->id, $course->id, $student->picture, false, true),
466 '<strong><a href="'.$CFG->wwwroot.'/user/view.php?id='.$student->id.'&amp;course='.$course->id.'">'.fullname($student).'</a></strong>',
467 $student->city,
468 $country,
6746bb8a 469 $lastaccess);
470 if ($course->enrolperiod) {
471 $data[] = userdate($student->timeend, $timeformat);
472 }
473 if ($isteacher) {
474 $data[] = '<input type="checkbox" name="user'.$student->id.'" />';
475 }
476 $table->add_data($data);
f19570d0 477
4969ad74 478 }
cc038b47 479 }
dc0dc7d5 480
cc038b47 481 $table->print_html();
6b281f1f 482
cc038b47 483 }
eca1c454 484
f19570d0 485 if ($isteacher) {
6746bb8a 486 echo '<br /><center>';
0f24c257 487 echo '<input type="button" onclick="checkall()" value="'.get_string('selectall').'" /> ';
488 echo '<input type="button" onclick="checknone()" value="'.get_string('deselectall').'" /> ';
6746bb8a 489 $displaylist['messageselect.php'] = get_string('messageselectadd');
490 if ($course->enrolperiod) {
491 $displaylist['extendenrol.php'] = get_string('extendenrol');
492 }
5e2c3024 493 choose_from_menu ($displaylist, "formaction", "", get_string("withselectedusers"), "if(checksubmit(this.form))this.form.submit();", "");
6746bb8a 494 echo '<input type="submit" value="' . get_string('ok') . '"';
f19570d0 495 echo '</center></form>';
496 }
497
cc038b47 498 if ($perpage == 99999) {
499 echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage='.DEFAULT_PAGE_SIZE.'">'.get_string('showperpage', '', DEFAULT_PAGE_SIZE).'</a></div>';
500 }
501 else if ($matchcount > 0 && $perpage < $matchcount) {
502 echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage=99999">'.get_string('showall', '', $matchcount).'</a></div>';
503 }
504
505 print_footer($course);
f9903ed0 506
f9903ed0 507?>