MDL-72193 enrol_manual: support custom user profile fields.

This commit is contained in:
Paul Holden 2021-07-20 16:03:48 +01:00
parent 998c632b5d
commit 14422572b9
2 changed files with 56 additions and 13 deletions

View File

@ -36,6 +36,7 @@ class enrol_manual_potential_participant extends user_selector_base {
public function __construct($name, $options) {
$this->enrolid = $options['enrolid'];
$options['includecustomfields'] = true;
parent::__construct($name, $options);
}
@ -46,19 +47,23 @@ class enrol_manual_potential_participant extends user_selector_base {
*/
public function find_users($search) {
global $DB;
// By default wherecondition retrieves all users except the deleted, not confirmed and guest.
list($wherecondition, $params) = $this->search_sql($search, 'u');
$params = array_merge($params, $this->userfieldsparams);
$params['enrolid'] = $this->enrolid;
$fields = 'SELECT ' . $this->required_fields_sql('u');
$fields = 'SELECT u.id, ' . $this->userfieldsselects;
$countfields = 'SELECT COUNT(1)';
$sql = " FROM {user} u
LEFT JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid = :enrolid)
$this->userfieldsjoin
WHERE $wherecondition
AND ue.id IS NULL";
list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext, $this->userfieldsmappings);
$order = ' ORDER BY ' . $sort;
if (!$this->is_validating()) {
@ -101,6 +106,7 @@ class enrol_manual_current_participant extends user_selector_base {
public function __construct($name, $options) {
$this->enrolid = $options['enrolid'];
$options['includecustomfields'] = true;
parent::__construct($name, $options);
}
@ -111,18 +117,22 @@ class enrol_manual_current_participant extends user_selector_base {
*/
public function find_users($search) {
global $DB;
// By default wherecondition retrieves all users except the deleted, not confirmed and guest.
list($wherecondition, $params) = $this->search_sql($search, 'u');
$params = array_merge($params, $this->userfieldsparams);
$params['enrolid'] = $this->enrolid;
$fields = 'SELECT ' . $this->required_fields_sql('u');
$fields = 'SELECT u.id, ' . $this->userfieldsselects;
$countfields = 'SELECT COUNT(1)';
$sql = " FROM {user} u
JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid = :enrolid)
$this->userfieldsjoin
WHERE $wherecondition";
list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext, $this->userfieldsmappings);
$order = ' ORDER BY ' . $sort;
if (!$this->is_validating()) {

View File

@ -5,11 +5,14 @@ Feature: A teacher can manage manually enrolled users in their course
I can manually add and remove users in my course
Background:
Given the following "users" exist:
| username | firstname | middlename | lastname | email |
| teacher | Teacher | | User | teacher@example.com |
| user1 | First | Alice | User | first@example.com |
| user2 | Second | Bob | User | second@example.com |
Given the following "custom profile fields" exist:
| datatype | shortname | name |
| text | fruit | Fruit |
And the following "users" exist:
| username | firstname | middlename | lastname | email | profile_field_fruit |
| teacher | Teacher | | User | teacher@example.com | |
| user1 | First | Alice | User | first@example.com | Apple |
| user2 | Second | Bob | User | second@example.com | Banana |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
@ -24,8 +27,7 @@ Feature: A teacher can manage manually enrolled users in their course
And the following "permission overrides" exist:
| capability | permission | role | contextlevel | reference |
| moodle/site:viewfullnames | <permission> | editingteacher | Course | C1 |
When I log in as "teacher"
And I am on the "Course 1" "enrolment methods" page
When I am on the "Course 1" "enrolment methods" page logged in as "teacher"
And I click on "Enrol users" "link" in the "Manual enrolments" "table_row"
And I set the field "addselect_searchtext" to "First"
And I wait "1" seconds
@ -48,8 +50,7 @@ Feature: A teacher can manage manually enrolled users in their course
| user | course | role |
| user1 | C1 | student |
| user2 | C1 | student |
When I log in as "teacher"
And I am on the "Course 1" "enrolment methods" page
When I am on the "Course 1" "enrolment methods" page logged in as "teacher"
And I click on "Enrol users" "link" in the "Manual enrolments" "table_row"
And I set the field "removeselect_searchtext" to "First"
And I wait "1" seconds
@ -60,3 +61,35 @@ Feature: A teacher can manage manually enrolled users in their course
| permission | expectedfullname |
| Allow | First Alice User |
| Prohibit | First User |
@javascript
Scenario: Manually enrol users in course using custom user profile fields
Given the following config values are set as admin:
| showuseridentity | email,profile_field_fruit |
When I am on the "Course 1" "enrolment methods" page logged in as "teacher"
And I click on "Enrol users" "link" in the "Manual enrolments" "table_row"
Then the "Not enrolled users" select box should contain "Second User (second@example.com\, Banana)"
And I set the field "addselect_searchtext" to "Apple"
And I wait "1" seconds
And the "Not enrolled users" select box should not contain "Second User (second@example.com\, Banana)"
And I set the field "Not enrolled users" to "First User (first@example.com\, Apple)"
And I press "Add"
And the "Enrolled users" select box should contain "First User (first@example.com\, Apple)"
@javascript
Scenario: Manually unenrol users in course using custom user profile fields
Given the following config values are set as admin:
| showuseridentity | email,profile_field_fruit |
And the following "course enrolments" exist:
| user | course | role |
| user1 | C1 | student |
| user2 | C1 | student |
When I am on the "Course 1" "enrolment methods" page logged in as "teacher"
And I click on "Enrol users" "link" in the "Manual enrolments" "table_row"
Then the "Enrolled users" select box should contain "Second User (second@example.com\, Banana)"
And I set the field "removeselect_searchtext" to "Apple"
And I wait "1" seconds
And the "Enrolled users" select box should not contain "Second User (second@example.com\, Banana)"
And I set the field "Enrolled users" to "First User (first@example.com\, Apple)"
And I press "Remove"
And the "Not enrolled users" select box should contain "First User (first@example.com\, Apple)"