libdir.'/tablelib.php');
require_once($CFG->libdir.'/filelib.php');
define('USER_SMALL_CLASS', 20); // Below this is considered small
define('USER_LARGE_CLASS', 200); // Above this is considered large
define('DEFAULT_PAGE_SIZE', 20);
define('SHOW_ALL_PAGE_SIZE', 5000);
define('MODE_BRIEF', 0);
define('MODE_USERDETAILS', 1);
$page = optional_param('page', 0, PARAM_INT); // which page to show
$perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // how many per page
$mode = optional_param('mode', NULL, PARAM_INT); // use the MODE_ constants
$accesssince = optional_param('accesssince',0,PARAM_INT); // filter by last access. -1 = never
$search = optional_param('search','',PARAM_RAW); // make sure it is processed with p() or s() when sending to output!
$roleid = optional_param('roleid', 0, PARAM_INT); // optional roleid, 0 means all enrolled users (or all on the frontpage)
$contextid = optional_param('contextid', 0, PARAM_INT); // one of this or
$courseid = optional_param('id', 0, PARAM_INT); // this are required
$PAGE->set_url('/user/index.php', array(
'page' => $page,
'perpage' => $perpage,
'mode' => $mode,
'accesssince' => $accesssince,
'search' => $search,
'roleid' => $roleid,
'contextid' => $contextid,
'id' => $courseid));
if ($contextid) {
$context = get_context_instance_by_id($contextid, MUST_EXIST);
if ($context->contextlevel != CONTEXT_COURSE) {
print_error('invalidcontext');
}
$course = $DB->get_record('course', array('id'=>$context->instanceid), '*', MUST_EXIST);
} else {
$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
$context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
}
// not needed anymore
unset($contextid);
unset($courseid);
require_login($course);
$systemcontext = get_context_instance(CONTEXT_SYSTEM);
$isfrontpage = ($course->id == SITEID);
$frontpagectx = get_context_instance(CONTEXT_COURSE, SITEID);
if ($isfrontpage) {
$PAGE->set_pagelayout('admin');
require_capability('moodle/site:viewparticipants', $systemcontext);
} else {
$PAGE->set_pagelayout('incourse');
require_capability('moodle/course:viewparticipants', $context);
}
$rolenamesurl = new moodle_url("$CFG->wwwroot/user/index.php?contextid=$context->id&sifirst=&silast=");
$allroles = get_all_roles();
$roles = get_profile_roles($context);
$allrolenames = array();
if ($isfrontpage) {
$rolenames = array(0=>get_string('allsiteusers', 'role'));
} else {
$rolenames = array(0=>get_string('allparticipants'));
}
foreach ($allroles as $role) {
$allrolenames[$role->id] = strip_tags(role_get_name($role, $context)); // Used in menus etc later on
if (isset($roles[$role->id])) {
$rolenames[$role->id] = $allrolenames[$role->id];
}
}
// make sure other roles may not be selected by any means
if (empty($rolenames[$roleid])) {
print_error('noparticipants');
}
// no roles to display yet?
// frontpage course is an exception, on the front page course we should display all users
if (empty($rolenames) && !$isfrontpage) {
if (has_capability('moodle/role:assign', $context)) {
redirect($CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$context->id);
} else {
print_error('noparticipants');
}
}
add_to_log($course->id, 'user', 'view all', 'index.php?id='.$course->id, '');
$bulkoperations = has_capability('moodle/course:bulkmessaging', $context);
$countries = get_string_manager()->get_list_of_countries();
$strnever = get_string('never');
$datestring = new stdClass();
$datestring->year = get_string('year');
$datestring->years = get_string('years');
$datestring->day = get_string('day');
$datestring->days = get_string('days');
$datestring->hour = get_string('hour');
$datestring->hours = get_string('hours');
$datestring->min = get_string('min');
$datestring->mins = get_string('mins');
$datestring->sec = get_string('sec');
$datestring->secs = get_string('secs');
if ($mode !== NULL) {
$mode = (int)$mode;
$SESSION->userindexmode = $mode;
} else if (isset($SESSION->userindexmode)) {
$mode = (int)$SESSION->userindexmode;
} else {
$mode = MODE_BRIEF;
}
/// Check to see if groups are being used in this course
/// and if so, set $currentgroup to reflect the current group
$groupmode = groups_get_course_groupmode($course); // Groups are being used
$currentgroup = groups_get_course_group($course, true);
if (!$currentgroup) { // To make some other functions work better later
$currentgroup = NULL;
}
$isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context));
$PAGE->set_title("$course->shortname: ".get_string('participants'));
$PAGE->set_heading($course->fullname);
$PAGE->set_pagetype('course-view-' . $course->format);
$PAGE->add_body_class('path-user'); // So we can style it independently
$PAGE->set_other_editing_capability('moodle/course:manageactivities');
echo $OUTPUT->header();
echo '
';
if ($isseparategroups and (!$currentgroup) ) {
// The user is not in the group so show message and exit
echo $OUTPUT->heading(get_string("notingroup"));
echo $OUTPUT->footer();
exit;
}
// Should use this variable so that we don't break stuff every time a variable is added or changed.
$baseurl = new moodle_url('/user/index.php', array(
'contextid' => $context->id,
'roleid' => $roleid,
'id' => $course->id,
'perpage' => $perpage,
'accesssince' => $accesssince,
'search' => s($search)));
/// setting up tags
if ($course->id == SITEID) {
$filtertype = 'site';
} else if ($course->id && !$currentgroup) {
$filtertype = 'course';
$filterselect = $course->id;
} else {
$filtertype = 'group';
$filterselect = $currentgroup;
}
/// Get the hidden field list
if (has_capability('moodle/course:viewhiddenuserfields', $context)) {
$hiddenfields = array(); // teachers and admins are allowed to see everything
} else {
$hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
}
if (isset($hiddenfields['lastaccess'])) {
// do not allow access since filtering
$accesssince = 0;
}
/// Print settings and things in a table across the top
$controlstable = new html_table();
$controlstable->attributes['class'] = 'controls';
$controlstable->cellspacing = 0;
$controlstable->data[] = new html_table_row();
/// Print my course menus
if ($mycourses = enrol_get_my_courses()) {
$courselist = array();
$popupurl = new moodle_url('/user/index.php?roleid='.$roleid.'&sifirst=&silast=');
foreach ($mycourses as $mycourse) {
$coursecontext = get_context_instance(CONTEXT_COURSE, $mycourse->id);
$courselist[$mycourse->id] = format_string($mycourse->shortname, true, array('context' => $coursecontext));
}
if (has_capability('moodle/site:viewparticipants', $systemcontext)) {
unset($courselist[SITEID]);
$courselist = array(SITEID => format_string($SITE->shortname, true, array('context' => $systemcontext))) + $courselist;
}
$select = new single_select($popupurl, 'id', $courselist, $course->id, array(''=>'choosedots'), 'courseform');
$select->set_label(get_string('mycourses'));
$controlstable->data[0]->cells[] = $OUTPUT->render($select);
}
$controlstable->data[0]->cells[] = groups_print_course_menu($course, $baseurl->out(), true);
if (!isset($hiddenfields['lastaccess'])) {
// get minimum lastaccess for this course and display a dropbox to filter by lastaccess going back this far.
// we need to make it diferently for normal courses and site course
if (!$isfrontpage) {
$minlastaccess = $DB->get_field_sql('SELECT min(timeaccess)
FROM {user_lastaccess}
WHERE courseid = ?
AND timeaccess != 0', array($course->id));
$lastaccess0exists = $DB->record_exists('user_lastaccess', array('courseid'=>$course->id, 'timeaccess'=>0));
} else {
$minlastaccess = $DB->get_field_sql('SELECT min(lastaccess)
FROM {user}
WHERE lastaccess != 0');
$lastaccess0exists = $DB->record_exists('user', array('lastaccess'=>0));
}
$now = usergetmidnight(time());
$timeaccess = array();
$baseurl->remove_params('accesssince');
// makes sense for this to go first.
$timeoptions[0] = get_string('selectperiod');
// days
for ($i = 1; $i < 7; $i++) {
if (strtotime('-'.$i.' days',$now) >= $minlastaccess) {
$timeoptions[strtotime('-'.$i.' days',$now)] = get_string('numdays','moodle',$i);
}
}
// weeks
for ($i = 1; $i < 10; $i++) {
if (strtotime('-'.$i.' weeks',$now) >= $minlastaccess) {
$timeoptions[strtotime('-'.$i.' weeks',$now)] = get_string('numweeks','moodle',$i);
}
}
// months
for ($i = 2; $i < 12; $i++) {
if (strtotime('-'.$i.' months',$now) >= $minlastaccess) {
$timeoptions[strtotime('-'.$i.' months',$now)] = get_string('nummonths','moodle',$i);
}
}
// try a year
if (strtotime('-1 year',$now) >= $minlastaccess) {
$timeoptions[strtotime('-1 year',$now)] = get_string('lastyear');
}
if (!empty($lastaccess0exists)) {
$timeoptions[-1] = get_string('never');
}
if (count($timeoptions) > 1) {
$select = new single_select($baseurl, 'accesssince', $timeoptions, $accesssince, null, 'timeoptions');
$select->set_label(get_string('usersnoaccesssince'));
$controlstable->data[0]->cells[] = $OUTPUT->render($select);
}
}
$formatmenu = array( '0' => get_string('brief'),
'1' => get_string('userdetails'));
$select = new single_select($baseurl, 'mode', $formatmenu, $mode, null, 'formatmenu');
$select->set_label(get_string('userlist'));
$userlistcell = new html_table_cell();
$userlistcell->attributes['class'] = 'right';
$userlistcell->text = $OUTPUT->render($select);
$controlstable->data[0]->cells[] = $userlistcell;
echo html_writer::table($controlstable);
if ($currentgroup and (!$isseparategroups or has_capability('moodle/site:accessallgroups', $context))) { /// Display info about the group
if ($group = groups_get_group($currentgroup)) {
if (!empty($group->description) or (!empty($group->picture) and empty($group->hidepicture))) {
$groupinfotable = new html_table();
$groupinfotable->attributes['class'] = 'groupinfobox';
$picturecell = new html_table_cell();
$picturecell->attributes['class'] = 'left side picture';
$picturecell->text = print_group_picture($group, $course->id, true, true, false);
$contentcell = new html_table_cell();
$contentcell->attributes['class'] = 'content';
$contentheading = $group->name;
if (has_capability('moodle/course:managegroups', $context)) {
$aurl = new moodle_url('/group/group.php', array('id' => $group->id, 'courseid' => $group->courseid));
$contentheading .= ' ' . $OUTPUT->action_icon($aurl, new pix_icon('t/edit', get_string('editgroupprofile')));
}
$group->description = file_rewrite_pluginfile_urls($group->description, 'pluginfile.php', $context->id, 'group', 'description', $group->id);
if (!isset($group->descriptionformat)) {
$group->descriptionformat = FORMAT_MOODLE;
}
$options = array('overflowdiv'=>true);
$contentcell->text = $OUTPUT->heading($contentheading, 3) . format_text($group->description, $group->descriptionformat, $options);
$groupinfotable->data[] = new html_table_row(array($picturecell, $contentcell));
echo html_writer::table($groupinfotable);
}
}
}
/// Define a table showing a list of users in the current role selection
$tablecolumns = array('userpic', 'fullname');
$extrafields = get_extra_user_fields($context);
$tableheaders = array(get_string('userpic'), get_string('fullnameuser'));
if ($mode === MODE_BRIEF) {
foreach ($extrafields as $field) {
$tablecolumns[] = $field;
$tableheaders[] = get_user_field_name($field);
}
}
if ($mode === MODE_BRIEF && !isset($hiddenfields['city'])) {
$tablecolumns[] = 'city';
$tableheaders[] = get_string('city');
}
if ($mode === MODE_BRIEF && !isset($hiddenfields['country'])) {
$tablecolumns[] = 'country';
$tableheaders[] = get_string('country');
}
if (!isset($hiddenfields['lastaccess'])) {
$tablecolumns[] = 'lastaccess';
$tableheaders[] = get_string('lastaccess');
}
if ($bulkoperations) {
$tablecolumns[] = 'select';
$tableheaders[] = get_string('select');
}
$table = new flexible_table('user-index-participants-'.$course->id);
$table->define_columns($tablecolumns);
$table->define_headers($tableheaders);
$table->define_baseurl($baseurl->out());
if (!isset($hiddenfields['lastaccess'])) {
$table->sortable(true, 'lastaccess', SORT_DESC);
} else {
$table->sortable(true, 'firstname', SORT_ASC);
}
$table->no_sorting('roles');
$table->no_sorting('groups');
$table->no_sorting('groupings');
$table->no_sorting('select');
$table->set_attribute('cellspacing', '0');
$table->set_attribute('id', 'participants');
$table->set_attribute('class', 'generaltable generalbox');
$table->set_control_variables(array(
TABLE_VAR_SORT => 'ssort',
TABLE_VAR_HIDE => 'shide',
TABLE_VAR_SHOW => 'sshow',
TABLE_VAR_IFIRST => 'sifirst',
TABLE_VAR_ILAST => 'silast',
TABLE_VAR_PAGE => 'spage'
));
$table->setup();
// we are looking for all users with this role assigned in this context or higher
$contextlist = get_related_contexts_string($context);
list($esql, $params) = get_enrolled_sql($context, NULL, $currentgroup, true);
$joins = array("FROM {user} u");
$wheres = array();
$extrasql = get_extra_user_fields_sql($context, 'u', '', array(
'id', 'username', 'firstname', 'lastname', 'email', 'city', 'country',
'picture', 'lang', 'timezone', 'maildisplay', 'imagealt', 'lastaccess'));
if ($isfrontpage) {
$select = "SELECT u.id, u.username, u.firstname, u.lastname,
u.email, u.city, u.country, u.picture,
u.lang, u.timezone, u.maildisplay, u.imagealt,
u.lastaccess$extrasql";
$joins[] = "JOIN ($esql) e ON e.id = u.id"; // everybody on the frontpage usually
if ($accesssince) {
$wheres[] = get_user_lastaccess_sql($accesssince);
}
} else {
$select = "SELECT u.id, u.username, u.firstname, u.lastname,
u.email, u.city, u.country, u.picture,
u.lang, u.timezone, u.maildisplay, u.imagealt,
COALESCE(ul.timeaccess, 0) AS lastaccess$extrasql";
$joins[] = "JOIN ($esql) e ON e.id = u.id"; // course enrolled users only
$joins[] = "LEFT JOIN {user_lastaccess} ul ON (ul.userid = u.id AND ul.courseid = :courseid)"; // not everybody accessed course yet
$params['courseid'] = $course->id;
if ($accesssince) {
$wheres[] = get_course_lastaccess_sql($accesssince);
}
}
// performance hacks - we preload user contexts together with accounts
list($ccselect, $ccjoin) = context_instance_preload_sql('u.id', CONTEXT_USER, 'ctx');
$select .= $ccselect;
$joins[] = $ccjoin;
// limit list to users with some role only
if ($roleid) {
$wheres[] = "u.id IN (SELECT userid FROM {role_assignments} WHERE roleid = :roleid AND contextid $contextlist)";
$params['roleid'] = $roleid;
}
$from = implode("\n", $joins);
if ($wheres) {
$where = "WHERE " . implode(" AND ", $wheres);
} else {
$where = "";
}
$totalcount = $DB->count_records_sql("SELECT COUNT(u.id) $from $where", $params);
if (!empty($search)) {
$fullname = $DB->sql_fullname('u.firstname','u.lastname');
$wheres[] = "(". $DB->sql_like($fullname, ':search1', false, false) .
" OR ". $DB->sql_like('email', ':search2', false, false) .
" OR ". $DB->sql_like('idnumber', ':search3', false, false) .") ";
$params['search1'] = "%$search%";
$params['search2'] = "%$search%";
$params['search3'] = "%$search%";
}
list($twhere, $tparams) = $table->get_sql_where();
if ($twhere) {
$wheres[] = $twhere;
$params = array_merge($params, $tparams);
}
$from = implode("\n", $joins);
if ($wheres) {
$where = "WHERE " . implode(" AND ", $wheres);
} else {
$where = "";
}
if ($table->get_sql_sort()) {
$sort = ' ORDER BY '.$table->get_sql_sort();
} else {
$sort = '';
}
$matchcount = $DB->count_records_sql("SELECT COUNT(u.id) $from $where", $params);
$table->initialbars(true);
$table->pagesize($perpage, $matchcount);
// list of users at the current visible page - paging makes it relatively short
$userlist = $DB->get_recordset_sql("$select $from $where $sort", $params, $table->get_page_start(), $table->get_page_size());
/// If there are multiple Roles in the course, then show a drop down menu for switching
if (count($rolenames) > 1) {
echo '