Merge branch 'w42_MDL-29866_m22_setupinit' of git://github.com/skodak/moodle
[moodle.git] / user / index.php
blobf54a5d249620889917e1830ded1b61cd0b6f7934
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 'courseid' => $courseid));
36 if ($contextid) {
37 $context = get_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 = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
46 // not needed anymore
47 unset($contextid);
48 unset($courseid);
50 require_login($course);
52 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
53 $isfrontpage = ($course->id == SITEID);
55 $frontpagectx = get_context_instance(CONTEXT_COURSE, 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 $allroles = get_all_roles();
68 $roles = get_profile_roles($context);
69 $allrolenames = array();
70 if ($isfrontpage) {
71 $rolenames = array(0=>get_string('allsiteusers', 'role'));
72 } else {
73 $rolenames = array(0=>get_string('allparticipants'));
76 foreach ($allroles as $role) {
77 $allrolenames[$role->id] = strip_tags(role_get_name($role, $context)); // Used in menus etc later on
78 if (isset($roles[$role->id])) {
79 $rolenames[$role->id] = $allrolenames[$role->id];
83 // make sure other roles may not be selected by any means
84 if (empty($rolenames[$roleid])) {
85 print_error('noparticipants');
88 // no roles to display yet?
89 // frontpage course is an exception, on the front page course we should display all users
90 if (empty($rolenames) && !$isfrontpage) {
91 if (has_capability('moodle/role:assign', $context)) {
92 redirect($CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$context->id);
93 } else {
94 print_error('noparticipants');
98 add_to_log($course->id, 'user', 'view all', 'index.php?id='.$course->id, '');
100 $bulkoperations = has_capability('moodle/course:bulkmessaging', $context);
102 $countries = get_string_manager()->get_list_of_countries();
104 $strnever = get_string('never');
106 $datestring->year = get_string('year');
107 $datestring->years = get_string('years');
108 $datestring->day = get_string('day');
109 $datestring->days = get_string('days');
110 $datestring->hour = get_string('hour');
111 $datestring->hours = get_string('hours');
112 $datestring->min = get_string('min');
113 $datestring->mins = get_string('mins');
114 $datestring->sec = get_string('sec');
115 $datestring->secs = get_string('secs');
117 if ($mode !== NULL) {
118 $mode = (int)$mode;
119 $SESSION->userindexmode = $mode;
120 } else if (isset($SESSION->userindexmode)) {
121 $mode = (int)$SESSION->userindexmode;
122 } else {
123 $mode = MODE_BRIEF;
126 /// Check to see if groups are being used in this course
127 /// and if so, set $currentgroup to reflect the current group
129 $groupmode = groups_get_course_groupmode($course); // Groups are being used
130 $currentgroup = groups_get_course_group($course, true);
132 if (!$currentgroup) { // To make some other functions work better later
133 $currentgroup = NULL;
136 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context));
138 if ($course->id===SITEID) {
139 $PAGE->navbar->ignore_active();
142 $PAGE->navbar->add(get_string('participants'));
143 $PAGE->set_title("$course->shortname: ".get_string('participants'));
144 $PAGE->set_heading($course->fullname);
145 $PAGE->set_pagetype('course-view-' . $course->format);
146 $PAGE->add_body_class('path-user'); // So we can style it independently
147 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
149 echo $OUTPUT->header();
151 echo '<div class="userlist">';
153 if ($isseparategroups and (!$currentgroup) ) {
154 // The user is not in the group so show message and exit
155 echo $OUTPUT->heading(get_string("notingroup"));
156 echo $OUTPUT->footer();
157 exit;
161 // Should use this variable so that we don't break stuff every time a variable is added or changed.
162 $baseurl = new moodle_url('/user/index.php', array(
163 'contextid' => $context->id,
164 'roleid' => $roleid,
165 'id' => $course->id,
166 'perpage' => $perpage,
167 'accesssince' => $accesssince,
168 'search' => s($search)));
170 /// setting up tags
171 if ($course->id == SITEID) {
172 $filtertype = 'site';
173 } else if ($course->id && !$currentgroup) {
174 $filtertype = 'course';
175 $filterselect = $course->id;
176 } else {
177 $filtertype = 'group';
178 $filterselect = $currentgroup;
183 /// Get the hidden field list
184 if (has_capability('moodle/course:viewhiddenuserfields', $context)) {
185 $hiddenfields = array(); // teachers and admins are allowed to see everything
186 } else {
187 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
190 if (isset($hiddenfields['lastaccess'])) {
191 // do not allow access since filtering
192 $accesssince = 0;
195 /// Print settings and things in a table across the top
196 $controlstable = new html_table();
197 $controlstable->attributes['class'] = 'controls';
198 $controlstable->cellspacing = 0;
199 $controlstable->data[] = new html_table_row();
201 /// Print my course menus
202 if ($mycourses = enrol_get_my_courses()) {
203 $courselist = array();
204 $popupurl = new moodle_url('/user/index.php?roleid='.$roleid.'&sifirst=&silast=');
205 foreach ($mycourses as $mycourse) {
206 $coursecontext = get_context_instance(CONTEXT_COURSE, $mycourse->id);
207 $courselist[$mycourse->id] = format_string($mycourse->shortname, true, array('context' => $coursecontext));
209 if (has_capability('moodle/site:viewparticipants', $systemcontext)) {
210 unset($courselist[SITEID]);
211 $courselist = array(SITEID => format_string($SITE->shortname, true, array('context' => $systemcontext))) + $courselist;
213 $select = new single_select($popupurl, 'id', $courselist, $course->id, array(''=>'choosedots'), 'courseform');
214 $select->set_label(get_string('mycourses'));
215 $controlstable->data[0]->cells[] = $OUTPUT->render($select);
218 $controlstable->data[0]->cells[] = groups_print_course_menu($course, $baseurl->out(), true);
220 if (!isset($hiddenfields['lastaccess'])) {
221 // get minimum lastaccess for this course and display a dropbox to filter by lastaccess going back this far.
222 // we need to make it diferently for normal courses and site course
223 if (!$isfrontpage) {
224 $minlastaccess = $DB->get_field_sql('SELECT min(timeaccess)
225 FROM {user_lastaccess}
226 WHERE courseid = ?
227 AND timeaccess != 0', array($course->id));
228 $lastaccess0exists = $DB->record_exists('user_lastaccess', array('courseid'=>$course->id, 'timeaccess'=>0));
229 } else {
230 $minlastaccess = $DB->get_field_sql('SELECT min(lastaccess)
231 FROM {user}
232 WHERE lastaccess != 0');
233 $lastaccess0exists = $DB->record_exists('user', array('lastaccess'=>0));
236 $now = usergetmidnight(time());
237 $timeaccess = array();
238 $baseurl->remove_params('accesssince');
240 // makes sense for this to go first.
241 $timeoptions[0] = get_string('selectperiod');
243 // days
244 for ($i = 1; $i < 7; $i++) {
245 if (strtotime('-'.$i.' days',$now) >= $minlastaccess) {
246 $timeoptions[strtotime('-'.$i.' days',$now)] = get_string('numdays','moodle',$i);
249 // weeks
250 for ($i = 1; $i < 10; $i++) {
251 if (strtotime('-'.$i.' weeks',$now) >= $minlastaccess) {
252 $timeoptions[strtotime('-'.$i.' weeks',$now)] = get_string('numweeks','moodle',$i);
255 // months
256 for ($i = 2; $i < 12; $i++) {
257 if (strtotime('-'.$i.' months',$now) >= $minlastaccess) {
258 $timeoptions[strtotime('-'.$i.' months',$now)] = get_string('nummonths','moodle',$i);
261 // try a year
262 if (strtotime('-1 year',$now) >= $minlastaccess) {
263 $timeoptions[strtotime('-1 year',$now)] = get_string('lastyear');
266 if (!empty($lastaccess0exists)) {
267 $timeoptions[-1] = get_string('never');
270 if (count($timeoptions) > 1) {
271 $select = new single_select($baseurl, 'accesssince', $timeoptions, $accesssince, null, 'timeoptions');
272 $select->set_label(get_string('usersnoaccesssince'));
273 $controlstable->data[0]->cells[] = $OUTPUT->render($select);
277 $formatmenu = array( '0' => get_string('brief'),
278 '1' => get_string('userdetails'));
279 $select = new single_select($baseurl, 'mode', $formatmenu, $mode, null, 'formatmenu');
280 $select->set_label(get_string('userlist'));
281 $userlistcell = new html_table_cell();
282 $userlistcell->attributes['class'] = 'right';
283 $userlistcell->text = $OUTPUT->render($select);
284 $controlstable->data[0]->cells[] = $userlistcell;
286 echo html_writer::table($controlstable);
288 if ($currentgroup and (!$isseparategroups or has_capability('moodle/site:accessallgroups', $context))) { /// Display info about the group
289 if ($group = groups_get_group($currentgroup)) {
290 if (!empty($group->description) or (!empty($group->picture) and empty($group->hidepicture))) {
291 $groupinfotable = new html_table();
292 $groupinfotable->attributes['class'] = 'groupinfobox';
293 $picturecell = new html_table_cell();
294 $picturecell->attributes['class'] = 'left side picture';
295 $picturecell->text = print_group_picture($group, $course->id, true, true, false);
297 $contentcell = new html_table_cell();
298 $contentcell->attributes['class'] = 'content';
300 $contentheading = $group->name;
301 if (has_capability('moodle/course:managegroups', $context)) {
302 $aurl = new moodle_url('/group/group.php', array('id' => $group->id, 'courseid' => $group->courseid));
303 $contentheading .= '&nbsp;' . $OUTPUT->action_icon($aurl, new pix_icon('t/edit', get_string('editgroupprofile')));
306 $group->description = file_rewrite_pluginfile_urls($group->description, 'pluginfile.php', $context->id, 'group', 'description', $group->id);
307 if (!isset($group->descriptionformat)) {
308 $group->descriptionformat = FORMAT_MOODLE;
310 $options = array('overflowdiv'=>true);
311 $contentcell->text = $OUTPUT->heading($contentheading, 3) . format_text($group->description, $group->descriptionformat, $options);
312 $groupinfotable->data[] = new html_table_row(array($picturecell, $contentcell));
313 echo html_writer::table($groupinfotable);
318 /// Define a table showing a list of users in the current role selection
320 $tablecolumns = array('userpic', 'fullname');
321 $tableheaders = array(get_string('userpic'), get_string('fullnameuser'));
322 if ($mode === MODE_BRIEF && !isset($hiddenfields['city'])) {
323 $tablecolumns[] = 'city';
324 $tableheaders[] = get_string('city');
326 if ($mode === MODE_BRIEF && !isset($hiddenfields['country'])) {
327 $tablecolumns[] = 'country';
328 $tableheaders[] = get_string('country');
330 if (!isset($hiddenfields['lastaccess'])) {
331 $tablecolumns[] = 'lastaccess';
332 $tableheaders[] = get_string('lastaccess');
335 if ($bulkoperations) {
336 $tablecolumns[] = 'select';
337 $tableheaders[] = get_string('select');
340 $table = new flexible_table('user-index-participants-'.$course->id);
342 $table->define_columns($tablecolumns);
343 $table->define_headers($tableheaders);
344 $table->define_baseurl($baseurl->out());
346 if (!isset($hiddenfields['lastaccess'])) {
347 $table->sortable(true, 'lastaccess', SORT_DESC);
350 $table->no_sorting('roles');
351 $table->no_sorting('groups');
352 $table->no_sorting('groupings');
353 $table->no_sorting('select');
355 $table->set_attribute('cellspacing', '0');
356 $table->set_attribute('id', 'participants');
357 $table->set_attribute('class', 'generaltable generalbox');
359 $table->set_control_variables(array(
360 TABLE_VAR_SORT => 'ssort',
361 TABLE_VAR_HIDE => 'shide',
362 TABLE_VAR_SHOW => 'sshow',
363 TABLE_VAR_IFIRST => 'sifirst',
364 TABLE_VAR_ILAST => 'silast',
365 TABLE_VAR_PAGE => 'spage'
367 $table->setup();
369 // we are looking for all users with this role assigned in this context or higher
370 $contextlist = get_related_contexts_string($context);
372 list($esql, $params) = get_enrolled_sql($context, NULL, $currentgroup, true);
373 $joins = array("FROM {user} u");
374 $wheres = array();
376 if ($isfrontpage) {
377 $select = "SELECT u.id, u.username, u.firstname, u.lastname,
378 u.email, u.city, u.country, u.picture,
379 u.lang, u.timezone, u.maildisplay, u.imagealt,
380 u.lastaccess";
381 $joins[] = "JOIN ($esql) e ON e.id = u.id"; // everybody on the frontpage usually
382 if ($accesssince) {
383 $wheres[] = get_user_lastaccess_sql($accesssince);
386 } else {
387 $select = "SELECT u.id, u.username, u.firstname, u.lastname,
388 u.email, u.city, u.country, u.picture,
389 u.lang, u.timezone, u.maildisplay, u.imagealt,
390 COALESCE(ul.timeaccess, 0) AS lastaccess";
391 $joins[] = "JOIN ($esql) e ON e.id = u.id"; // course enrolled users only
392 $joins[] = "LEFT JOIN {user_lastaccess} ul ON (ul.userid = u.id AND ul.courseid = :courseid)"; // not everybody accessed course yet
393 $params['courseid'] = $course->id;
394 if ($accesssince) {
395 $wheres[] = get_course_lastaccess_sql($accesssince);
399 // performance hacks - we preload user contexts together with accounts
400 list($ccselect, $ccjoin) = context_instance_preload_sql('u.id', CONTEXT_USER, 'ctx');
401 $select .= $ccselect;
402 $joins[] = $ccjoin;
405 // limit list to users with some role only
406 if ($roleid) {
407 $wheres[] = "u.id IN (SELECT userid FROM {role_assignments} WHERE roleid = :roleid AND contextid $contextlist)";
408 $params['roleid'] = $roleid;
411 $from = implode("\n", $joins);
412 if ($wheres) {
413 $where = "WHERE " . implode(" AND ", $wheres);
414 } else {
415 $where = "";
418 $totalcount = $DB->count_records_sql("SELECT COUNT(u.id) $from $where", $params);
420 if (!empty($search)) {
421 $fullname = $DB->sql_fullname('u.firstname','u.lastname');
422 $wheres[] = "(". $DB->sql_like($fullname, ':search1', false, false) .
423 " OR ". $DB->sql_like('email', ':search2', false, false) .
424 " OR ". $DB->sql_like('idnumber', ':search3', false, false) .") ";
425 $params['search1'] = "%$search%";
426 $params['search2'] = "%$search%";
427 $params['search3'] = "%$search%";
430 list($twhere, $tparams) = $table->get_sql_where();
431 if ($twhere) {
432 $wheres[] = $twhere;
433 $params = array_merge($params, $tparams);
436 $from = implode("\n", $joins);
437 if ($wheres) {
438 $where = "WHERE " . implode(" AND ", $wheres);
439 } else {
440 $where = "";
443 if ($table->get_sql_sort()) {
444 $sort = ' ORDER BY '.$table->get_sql_sort();
445 } else {
446 $sort = '';
449 $matchcount = $DB->count_records_sql("SELECT COUNT(u.id) $from $where", $params);
451 $table->initialbars(true);
452 $table->pagesize($perpage, $matchcount);
454 // list of users at the current visible page - paging makes it relatively short
455 $userlist = $DB->get_recordset_sql("$select $from $where $sort", $params, $table->get_page_start(), $table->get_page_size());
457 /// If there are multiple Roles in the course, then show a drop down menu for switching
458 if (count($rolenames) > 1) {
459 echo '<div class="rolesform">';
460 echo '<label for="rolesform_jump">'.get_string('currentrole', 'role').'&nbsp;</label>';
461 echo $OUTPUT->single_select($rolenamesurl, 'roleid', $rolenames, $roleid, null, 'rolesform');
462 echo '</div>';
464 } else if (count($rolenames) == 1) {
465 // when all users with the same role - print its name
466 echo '<div class="rolesform">';
467 echo get_string('role').get_string('labelsep', 'langconfig');
468 $rolename = reset($rolenames);
469 echo $rolename;
470 echo '</div>';
473 if ($roleid > 0) {
474 $a->number = $totalcount;
475 $a->role = $rolenames[$roleid];
476 $heading = format_string(get_string('xuserswiththerole', 'role', $a));
478 if ($currentgroup and $group) {
479 $a->group = $group->name;
480 $heading .= ' ' . format_string(get_string('ingroup', 'role', $a));
483 if ($accesssince) {
484 $a->timeperiod = $timeoptions[$accesssince];
485 $heading .= ' ' . format_string(get_string('inactiveformorethan', 'role', $a));
488 $heading .= ": $a->number";
489 if (user_can_assign($context, $roleid)) {
490 $heading .= ' <a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?roleid='.$roleid.'&amp;contextid='.$context->id.'">';
491 $heading .= '<img src="'.$OUTPUT->pix_url('i/edit') . '" class="icon" alt="" /></a>';
493 echo $OUTPUT->heading($heading, 3);
494 } else {
495 if ($course->id != SITEID && has_capability('moodle/course:enrolreview', $context)) {
496 $editlink = $OUTPUT->action_icon(new moodle_url('/enrol/users.php', array('id' => $course->id)),
497 new pix_icon('i/edit', get_string('edit')));
498 } else {
499 $editlink = '';
501 if ($course->id == SITEID and $roleid < 0) {
502 $strallparticipants = get_string('allsiteusers', 'role');
503 } else {
504 $strallparticipants = get_string('allparticipants');
506 if ($matchcount < $totalcount) {
507 echo $OUTPUT->heading($strallparticipants.get_string('labelsep', 'langconfig').$matchcount.'/'.$totalcount . $editlink, 3);
508 } else {
509 echo $OUTPUT->heading($strallparticipants.get_string('labelsep', 'langconfig').$matchcount . $editlink, 3);
514 if ($bulkoperations) {
515 echo '<form action="action_redir.php" method="post" id="participantsform">';
516 echo '<div>';
517 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
518 echo '<input type="hidden" name="returnto" value="'.s(me()).'" />';
521 if ($mode === MODE_USERDETAILS) { // Print simple listing
522 if ($totalcount < 1) {
523 echo $OUTPUT->heading(get_string('nothingtodisplay'));
524 } else {
525 if ($totalcount > $perpage) {
527 $firstinitial = $table->get_initial_first();
528 $lastinitial = $table->get_initial_last();
529 $strall = get_string('all');
530 $alpha = explode(',', get_string('alphabet', 'langconfig'));
532 // Bar of first initials
534 echo '<div class="initialbar firstinitial">'.get_string('firstname').' : ';
535 if(!empty($firstinitial)) {
536 echo '<a href="'.$baseurl->out().'&amp;sifirst=">'.$strall.'</a>';
537 } else {
538 echo '<strong>'.$strall.'</strong>';
540 foreach ($alpha as $letter) {
541 if ($letter == $firstinitial) {
542 echo ' <strong>'.$letter.'</strong>';
543 } else {
544 echo ' <a href="'.$baseurl->out().'&amp;sifirst='.$letter.'">'.$letter.'</a>';
547 echo '</div>';
549 // Bar of last initials
551 echo '<div class="initialbar lastinitial">'.get_string('lastname').' : ';
552 if(!empty($lastinitial)) {
553 echo '<a href="'.$baseurl->out().'&amp;silast=">'.$strall.'</a>';
554 } else {
555 echo '<strong>'.$strall.'</strong>';
557 foreach ($alpha as $letter) {
558 if ($letter == $lastinitial) {
559 echo ' <strong>'.$letter.'</strong>';
560 } else {
561 echo ' <a href="'.$baseurl->out().'&amp;silast='.$letter.'">'.$letter.'</a>';
564 echo '</div>';
566 $pagingbar = new paging_bar($matchcount, intval($table->get_page_start() / $perpage), $perpage, $baseurl);
567 $pagingbar->pagevar = 'spage';
568 echo $OUTPUT->render($pagingbar);
571 if ($matchcount > 0) {
572 $usersprinted = array();
573 foreach ($userlist as $user) {
574 if (in_array($user->id, $usersprinted)) { /// Prevent duplicates by r.hidden - MDL-13935
575 continue;
577 $usersprinted[] = $user->id; /// Add new user to the array of users printed
579 context_instance_preload($user);
581 $context = get_context_instance(CONTEXT_COURSE, $course->id);
582 $usercontext = get_context_instance(CONTEXT_USER, $user->id);
584 $countries = get_string_manager()->get_list_of_countries();
586 /// Get the hidden field list
587 if (has_capability('moodle/course:viewhiddenuserfields', $context)) {
588 $hiddenfields = array();
589 } else {
590 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
592 $table = new html_table();
593 $table->attributes['class'] = 'userinfobox';
595 $row = new html_table_row();
596 $row->cells[0] = new html_table_cell();
597 $row->cells[0]->attributes['class'] = 'left side';
599 $row->cells[0]->text = $OUTPUT->user_picture($user, array('size' => 100, 'courseid'=>$course->id));
600 $row->cells[1] = new html_table_cell();
601 $row->cells[1]->attributes['class'] = 'content';
603 $row->cells[1]->text = $OUTPUT->container(fullname($user, has_capability('moodle/site:viewfullnames', $context)), 'username');
604 $row->cells[1]->text .= $OUTPUT->container_start('info');
606 if (!empty($user->role)) {
607 $row->cells[1]->text .= get_string('role').get_string('labelsep', 'langconfig').$user->role.'<br />';
609 if ($user->maildisplay == 1 or ($user->maildisplay == 2 and ($course->id != SITEID) and !isguestuser()) or
610 has_capability('moodle/course:viewhiddenuserfields', $context)) {
611 $row->cells[1]->text .= get_string('email').get_string('labelsep', 'langconfig').html_writer::link("mailto:$user->email", $user->email) . '<br />';
613 if (($user->city or $user->country) and (!isset($hiddenfields['city']) or !isset($hiddenfields['country']))) {
614 $row->cells[1]->text .= get_string('city').get_string('labelsep', 'langconfig');
615 if ($user->city && !isset($hiddenfields['city'])) {
616 $row->cells[1]->text .= $user->city;
618 if (!empty($countries[$user->country]) && !isset($hiddenfields['country'])) {
619 if ($user->city && !isset($hiddenfields['city'])) {
620 $row->cells[1]->text .= ', ';
622 $row->cells[1]->text .= $countries[$user->country];
624 $row->cells[1]->text .= '<br />';
627 if (!isset($hiddenfields['lastaccess'])) {
628 if ($user->lastaccess) {
629 $row->cells[1]->text .= get_string('lastaccess').get_string('labelsep', 'langconfig').userdate($user->lastaccess);
630 $row->cells[1]->text .= '&nbsp; ('. format_time(time() - $user->lastaccess, $datestring) .')';
631 } else {
632 $row->cells[1]->text .= get_string('lastaccess').get_string('labelsep', 'langconfig').get_string('never');
636 $row->cells[1]->text .= $OUTPUT->container_end();
638 $row->cells[2] = new html_table_cell();
639 $row->cells[2]->attributes['class'] = 'links';
640 $row->cells[2]->text = '';
642 $links = array();
644 if ($CFG->bloglevel > 0) {
645 $links[] = html_writer::link(new moodle_url('/blog/index.php?userid='.$user->id), get_string('blogs','blog'));
648 if (!empty($CFG->enablenotes) and (has_capability('moodle/notes:manage', $context) || has_capability('moodle/notes:view', $context))) {
649 $links[] = html_writer::link(new moodle_url('/notes/index.php?course=' . $course->id. '&user='.$user->id), get_string('notes','notes'));
652 if (has_capability('moodle/site:viewreports', $context) or has_capability('moodle/user:viewuseractivitiesreport', $usercontext)) {
653 $links[] = html_writer::link(new moodle_url('/course/user.php?id='. $course->id .'&user='. $user->id), get_string('activity'));
656 if ($USER->id != $user->id && !session_is_loggedinas() && has_capability('moodle/user:loginas', $context) && !is_siteadmin($user->id)) {
657 $links[] = html_writer::link(new moodle_url('/course/loginas.php?id='. $course->id .'&user='. $user->id .'&sesskey='. sesskey()), get_string('loginas'));
660 $links[] = html_writer::link(new moodle_url('/user/view.php?id='. $user->id .'&course='. $course->id), get_string('fullprofile') . '...');
662 $row->cells[2]->text .= implode('', $links);
664 if (!empty($messageselect)) {
665 $row->cells[2]->text .= '<br /><input type="checkbox" name="user'.$user->id.'" /> ';
667 $table->data = array($row);
668 echo html_writer::table($table);
671 } else {
672 echo $OUTPUT->heading(get_string('nothingtodisplay'));
676 } else {
677 $countrysort = (strpos($sort, 'country') !== false);
678 $timeformat = get_string('strftimedate');
681 if ($userlist) {
683 $usersprinted = array();
684 foreach ($userlist as $user) {
685 if (in_array($user->id, $usersprinted)) { /// Prevent duplicates by r.hidden - MDL-13935
686 continue;
688 $usersprinted[] = $user->id; /// Add new user to the array of users printed
690 context_instance_preload($user);
692 if ($user->lastaccess) {
693 $lastaccess = format_time(time() - $user->lastaccess, $datestring);
694 } else {
695 $lastaccess = $strnever;
698 if (empty($user->country)) {
699 $country = '';
701 } else {
702 if($countrysort) {
703 $country = '('.$user->country.') '.$countries[$user->country];
705 else {
706 $country = $countries[$user->country];
710 $usercontext = get_context_instance(CONTEXT_USER, $user->id);
712 if ($piclink = ($USER->id == $user->id || has_capability('moodle/user:viewdetails', $context) || has_capability('moodle/user:viewdetails', $usercontext))) {
713 $profilelink = '<strong><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></strong>';
714 } else {
715 $profilelink = '<strong>'.fullname($user).'</strong>';
718 $data = array ($OUTPUT->user_picture($user, array('size' => 35, 'courseid'=>$course->id)), $profilelink);
720 if ($mode === MODE_BRIEF && !isset($hiddenfields['city'])) {
721 $data[] = $user->city;
723 if ($mode === MODE_BRIEF && !isset($hiddenfields['country'])) {
724 $data[] = $country;
726 if (!isset($hiddenfields['lastaccess'])) {
727 $data[] = $lastaccess;
730 if (isset($userlist_extra) && isset($userlist_extra[$user->id])) {
731 $ras = $userlist_extra[$user->id]['ra'];
732 $rastring = '';
733 foreach ($ras AS $key=>$ra) {
734 $rolename = $allrolenames[$ra['roleid']] ;
735 if ($ra['ctxlevel'] == CONTEXT_COURSECAT) {
736 $rastring .= $rolename. ' @ ' . '<a href="'.$CFG->wwwroot.'/course/category.php?id='.$ra['ctxinstanceid'].'">'.s($ra['ccname']).'</a>';
737 } elseif ($ra['ctxlevel'] == CONTEXT_SYSTEM) {
738 $rastring .= $rolename. ' - ' . get_string('globalrole','role');
739 } else {
740 $rastring .= $rolename;
743 $data[] = $rastring;
744 if ($groupmode != 0) {
745 // htmlescape with s() and implode the array
746 $data[] = implode(', ', array_map('s',$userlist_extra[$user->id]['group']));
747 $data[] = implode(', ', array_map('s', $userlist_extra[$user->id]['gping']));
751 if ($bulkoperations) {
752 $data[] = '<input type="checkbox" class="usercheckbox" name="user'.$user->id.'" />';
754 $table->add_data($data);
759 $table->print_html();
763 if ($bulkoperations) {
764 echo '<br /><div class="buttons">';
765 echo '<input type="button" id="checkall" value="'.get_string('selectall').'" /> ';
766 echo '<input type="button" id="checknone" value="'.get_string('deselectall').'" /> ';
767 $displaylist = array();
768 $displaylist['messageselect.php'] = get_string('messageselectadd');
769 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
770 $displaylist['addnote.php'] = get_string('addnewnote', 'notes');
771 $displaylist['groupaddnote.php'] = get_string('groupaddnewnote', 'notes');
774 echo $OUTPUT->help_icon('withselectedusers');
775 echo html_writer::tag('label', get_string("withselectedusers"), array('for'=>'formactionid'));
776 echo html_writer::select($displaylist, 'formaction', '', array(''=>'choosedots'), array('id'=>'formactionid'));
778 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
779 echo '<noscript style="display:inline">';
780 echo '<div><input type="submit" value="'.get_string('ok').'" /></div>';
781 echo '</noscript>';
782 echo '</div></div>';
783 echo '</form>';
785 $module = array('name'=>'core_user', 'fullpath'=>'/user/module.js');
786 $PAGE->requires->js_init_call('M.core_user.init_participation', null, false, $module);
789 if (has_capability('moodle/site:viewparticipants', $context) && $totalcount > ($perpage*3)) {
790 echo '<form action="index.php" class="searchform"><div><input type="hidden" name="id" value="'.$course->id.'" />'.get_string('search').':&nbsp;'."\n";
791 echo '<input type="text" name="search" value="'.s($search).'" />&nbsp;<input type="submit" value="'.get_string('search').'" /></div></form>'."\n";
794 $perpageurl = clone($baseurl);
795 $perpageurl->remove_params('perpage');
796 if ($perpage == SHOW_ALL_PAGE_SIZE) {
797 $perpageurl->param('perpage', DEFAULT_PAGE_SIZE);
798 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showperpage', '', DEFAULT_PAGE_SIZE)), array(), 'showall');
800 } else if ($matchcount > 0 && $perpage < $matchcount) {
801 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
802 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showall', '', $matchcount)), array(), 'showall');
805 echo '</div>'; // userlist
807 echo $OUTPUT->footer();
809 if ($userlist) {
810 $userlist->close();
814 function get_course_lastaccess_sql($accesssince='') {
815 if (empty($accesssince)) {
816 return '';
818 if ($accesssince == -1) { // never
819 return 'ul.timeaccess = 0';
820 } else {
821 return 'ul.timeaccess != 0 AND ul.timeaccess < '.$accesssince;
825 function get_user_lastaccess_sql($accesssince='') {
826 if (empty($accesssince)) {
827 return '';
829 if ($accesssince == -1) { // never
830 return 'u.lastaccess = 0';
831 } else {
832 return 'u.lastaccess != 0 AND u.lastaccess < '.$accesssince;