Issue Details (XML | Word | Printable)

Key: MDL-7163
Type: Improvement Improvement
Status: Open Open
Priority: Minor Minor
Assignee: Martin Dougiamas
Reporter: Anthony Borrow
Votes: 26
Watchers: 10
Operations

Add/Edit UI Mockup to this issue
If you were logged in you would be able to see more operations.
Moodle

Allow sorting of people in course by lastname, firstname, and username when assigning groups

Created: 22/Oct/06 10:45 PM   Updated: 21/Oct/09 04:17 PM
Return to search
Component/s: Course
Affects Version/s: 1.6.3, 1.9.5
Fix Version/s: 1.9.7

File Attachments: 1. Text File MDL-7163.patch (2 kB)
2. Text File MDL-7163.patch (2 kB)
3. Text File MDL-7163.patch (1 kB)
4. Text File MDL-7163.patch (1 kB)

Environment: n/a

URL: /course/groups.php
Participants: A. T. Wyatt, Anthony Borrow, Brian Pool, Kenneth Newquist, Martin Dougiamas, Matt Tate, Ray Lawrence and Wen Hao Chuang
Security Level: None
Affected Branches: MOODLE_16_STABLE, MOODLE_19_STABLE
Fixed Branches: MOODLE_19_STABLE


 Description  « Hide
Currently sorting of students (people in course) is by fullname which I find somewhat awkward as most users prefer to see firstname + lastname; however, most lists from the school (such as school rosters) are sorted by lastname. While it makes sense to me to sort by lastname, I think ideally it is best to give the users (i.e. teachers) the option to sort either by lastname, firstname, or username. This could be implemented either at the site or course level (my preference would be the course level so that each teacher can control how the names are sorted; however, AT Wyatt suggested the site level. As I envision it, the field could be added in with the group mode dropdown boxes in /course/edit.php as group members sorted by: with options of - Firstname, Lastname, Username.

I am requesting this because adding group members to the sections was a tedious process for me and my teachers because the list we had been given from the school was the student's official firstname; however, when I had pulled in the enrollment data I used the student's preferred name which means that what was on the teacher's roster as William Jones was in the group listing of people in course as Billy Jones. It was difficult to find Jones and required visually scanning the whole list. Hence, it would have been really helpful to be able to sort by lastname.

Thanks for your consideration of this improvement and let me know if there is anything I can do to support its introduction into Moodle.

Peace - Anthony

 All   Comments   Change History   Version Control      Sort Order: Ascending order - Click to sort in descending order
Anthony Borrow added a comment - 23/Oct/06 06:26 AM
I looked at the code in course/groups.php (revision 1.24) earlier and at least in 1.6.3 the sort is performed on line 211 with:

natcasesort($nonmembers);

I can produce the desired behavior by modifying /course/groups.php file

If I comment out line 174 (the original)
// $nonmembers[$student->id] = fullname($student, true);

and insert a modified version of the line:
$nonmembers[$student->id] = $student->lastname.', '.$student->firstname;

I am able to get the behavior that I want. Similarly, if someone want to sort by username they could instead use:
$nonmembers[$student->id] = $student->username.' ('.$student->lastname.', '.$student->firstname.')';

For consistency the same procedure should be applied to the teacher array on line 185
commenting out line 185
// $nonmembers[$teacher->id] = $prefix.fullname($teacher, true);

and replacing it with either (to sort by lastname)
$nonmembers[$teacher->id] = $prefix.$teacher->lastname.', '.$teacher->firstname;

or (to sort by username)
// $nonmembers[$teacher->id] = $prefix.$teacher->username.' ('.$teacher->lastname.', '.$teacher->firstname.')';

It is just a matter of setting up a course variable (for example, $course->groupsortby) and adding some if statements or a case statement to it and then modifying the course edit.php file to include a dropdown box with the options for fullname, lastname, firstname or username.


Ray Lawrence added a comment - 22/Jan/07 06:03 AM
Just a quick comment on this request.

Unless I'm missing something obvious (highly likely as I've only had a quick think about this) to see a student's username a teacher would need to log in as a student (for each student) to find their username. So would the ability to be able to sort by username be useful in practice?


Anthony Borrow added a comment - 22/Jan/07 06:54 AM
We use the students school id as their username and have some lists that come sorted by it. It is admittedly not as important as being able to sort by lastname then firstname (which currently can be done if you switch the fullname to be surname+firstname; however, in practice most folks (at least here in the U.S.) prefer firstname+lastname) for display purposes but lastname, firstname for sorting purposes. I think having the ability to choose is the key point of this request. Whether or not username is part of that is not as important - it was just another option that I thought might be useful to some.

Ray Lawrence added a comment - 22/Jan/07 07:17 AM
Agreed. Best wishes with this request.

Matt Tate added a comment - 25/Feb/07 02:27 AM
This fix does not seem to work with 1.7.1.

I have commented out the equivalent line ie ( // $nonmembers[$contextuser->id] = fullname($contextuser, true)
and altered the suggested code to match the changes:
$nonmembers[$contextuser->id] = $contextuser->firstname.', '.$contextuser->lastname;

Doing this I can alter the order of the display but it STILL sorts by lastname ...

(ps I want to sort by firstname)

Any idea what I am missing?

Thanks


Anthony Borrow added a comment - 25/Feb/07 03:29 AM
Matt - I took a quick look at the 1.7 /course/groups.php code and I did not see the previous natcasesort($nonmembers); line that was in 1.6 around 211. I added this in to sort the list after we populate it and I think it will give you the desired functionality that you are looking for.

if ($contextusers = get_role_users($roleid, $context)) {
foreach ($contextusers as $contextuser) { # $nonmembers[$contextuser->id] = fullname($contextuser, true); $nonmembers[$contextuser->id] = $contextuser->firstname.', '.$contextuser->lastname; }
}
natcasesort($nonmembers);
unset($contextusers);


Matt Tate added a comment - 25/Feb/07 05:20 AM
Thank you - works spot on.

I have now got an easy way for teachers to add pupils to their groups.

Thanks

Matt


Matt Tate added a comment - 25/Feb/07 11:06 PM
This is slightly off topic. I want to achieve the same thing in /admin/roles/assign.php but the format seems very different...

-wish i knew more about PHP


Anthony Borrow added a comment - 25/Feb/07 11:29 PM
Matt - in order to avoid having non-related material on the tracker issue which IMHO only creates unnecessary reading for the developers - let's keep these types of inquires in the forums. Thanks - Anthony

Matt Tate added a comment - 25/Feb/07 11:39 PM
Apologies - thought it was worth thinking about in terms of the additional functionality of sorting by firstname/surname etc in other parts of moodle.

Anthony Borrow added a comment - 18/Mar/07 11:16 AM
Matt - not a problem, I just know how swamped the developers are and was being a little protective. As 1.8 gets ready for release I am hoping that this bug might get some attention.

Anthony Borrow added a comment - 18/Mar/07 11:36 AM
I am a little confused about how this is being implemented in 1.8. If I use the link for groups on the course page I am now taken to /group/index.php; however /course/groups.php still exists (without an obvious link to it) and without any interface for groupings. There seems to be an inconsistency here and perhaps the priority of this bug should be raised so that this question about groupings and groups and how the UI is going to be handled in 1.8 can be worked out.

Anthony Borrow added a comment - 18/Mar/07 11:43 AM

Anthony Borrow added a comment - 18/Mar/07 12:54 PM
To change the order of the list of members in the group members box I changed line 63 of /group/lib/utillib.php

from:
$fullname = fullname($user, true);

to:
$fullname = $user->lastname.', '.$user->firstname;

this is significantly simpler in 1.8 which makes make think that giving the user a choice would be fairly easy

if {some condition like $sortbyfullname=true}
then $fullname = fullname($user, true);
else $fullname = $user->lastname.', '.$user->firstname;

I'm sure there are 100 better ways to implement this but it should be fairly simple and I could see it giving greater flexibility in terms of how names are sorted and shown.


A. T. Wyatt added a comment - 03/Jul/07 12:05 AM

Brian Pool added a comment - 21/Sep/07 05:13 AM
I have made the code change to make users list by lastname, firstname and was suprised this option was not in version 1.8. I actually like groups less now than in 1.7. 1.7 interface was simple and let you know how many groups a person was in (ie if you already had him.)

I actually think in would be useful to be able to only list users not already in a group so as you add members the list keeps getting smaller to wade through. We are now using Moodle as our K-12 online gradebook and our "Groups" are being used as our course sections, so students are only members of a single group for a particular course. I has hoped to drop the 1.7 code in 1.8 but the structure changed enough that it wasn't possible.


Anthony Borrow added a comment - 21/Sep/07 06:20 AM
Brian - Keep in mind that the group list used to get smaller as members were added to a group; however, that assumed that members were only in one group. Now members can be in multiple groups. While it may be a little more complex and/or time consuming to set them up the goal is ultimately to make the use of groups more flexible for teachers. Peace - Anthony

Kenneth Newquist added a comment - 19/Dec/08 01:48 AM
According to MoodleDocs: Group FAQ, there's supposed to be a configuration option somewhere that allows you to affect the sort order of groups:

http://docs.moodle.org/en/Groups_FAQ

Quoting:
---------------------------
How can I change the sort order of the participants?
The admin can change the "fullnamedisplay" which will affect the sort order of participants.
Have your admin go to:
Administration >> Configuration >> Variables
Then under "User," see "fullnamedisplay."
(Tested using Moodle 1.6.3.)
---------------------------

Tracker #734 also references this variable, and closes the ticket:
http://tracker.moodle.org/browse/MDL-734

I've dug around through 1.9, and can find anything that matches what they're describing. I suspect we lost this functionality in the jump from 1.6x to 1.7x, though if it existed under 1.6x, why was this tracker ticket spawned in the first place?

Can we update this ticket to be a feature request for Moodle 2.0? (or does another ticket out there duplicate this one for 2.0?) With 18 votes, it seems like a pretty popular request.


Anthony Borrow added a comment - 19/Dec/08 08:18 AM
Kenneth - I think what you are looking for was moved to the Site Administration block -> Security -> Site policies -> Full Name Format. Although I think that casing of the string should probably be changed to Full name format to be consistent. Let me know if that is now what you are looking for. Peace - Anthony

Anthony Borrow added a comment - 10/Jun/09 06:57 AM
self bump

Kenneth Newquist added a comment - 10/Jun/09 03:32 PM
That did the trick for me. Thanks (and sorry about not closing the loop back in December).

Anthony Borrow added a comment - 30/Jul/09 09:14 PM
Ken - Thanks for the follow up. It has been a while since I have looked at this issue. From what I was reading it sounds like this issue may be able to be closed as fixed. I would want to give it a closer look but do not have time at the moment so I figured I would try to get folks opinion about whether more needs to be done with this. Hopefully those who have voted and are watching this issue will have some thoughts. If you can you may want to raise the question in the forums. Thanks for your help in looking at this issue. Peace - Anthony

Wen Hao Chuang added a comment - 17/Oct/09 05:23 AM - edited
Seems like this sorting issue is still happening in the latest 1.9.5+ codes (as of 10/16/2009)?

Anthony Borrow added a comment - 17/Oct/09 09:41 AM
Wen - I want to focus the intent of this particular tracker issue. It is focused on the sorting of potential group members when assigning them to a group. Currently, it is sorted by lastname only which is the behavior you are finding problematic. This can easily be fixed by changing /group/lib.php around line 398 to:

function groups_get_users_not_in_group_by_role($courseid, $groupid, $searchtext='', $sort = 'u.lastname ASC, u.firstname ASC') {

I think we need to decide, and perhaps you can encourage some discussion on this as to the behavior desired for sorting. I had initially created the issue because I wanted to display the student name as firstname lastname; however, I wanted an option to sort them by lastname firstname or possibly even username. When I created the issue username was more important to me than it is now. So I think we have a number of possible areas for improvement here.

1) simply add firstname to the sort list so that we get an accurate sorting (step 1)

2) have the sort order follow whatever is set by $CFG->fullnamedisplay (keeping in mind that there are four options and that if the firstname display name is chosen that we may want to sort by firstname lastname - I have mixed feelings about how to deal with the question of firstname only and would need to explore further what happens when using the language locale order). Currently, the sort is always lastname and this should probably be flexible.

3) Even with some flexibility by using $CFG->fullnamedisplay, I think that it would be helpful to have a separte variable for a default sorting order. Perhaps something like $CFG->usersortdefault which could be set to be fullname, lastname/firstname, firstname/lastname, user id, idnumber, etc. Then names could be displayed one way and sorted another and this would be configurable by the site admin.

Let me know if this plan of attack makes sense and then we can move on to role assignments and other places were the sort order is not ideal. Peace - Anthony


Anthony Borrow added a comment - 17/Oct/09 09:43 AM
Martin - Given the number of votes on this and the relative ease of which at least step one could be taken, it might be good to get this in 1.9.6. Just say the word and I will happily add the firstname to the sort order so that it is lastname ASC, firstname ASC Peace - Anthony

Anthony Borrow added a comment - 17/Oct/09 11:21 AM
Here is a patch file that implements what I referred to as step 2 which would use the $CFG->fullnamedisplay value to determine how the names are sorted. If displayed lastname, firstname then that is how they will be sorted. Peace - Anthony

Martin Dougiamas added a comment - 19/Oct/09 02:40 PM
The logic looks good, however, doing it inside the function like that means that the parameter is now completely ignored.

I would set the default $sort value for the parameter to "", and then only run your new logic if empty($sort).

Finally, if $CFG->fullnamedisplay was something other than (lastname firstname) or (lastname firstname) then I would explicitly set $sort = 'u.lastname ASC, u.firstname ASC' at that point.

That could squeeze into 1.9.6

Cheers and thanks!


Anthony Borrow added a comment - 19/Oct/09 03:10 PM
MD - thanks for the catch on overriding $sort. Here is a revised patch setting default $sort to '' and checking if empty before beginning.

$CFG->fullnamedisplay can have four possible (valid) values:
firstname lastname
lastname firstname
firstname
language

The fullnamedisplay language string should be either:
firstname lastname
lastname firstname
firstname

I opted to make firstname ASC, lastname ASC the default to avoid some additional checks and thus lumping firstname lastname and firstname together. Then we only need to check if it is lastname. The down side to this is that if someone were to put in a bogus string then the function will default to firstname ASC, lastname ASC but that seems relatively trivial.

I was not 100% clear on your last comment about "if $CFG->fullnamedisplay was something other than" but I think we have all the cases reasonably covered.

Peace - Anthony


Anthony Borrow added a comment - 19/Oct/09 03:33 PM
Testing revealed that I had not paid attention to how the language string is setup:

$string['fullnamedisplay'] = '$a->firstname $a->lastname';

so here is a revised patch that follows the logic used in /admin/user.php although I will admit it seems unnecessarily complex but perhaps there was some hope the string could be used in different ways but we can save evaluating that one for another day.

Peace - Anthony


Martin Dougiamas added a comment - 19/Oct/09 03:57 PM - edited
Busy! The last thing was just for this case: If $sort starts out empty and $CFG->fullnamedisplay is empty then $sort needs to get a value.

Something like:

if (empty($sort)) {    // Determine a default from system settings
        $sort = 'u.firstname ASC, u.lastname ASC';
        if (!empty($CFG->fullnamedisplay) ) { 
            if ($CFG->fullnamedisplay == 'lastname firstname') {
                 $sort = 'u.lastname ASC, u.firstname ASC';
            } else if ($CFG->fullnamedisplay == 'language') {
                $override = new object();
                $override->firstname = 'firstname';
                $override->lastname = 'lastname';
                $fullnamelanguage = get_string('fullnamedisplay', '', $override);
                if ($fullnamelanguage == 'lastname firstname' ) {  
                    $sort = 'u.lastname ASC, u.firstname ASC';
                }
            }
        }
    }

My only slight worry is: will this make sense in 1.9.6?

a) It flips the current default sort in some cases.
b) Does it make sense to everyone to always sort by the first word first? (Just asking)


Anthony Borrow added a comment - 19/Oct/09 10:56 PM
OK Regis, this is my final answer

If $sort and $CFG->fullnamedisplay are both empty then the default is lastname firstname; however, I fear this would be a challenging state to get in as someone would have to work at this by either deleting the value in the config database or setting it to blank in config.

a) It flips the current default sort in some cases.

Since the default fullnamedisplay (from Security->Site policies) is 'firstname lastname' the new default behavior would be to sort by firstname lastname which represents a change in 1.9 behavior. I would still consider it an improvement because with the patch the site admin has the option to control the desired behavior. As the code is now, the list is always sorted 'lastname firstname' unless specified by the code (with $sort) and thus effectively ignores the fullnamedisplay setting. My argument for including the patch in 1.9.6 is that this is the behavior specific in the documentation (http://docs.moodle.org/en/Groups_FAQ#How_can_I_change_the_sort_order_of_the_participants.3F) which I assume is what folks expect to happen. That said, I do not like to see the behavior changed in a stable release. What we may want to do is simply leave the patch here for those who want it for 1.9, apply the patch to HEAD, and update Moodle Docs such that for 1.9 there is a reference to the patch and an explanation about the change of (improved) behavior in 2.0.

b) Does it make sense to everyone to always sort by the first word first? (Just asking)

Good question as that is essentially what initially prompted me to create the issue. I wanted the users sorted by lastname firstname but displayed firstname lastname. This would require a second variable like fullnamesort. In the process of looking at things, I noticed the difference in what was in Moodle Docs and what was actually happening.

Let me know if you want a patch (2.0 and/or 1.9) for what I described above (comment dated 17/Oct/09 09:41 AM) as the third step toward improvement. This brings us back to the first step which may want to do for 1.9.6 is simply to add 'firstname ASC' to the default $sort so that it sorts first on lastname and secondarily on firstname displaying (but not sorting) as determined by $CFG->fullnamedisplay.

Peace - Anthony