Merge branch 'MDL-81525-main' of https://github.com/andrewnicols/moodle
[moodle.git] / admin / user / user_bulk_display.php
blob7ce20d75879f4517bbdd1566efde7200ae89bbc6
1 <?php
3 require_once('../../config.php');
4 require_once($CFG->libdir.'/adminlib.php');
6 $sort = optional_param('sort', 'fullname', PARAM_ALPHA);
7 $dir = optional_param('dir', 'asc', PARAM_ALPHA);
9 admin_externalpage_setup('userbulk');
11 $returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
12 $return = new moodle_url($returnurl ?: '/admin/user/user_bulk.php');
14 if (empty($SESSION->bulk_users)) {
15 redirect($return);
18 $users = $SESSION->bulk_users;
19 $usertotal = get_users(false);
20 $usercount = count($users);
22 $strnever = get_string('never');
24 $PAGE->set_primary_active_tab('siteadminnode');
25 $PAGE->set_secondary_active_tab('users');
27 echo $OUTPUT->header();
29 $countries = get_string_manager()->get_list_of_countries(true);
31 $userfieldsapi = \core_user\fields::for_name();
32 $namefields = $userfieldsapi->get_sql('', false, '', '', false)->selects;
33 foreach ($users as $key => $id) {
34 $user = $DB->get_record('user', array('id'=>$id), 'id, ' . $namefields . ', username, email, country, lastaccess, city');
35 $user->fullname = fullname($user, true);
36 $user->country = @$countries[$user->country];
37 unset($user->firstname);
38 unset($user->lastname);
39 $users[$key] = $user;
41 unset($countries);
43 // Need to sort by date
44 function sort_compare($a, $b) {
45 global $sort, $dir;
46 if($sort == 'lastaccess') {
47 $rez = $b->lastaccess - $a->lastaccess;
48 } else {
49 $rez = strcasecmp(@$a->$sort, @$b->$sort);
51 return $dir == 'desc' ? -$rez : $rez;
53 usort($users, 'sort_compare');
55 $table = new html_table();
56 $table->width = "95%";
57 $columns = array('fullname', /*'username', */'email', 'city', 'country', 'lastaccess');
58 foreach ($columns as $column) {
59 $strtitle = get_string($column);
60 if ($sort != $column) {
61 $columnicon = '';
62 $columndir = 'asc';
63 } else {
64 $columndir = $dir == 'asc' ? 'desc' : 'asc';
65 $icon = 't/down';
66 $iconstr = $columndir;
67 if ($dir != 'asc') {
68 $icon = 't/up';
70 $columnicon = ' ' . $OUTPUT->pix_icon($icon, get_string($iconstr));
72 $table->head[] = '<a href="user_bulk_display.php?sort='.$column.'&amp;dir='.$columndir.'">'.$strtitle.'</a>'.$columnicon;
73 $table->align[] = 'left';
76 foreach($users as $user) {
77 $table->data[] = array (
78 '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.SITEID.'">'.$user->fullname.'</a>',
79 // $user->username,
80 s($user->email),
81 $user->city,
82 $user->country,
83 $user->lastaccess ? format_time(time() - $user->lastaccess) : $strnever
87 echo $OUTPUT->heading("$usercount / $usertotal ".get_string('users'));
88 echo html_writer::table($table);
90 echo $OUTPUT->continue_button($return);
92 echo $OUTPUT->footer();