weekly release 2.7dev
[moodle.git] / user / index.php
CommitLineData
aa6c1ced 1<?php
f9903ed0 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');
99d19c13 7 require_once($CFG->libdir.'/filelib.php');
951b22a8 8
9 define('USER_SMALL_CLASS', 20); // Below this is considered small
10 define('USER_LARGE_CLASS', 200); // Above this is considered large
cc038b47 11 define('DEFAULT_PAGE_SIZE', 20);
36075e09 12 define('SHOW_ALL_PAGE_SIZE', 5000);
073af1a6 13 define('MODE_BRIEF', 0);
14 define('MODE_USERDETAILS', 1);
f9903ed0 15
cc038b47 16 $page = optional_param('page', 0, PARAM_INT); // which page to show
17 $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // how many per page
0f21a964 18 $mode = optional_param('mode', NULL, PARAM_INT); // use the MODE_ constants
5f9e296a 19 $accesssince = optional_param('accesssince',0,PARAM_INT); // filter by last access. -1 = never
93de0ac2
PS
20 $search = optional_param('search','',PARAM_RAW); // make sure it is processed with p() or s() when sending to output!
21 $roleid = optional_param('roleid', 0, PARAM_INT); // optional roleid, 0 means all enrolled users (or all on the frontpage)
e957cc26 22
5f9e296a 23 $contextid = optional_param('contextid', 0, PARAM_INT); // one of this or
24 $courseid = optional_param('id', 0, PARAM_INT); // this are required
3aac07d8 25
a6855934 26 $PAGE->set_url('/user/index.php', array(
aa6c1ced
PS
27 'page' => $page,
28 'perpage' => $perpage,
29 'mode' => $mode,
30 'accesssince' => $accesssince,
31 'search' => $search,
32 'roleid' => $roleid,
33 'contextid' => $contextid,
ae8e1645 34 'id' => $courseid));
f1e24e7a 35
e957cc26 36 if ($contextid) {
d197ea43 37 $context = context::instance_by_id($contextid, MUST_EXIST);
4f0c2d00 38 if ($context->contextlevel != CONTEXT_COURSE) {
06e84d52 39 print_error('invalidcontext');
e957cc26 40 }
74df2951 41 $course = $DB->get_record('course', array('id'=>$context->instanceid), '*', MUST_EXIST);
e957cc26 42 } else {
74df2951 43 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
43731030 44 $context = context_course::instance($course->id, MUST_EXIST);
f9903ed0 45 }
87a13824 46 // not needed anymore
47 unset($contextid);
48 unset($courseid);
f9903ed0 49
224aa44a 50 require_login($course);
f9903ed0 51
43731030 52 $systemcontext = context_system::instance();
4f0c2d00
PS
53 $isfrontpage = ($course->id == SITEID);
54
43731030 55 $frontpagectx = context_course::instance(SITEID);
224aa44a 56
4f0c2d00 57 if ($isfrontpage) {
c07dfe70 58 $PAGE->set_pagelayout('admin');
1118434a 59 require_capability('moodle/site:viewparticipants', $systemcontext);
4f0c2d00 60 } else {
c07dfe70 61 $PAGE->set_pagelayout('incourse');
4f0c2d00 62 require_capability('moodle/course:viewparticipants', $context);
6d3c57f4 63 }
64
d7270311 65 $rolenamesurl = new moodle_url("$CFG->wwwroot/user/index.php?contextid=$context->id&sifirst=&silast=");
66
c52551dc 67 $rolenames = role_fix_names(get_profile_roles($context), $context, ROLENAME_ALIAS, true);
4f0c2d00 68 if ($isfrontpage) {
d3c0f411 69 $rolenames[0] = get_string('allsiteusers', 'role');
4f0c2d00 70 } else {
d3c0f411 71 $rolenames[0] = get_string('allparticipants');
4f0c2d00 72 }
5f9e296a 73
4f0c2d00
PS
74 // make sure other roles may not be selected by any means
75 if (empty($rolenames[$roleid])) {
76 print_error('noparticipants');
5f9e296a 77 }
78
79 // no roles to display yet?
165088f6 80 // frontpage course is an exception, on the front page course we should display all users
4f0c2d00 81 if (empty($rolenames) && !$isfrontpage) {
294a176f 82 if (has_capability('moodle/role:assign', $context)) {
87a13824 83 redirect($CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$context->id);
41d7209c 84 } else {
06e84d52 85 print_error('noparticipants');
41d7209c 86 }
87 }
88
f2f694f3
MN
89 $event = \core\event\user_list_viewed::create(array(
90 'context' => $context,
91 'objectid' => $course->id,
92 'other' => array(
93 'courseid' => $course->id,
94 'courseshortname' => $course->shortname,
95 'coursefullname' => $course->fullname
96 )
97 ));
98 $event->trigger();
f9903ed0 99
77c645df 100 $bulkoperations = has_capability('moodle/course:bulkmessaging', $context);
f1e24e7a 101
0aa759b0 102 $countries = get_string_manager()->get_list_of_countries();
68b38e8d 103
cc038b47 104 $strnever = get_string('never');
68b38e8d 105
b85b25eb 106 $datestring = new stdClass();
a59e5a1c 107 $datestring->year = get_string('year');
108 $datestring->years = get_string('years');
cc038b47 109 $datestring->day = get_string('day');
110 $datestring->days = get_string('days');
111 $datestring->hour = get_string('hour');
112 $datestring->hours = get_string('hours');
113 $datestring->min = get_string('min');
114 $datestring->mins = get_string('mins');
115 $datestring->sec = get_string('sec');
116 $datestring->secs = get_string('secs');
d578afc8 117
ff3caf30 118 if ($mode !== NULL) {
073af1a6 119 $mode = (int)$mode;
120 $SESSION->userindexmode = $mode;
ff3caf30 121 } else if (isset($SESSION->userindexmode)) {
073af1a6 122 $mode = (int)$SESSION->userindexmode;
ff3caf30 123 } else {
073af1a6 124 $mode = MODE_BRIEF;
ff3caf30 125 }
126
4cd0d3a0 127/// Check to see if groups are being used in this course
ff3caf30 128/// and if so, set $currentgroup to reflect the current group
129
ffc536af 130 $groupmode = groups_get_course_groupmode($course); // Groups are being used
131 $currentgroup = groups_get_course_group($course, true);
ff3caf30 132
cc038b47 133 if (!$currentgroup) { // To make some other functions work better later
134 $currentgroup = NULL;
135 }
ff3caf30 136
728a4ebe 137 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context));
2cb2ce61 138
caa8363f 139 $PAGE->set_title("$course->shortname: ".get_string('participants'));
140 $PAGE->set_heading($course->fullname);
03d9401e 141 $PAGE->set_pagetype('course-view-' . $course->format);
99cca847 142 $PAGE->add_body_class('path-user'); // So we can style it independently
03d9401e 143 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
caa8363f 144
145 echo $OUTPUT->header();
0be6f678 146
99cca847
MD
147 echo '<div class="userlist">';
148
caa8363f 149 if ($isseparategroups and (!$currentgroup) ) {
150 // The user is not in the group so show message and exit
8fa89bfd 151 echo $OUTPUT->heading(get_string("notingroup"));
0ab75c34 152 echo $OUTPUT->footer();
ff3caf30 153 exit;
154 }
155
03d9401e 156
cc038b47 157 // Should use this variable so that we don't break stuff every time a variable is added or changed.
a6855934 158 $baseurl = new moodle_url('/user/index.php', array(
d7270311 159 'contextid' => $context->id,
160 'roleid' => $roleid,
161 'id' => $course->id,
162 'perpage' => $perpage,
163 'accesssince' => $accesssince,
164 'search' => s($search)));
ff3caf30 165
224aa44a 166/// setting up tags
e957cc26 167 if ($course->id == SITEID) {
1242eb8f 168 $filtertype = 'site';
e957cc26 169 } else if ($course->id && !$currentgroup) {
1242eb8f 170 $filtertype = 'course';
e957cc26 171 $filterselect = $course->id;
1242eb8f 172 } else {
173 $filtertype = 'group';
174 $filterselect = $currentgroup;
175 }
1242eb8f 176
03d9401e 177
1242eb8f 178
3468d58a 179/// Get the hidden field list
224aa44a 180 if (has_capability('moodle/course:viewhiddenuserfields', $context)) {
3468d58a 181 $hiddenfields = array(); // teachers and admins are allowed to see everything
182 } else {
183 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
184 }
185
5ff311f0 186 if (isset($hiddenfields['lastaccess'])) {
187 // do not allow access since filtering
188 $accesssince = 0;
189 }
bbbf2d40 190
ff3caf30 191/// Print settings and things in a table across the top
4e487ce2 192 $controlstable = new html_table();
16be8974 193 $controlstable->attributes['class'] = 'controls';
4e487ce2 194 $controlstable->cellspacing = 0;
195 $controlstable->data[] = new html_table_row();
ff3caf30 196
a72784d3 197/// Print my course menus
df997f84 198 if ($mycourses = enrol_get_my_courses()) {
7da0af9f 199 $courselist = array();
a6855934 200 $popupurl = new moodle_url('/user/index.php?roleid='.$roleid.'&sifirst=&silast=');
7da0af9f 201 foreach ($mycourses as $mycourse) {
43731030 202 $coursecontext = context_course::instance($mycourse->id);
8ebbb06a 203 $courselist[$mycourse->id] = format_string($mycourse->shortname, true, array('context' => $coursecontext));
7da0af9f 204 }
1118434a 205 if (has_capability('moodle/site:viewparticipants', $systemcontext)) {
5f9e296a 206 unset($courselist[SITEID]);
8ebbb06a 207 $courselist = array(SITEID => format_string($SITE->shortname, true, array('context' => $systemcontext))) + $courselist;
5f9e296a 208 }
97b659a1 209 $select = new single_select($popupurl, 'id', $courselist, $course->id, null, 'courseform');
d7270311 210 $select->set_label(get_string('mycourses'));
f8dab966 211 $controlstable->data[0]->cells[] = $OUTPUT->render($select);
7da0af9f 212 }
87a13824 213
b60cae86 214 $controlstable->data[0]->cells[] = groups_print_course_menu($course, $baseurl->out(), true);
ff3caf30 215
5ff311f0 216 if (!isset($hiddenfields['lastaccess'])) {
217 // get minimum lastaccess for this course and display a dropbox to filter by lastaccess going back this far.
218 // we need to make it diferently for normal courses and site course
4f0c2d00 219 if (!$isfrontpage) {
5ff311f0 220 $minlastaccess = $DB->get_field_sql('SELECT min(timeaccess)
221 FROM {user_lastaccess}
222 WHERE courseid = ?
223 AND timeaccess != 0', array($course->id));
224 $lastaccess0exists = $DB->record_exists('user_lastaccess', array('courseid'=>$course->id, 'timeaccess'=>0));
225 } else {
226 $minlastaccess = $DB->get_field_sql('SELECT min(lastaccess)
227 FROM {user}
228 WHERE lastaccess != 0');
229 $lastaccess0exists = $DB->record_exists('user', array('lastaccess'=>0));
230 }
0749caef 231
5ff311f0 232 $now = usergetmidnight(time());
233 $timeaccess = array();
d7270311 234 $baseurl->remove_params('accesssince');
87a13824 235
5ff311f0 236 // makes sense for this to go first.
237 $timeoptions[0] = get_string('selectperiod');
87a13824 238
5ff311f0 239 // days
240 for ($i = 1; $i < 7; $i++) {
241 if (strtotime('-'.$i.' days',$now) >= $minlastaccess) {
242 $timeoptions[strtotime('-'.$i.' days',$now)] = get_string('numdays','moodle',$i);
243 }
ca792680 244 }
5ff311f0 245 // weeks
246 for ($i = 1; $i < 10; $i++) {
247 if (strtotime('-'.$i.' weeks',$now) >= $minlastaccess) {
248 $timeoptions[strtotime('-'.$i.' weeks',$now)] = get_string('numweeks','moodle',$i);
249 }
ca792680 250 }
5ff311f0 251 // months
252 for ($i = 2; $i < 12; $i++) {
253 if (strtotime('-'.$i.' months',$now) >= $minlastaccess) {
254 $timeoptions[strtotime('-'.$i.' months',$now)] = get_string('nummonths','moodle',$i);
255 }
256 }
257 // try a year
258 if (strtotime('-1 year',$now) >= $minlastaccess) {
259 $timeoptions[strtotime('-1 year',$now)] = get_string('lastyear');
31b71336 260 }
87a13824 261
5ff311f0 262 if (!empty($lastaccess0exists)) {
263 $timeoptions[-1] = get_string('never');
264 }
87a13824 265
5ff311f0 266 if (count($timeoptions) > 1) {
f8dab966 267 $select = new single_select($baseurl, 'accesssince', $timeoptions, $accesssince, null, 'timeoptions');
d7270311 268 $select->set_label(get_string('usersnoaccesssince'));
f8dab966 269 $controlstable->data[0]->cells[] = $OUTPUT->render($select);
5ff311f0 270 }
77c645df 271 }
87a13824 272
073af1a6 273 $formatmenu = array( '0' => get_string('brief'),
274 '1' => get_string('userdetails'));
f8dab966 275 $select = new single_select($baseurl, 'mode', $formatmenu, $mode, null, 'formatmenu');
d7270311 276 $select->set_label(get_string('userlist'));
4e487ce2 277 $userlistcell = new html_table_cell();
16be8974 278 $userlistcell->attributes['class'] = 'right';
f8dab966 279 $userlistcell->text = $OUTPUT->render($select);
4e487ce2 280 $controlstable->data[0]->cells[] = $userlistcell;
281
16be8974 282 echo html_writer::table($controlstable);
ff3caf30 283
224aa44a 284 if ($currentgroup and (!$isseparategroups or has_capability('moodle/site:accessallgroups', $context))) { /// Display info about the group
ffc536af 285 if ($group = groups_get_group($currentgroup)) {
87a13824 286 if (!empty($group->description) or (!empty($group->picture) and empty($group->hidepicture))) {
4e487ce2 287 $groupinfotable = new html_table();
16be8974 288 $groupinfotable->attributes['class'] = 'groupinfobox';
4e487ce2 289 $picturecell = new html_table_cell();
16be8974 290 $picturecell->attributes['class'] = 'left side picture';
b60cae86 291 $picturecell->text = print_group_picture($group, $course->id, true, true, false);
aa6c1ced 292
4e487ce2 293 $contentcell = new html_table_cell();
16be8974 294 $contentcell->attributes['class'] = 'content';
4e487ce2 295
296 $contentheading = $group->name;
224aa44a 297 if (has_capability('moodle/course:managegroups', $context)) {
a6855934 298 $aurl = new moodle_url('/group/group.php', array('id' => $group->id, 'courseid' => $group->courseid));
c63923bd 299 $contentheading .= '&nbsp;' . $OUTPUT->action_icon($aurl, new pix_icon('t/edit', get_string('editgroupprofile')));
2c27cd19 300 }
8bdc9cac 301
64f93798 302 $group->description = file_rewrite_pluginfile_urls($group->description, 'pluginfile.php', $context->id, 'group', 'description', $group->id);
8bdc9cac
SH
303 if (!isset($group->descriptionformat)) {
304 $group->descriptionformat = FORMAT_MOODLE;
305 }
367a75fa
SH
306 $options = array('overflowdiv'=>true);
307 $contentcell->text = $OUTPUT->heading($contentheading, 3) . format_text($group->description, $group->descriptionformat, $options);
8cea545e 308 $groupinfotable->data[] = new html_table_row(array($picturecell, $contentcell));
16be8974 309 echo html_writer::table($groupinfotable);
ff3caf30 310 }
311 }
312 }
313
77c645df 314 /// Define a table showing a list of users in the current role selection
c238e1e2
DM
315 $tablecolumns = array();
316 $tableheaders = array();
317 if ($bulkoperations && $mode === MODE_BRIEF) {
318 $tablecolumns[] = 'select';
319 $tableheaders[] = get_string('select');
320 }
321 $tablecolumns[] = 'userpic';
322 $tablecolumns[] = 'fullname';
77c645df 323
43aa295b 324 $extrafields = get_extra_user_fields($context);
c238e1e2
DM
325 $tableheaders[] = get_string('userpic');
326 $tableheaders[] = get_string('fullnameuser');
327
43aa295b 328 if ($mode === MODE_BRIEF) {
329 foreach ($extrafields as $field) {
330 $tablecolumns[] = $field;
331 $tableheaders[] = get_user_field_name($field);
332 }
333 }
073af1a6 334 if ($mode === MODE_BRIEF && !isset($hiddenfields['city'])) {
77c645df 335 $tablecolumns[] = 'city';
336 $tableheaders[] = get_string('city');
337 }
073af1a6 338 if ($mode === MODE_BRIEF && !isset($hiddenfields['country'])) {
77c645df 339 $tablecolumns[] = 'country';
340 $tableheaders[] = get_string('country');
341 }
342 if (!isset($hiddenfields['lastaccess'])) {
343 $tablecolumns[] = 'lastaccess';
344 $tableheaders[] = get_string('lastaccess');
345 }
346
c238e1e2 347 if ($bulkoperations && $mode === MODE_USERDETAILS) {
664fe87f 348 $tablecolumns[] = 'select';
77c645df 349 $tableheaders[] = get_string('select');
350 }
351
04186cd9 352 $table = new flexible_table('user-index-participants-'.$course->id);
77c645df 353 $table->define_columns($tablecolumns);
354 $table->define_headers($tableheaders);
d7270311 355 $table->define_baseurl($baseurl->out());
0be6f678 356
5ff311f0 357 if (!isset($hiddenfields['lastaccess'])) {
358 $table->sortable(true, 'lastaccess', SORT_DESC);
1b80a504
AB
359 } else {
360 $table->sortable(true, 'firstname', SORT_ASC);
5ff311f0 361 }
362
664fe87f 363 $table->no_sorting('roles');
364 $table->no_sorting('groups');
365 $table->no_sorting('groupings');
366 $table->no_sorting('select');
77c645df 367
368 $table->set_attribute('cellspacing', '0');
04186cd9 369 $table->set_attribute('id', 'participants');
77c645df 370 $table->set_attribute('class', 'generaltable generalbox');
371
372 $table->set_control_variables(array(
373 TABLE_VAR_SORT => 'ssort',
374 TABLE_VAR_HIDE => 'shide',
375 TABLE_VAR_SHOW => 'sshow',
376 TABLE_VAR_IFIRST => 'sifirst',
377 TABLE_VAR_ILAST => 'silast',
378 TABLE_VAR_PAGE => 'spage'
379 ));
380 $table->setup();
381
505c641a 382 list($esql, $params) = get_enrolled_sql($context, NULL, $currentgroup, true);
4f0c2d00
PS
383 $joins = array("FROM {user} u");
384 $wheres = array();
385
43aa295b 386 $extrasql = get_extra_user_fields_sql($context, 'u', '', array(
387 'id', 'username', 'firstname', 'lastname', 'email', 'city', 'country',
388 'picture', 'lang', 'timezone', 'maildisplay', 'imagealt', 'lastaccess'));
389
a327f25e
AG
390 $mainuserfields = user_picture::fields('u', array('username', 'email', 'city', 'country', 'lang', 'timezone', 'maildisplay'));
391
4f0c2d00 392 if ($isfrontpage) {
a327f25e 393 $select = "SELECT $mainuserfields, u.lastaccess$extrasql";
4f0c2d00
PS
394 $joins[] = "JOIN ($esql) e ON e.id = u.id"; // everybody on the frontpage usually
395 if ($accesssince) {
396 $wheres[] = get_user_lastaccess_sql($accesssince);
397 }
398
399 } else {
a327f25e 400 $select = "SELECT $mainuserfields, COALESCE(ul.timeaccess, 0) AS lastaccess$extrasql";
4f0c2d00
PS
401 $joins[] = "JOIN ($esql) e ON e.id = u.id"; // course enrolled users only
402 $joins[] = "LEFT JOIN {user_lastaccess} ul ON (ul.userid = u.id AND ul.courseid = :courseid)"; // not everybody accessed course yet
403 $params['courseid'] = $course->id;
404 if ($accesssince) {
405 $wheres[] = get_course_lastaccess_sql($accesssince);
5f9e296a 406 }
165088f6 407 }
5f9e296a 408
4f0c2d00 409 // performance hacks - we preload user contexts together with accounts
2e4c0c91
FM
410 $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
411 $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)";
412 $params['contextlevel'] = CONTEXT_USER;
4f0c2d00
PS
413 $select .= $ccselect;
414 $joins[] = $ccjoin;
c0527567 415
4f0c2d00
PS
416
417 // limit list to users with some role only
418 if ($roleid) {
4e829d48
MN
419 // We want to query both the current context and parent contexts.
420 list($relatedctxsql, $relatedctxparams) = $DB->get_in_or_equal($context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'relatedctx');
421
422 $wheres[] = "u.id IN (SELECT userid FROM {role_assignments} WHERE roleid = :roleid AND contextid $relatedctxsql)";
423 $params = array_merge($params, array('roleid' => $roleid), $relatedctxparams);
c0527567 424 }
0be6f678 425
4f0c2d00
PS
426 $from = implode("\n", $joins);
427 if ($wheres) {
428 $where = "WHERE " . implode(" AND ", $wheres);
165088f6 429 } else {
4f0c2d00 430 $where = "";
165088f6 431 }
4f0c2d00
PS
432
433 $totalcount = $DB->count_records_sql("SELECT COUNT(u.id) $from $where", $params);
77c645df 434
435 if (!empty($search)) {
ce8c75ee 436 $fullname = $DB->sql_fullname('u.firstname','u.lastname');
f63ac65a
PS
437 $wheres[] = "(". $DB->sql_like($fullname, ':search1', false, false) .
438 " OR ". $DB->sql_like('email', ':search2', false, false) .
439 " OR ". $DB->sql_like('idnumber', ':search3', false, false) .") ";
ce8c75ee 440 $params['search1'] = "%$search%";
441 $params['search2'] = "%$search%";
442 $params['search3'] = "%$search%";
77c645df 443 }
444
0f21a964
PS
445 list($twhere, $tparams) = $table->get_sql_where();
446 if ($twhere) {
447 $wheres[] = $twhere;
448 $params = array_merge($params, $tparams);
77c645df 449 }
450
4f0c2d00
PS
451 $from = implode("\n", $joins);
452 if ($wheres) {
453 $where = "WHERE " . implode(" AND ", $wheres);
454 } else {
455 $where = "";
77c645df 456 }
457
458 if ($table->get_sql_sort()) {
4d93bc9e 459 $sort = ' ORDER BY '.$table->get_sql_sort();
77c645df 460 } else {
4d93bc9e 461 $sort = '';
77c645df 462 }
463
4f0c2d00 464 $matchcount = $DB->count_records_sql("SELECT COUNT(u.id) $from $where", $params);
77c645df 465
281917e9 466 $table->initialbars(true);
77c645df 467 $table->pagesize($perpage, $matchcount);
468
4f0c2d00
PS
469 // list of users at the current visible page - paging makes it relatively short
470 $userlist = $DB->get_recordset_sql("$select $from $where $sort", $params, $table->get_page_start(), $table->get_page_size());
3e219038 471
5f9e296a 472 /// If there are multiple Roles in the course, then show a drop down menu for switching
6d3c57f4 473 if (count($rolenames) > 1) {
77c645df 474 echo '<div class="rolesform">';
5f9e296a 475 echo '<label for="rolesform_jump">'.get_string('currentrole', 'role').'&nbsp;</label>';
f8dab966 476 echo $OUTPUT->single_select($rolenamesurl, 'roleid', $rolenames, $roleid, null, 'rolesform');
0f870a49 477 echo '</div>';
5f9e296a 478
479 } else if (count($rolenames) == 1) {
480 // when all users with the same role - print its name
481 echo '<div class="rolesform">';
995f2d51 482 echo get_string('role').get_string('labelsep', 'langconfig');
5f9e296a 483 $rolename = reset($rolenames);
484 echo $rolename;
485 echo '</div>';
77c645df 486 }
3e219038 487
5f9e296a 488 if ($roleid > 0) {
b85b25eb 489 $a = new stdClass();
b90e2f19 490 $a->number = $totalcount;
4f0c2d00 491 $a->role = $rolenames[$roleid];
5f9e296a 492 $heading = format_string(get_string('xuserswiththerole', 'role', $a));
f642a21a 493
494 if ($currentgroup and $group) {
495 $a->group = $group->name;
5f9e296a 496 $heading .= ' ' . format_string(get_string('ingroup', 'role', $a));
497 }
f642a21a 498
499 if ($accesssince) {
500 $a->timeperiod = $timeoptions[$accesssince];
5f9e296a 501 $heading .= ' ' . format_string(get_string('inactiveformorethan', 'role', $a));
f642a21a 502 }
503
504 $heading .= ": $a->number";
b85b25eb 505
d02eeded 506 if (user_can_assign($context, $roleid)) {
5cc0c73a
AG
507 $headingurl = new moodle_url($CFG->wwwroot . '/' . $CFG->admin . '/roles/assign.php',
508 array('roleid' => $roleid, 'contextid' => $context->id));
509 $heading .= $OUTPUT->action_icon($headingurl, new pix_icon('t/edit', get_string('edit')));
3e219038 510 }
04a7ba52 511 echo $OUTPUT->heading($heading, 3);
3e219038 512 } else {
d9e29086
DM
513 if ($course->id != SITEID && has_capability('moodle/course:enrolreview', $context)) {
514 $editlink = $OUTPUT->action_icon(new moodle_url('/enrol/users.php', array('id' => $course->id)),
5cc0c73a 515 new pix_icon('t/edit', get_string('edit')));
15234a92 516 } else {
517 $editlink = '';
518 }
5f9e296a 519 if ($course->id == SITEID and $roleid < 0) {
520 $strallparticipants = get_string('allsiteusers', 'role');
521 } else {
522 $strallparticipants = get_string('allparticipants');
523 }
23f5ee25 524 if ($matchcount < $totalcount) {
995f2d51 525 echo $OUTPUT->heading($strallparticipants.get_string('labelsep', 'langconfig').$matchcount.'/'.$totalcount . $editlink, 3);
23f5ee25 526 } else {
995f2d51 527 echo $OUTPUT->heading($strallparticipants.get_string('labelsep', 'langconfig').$matchcount . $editlink, 3);
23f5ee25 528 }
3e219038 529 }
3e219038 530
77c645df 531
532 if ($bulkoperations) {
2ea56951 533 echo '<form action="action_redir.php" method="post" id="participantsform">';
c1138797 534 echo '<div>';
d4a1fcaf 535 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
f0202ae9 536 echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
77c645df 537 }
538
4f0c2d00 539 if ($mode === MODE_USERDETAILS) { // Print simple listing
77c645df 540 if ($totalcount < 1) {
8fa89bfd 541 echo $OUTPUT->heading(get_string('nothingtodisplay'));
77c645df 542 } else {
543 if ($totalcount > $perpage) {
544
545 $firstinitial = $table->get_initial_first();
546 $lastinitial = $table->get_initial_last();
547 $strall = get_string('all');
61bb07c2 548 $alpha = explode(',', get_string('alphabet', 'langconfig'));
77c645df 549
550 // Bar of first initials
551
552 echo '<div class="initialbar firstinitial">'.get_string('firstname').' : ';
553 if(!empty($firstinitial)) {
d7270311 554 echo '<a href="'.$baseurl->out().'&amp;sifirst=">'.$strall.'</a>';
77c645df 555 } else {
556 echo '<strong>'.$strall.'</strong>';
557 }
558 foreach ($alpha as $letter) {
559 if ($letter == $firstinitial) {
560 echo ' <strong>'.$letter.'</strong>';
ee4a52c5 561 } else {
d7270311 562 echo ' <a href="'.$baseurl->out().'&amp;sifirst='.$letter.'">'.$letter.'</a>';
ee4a52c5 563 }
ee4a52c5 564 }
77c645df 565 echo '</div>';
566
567 // Bar of last initials
568
569 echo '<div class="initialbar lastinitial">'.get_string('lastname').' : ';
570 if(!empty($lastinitial)) {
d7270311 571 echo '<a href="'.$baseurl->out().'&amp;silast=">'.$strall.'</a>';
77c645df 572 } else {
573 echo '<strong>'.$strall.'</strong>';
574 }
575 foreach ($alpha as $letter) {
576 if ($letter == $lastinitial) {
577 echo ' <strong>'.$letter.'</strong>';
578 } else {
d7270311 579 echo ' <a href="'.$baseurl->out().'&amp;silast='.$letter.'">'.$letter.'</a>';
ee4a52c5 580 }
581 }
77c645df 582 echo '</div>';
583
929d7a83 584 $pagingbar = new paging_bar($matchcount, intval($table->get_page_start() / $perpage), $perpage, $baseurl);
da9ac19f 585 $pagingbar->pagevar = 'spage';
105666bb 586 echo $OUTPUT->render($pagingbar);
77c645df 587 }
588
589 if ($matchcount > 0) {
1f807bc4 590 $usersprinted = array();
ce8c75ee 591 foreach ($userlist as $user) {
1f807bc4 592 if (in_array($user->id, $usersprinted)) { /// Prevent duplicates by r.hidden - MDL-13935
593 continue;
594 }
595 $usersprinted[] = $user->id; /// Add new user to the array of users printed
596
db314f34 597 context_helper::preload_from_record($user);
c351150f 598
43731030
FM
599 $context = context_course::instance($course->id);
600 $usercontext = context_user::instance($user->id);
c351150f 601
0aa759b0 602 $countries = get_string_manager()->get_list_of_countries();
c351150f 603
604 /// Get the hidden field list
605 if (has_capability('moodle/course:viewhiddenuserfields', $context)) {
606 $hiddenfields = array();
607 } else {
608 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
609 }
610 $table = new html_table();
16be8974 611 $table->attributes['class'] = 'userinfobox';
c351150f 612
613 $row = new html_table_row();
614 $row->cells[0] = new html_table_cell();
16be8974 615 $row->cells[0]->attributes['class'] = 'left side';
f2f085ee 616
9c5cafd6 617 $row->cells[0]->text = $OUTPUT->user_picture($user, array('size' => 100, 'courseid'=>$course->id));
c351150f 618 $row->cells[1] = new html_table_cell();
16be8974 619 $row->cells[1]->attributes['class'] = 'content';
c351150f 620
621 $row->cells[1]->text = $OUTPUT->container(fullname($user, has_capability('moodle/site:viewfullnames', $context)), 'username');
622 $row->cells[1]->text .= $OUTPUT->container_start('info');
623
624 if (!empty($user->role)) {
995f2d51 625 $row->cells[1]->text .= get_string('role').get_string('labelsep', 'langconfig').$user->role.'<br />';
c351150f 626 }
474f6bfe 627 if ($user->maildisplay == 1 or ($user->maildisplay == 2 and ($course->id != SITEID) and !isguestuser()) or
43aa295b 628 has_capability('moodle/course:viewhiddenuserfields', $context) or
68e12697 629 in_array('email', $extrafields) or ($user->id == $USER->id)) {
995f2d51 630 $row->cells[1]->text .= get_string('email').get_string('labelsep', 'langconfig').html_writer::link("mailto:$user->email", $user->email) . '<br />';
c351150f 631 }
43aa295b 632 foreach ($extrafields as $field) {
633 if ($field === 'email') {
634 // Skip email because it was displayed with different
635 // logic above (because this page is intended for
636 // students too)
637 continue;
638 }
639 $row->cells[1]->text .= get_user_field_name($field) .
640 get_string('labelsep', 'langconfig') . s($user->{$field}) . '<br />';
641 }
c351150f 642 if (($user->city or $user->country) and (!isset($hiddenfields['city']) or !isset($hiddenfields['country']))) {
995f2d51 643 $row->cells[1]->text .= get_string('city').get_string('labelsep', 'langconfig');
c351150f 644 if ($user->city && !isset($hiddenfields['city'])) {
645 $row->cells[1]->text .= $user->city;
646 }
647 if (!empty($countries[$user->country]) && !isset($hiddenfields['country'])) {
648 if ($user->city && !isset($hiddenfields['city'])) {
649 $row->cells[1]->text .= ', ';
650 }
651 $row->cells[1]->text .= $countries[$user->country];
652 }
653 $row->cells[1]->text .= '<br />';
654 }
655
656 if (!isset($hiddenfields['lastaccess'])) {
657 if ($user->lastaccess) {
995f2d51 658 $row->cells[1]->text .= get_string('lastaccess').get_string('labelsep', 'langconfig').userdate($user->lastaccess);
c351150f 659 $row->cells[1]->text .= '&nbsp; ('. format_time(time() - $user->lastaccess, $datestring) .')';
660 } else {
995f2d51 661 $row->cells[1]->text .= get_string('lastaccess').get_string('labelsep', 'langconfig').get_string('never');
c351150f 662 }
663 }
664
665 $row->cells[1]->text .= $OUTPUT->container_end();
3aac07d8 666
c351150f 667 $row->cells[2] = new html_table_cell();
16be8974 668 $row->cells[2]->attributes['class'] = 'links';
c351150f 669 $row->cells[2]->text = '';
3aac07d8 670
c351150f 671 $links = array();
672
673 if ($CFG->bloglevel > 0) {
a6855934 674 $links[] = html_writer::link(new moodle_url('/blog/index.php?userid='.$user->id), get_string('blogs','blog'));
c351150f 675 }
676
677 if (!empty($CFG->enablenotes) and (has_capability('moodle/notes:manage', $context) || has_capability('moodle/notes:view', $context))) {
a6855934 678 $links[] = html_writer::link(new moodle_url('/notes/index.php?course=' . $course->id. '&user='.$user->id), get_string('notes','notes'));
c351150f 679 }
680
681 if (has_capability('moodle/site:viewreports', $context) or has_capability('moodle/user:viewuseractivitiesreport', $usercontext)) {
a6855934 682 $links[] = html_writer::link(new moodle_url('/course/user.php?id='. $course->id .'&user='. $user->id), get_string('activity'));
c351150f 683 }
684
d79d5ac2 685 if ($USER->id != $user->id && !\core\session\manager::is_loggedinas() && has_capability('moodle/user:loginas', $context) && !is_siteadmin($user->id)) {
a6855934 686 $links[] = html_writer::link(new moodle_url('/course/loginas.php?id='. $course->id .'&user='. $user->id .'&sesskey='. sesskey()), get_string('loginas'));
c351150f 687 }
688
a6855934 689 $links[] = html_writer::link(new moodle_url('/user/view.php?id='. $user->id .'&course='. $course->id), get_string('fullprofile') . '...');
3aac07d8 690
f8dab966 691 $row->cells[2]->text .= implode('', $links);
c351150f 692
1366a7fa
RT
693 if ($bulkoperations) {
694 $row->cells[2]->text .= '<br /><input type="checkbox" class="usercheckbox" name="user'.$user->id.'" /> ';
c351150f 695 }
696 $table->data = array($row);
16be8974 697 echo html_writer::table($table);
ee4a52c5 698 }
77c645df 699
700 } else {
8fa89bfd 701 echo $OUTPUT->heading(get_string('nothingtodisplay'));
ee4a52c5 702 }
703 }
77c645df 704
705 } else {
706 $countrysort = (strpos($sort, 'country') !== false);
707 $timeformat = get_string('strftimedate');
3997cb40 708
709
03cedd62 710 if ($userlist) {
664fe87f 711
1f807bc4 712 $usersprinted = array();
ce8c75ee 713 foreach ($userlist as $user) {
1f807bc4 714 if (in_array($user->id, $usersprinted)) { /// Prevent duplicates by r.hidden - MDL-13935
715 continue;
716 }
717 $usersprinted[] = $user->id; /// Add new user to the array of users printed
718
db314f34 719 context_helper::preload_from_record($user);
0be6f678 720
04186cd9 721 if ($user->lastaccess) {
722 $lastaccess = format_time(time() - $user->lastaccess, $datestring);
77c645df 723 } else {
724 $lastaccess = $strnever;
725 }
726
04186cd9 727 if (empty($user->country)) {
77c645df 728 $country = '';
729
730 } else {
731 if($countrysort) {
04186cd9 732 $country = '('.$user->country.') '.$countries[$user->country];
ee4a52c5 733 }
734 else {
04186cd9 735 $country = $countries[$user->country];
ee4a52c5 736 }
77c645df 737 }
0be6f678 738
43731030 739 $usercontext = context_user::instance($user->id);
0be6f678 740
b08261a1 741 if ($piclink = ($USER->id == $user->id || has_capability('moodle/user:viewdetails', $context) || has_capability('moodle/user:viewdetails', $usercontext))) {
22ae509e 742 $profilelink = '<strong><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></strong>';
743 } else {
0be6f678 744 $profilelink = '<strong>'.fullname($user).'</strong>';
22ae509e 745 }
0be6f678 746
c238e1e2
DM
747 $data = array();
748 if ($bulkoperations) {
749 $data[] = '<input type="checkbox" class="usercheckbox" name="user'.$user->id.'" />';
750 }
751 $data[] = $OUTPUT->user_picture($user, array('size' => 35, 'courseid'=>$course->id));
752 $data[] = $profilelink;
22ae509e 753
43aa295b 754 if ($mode === MODE_BRIEF) {
755 foreach ($extrafields as $field) {
756 $data[] = $user->{$field};
757 }
758 }
073af1a6 759 if ($mode === MODE_BRIEF && !isset($hiddenfields['city'])) {
04186cd9 760 $data[] = $user->city;
77c645df 761 }
073af1a6 762 if ($mode === MODE_BRIEF && !isset($hiddenfields['country'])) {
77c645df 763 $data[] = $country;
764 }
765 if (!isset($hiddenfields['lastaccess'])) {
766 $data[] = $lastaccess;
767 }
664fe87f 768
769 if (isset($userlist_extra) && isset($userlist_extra[$user->id])) {
770 $ras = $userlist_extra[$user->id]['ra'];
771 $rastring = '';
772 foreach ($ras AS $key=>$ra) {
4f0c2d00 773 $rolename = $allrolenames[$ra['roleid']] ;
664fe87f 774 if ($ra['ctxlevel'] == CONTEXT_COURSECAT) {
30142c19 775 $rastring .= $rolename. ' @ ' . '<a href="'.$CFG->wwwroot.'/course/index.php?categoryid='.$ra['ctxinstanceid'].'">'.s($ra['ccname']).'</a>';
664fe87f 776 } elseif ($ra['ctxlevel'] == CONTEXT_SYSTEM) {
073af1a6 777 $rastring .= $rolename. ' - ' . get_string('globalrole','role');
664fe87f 778 } else {
779 $rastring .= $rolename;
780 }
664fe87f 781 }
06e84d52 782 $data[] = $rastring;
a32e05df 783 if ($groupmode != 0) {
664fe87f 784 // htmlescape with s() and implode the array
785 $data[] = implode(', ', array_map('s',$userlist_extra[$user->id]['group']));
98da6021 786 $data[] = implode(', ', array_map('s', $userlist_extra[$user->id]['gping']));
664fe87f 787 }
664fe87f 788 }
789
77c645df 790 $table->add_data($data);
ee4a52c5 791 }
ee4a52c5 792 }
b90e2f19 793
77c645df 794 $table->print_html();
795
796 }
797
798 if ($bulkoperations) {
c1138797 799 echo '<br /><div class="buttons">';
2ea56951
PS
800 echo '<input type="button" id="checkall" value="'.get_string('selectall').'" /> ';
801 echo '<input type="button" id="checknone" value="'.get_string('deselectall').'" /> ';
82d561ee 802 $displaylist = array();
576ad290 803 $displaylist['messageselect.php'] = get_string('messageselectadd');
90658eef 804 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
eca3af25 805 $displaylist['addnote.php'] = get_string('addnewnote', 'notes');
806 $displaylist['groupaddnote.php'] = get_string('groupaddnewnote', 'notes');
807 }
576ad290 808
62357718 809 echo $OUTPUT->help_icon('withselectedusers');
26acc814 810 echo html_writer::tag('label', get_string("withselectedusers"), array('for'=>'formactionid'));
2ea56951
PS
811 echo html_writer::select($displaylist, 'formaction', '', array(''=>'choosedots'), array('id'=>'formactionid'));
812
70ce923e 813 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
2ea56951 814 echo '<noscript style="display:inline">';
f53f73ca 815 echo '<div><input type="submit" value="'.get_string('ok').'" /></div>';
2ea56951
PS
816 echo '</noscript>';
817 echo '</div></div>';
c1138797 818 echo '</form>';
d88c5043 819
2ea56951
PS
820 $module = array('name'=>'core_user', 'fullpath'=>'/user/module.js');
821 $PAGE->requires->js_init_call('M.core_user.init_participation', null, false, $module);
77c645df 822 }
b90e2f19 823
d6e52b00
RS
824 // Show a search box if all participants don't fit on a single screen
825 if ($totalcount > $perpage) {
58f85786
RW
826 echo '<form action="index.php" class="searchform"><div><input type="hidden" name="id" value="'.$course->id.'" />';
827 echo '<label for="search">' . get_string('search', 'search') . ' </label>';
2134ddcb 828 echo '<input type="text" id="search" name="search" value="'.s($search).'" />&nbsp;<input type="submit" value="'.get_string('search').'" /></div></form>'."\n";
77c645df 829 }
b90e2f19 830
d7270311 831 $perpageurl = clone($baseurl);
832 $perpageurl->remove_params('perpage');
77c645df 833 if ($perpage == SHOW_ALL_PAGE_SIZE) {
d7270311 834 $perpageurl->param('perpage', DEFAULT_PAGE_SIZE);
0f4c64b7 835 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showperpage', '', DEFAULT_PAGE_SIZE)), array(), 'showall');
77c645df 836
837 } else if ($matchcount > 0 && $perpage < $matchcount) {
d7270311 838 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
0f4c64b7 839 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showall', '', $matchcount)), array(), 'showall');
b90e2f19 840 }
77c645df 841
99cca847
MD
842 echo '</div>'; // userlist
843
0ab75c34 844 echo $OUTPUT->footer();
f9903ed0 845
03cedd62 846 if ($userlist) {
ce8c75ee 847 $userlist->close();
03cedd62 848 }
77c645df 849
850
0749caef 851function get_course_lastaccess_sql($accesssince='') {
31b71336 852 if (empty($accesssince)) {
853 return '';
854 }
855 if ($accesssince == -1) { // never
4f0c2d00 856 return 'ul.timeaccess = 0';
31b71336 857 } else {
4f0c2d00 858 return 'ul.timeaccess != 0 AND ul.timeaccess < '.$accesssince;
0749caef 859 }
860}
861
862function get_user_lastaccess_sql($accesssince='') {
863 if (empty($accesssince)) {
864 return '';
865 }
866 if ($accesssince == -1) { // never
4f0c2d00 867 return 'u.lastaccess = 0';
0749caef 868 } else {
4f0c2d00 869 return 'u.lastaccess != 0 AND u.lastaccess < '.$accesssince;
31b71336 870 }
871}