Customising display of of manual enrolment search

Customising display of of manual enrolment search

by Stuart Anderson -
Number of replies: 3

Hi,

I'm using multiple authentication methods and under certain circumstances I get duplicated users added to my system e.g. user with LDAP authand same user later with LTI auth.

I want to customise the display of the manual enrolment search results so the auth method is included in the results search so I can distinguish between the two types of user.  I've found the correct template file and can change that (see: enrol\manual\templates\form-user-selector-suggestion.mustache).  See example image attached.  However, I don't have or know the correct context variable to pass in the authentication method field from the user's profile,   Can anyone advise how I go about adding this context variable so I can include it in the template?



Thanks,

Stuart

Average of ratings: -
In reply to Stuart Anderson

Re: Customising display of of manual enrolment search

by Stuart Anderson -
For what it's worth and for the benifit of anyone looking for a solution to this I managed to trace through all the relevant moodle code to find a solution. In the latest version of moodle at time of writing (moodle 3.9.1), you just need to add one small line of code to the lib/moodlelib.php file.  This is to add the "auth" type (email, LDAP, manual, etc) to the list of identifiers used will suggesting potential users to enrol.  This change will mean that the auth time is always displayed as one of the identifiers when manually enrolling a user to a space.  The get_extra_user_fields() function is called when fetching potential users to enrol.  There are probably other ways of doing this, but this wored for me.  Note also that this the value for "auth" is also available in the enrol/manual/template/form-user-selector-suggestion.mustache template as {{auth}} should you wish to add extra emphasis to it.

Update: lib/moodlelib.php

function get_extra_user_fields($context, $already = array()) {
    global $CFG;
    // Only users with permission get the extra fields.
    if (!has_capability('moodle/site:viewuseridentity', $context)) {
        return array();
    }
    // Split showuseridentity on comma (filter needed in case the showuseridentity is empty).
    $extra = array_filter(explode(',', $CFG->showuseridentity));
    array_push($extra, "auth"); 
    foreach ($extra as $key => $field) {
        if (in_array($field, $already)) {
            unset($extra[$key]);
        }
    }
    … more code …
    return $extra;
}


In the resulting suggested users to enrol, this will output something similar to:

Joe Bloggs joe.bloggs@exampleuni.ac.uk, ldap


Obviously this fix will need to be reapplied after every moodle update as get_extra_user_fields() will get overwritten with the new version of moodle.

Hope thisis useful to someone.

In reply to Stuart Anderson

Re: Customising display of of manual enrolment search

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Given the code above the change:

$extra = array_filter(explode(',', $CFG->showuseridentity));

does not adding:

$CFG->showuseridentity = 'auth, email';

to your main config.php file do the same thing without modifying core code or a file that changes on upgrade?

In reply to Gareth J Barnard

Re: Customising display of of manual enrolment search

by Stuart Anderson -
Thanks Gareth. That is a very helpful reply. Your suggestion just needed a small change plus a caveat.

Firstly, the space after the comma must be removed otherwise php parses this as " email" and the email address doesn't get rendered. So you actually must have:
$CFG->showuseridentity = 'auth,email';
Secondly, this doesn't work for older versions of moodle.  I started out working on my own solution for moodle 3.4.2 and changing the $CFG does not work for this older moodle (my solution does).  However, with luck we will be upgrading to moodle 3.9.1 next week where changing the $CFG in config.php does work (and very well).

Your solution is obviously much more sensible and sustainable so thanks!

Best wishes,

Stuart