Skip to content

Commit

Permalink
Merge branch 'MDL-72193' of https://github.com/paulholden/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta authored and sarjona committed Jun 10, 2022
2 parents 90f2c3f + 1442257 commit 545d848
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
18 changes: 14 additions & 4 deletions enrol/manual/locallib.php
Expand Up @@ -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);
}

Expand All @@ -46,19 +47,23 @@ public function __construct($name, $options) {
*/
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()) {
Expand Down Expand Up @@ -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);
}

Expand All @@ -111,18 +117,22 @@ public function __construct($name, $options) {
*/
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()) {
Expand Down
51 changes: 42 additions & 9 deletions enrol/manual/tests/behat/manage.feature
Expand Up @@ -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 |
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)"

0 comments on commit 545d848

Please sign in to comment.