3 // Lists all the users within a given course
5 require_once('../config.php');
6 require_once($CFG->libdir
.'/tablelib.php');
8 define('USER_SMALL_CLASS', 20); // Below this is considered small
9 define('USER_LARGE_CLASS', 200); // Above this is considered large
10 define('DEFAULT_PAGE_SIZE', 20);
11 define('SHOW_ALL_PAGE_SIZE', 5000);
13 $group = optional_param('group', -1, PARAM_INT
); // Group to show
14 $page = optional_param('page', 0, PARAM_INT
); // which page to show
15 $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE
, PARAM_INT
); // how many per page
16 $mode = optional_param('mode', NULL); // '0' for less details, '1' for more
17 $accesssince = optional_param('accesssince',0,PARAM_INT
); // filter by last access. -1 = never
18 $search = optional_param('search','',PARAM_CLEAN
);
19 $roleid = optional_param('roleid', 0, PARAM_INT
); // optional roleid
21 $contextid = optional_param('contextid', 0, PARAM_INT
); // one of this or
22 $courseid = optional_param('id', 0, PARAM_INT
); // this are required
25 if (! $context = get_context_instance_by_id($contextid)) {
26 error("Context ID is incorrect");
28 if (! $course = get_record('course', 'id', $context->instanceid
)) {
29 error("Course ID is incorrect");
32 if (! $course = get_record('course', 'id', $courseid)) {
33 error("Course ID is incorrect");
35 if (! $context = get_context_instance(CONTEXT_COURSE
, $course->id
)) {
36 error("Context ID is incorrect");
43 require_login($course->id
);
45 if (!has_capability('moodle/course:viewparticipants', $context)
46 && !has_capability('moodle/site:viewparticipants', $context)) {
47 print_error('nopermissions');
52 if ($roles = get_roles_used_in_context($context, true)) {
53 // We should exclude "admin" users (those with "doanything" at site level) because
54 // Otherwise they appear in every participant list
56 $sitecontext = get_context_instance(CONTEXT_SYSTEM
);
57 $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW
, $sitecontext);
59 foreach ($roles as $role) {
60 if (isset($doanythingroles[$role->id
])) { // Avoid this role (ie admin)
61 unset($roles[$role->id
]);
64 $rolenames[$role->id
] = strip_tags(format_string($role->name
)); // Used in menus etc later on
68 // no roles to display yet?
69 if (empty($rolenames)) {
70 if (has_capability('moodle/user:assign', $context)) {
71 redirect($CFG->wwwroot
.'/'.$CFG->admin
.'/roles/assign.php?contextid='.$context->id
);
73 error ('No participants found for this course');
77 if ($course->id
== SITEID
) {
78 if (!has_capability('moodle/course:viewparticipants', get_context_instance(CONTEXT_SYSTEM
, SITEID
))) {
79 print_header("$course->shortname: ".get_string('participants'), $course->fullname
,
80 get_string('participants'), "", "", true, " ", navmenu($course));
81 notice(get_string('sitepartlist'));
85 add_to_log($course->id
, 'user', 'view all', 'index.php?id='.$course->id
, '');
87 $bulkoperations = has_capability('moodle/course:bulkmessaging', $context);
89 $countries = get_list_of_countries();
91 $strnever = get_string('never');
93 $datestring->day
= get_string('day');
94 $datestring->days
= get_string('days');
95 $datestring->hour
= get_string('hour');
96 $datestring->hours
= get_string('hours');
97 $datestring->min
= get_string('min');
98 $datestring->mins
= get_string('mins');
99 $datestring->sec
= get_string('sec');
100 $datestring->secs
= get_string('secs');
102 if ($mode !== NULL) {
103 $SESSION->userindexmode
= $fullmode = ($mode == 1);
104 } else if (isset($SESSION->userindexmode
)) {
105 $fullmode = $SESSION->userindexmode
;
110 /// Check to see if groups are being used in this forum
111 /// and if so, set $currentgroup to reflect the current group
113 $groupmode = groupmode($course); // Groups are being used
114 $currentgroup = get_and_set_current_group($course, $groupmode, $group);
116 if (!$currentgroup) { // To make some other functions work better later
117 $currentgroup = NULL;
120 $isseparategroups = ($course->groupmode
== SEPARATEGROUPS
and $course->groupmodeforce
and
121 !has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_COURSE
, $course->id
)));
123 if ($isseparategroups and (!$currentgroup) ) {
124 print_header("$course->shortname: ".get_string('participants'), $course->fullname
,
125 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> ".
126 get_string('participants'), "", "", true, " ", navmenu($course));
127 print_heading(get_string("notingroup", "forum"));
128 print_footer($course);
132 // Should use this variable so that we don't break stuff every time a variable is added or changed.
133 $baseurl = $CFG->wwwroot
.'/user/index.php?contextid='.$context->id
.'&roleid='.$roleid.'&id='.$course->id
.'&group='.$currentgroup.'&perpage='.$perpage.'&accesssince='.$accesssince.'&search='.$search;
137 if ($course->id
!= SITEID
) {
138 print_header("$course->shortname: ".get_string('participants'), $course->fullname
,
139 "<a href=\"../course/view.php?id=$course->id\">$course->shortname</a> -> ".
140 get_string('participants'), "", "", true, " ", navmenu($course));
142 print_header("$course->shortname: ".get_string('participants'), $course->fullname
,
143 get_string('participants'), "", "", true, " ", navmenu($course));
148 if ($course->id
== SITEID
) {
149 $filtertype = 'site';
150 } else if ($course->id
&& !$currentgroup) {
151 $filtertype = 'course';
152 $filterselect = $course->id
;
154 $filtertype = 'group';
155 $filterselect = $currentgroup;
157 $currenttab = 'participants';
160 require_once($CFG->dirroot
.'/user/tabs.php');
163 /// Get the hidden field list
164 if (has_capability('moodle/course:viewhiddenuserfields', get_context_instance(CONTEXT_COURSE
, $course->id
))) {
165 $hiddenfields = array(); // teachers and admins are allowed to see everything
167 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields
));
171 /// Print settings and things in a table across the top
173 echo '<table class="controls" cellspacing="0"><tr>';
175 /// Print my course menus
176 if ($mycourses = get_my_courses($USER->id
)) {
177 echo '<td class="left">';
178 print_string('mycourses');
181 $courselist = array();
182 foreach ($mycourses as $mycourse) {
183 $courselist[$mycourse->id
] = $mycourse->shortname
;
185 popup_form($CFG->wwwroot
.'/user/index.php?roleid='.$roleid.'&sifirst=&silast=&id=',
186 $courselist, 'courseform',$course->id
);
190 if ($groupmode == VISIBLEGROUPS
or ($groupmode and has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_COURSE
, $course->id
)))) {
191 if ($groups_names = groups_get_groups_names($course->id
)) { //TODO:
192 echo '<td class="left">';
193 print_group_menu($groups_names, $groupmode, $currentgroup, $baseurl);
198 // get minimum lastaccess for this course and display a dropbox to filter by lastaccess going back this far.
199 // this might not work anymore because you always going to get yourself as the most recent entry? added $USER!=$user ch
200 $minlastaccess = get_field_sql('SELECT min(timeaccess) FROM '.$CFG->prefix
.'user_lastaccess WHERE courseid = '.$course->id
.' AND timeaccess != 0 AND userid!='.$USER->id
);
201 $lastaccess0exists = record_exists('user_lastaccess','courseid',$course->id
,'timeaccess',0);
202 $now = usergetmidnight(time());
203 $timeaccess = array();
205 // makes sense for this to go first.
206 $timeoptions[0] = get_string('selectperiod');
209 for ($i = 1; $i < 7; $i++
) {
210 if (strtotime('-'.$i.' days',$now) >= $minlastaccess) {
211 $timeoptions[strtotime('-'.$i.' days',$now)] = get_string('numdays','moodle',$i);
215 for ($i = 1; $i < 10; $i++
) {
216 if (strtotime('-'.$i.' weeks',$now) >= $minlastaccess) {
217 $timeoptions[strtotime('-'.$i.' weeks',$now)] = get_string('numweeks','moodle',$i);
221 for ($i = 2; $i < 12; $i++
) {
222 if (strtotime('-'.$i.' months',$now) >= $minlastaccess) {
223 $timeoptions[strtotime('-'.$i.' months',$now)] = get_string('nummonths','moodle',$i);
227 if (strtotime('-1 year',$now) >= $minlastaccess) {
228 $timeoptions[strtotime('-1 year',$now)] = get_string('lastyear');
231 if (!empty($lastaccess0exists)) {
232 $timeoptions[-1] = get_string('never');
235 if (count($timeoptions) > 1) {
236 echo '<td class="left">';
237 echo get_string('usersnoaccesssince').': ';
238 $baseurl = preg_replace('/&accesssince='.$accesssince.'/','',$baseurl);
239 echo popup_form($baseurl.'&accesssince=',$timeoptions,'timeoptions',$accesssince,'','','',true);
244 echo '<td class="right">';
245 echo get_string('userlist').': ';
246 $formatmenu = array( '0' => get_string('detailedless'),
247 '1' => get_string('detailedmore'));
248 echo popup_form($baseurl.'&mode=', $formatmenu, 'formatmenu', $fullmode, '', '', '', true);
249 echo '</td></tr></table>';
251 if ($currentgroup and (!$isseparategroups or has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_COURSE
, $course->id
)))) { /// Display info about the group
252 if ($group = groups_get_group($currentgroup)) { //TODO:
253 if (!empty($group->description
) or (!empty($group->picture
) and empty($group->hidepicture
))) {
254 echo '<table class="groupinfobox"><tr><td class="left side picture">';
255 print_group_picture($group, $course->id
, true, false, false);
256 echo '</td><td class="content">';
257 echo '<h3>'.$group->name
;
258 if (has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE
, $course->id
))) {
259 echo ' <a title="'.get_string('editgroupprofile').'" href="'.groups_group_edit_url($course->id
, $group->id
).'">';
260 echo '<img src="'.$CFG->pixpath
.'/t/edit.gif" alt="'.get_string('editgroupprofile').'" />';
264 echo format_text($group->description
);
265 echo '</td></tr></table>';
271 /// Define a table showing a list of users in the current role selection
273 $tablecolumns = array('userpic', 'fullname');
274 $tableheaders = array(get_string('userpic'), get_string('fullname'));
275 if (!isset($hiddenfields['city'])) {
276 $tablecolumns[] = 'city';
277 $tableheaders[] = get_string('city');
279 if (!isset($hiddenfields['country'])) {
280 $tablecolumns[] = 'country';
281 $tableheaders[] = get_string('country');
283 if (!isset($hiddenfields['lastaccess'])) {
284 $tablecolumns[] = 'lastaccess';
285 $tableheaders[] = get_string('lastaccess');
288 if ($course->enrolperiod
) {
289 $tablecolumns[] = 'timeend';
290 $tableheaders[] = get_string('enrolmentend');
293 if ($bulkoperations) {
294 $tablecolumns[] = '';
295 $tableheaders[] = get_string('select');
298 $table = new flexible_table('user-index-participants-'.$course->id
);
300 $table->define_columns($tablecolumns);
301 $table->define_headers($tableheaders);
302 $table->define_baseurl($baseurl);
304 $table->sortable(true, 'lastaccess', SORT_DESC
);
306 $table->set_attribute('cellspacing', '0');
307 $table->set_attribute('id', 'participants');
308 $table->set_attribute('class', 'generaltable generalbox');
310 $table->set_control_variables(array(
311 TABLE_VAR_SORT
=> 'ssort',
312 TABLE_VAR_HIDE
=> 'shide',
313 TABLE_VAR_SHOW
=> 'sshow',
314 TABLE_VAR_IFIRST
=> 'sifirst',
315 TABLE_VAR_ILAST
=> 'silast',
316 TABLE_VAR_PAGE
=> 'spage'
321 // we are looking for all users with this role assigned in this context or higher
322 if ($usercontexts = get_parent_contexts($context)) {
323 $listofcontexts = '('.implode(',', $usercontexts).')';
325 $sitecontext = get_context_instance(CONTEXT_SYSTEM
, SITEID
);
326 $listofcontexts = '('.$sitecontext->id
.')'; // must be site
329 $selectrole = " AND r.roleid = $roleid ";
333 $select = 'SELECT u.id, u.username, u.firstname, u.lastname, u.email, u.city, u.country, u.picture, u.lang, u.timezone, u.emailstop, u.maildisplay, ul.timeaccess AS lastaccess, r.hidden '; // s.lastaccess
334 $select .= $course->enrolperiod?
', r.timeend ':'';
336 $from = "FROM {$CFG->prefix}user u INNER JOIN
337 {$CFG->prefix}role_assignments r on u.id=r.userid LEFT OUTER JOIN
338 {$CFG->prefix}user_lastaccess ul on (r.userid=ul.userid and ul.courseid = $course->id)";
340 $hiddensql = has_capability('moodle/role:viewhiddenassigns', $context)?
'':' AND r.hidden = 0 ';
342 // excluse users with these admin role assignments
343 if ($doanythingroles) {
344 $adminroles = 'AND r.roleid NOT IN (';
346 foreach ($doanythingroles as $aroleid=>$role) {
347 $adminroles .= "$aroleid,";
349 $adminroles = rtrim($adminroles,",");
355 // join on 2 conditions
356 // otherwise we run into the problem of having records in ul table, but not relevant course
357 // and user record is not pulled out
358 $where = "WHERE (r.contextid = $context->id OR r.contextid in $listofcontexts)
359 AND u.deleted = 0 $selectrole
360 AND (ul.courseid = $course->id OR ul.courseid IS NULL)
361 AND u.username != 'guest'
364 $where .= get_lastaccess_sql($accesssince);
368 if (!empty($search)) {
370 $fullname = sql_fullname('u.firstname','u.lastname');
371 $wheresearch .= ' AND ('. $fullname .' '. $LIKE .'\'%'. $search .'%\' OR email '. $LIKE .'\'%'. $search .'%\' OR idnumber '.$LIKE.' \'%'.$search.'%\') ';
375 if ($currentgroup) { // Displaying a group by choice
376 // FIX: TODO: This will not work if $currentgroup == 0, i.e. "those not in a group"
377 $from .= 'LEFT JOIN '.$CFG->prefix
.'groups_members gm ON u.id = gm.userid ';
378 $where .= ' AND gm.groupid = '.$currentgroup;
381 $totalcount = count_records_sql('SELECT COUNT(distinct u.id) '.$from.$where); // Each user could have > 1 role
383 if ($table->get_sql_where()) {
384 $where .= ' AND '.$table->get_sql_where();
387 if ($table->get_sql_sort()) {
388 $sort = ' ORDER BY '.$table->get_sql_sort();
393 $matchcount = count_records_sql('SELECT COUNT(distinct u.id) '.$from.$where.$wheresearch);
395 $table->initialbars($totalcount > $perpage);
396 $table->pagesize($perpage, $matchcount);
398 $userlist = get_records_sql($select.$from.$where.$wheresearch.$sort,
399 $table->get_page_start(), $table->get_page_size());
401 /// If there are multiple Roles in the course, then show a drop down menu for switching
403 if (count($rolenames) > 1) {
404 echo '<div class="rolesform">';
405 echo get_string('currentrole', 'role').': ';
406 $rolenames = array(0 => get_string('all')) +
$rolenames;
407 popup_form("$CFG->wwwroot/user/index.php?contextid=$context->id&sifirst=&silast=&roleid=", $rolenames,
408 'rolesform', $roleid, '');
413 if (!$currentrole = get_record('role','id',$roleid)) {
414 error('That role does not exist');
416 $a->number
= $totalcount;
417 $a->role
= $currentrole->name
;
418 $heading = get_string('xuserswiththerole', 'role', $a);
419 if (user_can_assign($context, $roleid)) {
420 $heading .= ' <a href="'.$CFG->wwwroot
.'/'.$CFG->admin
.'/roles/assign.php?roleid='.$roleid.'&contextid='.$context->id
.'">';
421 $heading .= '<img src="'.$CFG->pixpath
.'/i/edit.gif" class="icon" alt="" /></a>';
423 print_heading($heading, 'center', 3);
425 if ($matchcount < $totalcount) {
426 print_heading(get_string('allparticipants').': '.$matchcount.'/'.$totalcount, '', 3);
428 print_heading(get_string('allparticipants').': '.$matchcount, '', 3);
433 if ($bulkoperations) {
435 <script type="text/javascript">
437 function checksubmit(form) {
438 var destination = form.formaction.options[form.formaction.selectedIndex].value;
439 if (destination == "" || !checkchecked(form)) {
440 form.formaction.selectedIndex = 0;
447 function checkchecked(form) {
448 var inputs = document.getElementsByTagName(\'INPUT\');
450 inputs = filterByParent(inputs, function() {return form;});
451 for(var i = 0; i < inputs.length; ++i) {
452 if (inputs[i].type == \'checkbox\' && inputs[i].checked) {
461 echo '<form action="action_redir.php" method="post" id="participantsform" onsubmit="return checksubmit(this);">';
463 // added url encode for xhtml strict MDL-7861
464 echo '<input type="hidden" name="returnto" value="'.urlencode($_SERVER['REQUEST_URI']).'" />';
465 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey
.'" />';
468 if ($CFG->longtimenosee
> 0 && $CFG->longtimenosee
< 1000 && $totalcount > 0) {
469 echo '<p id="longtimenosee">('.get_string('unusedaccounts', '', $CFG->longtimenosee
).')</p>';
472 if ($fullmode) { // Print simple listing
473 if ($totalcount < 1) {
474 print_heading(get_string('nothingtodisplay'));
476 if ($totalcount > $perpage) {
478 $firstinitial = $table->get_initial_first();
479 $lastinitial = $table->get_initial_last();
480 $strall = get_string('all');
481 $alpha = explode(',', get_string('alphabet'));
483 // Bar of first initials
485 echo '<div class="initialbar firstinitial">'.get_string('firstname').' : ';
486 if(!empty($firstinitial)) {
487 echo '<a href="'.$baseurl.'&sifirst=">'.$strall.'</a>';
489 echo '<strong>'.$strall.'</strong>';
491 foreach ($alpha as $letter) {
492 if ($letter == $firstinitial) {
493 echo ' <strong>'.$letter.'</strong>';
495 echo ' <a href="'.$baseurl.'&sifirst='.$letter.'">'.$letter.'</a>';
500 // Bar of last initials
502 echo '<div class="initialbar lastinitial">'.get_string('lastname').' : ';
503 if(!empty($lastinitial)) {
504 echo '<a href="'.$baseurl.'&silast=">'.$strall.'</a>';
506 echo '<strong>'.$strall.'</strong>';
508 foreach ($alpha as $letter) {
509 if ($letter == $lastinitial) {
510 echo ' <strong>'.$letter.'</strong>';
512 echo ' <a href="'.$baseurl.'&silast='.$letter.'">'.$letter.'</a>';
517 print_paging_bar($matchcount, intval($table->get_page_start() / $perpage), $perpage, $baseurl.'&', 'spage');
520 if ($matchcount > 0) {
521 foreach ($userlist as $user) {
522 print_user($user, $course, $bulkoperations);
526 print_heading(get_string('nothingtodisplay'));
531 $countrysort = (strpos($sort, 'country') !== false);
532 $timeformat = get_string('strftimedate');
535 if (!empty($userlist)) {
536 foreach ($userlist as $user) {
538 // if the assignment is hidden, display icon
539 $hidden = "<img src=\"{$CFG->pixpath}/t/hide.gif\" alt=\"".get_string('hiddenassign')."\" class=\"hide-show-image\"/>";
544 if ($user->lastaccess
) {
545 $lastaccess = format_time(time() - $user->lastaccess
, $datestring);
547 $lastaccess = $strnever;
550 if (empty($user->country
)) {
555 $country = '('.$user->country
.') '.$countries[$user->country
];
558 $country = $countries[$user->country
];
562 $usercontext = get_context_instance(CONTEXT_USER
, $user->id
);
564 if ($piclink = ($USER->id
== $user->id ||
has_capability('moodle/user:viewdetails', $context) ||
has_capability('moodle/user:viewdetails', $context))) {
565 $profilelink = '<strong><a href="'.$CFG->wwwroot
.'/user/view.php?id='.$user->id
.'&course='.$course->id
.'">'.fullname($user).'</a></strong>';
567 $profilelink = '<strong>'.fullname($user).'</strong>';
571 print_user_picture($user->id
, $course->id
, $user->picture
, false, true, $piclink),
574 if (!isset($hiddenfields['city'])) {
575 $data[] = $user->city
;
577 if (!isset($hiddenfields['country'])) {
580 if (!isset($hiddenfields['lastaccess'])) {
581 $data[] = $lastaccess;
583 if ($course->enrolperiod
) {
584 if ($user->timeend
) {
585 $data[] = userdate($user->timeend
, $timeformat);
587 $data[] = get_string('unlimited');
590 if ($bulkoperations) {
591 $data[] = '<input type="checkbox" name="user'.$user->id
.'" />';
593 $table->add_data($data);
598 $table->print_html();
602 if ($bulkoperations) {
603 echo '<br /><div class="buttons">';
604 echo '<input type="button" onclick="checkall()" value="'.get_string('selectall').'" /> ';
605 echo '<input type="button" onclick="checknone()" value="'.get_string('deselectall').'" /> ';
606 $displaylist['messageselect.php'] = get_string('messageselectadd');
607 if ($course->enrolperiod
) {
608 $displaylist['extendenrol.php'] = get_string('extendenrol');
610 helpbutton("participantswithselectedusers", get_string("withselectedusers"));
611 choose_from_menu ($displaylist, "formaction", "", get_string("withselectedusers"), "if(checksubmit(this.form))this.form.submit();", "");
612 echo '<input type="hidden" name="id" value="'.$course->id
.'" />';
613 echo '<input type="submit" value="' . get_string('ok') . '" />';
619 if ($bulkoperations && $totalcount > ($perpage*3)) {
620 echo '<form action="index.php"><fieldset class="invisiblefieldset"><input type="hidden" name="id" value="'.$course->id
.'" />'.get_string('search').': '."\n";
621 echo '<input type="text" name="search" value="'.$search.'" /> <input type="submit" value="'.get_string('search').'" /></fieldset></form>'."\n";
624 if ($perpage == SHOW_ALL_PAGE_SIZE
) {
625 echo '<div id="showall"><a href="'.$baseurl.'&perpage='.DEFAULT_PAGE_SIZE
.'">'.get_string('showperpage', '', DEFAULT_PAGE_SIZE
).'</a></div>';
627 } else if ($matchcount > 0 && $perpage < $matchcount) {
628 echo '<div id="showall"><a href="'.$baseurl.'&perpage='.SHOW_ALL_PAGE_SIZE
.'">'.get_string('showall', '', $matchcount).'</a></div>';
631 print_footer($course);
636 function get_lastaccess_sql($accesssince='') {
637 if (empty($accesssince)) {
640 if ($accesssince == -1) { // never
641 return ' AND ul.timeaccess = 0';
643 return ' AND ul.timeaccess != 0 AND timeaccess < '.$accesssince;