Moodle (3.x) Browse Users - Adding Extra Columns

Moodle Logo

Adding a new column into the Browse Users page is not too simple, because even though modifying one file will allow you to display some extra columns, if the column you want isn't pulled out from the database by default, you'll have another issue to solve.

I wanted to add the Registration Date to the table so the closest thing I could find in the database was the timecreated field - which doesn't get pulled out automatically.

So... To get that column, open up /lib/datalib.php and find the get_users_listing function. Inside that function, around line 522, you should see the following:

return $DB->get_records_sql("SELECT id, username, email, city, country, lastaccess, confirmed, mnethostid, suspended $extrafields

I changed that to:

return $DB->get_records_sql("SELECT id, username, email,timecreated, city, country, lastaccess, confirmed, mnethostid, suspended $extrafields

Saved that then opened up /admin/user.php

In this file there are a couple of changes required.

Find the following (around line 169):

$columns = array_merge($allusernamefields, $extracolumns, array('city', 'country', 'lastaccess'));

And replace it with:

$columns = array_merge($allusernamefields, $extracolumns, array('timecreated', 'city', 'country', 'lastaccess'));

Next, find

$table->head[] = $city; 

(Around line 281) and on the line above it, insert the following:

$table->head[] = 'Registered';

Because I want a date field, I want to process it so that the date looks more readable, so above

if ($user->lastaccess) {

I added the following:

if ($user->timecreated) {
    $strtimecreated = format_time(time() - $user->timecreated);
} else {
    $strtimecreated = get_string('Unknown');

Then finally, around about line 375, find

$row[] = $user->city;

And on the line above, place the following:

$row[] = $strtimecreated;

Save that file, exit then go and have a look. If all worked well, you should see a new column called Registered! The same can be done for any user profile field, simply change the timecreated references above for the field you want. If you want more fields, just repeat the above steps.

If you have a better way to do this, please let me know, otherwise...

Happy Moodling!