Merge branch 'MDL-33487' of git://github.com/danpoltawski/moodle
[moodle.git] / user / index.php
blob8ea84f93f4fc40288caa988569746b5dd33900f5
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 = 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 = new stdClass();
107 $datestring->year = get_string('year');
108 $datestring->years = get_string('years');
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');
118 if ($mode !== NULL) {
119 $mode = (int)$mode;
120 $SESSION->userindexmode = $mode;
121 } else if (isset($SESSION->userindexmode)) {
122 $mode = (int)$SESSION->userindexmode;
123 } else {
124 $mode = MODE_BRIEF;
127 /// Check to see if groups are being used in this course
128 /// and if so, set $currentgroup to reflect the current group
130 $groupmode = groups_get_course_groupmode($course); // Groups are being used
131 $currentgroup = groups_get_course_group($course, true);
133 if (!$currentgroup) { // To make some other functions work better later
134 $currentgroup = NULL;
137 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context));
139 $PAGE->set_title("$course->shortname: ".get_string('participants'));
140 $PAGE->set_heading($course->fullname);
141 $PAGE->set_pagetype('course-view-' . $course->format);
142 $PAGE->add_body_class('path-user'); // So we can style it independently
143 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
145 echo $OUTPUT->header();
147 echo '<div class="userlist">';
149 if ($isseparategroups and (!$currentgroup) ) {
150 // The user is not in the group so show message and exit
151 echo $OUTPUT->heading(get_string("notingroup"));
152 echo $OUTPUT->footer();
153 exit;
157 // Should use this variable so that we don't break stuff every time a variable is added or changed.
158 $baseurl = new moodle_url('/user/index.php', array(
159 'contextid' => $context->id,
160 'roleid' => $roleid,
161 'id' => $course->id,
162 'perpage' => $perpage,
163 'accesssince' => $accesssince,
164 'search' => s($search)));
166 /// setting up tags
167 if ($course->id == SITEID) {
168 $filtertype = 'site';
169 } else if ($course->id && !$currentgroup) {
170 $filtertype = 'course';
171 $filterselect = $course->id;
172 } else {
173 $filtertype = 'group';
174 $filterselect = $currentgroup;
179 /// Get the hidden field list
180 if (has_capability('moodle/course:viewhiddenuserfields', $context)) {
181 $hiddenfields = array(); // teachers and admins are allowed to see everything
182 } else {
183 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
186 if (isset($hiddenfields['lastaccess'])) {
187 // do not allow access since filtering
188 $accesssince = 0;
191 /// Print settings and things in a table across the top
192 $controlstable = new html_table();
193 $controlstable->attributes['class'] = 'controls';
194 $controlstable->cellspacing = 0;
195 $controlstable->data[] = new html_table_row();
197 /// Print my course menus
198 if ($mycourses = enrol_get_my_courses()) {
199 $courselist = array();
200 $popupurl = new moodle_url('/user/index.php?roleid='.$roleid.'&sifirst=&silast=');
201 foreach ($mycourses as $mycourse) {
202 $coursecontext = get_context_instance(CONTEXT_COURSE, $mycourse->id);
203 $courselist[$mycourse->id] = format_string($mycourse->shortname, true, array('context' => $coursecontext));
205 if (has_capability('moodle/site:viewparticipants', $systemcontext)) {
206 unset($courselist[SITEID]);
207 $courselist = array(SITEID => format_string($SITE->shortname, true, array('context' => $systemcontext))) + $courselist;
209 $select = new single_select($popupurl, 'id', $courselist, $course->id, array(''=>'choosedots'), 'courseform');
210 $select->set_label(get_string('mycourses'));
211 $controlstable->data[0]->cells[] = $OUTPUT->render($select);
214 $controlstable->data[0]->cells[] = groups_print_course_menu($course, $baseurl->out(), true);
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
219 if (!$isfrontpage) {
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));
232 $now = usergetmidnight(time());
233 $timeaccess = array();
234 $baseurl->remove_params('accesssince');
236 // makes sense for this to go first.
237 $timeoptions[0] = get_string('selectperiod');
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);
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);
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);
257 // try a year
258 if (strtotime('-1 year',$now) >= $minlastaccess) {
259 $timeoptions[strtotime('-1 year',$now)] = get_string('lastyear');
262 if (!empty($lastaccess0exists)) {
263 $timeoptions[-1] = get_string('never');
266 if (count($timeoptions) > 1) {
267 $select = new single_select($baseurl, 'accesssince', $timeoptions, $accesssince, null, 'timeoptions');
268 $select->set_label(get_string('usersnoaccesssince'));
269 $controlstable->data[0]->cells[] = $OUTPUT->render($select);
273 $formatmenu = array( '0' => get_string('brief'),
274 '1' => get_string('userdetails'));
275 $select = new single_select($baseurl, 'mode', $formatmenu, $mode, null, 'formatmenu');
276 $select->set_label(get_string('userlist'));
277 $userlistcell = new html_table_cell();
278 $userlistcell->attributes['class'] = 'right';
279 $userlistcell->text = $OUTPUT->render($select);
280 $controlstable->data[0]->cells[] = $userlistcell;
282 echo html_writer::table($controlstable);
284 if ($currentgroup and (!$isseparategroups or has_capability('moodle/site:accessallgroups', $context))) { /// Display info about the group
285 if ($group = groups_get_group($currentgroup)) {
286 if (!empty($group->description) or (!empty($group->picture) and empty($group->hidepicture))) {
287 $groupinfotable = new html_table();
288 $groupinfotable->attributes['class'] = 'groupinfobox';
289 $picturecell = new html_table_cell();
290 $picturecell->attributes['class'] = 'left side picture';
291 $picturecell->text = print_group_picture($group, $course->id, true, true, false);
293 $contentcell = new html_table_cell();
294 $contentcell->attributes['class'] = 'content';
296 $contentheading = $group->name;
297 if (has_capability('moodle/course:managegroups', $context)) {
298 $aurl = new moodle_url('/group/group.php', array('id' => $group->id, 'courseid' => $group->courseid));
299 $contentheading .= '&nbsp;' . $OUTPUT->action_icon($aurl, new pix_icon('t/edit', get_string('editgroupprofile')));
302 $group->description = file_rewrite_pluginfile_urls($group->description, 'pluginfile.php', $context->id, 'group', 'description', $group->id);
303 if (!isset($group->descriptionformat)) {
304 $group->descriptionformat = FORMAT_MOODLE;
306 $options = array('overflowdiv'=>true);
307 $contentcell->text = $OUTPUT->heading($contentheading, 3) . format_text($group->description, $group->descriptionformat, $options);
308 $groupinfotable->data[] = new html_table_row(array($picturecell, $contentcell));
309 echo html_writer::table($groupinfotable);
314 /// Define a table showing a list of users in the current role selection
316 $tablecolumns = array('userpic', 'fullname');
317 $extrafields = get_extra_user_fields($context);
318 $tableheaders = array(get_string('userpic'), 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) {
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 $heading .= ' <a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?roleid='.$roleid.'&amp;contextid='.$context->id.'">';
501 $heading .= '<img src="'.$OUTPUT->pix_url('i/edit') . '" class="icon" alt="" /></a>';
503 echo $OUTPUT->heading($heading, 3);
504 } else {
505 if ($course->id != SITEID && has_capability('moodle/course:enrolreview', $context)) {
506 $editlink = $OUTPUT->action_icon(new moodle_url('/enrol/users.php', array('id' => $course->id)),
507 new pix_icon('i/edit', get_string('edit')));
508 } else {
509 $editlink = '';
511 if ($course->id == SITEID and $roleid < 0) {
512 $strallparticipants = get_string('allsiteusers', 'role');
513 } else {
514 $strallparticipants = get_string('allparticipants');
516 if ($matchcount < $totalcount) {
517 echo $OUTPUT->heading($strallparticipants.get_string('labelsep', 'langconfig').$matchcount.'/'.$totalcount . $editlink, 3);
518 } else {
519 echo $OUTPUT->heading($strallparticipants.get_string('labelsep', 'langconfig').$matchcount . $editlink, 3);
524 if ($bulkoperations) {
525 echo '<form action="action_redir.php" method="post" id="participantsform">';
526 echo '<div>';
527 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
528 echo '<input type="hidden" name="returnto" value="'.s($PAGE->url->out(false)).'" />';
531 if ($mode === MODE_USERDETAILS) { // Print simple listing
532 if ($totalcount < 1) {
533 echo $OUTPUT->heading(get_string('nothingtodisplay'));
534 } else {
535 if ($totalcount > $perpage) {
537 $firstinitial = $table->get_initial_first();
538 $lastinitial = $table->get_initial_last();
539 $strall = get_string('all');
540 $alpha = explode(',', get_string('alphabet', 'langconfig'));
542 // Bar of first initials
544 echo '<div class="initialbar firstinitial">'.get_string('firstname').' : ';
545 if(!empty($firstinitial)) {
546 echo '<a href="'.$baseurl->out().'&amp;sifirst=">'.$strall.'</a>';
547 } else {
548 echo '<strong>'.$strall.'</strong>';
550 foreach ($alpha as $letter) {
551 if ($letter == $firstinitial) {
552 echo ' <strong>'.$letter.'</strong>';
553 } else {
554 echo ' <a href="'.$baseurl->out().'&amp;sifirst='.$letter.'">'.$letter.'</a>';
557 echo '</div>';
559 // Bar of last initials
561 echo '<div class="initialbar lastinitial">'.get_string('lastname').' : ';
562 if(!empty($lastinitial)) {
563 echo '<a href="'.$baseurl->out().'&amp;silast=">'.$strall.'</a>';
564 } else {
565 echo '<strong>'.$strall.'</strong>';
567 foreach ($alpha as $letter) {
568 if ($letter == $lastinitial) {
569 echo ' <strong>'.$letter.'</strong>';
570 } else {
571 echo ' <a href="'.$baseurl->out().'&amp;silast='.$letter.'">'.$letter.'</a>';
574 echo '</div>';
576 $pagingbar = new paging_bar($matchcount, intval($table->get_page_start() / $perpage), $perpage, $baseurl);
577 $pagingbar->pagevar = 'spage';
578 echo $OUTPUT->render($pagingbar);
581 if ($matchcount > 0) {
582 $usersprinted = array();
583 foreach ($userlist as $user) {
584 if (in_array($user->id, $usersprinted)) { /// Prevent duplicates by r.hidden - MDL-13935
585 continue;
587 $usersprinted[] = $user->id; /// Add new user to the array of users printed
589 context_instance_preload($user);
591 $context = get_context_instance(CONTEXT_COURSE, $course->id);
592 $usercontext = get_context_instance(CONTEXT_USER, $user->id);
594 $countries = get_string_manager()->get_list_of_countries();
596 /// Get the hidden field list
597 if (has_capability('moodle/course:viewhiddenuserfields', $context)) {
598 $hiddenfields = array();
599 } else {
600 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
602 $table = new html_table();
603 $table->attributes['class'] = 'userinfobox';
605 $row = new html_table_row();
606 $row->cells[0] = new html_table_cell();
607 $row->cells[0]->attributes['class'] = 'left side';
609 $row->cells[0]->text = $OUTPUT->user_picture($user, array('size' => 100, 'courseid'=>$course->id));
610 $row->cells[1] = new html_table_cell();
611 $row->cells[1]->attributes['class'] = 'content';
613 $row->cells[1]->text = $OUTPUT->container(fullname($user, has_capability('moodle/site:viewfullnames', $context)), 'username');
614 $row->cells[1]->text .= $OUTPUT->container_start('info');
616 if (!empty($user->role)) {
617 $row->cells[1]->text .= get_string('role').get_string('labelsep', 'langconfig').$user->role.'<br />';
619 if ($user->maildisplay == 1 or ($user->maildisplay == 2 and ($course->id != SITEID) and !isguestuser()) or
620 has_capability('moodle/course:viewhiddenuserfields', $context) or
621 in_array('email', $extrafields)) {
622 $row->cells[1]->text .= get_string('email').get_string('labelsep', 'langconfig').html_writer::link("mailto:$user->email", $user->email) . '<br />';
624 foreach ($extrafields as $field) {
625 if ($field === 'email') {
626 // Skip email because it was displayed with different
627 // logic above (because this page is intended for
628 // students too)
629 continue;
631 $row->cells[1]->text .= get_user_field_name($field) .
632 get_string('labelsep', 'langconfig') . s($user->{$field}) . '<br />';
634 if (($user->city or $user->country) and (!isset($hiddenfields['city']) or !isset($hiddenfields['country']))) {
635 $row->cells[1]->text .= get_string('city').get_string('labelsep', 'langconfig');
636 if ($user->city && !isset($hiddenfields['city'])) {
637 $row->cells[1]->text .= $user->city;
639 if (!empty($countries[$user->country]) && !isset($hiddenfields['country'])) {
640 if ($user->city && !isset($hiddenfields['city'])) {
641 $row->cells[1]->text .= ', ';
643 $row->cells[1]->text .= $countries[$user->country];
645 $row->cells[1]->text .= '<br />';
648 if (!isset($hiddenfields['lastaccess'])) {
649 if ($user->lastaccess) {
650 $row->cells[1]->text .= get_string('lastaccess').get_string('labelsep', 'langconfig').userdate($user->lastaccess);
651 $row->cells[1]->text .= '&nbsp; ('. format_time(time() - $user->lastaccess, $datestring) .')';
652 } else {
653 $row->cells[1]->text .= get_string('lastaccess').get_string('labelsep', 'langconfig').get_string('never');
657 $row->cells[1]->text .= $OUTPUT->container_end();
659 $row->cells[2] = new html_table_cell();
660 $row->cells[2]->attributes['class'] = 'links';
661 $row->cells[2]->text = '';
663 $links = array();
665 if ($CFG->bloglevel > 0) {
666 $links[] = html_writer::link(new moodle_url('/blog/index.php?userid='.$user->id), get_string('blogs','blog'));
669 if (!empty($CFG->enablenotes) and (has_capability('moodle/notes:manage', $context) || has_capability('moodle/notes:view', $context))) {
670 $links[] = html_writer::link(new moodle_url('/notes/index.php?course=' . $course->id. '&user='.$user->id), get_string('notes','notes'));
673 if (has_capability('moodle/site:viewreports', $context) or has_capability('moodle/user:viewuseractivitiesreport', $usercontext)) {
674 $links[] = html_writer::link(new moodle_url('/course/user.php?id='. $course->id .'&user='. $user->id), get_string('activity'));
677 if ($USER->id != $user->id && !session_is_loggedinas() && has_capability('moodle/user:loginas', $context) && !is_siteadmin($user->id)) {
678 $links[] = html_writer::link(new moodle_url('/course/loginas.php?id='. $course->id .'&user='. $user->id .'&sesskey='. sesskey()), get_string('loginas'));
681 $links[] = html_writer::link(new moodle_url('/user/view.php?id='. $user->id .'&course='. $course->id), get_string('fullprofile') . '...');
683 $row->cells[2]->text .= implode('', $links);
685 if ($bulkoperations) {
686 $row->cells[2]->text .= '<br /><input type="checkbox" class="usercheckbox" name="user'.$user->id.'" /> ';
688 $table->data = array($row);
689 echo html_writer::table($table);
692 } else {
693 echo $OUTPUT->heading(get_string('nothingtodisplay'));
697 } else {
698 $countrysort = (strpos($sort, 'country') !== false);
699 $timeformat = get_string('strftimedate');
702 if ($userlist) {
704 $usersprinted = array();
705 foreach ($userlist as $user) {
706 if (in_array($user->id, $usersprinted)) { /// Prevent duplicates by r.hidden - MDL-13935
707 continue;
709 $usersprinted[] = $user->id; /// Add new user to the array of users printed
711 context_instance_preload($user);
713 if ($user->lastaccess) {
714 $lastaccess = format_time(time() - $user->lastaccess, $datestring);
715 } else {
716 $lastaccess = $strnever;
719 if (empty($user->country)) {
720 $country = '';
722 } else {
723 if($countrysort) {
724 $country = '('.$user->country.') '.$countries[$user->country];
726 else {
727 $country = $countries[$user->country];
731 $usercontext = get_context_instance(CONTEXT_USER, $user->id);
733 if ($piclink = ($USER->id == $user->id || has_capability('moodle/user:viewdetails', $context) || has_capability('moodle/user:viewdetails', $usercontext))) {
734 $profilelink = '<strong><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></strong>';
735 } else {
736 $profilelink = '<strong>'.fullname($user).'</strong>';
739 $data = array ($OUTPUT->user_picture($user, array('size' => 35, 'courseid'=>$course->id)), $profilelink);
741 if ($mode === MODE_BRIEF) {
742 foreach ($extrafields as $field) {
743 $data[] = $user->{$field};
746 if ($mode === MODE_BRIEF && !isset($hiddenfields['city'])) {
747 $data[] = $user->city;
749 if ($mode === MODE_BRIEF && !isset($hiddenfields['country'])) {
750 $data[] = $country;
752 if (!isset($hiddenfields['lastaccess'])) {
753 $data[] = $lastaccess;
756 if (isset($userlist_extra) && isset($userlist_extra[$user->id])) {
757 $ras = $userlist_extra[$user->id]['ra'];
758 $rastring = '';
759 foreach ($ras AS $key=>$ra) {
760 $rolename = $allrolenames[$ra['roleid']] ;
761 if ($ra['ctxlevel'] == CONTEXT_COURSECAT) {
762 $rastring .= $rolename. ' @ ' . '<a href="'.$CFG->wwwroot.'/course/category.php?id='.$ra['ctxinstanceid'].'">'.s($ra['ccname']).'</a>';
763 } elseif ($ra['ctxlevel'] == CONTEXT_SYSTEM) {
764 $rastring .= $rolename. ' - ' . get_string('globalrole','role');
765 } else {
766 $rastring .= $rolename;
769 $data[] = $rastring;
770 if ($groupmode != 0) {
771 // htmlescape with s() and implode the array
772 $data[] = implode(', ', array_map('s',$userlist_extra[$user->id]['group']));
773 $data[] = implode(', ', array_map('s', $userlist_extra[$user->id]['gping']));
777 if ($bulkoperations) {
778 $data[] = '<input type="checkbox" class="usercheckbox" name="user'.$user->id.'" />';
780 $table->add_data($data);
784 $table->print_html();
788 if ($bulkoperations) {
789 echo '<br /><div class="buttons">';
790 echo '<input type="button" id="checkall" value="'.get_string('selectall').'" /> ';
791 echo '<input type="button" id="checknone" value="'.get_string('deselectall').'" /> ';
792 $displaylist = array();
793 $displaylist['messageselect.php'] = get_string('messageselectadd');
794 if (!empty($CFG->enablenotes) && has_capability('moodle/notes:manage', $context) && $context->id != $frontpagectx->id) {
795 $displaylist['addnote.php'] = get_string('addnewnote', 'notes');
796 $displaylist['groupaddnote.php'] = get_string('groupaddnewnote', 'notes');
799 echo $OUTPUT->help_icon('withselectedusers');
800 echo html_writer::tag('label', get_string("withselectedusers"), array('for'=>'formactionid'));
801 echo html_writer::select($displaylist, 'formaction', '', array(''=>'choosedots'), array('id'=>'formactionid'));
803 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
804 echo '<noscript style="display:inline">';
805 echo '<div><input type="submit" value="'.get_string('ok').'" /></div>';
806 echo '</noscript>';
807 echo '</div></div>';
808 echo '</form>';
810 $module = array('name'=>'core_user', 'fullpath'=>'/user/module.js');
811 $PAGE->requires->js_init_call('M.core_user.init_participation', null, false, $module);
814 if (has_capability('moodle/site:viewparticipants', $context) && $totalcount > ($perpage*3)) {
815 echo '<form action="index.php" class="searchform"><div><input type="hidden" name="id" value="'.$course->id.'" />'.get_string('search').':&nbsp;'."\n";
816 echo '<input type="text" name="search" value="'.s($search).'" />&nbsp;<input type="submit" value="'.get_string('search').'" /></div></form>'."\n";
819 $perpageurl = clone($baseurl);
820 $perpageurl->remove_params('perpage');
821 if ($perpage == SHOW_ALL_PAGE_SIZE) {
822 $perpageurl->param('perpage', DEFAULT_PAGE_SIZE);
823 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showperpage', '', DEFAULT_PAGE_SIZE)), array(), 'showall');
825 } else if ($matchcount > 0 && $perpage < $matchcount) {
826 $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
827 echo $OUTPUT->container(html_writer::link($perpageurl, get_string('showall', '', $matchcount)), array(), 'showall');
830 echo '</div>'; // userlist
832 echo $OUTPUT->footer();
834 if ($userlist) {
835 $userlist->close();
839 function get_course_lastaccess_sql($accesssince='') {
840 if (empty($accesssince)) {
841 return '';
843 if ($accesssince == -1) { // never
844 return 'ul.timeaccess = 0';
845 } else {
846 return 'ul.timeaccess != 0 AND ul.timeaccess < '.$accesssince;
850 function get_user_lastaccess_sql($accesssince='') {
851 if (empty($accesssince)) {
852 return '';
854 if ($accesssince == -1) { // never
855 return 'u.lastaccess = 0';
856 } else {
857 return 'u.lastaccess != 0 AND u.lastaccess < '.$accesssince;