Merge branch 'wip-MDL-21097-m25' of git://github.com/marinaglancy/moodle into MOODLE_...
[moodle.git] / user / index.php
blob39ea25f66412e50d878d05b20fdbba8697841a32
1 <?php
3 // Lists all the users within a given course
5 require_once('../config.php');
6 require_once($CFG->libdir.'/tablelib.php');
7 require_once($CFG->libdir.'/filelib.php');
9 define('USER_SMALL_CLASS', 20); // Below this is considered small
10 define('USER_LARGE_CLASS', 200); // Above this is considered large
11 define('DEFAULT_PAGE_SIZE', 20);
12 define('SHOW_ALL_PAGE_SIZE', 5000);
13 define('MODE_BRIEF', 0);
14 define('MODE_USERDETAILS', 1);
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
18 $mode = optional_param('mode', NULL, PARAM_INT); // use the MODE_ constants
19 $accesssince = optional_param('accesssince',0,PARAM_INT); // filter by last access. -1 = never
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)
23 $contextid = optional_param('contextid', 0, PARAM_INT); // one of this or
24 $courseid = optional_param('id', 0, PARAM_INT); // this are required
26 $PAGE->set_url('/user/index.php', array(
27 'page' => $page,
28 'perpage' => $perpage,
29 'mode' => $mode,
30 'accesssince' => $accesssince,
31 'search' => $search,
32 'roleid' => $roleid,
33 'contextid' => $contextid,
34 'id' => $courseid));
36 if ($contextid) {
37 $context = context::instance_by_id($contextid, MUST_EXIST);
38 if ($context->contextlevel != CONTEXT_COURSE) {
39 print_error('invalidcontext');
41 $course = $DB->get_record('course', array('id'=>$context->instanceid), '*', MUST_EXIST);
42 } else {
43 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
44 $context = context_course::instance($course->id, MUST_EXIST);
46 // not needed anymore
47 unset($contextid);
48 unset($courseid);
50 require_login($course);
52 $systemcontext = context_system::instance();
53 $isfrontpage = ($course->id == SITEID);
55 $frontpagectx = context_course::instance(SITEID);
57 if ($isfrontpage) {
58 $PAGE->set_pagelayout('admin');
59 require_capability('moodle/site:viewparticipants', $systemcontext);
60 } else {
61 $PAGE->set_pagelayout('incourse');
62 require_capability('moodle/course:viewparticipants', $context);
65 $rolenamesurl = new moodle_url("$CFG->wwwroot/user/index.php?contextid=$context->id&sifirst=&silast=");
67 $rolenames = role_fix_names(get_profile_roles($context), $context, ROLENAME_ALIAS, true);
68 if ($isfrontpage) {
69 $rolenames[0] = get_string('allsiteusers', 'role');
70 } else {
71 $rolenames[0] = get_string('allparticipants');
74 // make sure other roles may not be selected by any means
75 if (empty($rolenames[$roleid])) {
76 print_error('noparticipants');
79 // no roles to display yet?
80 // frontpage course is an exception, on the front page course we should display all users
81 if (empty($rolenames) && !$isfrontpage) {
82 if (has_capability('moodle/role:assign', $context)) {
83 redirect($CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$context->id);
84 } else {
85 print_error('noparticipants');
89 add_to_log($course->id, 'user', 'view all', 'index.php?id='.$course->id, '');
91 $bulkoperations = has_capability('moodle/course:bulkmessaging', $context);
93 $countries = get_string_manager()->get_list_of_countries();
95 $strnever = get_string('never');
97 $datestring = new stdClass();
98 $datestring->year = get_string('year');
99 $datestring->years = get_string('years');
100 $datestring->day = get_string('day');
101 $datestring->days = get_string('days');
102 $datestring->hour = get_string('hour');
103 $datestring->hours = get_string('hours');
104 $datestring->min = get_string('min');
105 $datestring->mins = get_string('mins');
106 $datestring->sec = get_string('sec');
107 $datestring->secs = get_string('secs');
109 if ($mode !== NULL) {
110 $mode = (int)$mode;
111 $SESSION->userindexmode = $mode;
112 } else if (isset($SESSION->userindexmode)) {
113 $mode = (int)$SESSION->userindexmode;
114 } else {
115 $mode = MODE_BRIEF;
118 /// Check to see if groups are being used in this course
119 /// and if so, set $currentgroup to reflect the current group
121 $groupmode = groups_get_course_groupmode($course); // Groups are being used
122 $currentgroup = groups_get_course_group($course, true);
124 if (!$currentgroup) { // To make some other functions work better later
125 $currentgroup = NULL;
128 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context));
130 $PAGE->set_title("$course->shortname: ".get_string('participants'));
131 $PAGE->set_heading($course->fullname);
132 $PAGE->set_pagetype('course-view-' . $course->format);
133 $PAGE->add_body_class('path-user'); // So we can style it independently
134 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
136 echo $OUTPUT->header();
138 echo '<div class="userlist">';
140 if ($isseparategroups and (!$currentgroup) ) {
141 // The user is not in the group so show message and exit
142 echo $OUTPUT->heading(get_string("notingroup"));
143 echo $OUTPUT->footer();
144 exit;
148 // Should use this variable so that we don't break stuff every time a variable is added or changed.
149 $baseurl = new moodle_url('/user/index.php', array(
150 'contextid' => $context->id,
151 'roleid' => $roleid,
152 'id' => $course->id,
153 'perpage' => $perpage,
154 'accesssince' => $accesssince,
155 'search' => s($search)));
157 /// setting up tags
158 if ($course->id == SITEID) {
159 $filtertype = 'site';
160 } else if ($course->id && !$currentgroup) {
161 $filtertype = 'course';
162 $filterselect = $course->id;
163 } else {
164 $filtertype = 'group';
165 $filterselect = $currentgroup;
170 /// Get the hidden field list
171 if (has_capability('moodle/course:viewhiddenuserfields', $context)) {
172 $hiddenfields = array(); // teachers and admins are allowed to see everything
173 } else {
174 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
177 if (isset($hiddenfields['lastaccess'])) {
178 // do not allow access since filtering
179 $accesssince = 0;
182 /// Print settings and things in a table across the top
183 $controlstable = new html_table();
184 $controlstable->attributes['class'] = 'controls';
185 $controlstable->cellspacing = 0;
186 $controlstable->data[] = new html_table_row();
188 /// Print my course menus
189 if ($mycourses = enrol_get_my_courses()) {
190 $courselist = array();
191 $popupurl = new moodle_url('/user/index.php?roleid='.$roleid.'&sifirst=&silast=');
192 foreach ($mycourses as $mycourse) {
193 $coursecontext = context_course::instance($mycourse->id);
194 $courselist[$mycourse->id] = format_string($mycourse->shortname, true, array('context' => $coursecontext));
196 if (has_capability('moodle/site:viewparticipants', $systemcontext)) {
197 unset($courselist[SITEID]);
198 $courselist = array(SITEID => format_string($SITE->shortname, true, array('context' => $systemcontext))) + $courselist;
200 $select = new single_select($popupurl, 'id', $courselist, $course->id, array(''=>'choosedots'), 'courseform');
201 $select->set_label(get_string('mycourses'));
202 $controlstable->data[0]->cells[] = $OUTPUT->render($select);
205 $controlstable->data[0]->cells[] = groups_print_course_menu($course, $baseurl->out(), true);
207 if (!isset($hiddenfields['lastaccess'])) {
208 // get minimum lastaccess for this course and display a dropbox to filter by lastaccess going back this far.
209 // we need to make it diferently for normal courses and site course
210 if (!$isfrontpage) {
211 $minlastaccess = $DB->get_field_sql('SELECT min(timeaccess)
212 FROM {user_lastaccess}
213 WHERE courseid = ?
214 AND timeaccess != 0', array($course->id));
215 $lastaccess0exists = $DB->record_exists('user_lastaccess', array('courseid'=>$course->id, 'timeaccess'=>0));
216 } else {
217 $minlastaccess = $DB->get_field_sql('SELECT min(lastaccess)
218 FROM {user}
219 WHERE lastaccess != 0');
220 $lastaccess0exists = $DB->record_exists('user', array('lastaccess'=>0));
223 $now = usergetmidnight(time());
224 $timeaccess = array();
225 $baseurl->remove_params('accesssince');
227 // makes sense for this to go first.
228 $timeoptions[0] = get_string('selectperiod');
230 // days
231 for ($i = 1; $i < 7; $i++) {
232 if (strtotime('-'.$i.' days',$now) >= $minlastaccess) {
233 $timeoptions[strtotime('-'.$i.' days',$now)] = get_string('numdays','moodle',$i);
236 // weeks
237 for ($i = 1; $i < 10; $i++) {
238 if (strtotime('-'.$i.' weeks',$now) >= $minlastaccess) {
239 $timeoptions[strtotime('-'.$i.' weeks',$now)] = get_string('numweeks','moodle',$i);
242 // months
243 for ($i = 2; $i < 12; $i++) {
244 if (strtotime('-'.$i.' months',$now) >= $minlastaccess) {
245 $timeoptions[strtotime('-'.$i.' months',$now)] = get_string('nummonths','moodle',$i);
248 // try a year
249 if (strtotime('-1 year',$now) >= $minlastaccess) {
250 $timeoptions[strtotime('-1 year',$now)] = get_string('lastyear');
253 if (!empty($lastaccess0exists)) {
254 $timeoptions[-1] = get_string('never');
257 if (count($timeoptions) > 1) {
258 $select = new single_select($baseurl, 'accesssince', $timeoptions, $accesssince, null, 'timeoptions');
259 $select->set_label(get_string('usersnoaccesssince'));
260 $controlstable->data[0]->cells[] = $OUTPUT->render($select);
264 $formatmenu = array( '0' => get_string('brief'),
265 '1' => get_string('userdetails'));
266 $select = new single_select($baseurl, 'mode', $formatmenu, $mode, null, 'formatmenu');
267 $select->set_label(get_string('userlist'));
268 $userlistcell = new html_table_cell();
269 $userlistcell->attributes['class'] = 'right';
270 $userlistcell->text = $OUTPUT->render($select);
271 $controlstable->data[0]->cells[] = $userlistcell;
273 echo html_writer::table($controlstable);
275 if ($currentgroup and (!$isseparategroups or has_capability('moodle/site:accessallgroups', $context))) { /// Display info about the group
276 if ($group = groups_get_group($currentgroup)) {
277 if (!empty($group->description) or (!empty($group->picture) and empty($group->hidepicture))) {
278 $groupinfotable = new html_table();
279 $groupinfotable->attributes['class'] = 'groupinfobox';
280 $picturecell = new html_table_cell();
281 $picturecell->attributes['class'] = 'left side picture';
282 $picturecell->text = print_group_picture($group, $course->id, true, true, false);
284 $contentcell = new html_table_cell();
285 $contentcell->attributes['class'] = 'content';
287 $contentheading = $group->name;
288 if (has_capability('moodle/course:managegroups', $context)) {
289 $aurl = new moodle_url('/group/group.php', array('id' => $group->id, 'courseid' => $group->courseid));
290 $contentheading .= '&nbsp;' . $OUTPUT->action_icon($aurl, new pix_icon('t/edit', get_string('editgroupprofile')));
293 $group->description = file_rewrite_pluginfile_urls($group->description, 'pluginfile.php', $context->id, 'group', 'description', $group->id);
294 if (!isset($group->descriptionformat)) {
295 $group->descriptionformat = FORMAT_MOODLE;
297 $options = array('overflowdiv'=>true);
298 $contentcell->text = $OUTPUT->heading($contentheading, 3) . format_text($group->description, $group->descriptionformat, $options);
299 $groupinfotable->data[] = new html_table_row(array($picturecell, $contentcell));
300 echo html_writer::table($groupinfotable);
305 /// Define a table showing a list of users in the current role selection
306 $tablecolumns = array();
307 $tableheaders = array();
308 if ($bulkoperations && $mode === MODE_BRIEF) {
309 $tablecolumns[] = 'select';
310 $tableheaders[] = get_string('select');
312 $tablecolumns[] = 'userpic';
313 $tablecolumns[] = 'fullname';
315 $extrafields = get_extra_user_fields($context);
316 $tableheaders[] = get_string('userpic');
317 $tableheaders[] = get_string('fullnameuser');
319 if ($mode === MODE_BRIEF) {
320 foreach ($extrafields as $field) {
321 $tablecolumns[] = $field;
322 $tableheaders[] = get_user_field_name($field);
325 if ($mode === MODE_BRIEF && !isset($hiddenfields['city'])) {
326 $tablecolumns[] = 'city';
327 $tableheaders[] = get_string('city');
329 if ($mode === MODE_BRIEF && !isset($hiddenfields['country'])) {
330 $tablecolumns[] = 'country';
331 $tableheaders[] = get_string('country');
333 if (!isset($hiddenfields['lastaccess'])) {
334 $tablecolumns[] = 'lastaccess';
335 $tableheaders[] = get_string('lastaccess');
338 if ($bulkoperations && $mode === MODE_USERDETAILS) {
339 $tablecolumns[] = 'select';
340 $tableheaders[] = get_string('select');
343 $table = new flexible_table('user-index-participants-'.$course->id);
344 $table->define_columns($tablecolumns);
345 $table->define_headers($tableheaders);
346 $table->define_baseurl($baseurl->out());
348 if (!isset($hiddenfields['lastaccess'])) {
349 $table->sortable(true, 'lastaccess', SORT_DESC);
350 } else {
351 $table->sortable(true, 'firstname', SORT_ASC);
354 $table->no_sorting('roles');
355 $table->no_sorting('groups');
356 $table->no_sorting('groupings');
357 $table->no_sorting('select');
359 $table->set_attribute('cellspacing', '0');
360 $table->set_attribute('id', 'participants');
361 $table->set_attribute('class', 'generaltable generalbox');
363 $table->set_control_variables(array(
364 TABLE_VAR_SORT => 'ssort',
365 TABLE_VAR_HIDE => 'shide',
366 TABLE_VAR_SHOW => 'sshow',
367 TABLE_VAR_IFIRST => 'sifirst',
368 TABLE_VAR_ILAST => 'silast',
369 TABLE_VAR_PAGE => 'spage'
371 $table->setup();
373 // we are looking for all users with this role assigned in this context or higher
374 $contextlist = get_related_contexts_string($context);
376 list($esql, $params) = get_enrolled_sql($context, NULL, $currentgroup, true);
377 $joins = array("FROM {user} u");
378 $wheres = array();
380 $extrasql = get_extra_user_fields_sql($context, 'u', '', array(
381 'id', 'username', 'firstname', 'lastname', 'email', 'city', 'country',
382 'picture', 'lang', 'timezone', 'maildisplay', 'imagealt', 'lastaccess'));
384 if ($isfrontpage) {
385 $select = "SELECT u.id, u.username, u.firstname, u.lastname,
386 u.email, u.city, u.country, u.picture,
387 u.lang, u.timezone, u.maildisplay, u.imagealt,
388 u.lastaccess$extrasql";
389 $joins[] = "JOIN ($esql) e ON e.id = u.id"; // everybody on the frontpage usually
390 if ($accesssince) {
391 $wheres[] = get_user_lastaccess_sql($accesssince);
394 } else {
395 $select = "SELECT u.id, u.username, u.firstname, u.lastname,
396 u.email, u.city, u.country, u.picture,
397 u.lang, u.timezone, u.maildisplay, u.imagealt,
398 COALESCE(ul.timeaccess, 0) AS lastaccess$extrasql";
399 $joins[] = "JOIN ($esql) e ON e.id = u.id"; // course enrolled users only
400 $joins[] = "LEFT JOIN {user_lastaccess} ul ON (ul.userid = u.id AND ul.courseid = :courseid)"; // not everybody accessed course yet
401 $params['courseid'] = $course->id;
402 if ($accesssince) {
403 $wheres[] = get_course_lastaccess_sql($accesssince);
407 // performance hacks - we preload user contexts together with accounts
408 list($ccselect, $ccjoin) = context_instance_preload_sql('u.id', CONTEXT_USER, 'ctx');
409 $select .= $ccselect;
410 $joins[] = $ccjoin;
413 // limit list to users with some role only
414 if ($roleid) {
415 $wheres[] = "u.id IN (SELECT userid FROM {role_assignments} WHERE roleid = :roleid AND contextid $contextlist)";
416 $params['roleid'] = $roleid;
419 $from = implode("\n", $joins);
420 if ($wheres) {
421 $where = "WHERE " . implode(" AND ", $wheres);
422 } else {
423 $where = "";
426 $totalcount = $DB->count_records_sql("SELECT COUNT(u.id) $from $where", $params);
428 if (!empty($search)) {
429 $fullname = $DB->sql_fullname('u.firstname','u.lastname');
430 $wheres[] = "(". $DB->sql_like($fullname, ':search1', false, false) .
431 " OR ". $DB->sql_like('email', ':search2', false, false) .
432 " OR ". $DB->sql_like('idnumber', ':search3', false, false) .") ";
433 $params['search1'] = "%$search%";
434 $params['search2'] = "%$search%";
435 $params['search3'] = "%$search%";
438 list($twhere, $tparams) = $table->get_sql_where();
439 if ($twhere) {
440 $wheres[] = $twhere;
441 $params = array_merge($params, $tparams);
444 $from = implode("\n", $joins);
445 if ($wheres) {
446 $where = "WHERE " . implode(" AND ", $wheres);
447 } else {
448 $where = "";
451 if ($table->get_sql_sort()) {
452 $sort = ' ORDER BY '.$table->get_sql_sort();
453 } else {
454 $sort = '';
457 $matchcount = $DB->count_records_sql("SELECT COUNT(u.id) $from $where", $params);
459 $table->initialbars(true);
460 $table->pagesize($perpage, $matchcount);
462 // list of users at the current visible page - paging makes it relatively short
463 $userlist = $DB->get_recordset_sql("$select $from $where $sort", $params, $table->get_page_start(), $table->get_page_size());
465 /// If there are multiple Roles in the course, then show a drop down menu for switching
466 if (count($rolenames) > 1) {
467 echo '<div class="rolesform">';
468 echo '<label for="rolesform_jump">'.get_string('currentrole', 'role').'&nbsp;</label>';
469 echo $OUTPUT->single_select($rolenamesurl, 'roleid', $rolenames, $roleid, null, 'rolesform');
470 echo '</div>';
472 } else if (count($rolenames) == 1) {
473 // when all users with the same role - print its name
474 echo '<div class="rolesform">';
475 echo get_string('role').get_string('labelsep', 'langconfig');
476 $rolename = reset($rolenames);
477 echo $rolename;
478 echo '</div>';
481 if ($roleid > 0) {
482 $a = new stdClass();
483 $a->number = $totalcount;
484 $a->role = $rolenames[$roleid];
485 $heading = format_string(get_string('xuserswiththerole', 'role', $a));
487 if ($currentgroup and $group) {
488 $a->group = $group->name;
489 $heading .= ' ' . format_string(get_string('ingroup', 'role', $a));
492 if ($accesssince) {
493 $a->timeperiod = $timeoptions[$accesssince];
494 $heading .= ' ' . format_string(get_string('inactiveformorethan', 'role', $a));
497 $heading .= ": $a->number";
499 if (user_can_assign($context, $roleid)) {
500 $headingurl = new moodle_url($CFG->wwwroot . '/' . $CFG->admin . '/roles/assign.php',
501 array('roleid' => $roleid, 'contextid' => $context->id));
502 $heading .= $OUTPUT->action_icon($headingurl, new pix_icon('t/edit', get_string('edit')));
504 echo $OUTPUT->heading($heading, 3);
505 } else {
506 if ($course->id != SITEID && has_capability('moodle/course:enrolreview', $context)) {
507 $editlink = $OUTPUT->action_icon(new moodle_url('/enrol/users.php', array('id' => $course->id)),
508 new pix_icon('t/edit', get_string('edit')));
509 } else {
510 $editlink = '';
512 if ($course->id == SITEID and $roleid < 0) {
513 $strallparticipants = get_string('allsiteusers', 'role');
514 } else {
515 $strallparticipants = get_string('allparticipants');
517 if ($matchcount < $totalcount) {
518 echo $OUTPUT->heading($strallparticipants.get_string('labelsep', 'langconfig').$matchcount.'/'.$totalcount . $editlink, 3);
519 } else {
520 echo $OUTPUT->heading($strallparticipants.get_string('labelsep', 'langconfig').$matchcount . $editlink, 3);
525 if ($bulkoperations) {
526 echo '<form action="action_redir.php" method="post" id="participantsform">';
527 echo '<div>';
528 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
529 echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
532 if ($mode === MODE_USERDETAILS) { // Print simple listing
533 if ($totalcount < 1) {
534 echo $OUTPUT->heading(get_string('nothingtodisplay'));
535 } else {
536 if ($totalcount > $perpage) {
538 $firstinitial = $table->get_initial_first();
539 $lastinitial = $table->get_initial_last();
540 $strall = get_string('all');
541 $alpha = explode(',', get_string('alphabet', 'langconfig'));
543 // Bar of first initials
545 echo '<div class="initialbar firstinitial">'.get_string('firstname').' : ';
546 if(!empty($firstinitial)) {
547 echo '<a href="'.$baseurl->out().'&amp;sifirst=">'.$strall.'</a>';
548 } else {
549 echo '<strong>'.$strall.'</strong>';
551 foreach ($alpha as $letter) {
552 if ($letter == $firstinitial) {
553 echo ' <strong>'.$letter.'</strong>';
554 } else {
555 echo ' <a href="'.$baseurl->out().'&amp;sifirst='.$letter.'">'.$letter.'</a>';
558 echo '</div>';
560 // Bar of last initials
562 echo '<div class="initialbar lastinitial">'.get_string('lastname').' : ';
563 if(!empty($lastinitial)) {
564 echo '<a href="'.$baseurl->out().'&amp;silast=">'.$strall.'</a>';
565 } else {
566 echo '<strong>'.$strall.'</strong>';
568 foreach ($alpha as $letter) {
569 if ($letter == $lastinitial) {
570 echo ' <strong>'.$letter.'</strong>';
571 } else {
572 echo ' <a href="'.$baseurl->out().'&amp;silast='.$letter.'">'.$letter.'</a>';
575 echo '</div>';
577 $pagingbar = new paging_bar($matchcount, intval($table->get_page_start() / $perpage), $perpage, $baseurl);
578 $pagingbar->pagevar = 'spage';
579 echo $OUTPUT->render($pagingbar);
582 if ($matchcount > 0) {
583 $usersprinted = array();
584 foreach ($userlist as $user) {
585 if (in_array($user->id, $usersprinted)) { /// Prevent duplicates by r.hidden - MDL-13935
586 continue;
588 $usersprinted[] = $user->id; /// Add new user to the array of users printed
590 context_instance_preload($user);
592 $context = context_course::instance($course->id);
593 $usercontext = context_user::instance($user->id);
595 $countries = get_string_manager()->get_list_of_countries();
597 /// Get the hidden field list
598 if (has_capability('moodle/course:viewhiddenuserfields', $context)) {
599 $hiddenfields = array();
600 } else {
601 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
603 $table = new html_table();
604 $table->attributes['class'] = 'userinfobox';
606 $row = new html_table_row();
607 $row->cells[0] = new html_table_cell();
608 $row->cells[0]->attributes['class'] = 'left side';
610 $row->cells[0]->text = $OUTPUT->user_picture($user, array('size' => 100, 'courseid'=>$course->id));
611 $row->cells[1] = new html_table_cell();
612 $row->cells[1]->attributes['class'] = 'content';
614 $row->cells[1]->text = $OUTPUT->container(fullname($user, has_capability('moodle/site:viewfullnames', $context)), 'username');
615 $row->cells[1]->text .= $OUTPUT->container_start('info');
617 if (!empty($user->role)) {
618 $row->cells[1]->text .= get_string('role').get_string('labelsep', 'langconfig').$user->role.'<br />';
620 if ($user->maildisplay == 1 or ($user->maildisplay == 2 and ($course->id != SITEID) and !isguestuser()) or
621 has_capability('moodle/course:viewhiddenuserfields', $context) or
622 in_array('email', $extrafields) or ($user->id == $USER->id)) {
623 $row->cells[1]->text .= get_string('email').get_string('labelsep', 'langconfig').html_writer::link("mailto:$user->email", $user->email) . '<br />';
625 foreach ($extrafields as $field) {
626 if ($field === 'email') {
627 // Skip email because it was displayed with different
628 // logic above (because this page is intended for
629 // students too)
630 continue;
632 $row->cells[1]->text .= get_user_field_name($field) .
633 get_string('labelsep', 'langconfig') . s($user->{$field}) . '<br />';
635 if (($user->city or $user->country) and (!isset($hiddenfields['city']) or !isset($hiddenfields['country']))) {
636 $row->cells[1]->text .= get_string('city').get_string('labelsep', 'langconfig');
637 if ($user->city && !isset($hiddenfields['city'])) {
638 $row->cells[1]->text .= $user->city;
640 if (!empty($countries[$user->country]) && !isset($hiddenfields['country'])) {
641 if ($user->city && !isset($hiddenfields['city'])) {
642 $row->cells[1]->text .= ', ';
644 $row->cells[1]->text .= $countries[$user->country];
646 $row->cells[1]->text .= '<br />';
649 if (!isset($hiddenfields['lastaccess'])) {
650 if ($user->lastaccess) {
651 $row->cells[1]->text .= get_string('lastaccess').get_string('labelsep', 'langconfig').userdate($user->lastaccess);
652 $row->cells[1]->text .= '&nbsp; ('. format_time(time() - $user->lastaccess, $datestring) .')';
653 } else {
654 $row->cells[1]->text .= get_string('lastaccess').get_string('labelsep', 'langconfig').get_string('never');
658 $row->cells[1]->text .= $OUTPUT->container_end();
660 $row->cells[2] = new html_table_cell();
661 $row->cells[2]->attributes['class'] = 'links';
662 $row->cells[2]->text = '';
664 $links = array();
666 if ($CFG->bloglevel > 0) {
667 $links[] = html_writer::link(new moodle_url('/blog/index.php?userid='.$user->id), get_string('blogs','blog'));
670 if (!empty($CFG->enablenotes) and (has_capability('moodle/notes:manage', $context) || has_capability('moodle/notes:view', $context))) {
671 $links[] = html_writer::link(new moodle_url('/notes/index.php?course=' . $course->id. '&user='.$user->id), get_string('notes','notes'));
674 if (has_capability('moodle/site:viewreports', $context) or has_capability('moodle/user:viewuseractivitiesreport', $usercontext)) {
675 $links[] = html_writer::link(new moodle_url('/course/user.php?id='. $course->id .'&user='. $user->id), get_string('activity'));
678 if ($USER->id != $user->id && !session_is_loggedinas() && has_capability('moodle/user:loginas', $context) && !is_siteadmin($user->id)) {
679 $links[] = html_writer::link(new moodle_url('/course/loginas.php?id='. $course->id .'&user='. $user->id .'&sesskey='. sesskey()), get_string('loginas'));
682 $links[] = html_writer::link(new moodle_url('/user/view.php?id='. $user->id .'&course='. $course->id), get_string('fullprofile') . '...');
684 $row->cells[2]->text .= implode('', $links);
686 if ($bulkoperations) {
687 $row->cells[2]->text .= '<br /><input type="checkbox" class="usercheckbox" name="user'.$user->id.'" /> ';
689 $table->data = array($row);
690 echo html_writer::table($table);
693 } else {
694 echo $OUTPUT->heading(get_string('nothingtodisplay'));
698 } else {
699 $countrysort = (strpos($sort, 'country') !== false);
700 $timeformat = get_string('strftimedate');
703 if ($userlist) {
705 $usersprinted = array();
706 foreach ($userlist as $user) {
707 if (in_array($user->id, $usersprinted)) { /// Prevent duplicates by r.hidden - MDL-13935
708 continue;
710 $usersprinted[] = $user->id; /// Add new user to the array of users printed
712 context_instance_preload($user);
714 if ($user->lastaccess) {
715 $lastaccess = format_time(time() - $user->lastaccess, $datestring);
716 } else {
717 $lastaccess = $strnever;
720 if (empty($user->country)) {
721 $country = '';
723 } else {
724 if($countrysort) {
725 $country = '('.$user->country.') '.$countries[$user->country];
727 else {
728 $country = $countries[$user->country];
732 $usercontext = context_user::instance($user->id);
734 if ($piclink = ($USER->id == $user->id || has_capability('moodle/user:viewdetails', $context) || has_capability('moodle/user:viewdetails', $usercontext))) {
735 $profilelink = '<strong><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></strong>';
736 } else {
737 $profilelink = '<strong>'.fullname($user).'</strong>';
740 $data = array();
741 if ($bulkoperations) {
742 $data[] = '<input type="checkbox" class="usercheckbox" name="user'.$user->id.'" />';
744 $data[] = $OUTPUT->user_picture($user, array('size' => 35, 'courseid'=>$course->id));
745 $data[] = $profilelink;
747 if ($mode === MODE_BRIEF) {
748 foreach ($extrafields as $field) {
749 $data[] = $user->{$field};
752 if ($mode === MODE_BRIEF && !isset($hiddenfields['city'])) {
753 $data[] = $user->city;
755 if ($mode === MODE_BRIEF && !isset($hiddenfields['country'])) {
756 $data[] = $country;
758 if (!isset($hiddenfields['lastaccess'])) {
759 $data[] = $lastaccess;
762 if (isset($userlist_extra) && isset($userlist_extra[$user->id])) {
763 $ras = $userlist_extra[$user->id]['ra'];
764 $rastring = '';
765 foreach ($ras AS $key=>$ra) {
766 $rolename = $allrolenames[$ra['roleid']] ;
767 if ($ra['ctxlevel'] == CONTEXT_COURSECAT) {
768 $rastring .= $rolename. ' @ ' . '<a href="'.$CFG->wwwroot.'/course/index.php?categoryid='.$ra['ctxinstanceid'].'">'.s($ra['ccname']).'</a>';
769 } elseif ($ra['ctxlevel'] == CONTEXT_SYSTEM) {
770 $rastring .= $rolename. ' - ' . get_string('globalrole','role');
771 } else {
772 $rastring .= $rolename;
775 $data[] = $rastring;
776 if ($groupmode != 0) {
777 // htmlescape with s() and implode the array
778 $data[] = implode(', ', array_map('s',$userlist_extra[$user->id]['group']));
779 $data[] = implode(', ', array_map('s', $userlist_extra[$user->id]['gping']));
783 $table->add_data($data);
787 $table->print_html();
791 if ($bulkoperations) {
792 echo '<br /><div class="buttons">';
793 echo '<input type="button" id="checkall" value="'.get_string('selectall').'" /> ';
794 echo '<input type="button" id="checknone" value="'.get_string('deselectall').'" /> ';
795 $displaylist = array();
796 $displaylist['messageselect.php'] = get_string('messageselectadd');
797 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
798 $displaylist['addnote.php'] = get_string('addnewnote', 'notes');
799 $displaylist['groupaddnote.php'] = get_string('groupaddnewnote', 'notes');
802 echo $OUTPUT->help_icon('withselectedusers');
803 echo html_writer::tag('label', get_string("withselectedusers"), array('for'=>'formactionid'));
804 echo html_writer::select($displaylist, 'formaction', '', array(''=>'choosedots'), array('id'=>'formactionid'));
806 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
807 echo '<noscript style="display:inline">';
808 echo '<div><input type="submit" value="'.get_string('ok').'" /></div>';
809 echo '</noscript>';
810 echo '</div></div>';
811 echo '</form>';
813 $module = array('name'=>'core_user', 'fullpath'=>'/user/module.js');
814 $PAGE->requires->js_init_call('M.core_user.init_participation', null, false, $module);
817 if (has_capability('moodle/site:viewparticipants', $context) && $totalcount > ($perpage*3)) {
818 echo '<form action="index.php" class="searchform"><div><input type="hidden" name="id" value="'.$course->id.'" />';
819 echo '<label for="search">' . get_string('search', 'search') . ' </label>';
820 echo '<input type="text" id="search" name="search" value="'.s($search).'" />&nbsp;<input type="submit" value="'.get_string('search').'" /></div></form>'."\n";
823 $perpageurl = clone($baseurl);
824 $perpageurl->remove_params('perpage');
825 if ($perpage == SHOW_ALL_PAGE_SIZE) {
826 $perpageurl->param('perpage', DEFAULT_PAGE_SIZE);
827 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showperpage', '', DEFAULT_PAGE_SIZE)), array(), 'showall');
829 } else if ($matchcount > 0 && $perpage < $matchcount) {
830 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
831 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showall', '', $matchcount)), array(), 'showall');
834 echo '</div>'; // userlist
836 echo $OUTPUT->footer();
838 if ($userlist) {
839 $userlist->close();
843 function get_course_lastaccess_sql($accesssince='') {
844 if (empty($accesssince)) {
845 return '';
847 if ($accesssince == -1) { // never
848 return 'ul.timeaccess = 0';
849 } else {
850 return 'ul.timeaccess != 0 AND ul.timeaccess < '.$accesssince;
854 function get_user_lastaccess_sql($accesssince='') {
855 if (empty($accesssince)) {
856 return '';
858 if ($accesssince == -1) { // never
859 return 'u.lastaccess = 0';
860 } else {
861 return 'u.lastaccess != 0 AND u.lastaccess < '.$accesssince;