MDL-66269 enrol: added search_users method to course_enrolment_manager
Part of MDL-66075.
This commit is contained in:
committed by
Simey Lameze
parent
c3ac38129d
commit
151ef39cd8
@@ -531,6 +531,35 @@ class course_enrolment_manager {
|
||||
return $this->execute_search_queries($search, $fields, $countfields, $sql, $params, $page, $perpage, 0, $returnexactcount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches through the enrolled users in this course.
|
||||
*
|
||||
* @param string $search The search term.
|
||||
* @param bool $searchanywhere Can the search term be anywhere, or must it be at the start.
|
||||
* @param int $page Starting at 0.
|
||||
* @param int $perpage Number of users returned per page.
|
||||
* @param bool $returnexactcount Return the exact total users using count_record or not.
|
||||
* @return array with two or three elements:
|
||||
* int totalusers Number users matching the search. (This element only exist if $returnexactcount was set to true)
|
||||
* array users List of user objects returned by the query.
|
||||
* boolean moreusers True if there are still more users, otherwise is False.
|
||||
*/
|
||||
public function search_users(string $search = '', bool $searchanywhere = false, int $page = 0, int $perpage = 25,
|
||||
bool $returnexactcount = false) {
|
||||
list($ufields, $params, $wherecondition) = $this->get_basic_search_conditions($search, $searchanywhere);
|
||||
|
||||
$fields = 'SELECT ' . $ufields;
|
||||
$countfields = 'SELECT COUNT(u.id)';
|
||||
$sql = " FROM {user} u
|
||||
JOIN {user_enrolments} ue ON ue.userid = u.id
|
||||
JOIN {enrol} e ON ue.enrolid = e.id
|
||||
WHERE $wherecondition
|
||||
AND e.courseid = :courseid";
|
||||
$params['courseid'] = $this->course->id;
|
||||
|
||||
return $this->execute_search_queries($search, $fields, $countfields, $sql, $params, $page, $perpage, 0, $returnexactcount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an array containing some SQL to user for when selecting, params for
|
||||
* that SQL, and the filter that was used in constructing the sql.
|
||||
|
||||
@@ -325,7 +325,7 @@ class core_course_enrolment_manager_testcase extends advanced_testcase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case for test_get_potential_users and test_search_other_users tests.
|
||||
* Test case for test_get_potential_users, test_search_other_users and test_search_users tests.
|
||||
*
|
||||
* @return array Dataset
|
||||
*/
|
||||
@@ -337,4 +337,41 @@ class core_course_enrolment_manager_testcase extends advanced_testcase {
|
||||
[5, true, 3, 3, false]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test search_users function.
|
||||
*
|
||||
* @dataProvider search_users_provider
|
||||
*
|
||||
* @param int $perpage Number of users per page.
|
||||
* @param bool $returnexactcount Return the exact count or not.
|
||||
* @param int $expectedusers Expected number of users return.
|
||||
* @param int $expectedtotalusers Expected total of users in database.
|
||||
* @param bool $expectedmoreusers Expected for more users return or not.
|
||||
*/
|
||||
public function test_search_users($perpage, $returnexactcount, $expectedusers, $expectedtotalusers, $expectedmoreusers) {
|
||||
global $PAGE;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->getDataGenerator()->create_and_enrol($this->course, 'student', ['firstname' => 'sutest 1']);
|
||||
$this->getDataGenerator()->create_and_enrol($this->course, 'student', ['firstname' => 'sutest 2']);
|
||||
$this->getDataGenerator()->create_and_enrol($this->course, 'student', ['firstname' => 'sutest 3']);
|
||||
|
||||
$manager = new course_enrolment_manager($PAGE, $this->course);
|
||||
$users = $manager->search_users(
|
||||
'sutest',
|
||||
true,
|
||||
0,
|
||||
$perpage,
|
||||
$returnexactcount);
|
||||
|
||||
$this->assertCount($expectedusers, $users['users']);
|
||||
$this->assertEquals($expectedmoreusers, $users['moreusers']);
|
||||
if ($returnexactcount) {
|
||||
$this->assertArrayHasKey('totalusers', $users);
|
||||
$this->assertEquals($expectedtotalusers, $users['totalusers']);
|
||||
} else {
|
||||
$this->assertArrayNotHasKey('totalusers', $users);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user