MDL-67245 group: display user identity fields for group members.

This commit is contained in:
Paul Holden 2019-11-29 07:45:55 +00:00
parent 77d1c41502
commit 21253ed622
6 changed files with 106 additions and 30 deletions

View File

@ -99,8 +99,9 @@ if ($editform->is_cancelled()) {
// Display only active users if the option was selected or they do not have the capability to view suspended users.
$onlyactive = !empty($data->includeonlyactiveenrol) || !has_capability('moodle/course:viewsuspendedusers', $context);
$extrafields = get_extra_user_fields($context);
$users = groups_get_potential_members($data->courseid, $data->roleid, $source, $orderby, !empty($data->notingroup),
$onlyactive);
$onlyactive, $extrafields);
$usercnt = count($users);
if ($data->allocateby == 'random') {
@ -183,7 +184,16 @@ if ($editform->is_cancelled()) {
if ($data->allocateby != 'no') {
$unames = array();
foreach ($group['members'] as $user) {
$unames[] = fullname($user, true);
$fullname = fullname($user, true);
if ($extrafields) {
$extrafieldsdisplay = [];
foreach ($extrafields as $field) {
$extrafieldsdisplay[] = s($user->{$field});
}
$fullname .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
}
$unames[] = $fullname;
}
$line[] = implode(', ', $unames);
$line[] = count($group['members']);

View File

@ -80,7 +80,11 @@ switch ($action) {
case 'ajax_getmembersingroup':
$roles = array();
if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid, 'u.id, ' . get_all_user_name_fields(true, 'u'))) {
$extrafields = get_extra_user_fields($context);
if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid,
'u.id, ' . user_picture::fields('u', $extrafields))) {
foreach($groupmemberroles as $roleid=>$roledata) {
$shortroledata = new stdClass();
$shortroledata->name = $roledata->name;
@ -89,6 +93,14 @@ switch ($action) {
$shortmember = new stdClass();
$shortmember->id = $member->id;
$shortmember->name = fullname($member, true);
if ($extrafields) {
$extrafieldsdisplay = [];
foreach ($extrafields as $field) {
$extrafieldsdisplay[] = s($member->{$field});
}
$shortmember->name .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
}
$shortroledata->users[] = $shortmember;
}
$roles[] = $shortroledata;
@ -188,15 +200,25 @@ if ($groups) {
// Get list of group members to render if there is a single selected group.
$members = array();
if ($singlegroup) {
$usernamefields = get_all_user_name_fields(true, 'u');
if ($groupmemberroles = groups_get_members_by_role(reset($groupids), $courseid, 'u.id, ' . $usernamefields)) {
$extrafields = get_extra_user_fields($context);
if ($groupmemberroles = groups_get_members_by_role(reset($groupids), $courseid,
'u.id, ' . user_picture::fields('u', $extrafields))) {
foreach ($groupmemberroles as $roleid => $roledata) {
$users = array();
foreach ($roledata->users as $member) {
$users[] = (object)[
'value' => $member->id,
'text' => fullname($member, true)
];
$shortmember = new stdClass();
$shortmember->value = $member->id;
$shortmember->text = fullname($member, true);
if ($extrafields) {
$extrafieldsdisplay = [];
foreach ($extrafields as $field) {
$extrafieldsdisplay[] = s($member->{$field});
}
$shortmember->text .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
}
$users[] = $shortmember;
}
$members[] = (object)[
'role' => s($roledata->name),

View File

@ -785,11 +785,12 @@ function groups_get_possible_roles($context) {
* @param string $orderby The column to sort users by
* @param int $notingroup restrict to users not in existing groups
* @param bool $onlyactiveenrolments restrict to users who have an active enrolment in the course
* @param array $extrafields Extra user fields to return
* @return array An array of the users
*/
function groups_get_potential_members($courseid, $roleid = null, $source = null,
$orderby = 'lastname ASC, firstname ASC',
$notingroup = null, $onlyactiveenrolments = false) {
$notingroup = null, $onlyactiveenrolments = false, $extrafields = []) {
global $DB;
$context = context_course::instance($courseid);
@ -847,7 +848,7 @@ function groups_get_potential_members($courseid, $roleid = null, $source = null,
}
}
$allusernamefields = get_all_user_name_fields(true, 'u');
$allusernamefields = user_picture::fields('u', $extrafields);
$sql = "SELECT DISTINCT u.id, u.username, $allusernamefields, u.idnumber
FROM {user} u
JOIN ($esql) e ON e.id = u.id

View File

@ -110,7 +110,9 @@ if ($groupingid) {
list($sort, $sortparams) = users_order_by_sql('u');
$allnames = get_all_user_name_fields(true, 'u');
$extrafields = get_extra_user_fields($context);
$allnames = 'u.id, ' . user_picture::fields('u', $extrafields);
$sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, $allnames, u.idnumber, u.username
FROM {groups} g
LEFT JOIN {groupings_groups} gg ON g.id = gg.groupid
@ -121,8 +123,9 @@ $sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, $allnames, u.idnu
$rs = $DB->get_recordset_sql($sql, array_merge($params, $sortparams));
foreach ($rs as $row) {
$user = new stdClass();
$user = username_load_fields_from_object($user, $row, null, array('id' => 'userid', 'username', 'idnumber'));
$user = username_load_fields_from_object((object) [], $row, null,
array_merge(['id' => 'userid', 'username', 'idnumber'], $extrafields));
if (!$row->groupingid) {
$row->groupingid = OVERVIEW_GROUPING_GROUP_NO_GROUPING;
}
@ -250,7 +253,17 @@ foreach ($members as $gpgid=>$groupdata) {
}
$fullnames = array();
foreach ($users as $user) {
$fullnames[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.fullname($user, true).'</a>';
$displayname = fullname($user, true);
if ($extrafields) {
$extrafieldsdisplay = [];
foreach ($extrafields as $field) {
$extrafieldsdisplay[] = s($user->{$field});
}
$displayname .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
}
$fullnames[] = html_writer::link(new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $course->id]),
$displayname);
}
$line[] = implode(', ', $fullnames);
$line[] = count($users);

View File

@ -80,15 +80,13 @@ Feature: Automatic creation of groups
| Group/member count | 4 |
| Grouping of auto-created groups | New grouping |
| Grouping name | Grouping name |
| Allocate members | Alphabetically by last name, first name |
And I press "Preview"
Then I should see "Group members"
And I should see "User count"
And I should see "Group A" in the ".generaltable" "css_element"
And I should see "Group B" in the ".generaltable" "css_element"
And I should see "Group C" in the ".generaltable" "css_element"
And I should see "4" in the "Group A" "table_row"
And I should see "4" in the "Group B" "table_row"
And I should see "2" in the "Group C" "table_row"
Then the following should exist in the "generaltable" table:
| Groups (3) | Group members | User count (10) |
| Group A | Student 1 (student1@example.com) | 4 |
| Group B | Student 5 (student5@example.com) | 4 |
| Group C | Student 9 (student9@example.com) | 2 |
And I set the field "Prevent last small group" to "1"
And I press "Preview"
And I should see "Group A" in the ".generaltable" "css_element"
@ -163,7 +161,7 @@ Feature: Automatic creation of groups
And I set the field "Auto create based on" to "Members per group"
When I set the field "Group/member count" to "11"
And I press "Preview"
Then I should see "Suspended student 11"
Then I should see "Suspended student 11 (suspendedstudent11@example.com)"
Scenario: Do not display 'Include only active enrolments' if user does not have the 'moodle/course:viewsuspendedusers' capability
Given I log out

View File

@ -39,13 +39,15 @@ Feature: Organize students into groups
And I add "Student 2 (student2@example.com)" user to "Group 2" group members
And I add "Student 3 (student3@example.com)" user to "Group 2" group members
Then I set the field "groups" to "Group 1 (2)"
And the "members" select box should contain "Student 0"
And the "members" select box should contain "Student 1"
And the "members" select box should not contain "Student 2"
And the "members" select box should contain "Student 0 (student0@example.com)"
And the "members" select box should contain "Student 1 (student1@example.com)"
And the "members" select box should not contain "Student 2 (student2@example.com)"
And the "members" select box should not contain "Student 3 (student3@example.com)"
And I set the field "groups" to "Group 2 (2)"
And the "members" select box should contain "Student 2"
And the "members" select box should contain "Student 3"
And the "members" select box should not contain "Student 0"
And the "members" select box should contain "Student 2 (student2@example.com)"
And the "members" select box should contain "Student 3 (student3@example.com)"
And the "members" select box should not contain "Student 0 (student0@example.com)"
And the "members" select box should not contain "Student 1 (student1@example.com)"
And I navigate to course participants
And I open the autocomplete suggestions list
And I click on "Group: Group 1" item in the autocomplete list
@ -59,6 +61,36 @@ Feature: Organize students into groups
And I should see "Student 3"
And I should not see "Student 0"
@javascript
Scenario: Assign students to groups with site user identity configured
Given the following "courses" exist:
| fullname | shortname | groupmode |
| Course 1 | C1 | 1 |
And the following "users" exist:
| username | firstname | lastname | email | country |
| teacher | Teacher | 1 | teacher@example.com | GB |
| student | Student | 1 | student@example.com | DE |
And the following "course enrolments" exist:
| user | course | role |
| teacher | C1 | editingteacher |
| student | C1 | student |
And the following config values are set as admin:
| showuseridentity | email,country |
And I log in as "teacher"
And I am on "Course 1" course homepage
And I navigate to "Users > Groups" in current page administration
And I press "Create group"
And I set the following fields to these values:
| Group name | Group 1 |
And I press "Save changes"
When I add "Student 1 (student@example.com, DE)" user to "Group 1" group members
And I set the field "groups" to "Group 1 (1)"
Then the "members" select box should contain "Student 1 (student@example.com\, DE)"
# Non-AJAX version of the groups page.
And I press "Add/remove users"
And I press "Back to groups"
And the "members" select box should contain "Student 1 (student@example.com\, DE)"
Scenario: Create groups and groupings without the 'moodle/course:changeidnumber' capability
Given the following "courses" exist:
| fullname | shortname | category | groupmode |