Additional Fields in 'Browse list of users' (admin/users.php) for v2.2

Additional Fields in 'Browse list of users' (admin/users.php) for v2.2

by David Lopez -
Number of replies: 4

Found a thread for this for 1.9 and thought it should be updated for 2.2.  Due to some LDAP issues we needed to see the 'username' in the browse list of users table so I added the code below to make it work. (admin/users.php)

Step 1:  Change line 123

From array('city', 'country', 'lastaccess'));

to array('username', 'city', 'country', 'lastaccess'));

Step 2

Insert this line of code above 222

$table->head[ ] = $username;
$table->align[ ] = 'left';

Step 3

Insert this line of code above 302

$row[ ] = $user->username;

Obviously replace 'username' with whatever attribute you are looking for.

Average of ratings: -
In reply to David Lopez

Re: Additional Fields in 'Browse list of users' (admin/users.php) for v2.2

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

There is a better way in Moodle 2.2. See the final piont here: "MDL-26647 - Can choose in User policies which fields (email, idnumber, department, etc.) are included wherever lists of students are shown."

You cannot select username through the UI for security reasons, but if you hard-code this setting in your config.php, you can include username in the list of fields.

Average of ratings: Useful (1)
In reply to David Lopez

Additional (Custom) Fields in 'Browse list of users'

by Becka M -

I have 2.3, but followed this in a similar way to not show city and country. What I can't figure out is what I need to do/how I should reference the display of custom profile fields. Example: one of my fields is called Status, which is a drop-down list that has some items: Active, Inactive, Special.

 

How do I reference it in the code to display it? The $table and $row items obviously, but what else? And do I need to reference it as profile_field_Status, or just Status?

 

I have a little bit of HTML coding knowledge, and I'm learning C# at the local college.

In reply to Becka M

Re: Additional (Custom) Fields in 'Browse list of users'

by shakib ludin -

Hello Becka,

 

Were you able to solve the custom profile field display?

 

 

In reply to shakib ludin

Re: Additional (Custom) Fields in 'Browse list of users'

by Becka M -

Hi Shakib,


Yes, I got it to work.  It's been quite a while since I asked the question, and couldn't work on it much after that.  However, I picked it up again last week and got something working.


Since I don't quite know all the inner workings of the coding, I'm more of the copy-and-paste type with this.  But those coding classes definitely helped with understanding what code does what.  I still need to take a PHP class or two...


Anyway, my reasoning was that, if the /user/profile.php showed the custom profiles "automatically," then the code is in there.  Once I figured out the relevant function ( called profile_display_fields ) and I found the code in /user/profile/lib.php.  I made a copy of the function code and commented out the original, so if something got messed up, I could easily start over.  The original code would go through and display every custom field containing data, but I only want it to display one in particular.  I ended up doing some trial-and-error with the code to find out what does what.  I still had to do a little bit of coding to get it to display just one field.

In addition to taking in the user id, I also was able to get it to take a string.  It seems to compare the string to the shortnames of the custom fields, and gets the relevant id and datatype. I figured it would be easier to know the shortname, rather than trying to know or find out the id number and it's datatype.


Here's the original code:

The Function:

profile_display_fields($user->id);

The Function's Code:

function profile_display_fields($userid) {
   global $CFG, $USER, $DB;

    if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
        foreach ($categories as $category) {
            if ($fields = $DB->get_records('user_info_field', array('categoryid'=>$category->id), 'sortorder ASC')) {
                foreach ($fields as $field) {
                   require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
                    $newfield = 'profile_field_'.$field->datatype;
                    $formfield = new $newfield($field->id, $userid);
                    if ($formfield->is_visible() and !$formfield->is_empty()) {
                        print_row(format_string($formfield->field->name.':'), $formfield->display_data());
                    }
                }
            }
        }
    }
}


Here's my modified code:

The Function:

require_once($CFG->dirroot.'/user/profile/lib.php');
The above line, I usually put up with the other "require_once" lines, just to keep things organized.  It is required in order to make function below work.

$row[] = profile_display_custom_field($user->id, 'ActivityStatus');


The Function's Code:  (in /user/profile/lib.php)

//DISPLAY CUSTOM FIELD WITH SHORTNAME COMPARISION
function profile_display_custom_field($userid, $fieldshortname) 
{
    global $CFG, $USER, $DB;

   if ($fields = $DB->get_records('user_info_field', array('shortname'=>$fieldshortname), 'sortorder ASC')) 
	{

		foreach ($fields as $field)
			{
				$fielddatatype = $field->datatype;
				$fieldid = $field->id;

		                require_once($CFG->dirroot.'/user/profile/field/'.$fielddatatype.'/field.class.php');
                		$newfield = 'profile_field_'.$fielddatatype;
                		$formfield = new $newfield($fieldid, $userid);

                			if ($formfield->is_visible() and !$formfield->is_empty()) {
                  			return($formfield->display_data()); 
                    			}
					
			}

	  }
}


The part that I'm not quite happy with is the line:

if ($fields = $DB->get_records('user_info_field', array('shortname'=>$fieldshortname), 'sortorder ASC'))

I'm not happy with it because I'm not sure it's doing what I want the right way, or the best way to get it to do what I want.  I only say "the right way" because I don't quite understand what all this line is doing, just that the end result with the rest of the code I have displays what I want.


In pseudo-code, what I want is:

  • Compare the string $fieldshortname to the shortnames in the user_info_field table.
  • When the match is found (of which there should only be one, right?) take the corresponding id number as $fieldid, and combine it with $userid to get the specified field's data for the given user.
  • Display the data.


Here's a couple of questions, if someone could answer them:

  1. It seems that my pseudo-code is what my modified code seems to be doing, but the line for the comparison for the shortname seems to be more code than necessary.  Is there a way to do the comparison with less code?  (Like the foreach ($fields as $field) line...)

  2. Is my modified code in the proper place (/user/profile/lib.php)?  If not, where would be a better place to put it?

  3. My next challenge is to get the header to turn into a link to sort the column.  Currently it does not appear as a link, and I'm not sure how to get it to display as one.  Suggestions?

    The code I'm referring to is:
    $table->head[] = $lastaccess;
    $table->align[] = 'left';     
    
    $table->head[] = 'Activity Status';
    $table->align[] = 'left';

    The second header is mine for Activity Status, the first is for Last Access.  Last Access appears as a link to sort, but Activity Status does not.  There's obviously more to it, but I haven't been able to isolate it yet.