Skip to content

Commit

Permalink
MDL-78030 user: Implementing the count window function
Browse files Browse the repository at this point in the history
  • Loading branch information
meirzamoodle committed Jul 29, 2024
1 parent 42664ee commit 9354b80
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 27 deletions.
7 changes: 7 additions & 0 deletions .upgradenotes/MDL-78030-2024071101575428.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
issueNumber: MDL-78030
notes:
core_user:
- message: >-
The participants_search::get_total_participants_count() is no longer
used since the total count can be obtained from ::get_participants()
type: deprecated
12 changes: 5 additions & 7 deletions user/classes/table/participants.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,19 +404,17 @@ public function other_cols($colname, $data) {
* @param bool $useinitialsbar do you want to use the initials bar.
*/
public function query_db($pagesize, $useinitialsbar = true) {
global $DB;

list($twhere, $tparams) = $this->get_sql_where();
$psearch = new participants_search($this->course, $this->context, $this->filterset);

$total = $psearch->get_total_participants_count($twhere, $tparams);

$this->pagesize($pagesize, $total);

$sort = $this->get_sql_sort();
if ($sort) {
$sort = 'ORDER BY ' . $sort;
}

$this->use_pages = true;
$rawdata = $psearch->get_participants($twhere, $tparams, $sort, $this->get_page_start(), $this->get_page_size());
$total = $rawdata->current()->fullcount ?? 0;
$this->pagesize($pagesize, $total);

$this->rawdata = [];
foreach ($rawdata as $user) {
Expand Down
37 changes: 27 additions & 10 deletions user/classes/table/participants_search.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,43 @@ public function get_participants(string $additionalwhere = '', array $additional
'params' => $params,
] = $this->get_participants_sql($additionalwhere, $additionalparams);

$sql = "{$outerselect}
FROM ({$innerselect}
FROM {$innerjoins}
{$innerwhere}
) {$subqueryalias}
{$outerjoins}
{$outerwhere}
{$sort}";

return $DB->get_recordset_sql($sql, $params, $limitfrom, $limitnum);
$select = "{$outerselect}
FROM ({$innerselect}
FROM {$innerjoins}
{$innerwhere}
) {$subqueryalias}
{$outerjoins}
{$outerwhere}";
return $DB->get_counted_recordset_sql(
sql: $select,
fullcountcolumn: 'fullcount',
sort: $sort,
params: $params,
limitfrom: $limitfrom,
limitnum: $limitnum,
);
}

/**
* Returns the total number of participants for a given course.
*
* @deprecated Moodle 4.5 MDL-78030 - No longer used since the total count can be obtained from {@see ::get_participants()}.
* @todo Final deprecation on Moodle 6.0 MDL-82441.
*
* @param string $additionalwhere Any additional SQL to add to where.
* @param array $additionalparams The additional params used by $additionalwhere.
* @return int
*/
#[\core\attribute\deprecated(
'participants_search::get_participants()',
since: '4.5',
mdl: 'MDL-78030',
reason: 'No longer used since the total count can be obtained from {@see ::get_participants()}',
)]
public function get_total_participants_count(string $additionalwhere = '', array $additionalparams = []): int {

\core\deprecation::emit_deprecation_if_present([$this, __FUNCTION__]);

global $DB;

[
Expand Down
37 changes: 27 additions & 10 deletions user/tests/table/participants_search_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,11 @@ public function test_roles_filter(array $usersdata, array $testroles, int $joint
$rs = $search->get_participants();
$this->assertInstanceOf(moodle_recordset::class, $rs);
$records = $this->convert_recordset_to_array($rs);
$resetrecords = reset($records);
$totalparticipants = $resetrecords->fullcount ?? 0;

$this->assertCount($count, $records);
$this->assertEquals($count, $search->get_total_participants_count());
$this->assertEquals($count, $totalparticipants);

foreach ($expectedusers as $expecteduser) {
$this->assertArrayHasKey($users[$expecteduser]->id, $records);
Expand Down Expand Up @@ -779,9 +781,10 @@ public function test_country_filter(array $usersdata, array $countries, int $joi

// Run the search, assert count matches the number of expected users.
$search = new participants_search($course, context_course::instance($course->id), $filterset);
$this->assertEquals(count($expectedusers), $search->get_total_participants_count());

$rs = $search->get_participants();
$totalparticipants = $rs->current()->fullcount ?? 0;
$this->assertEquals(count($expectedusers), $totalparticipants);

$this->assertInstanceOf(moodle_recordset::class, $rs);

// Assert that each expected user is within the participant records.
Expand Down Expand Up @@ -977,9 +980,11 @@ public function test_keywords_filter(array $usersdata, array $keywords, int $joi
$rs = $search->get_participants();
$this->assertInstanceOf(moodle_recordset::class, $rs);
$records = $this->convert_recordset_to_array($rs);
$resetrecords = reset($records);
$totalparticipants = $resetrecords->fullcount ?? 0;

$this->assertCount($count, $records);
$this->assertEquals($count, $search->get_total_participants_count());
$this->assertEquals($count, $totalparticipants);

foreach ($expectedusers as $expecteduser) {
$this->assertArrayHasKey($users[$expecteduser]->id, $records);
Expand Down Expand Up @@ -1523,9 +1528,11 @@ public function test_status_filter(array $usersdata, array $statuses, int $joint
$rs = $search->get_participants();
$this->assertInstanceOf(moodle_recordset::class, $rs);
$records = $this->convert_recordset_to_array($rs);
$resetrecords = reset($records);
$totalparticipants = $resetrecords->fullcount ?? 0;

$this->assertCount($count, $records);
$this->assertEquals($count, $search->get_total_participants_count());
$this->assertEquals($count, $totalparticipants);

foreach ($expectedusers as $expecteduser) {
$this->assertArrayHasKey($users[$expecteduser]->id, $records);
Expand Down Expand Up @@ -1778,9 +1785,11 @@ public function test_enrolments_filter(array $usersdata, array $enrolmethods, in
$rs = $search->get_participants();
$this->assertInstanceOf(moodle_recordset::class, $rs);
$records = $this->convert_recordset_to_array($rs);
$resetrecords = reset($records);
$totalparticipants = $resetrecords->fullcount ?? 0;

$this->assertCount($count, $records);
$this->assertEquals($count, $search->get_total_participants_count());
$this->assertEquals($count, $totalparticipants);

foreach ($expectedusers as $expecteduser) {
$this->assertArrayHasKey($users[$expecteduser]->id, $records);
Expand Down Expand Up @@ -2002,9 +2011,11 @@ public function test_groups_filter(array $usersdata, array $groupsavailable, arr
$rs = $search->get_participants();
$this->assertInstanceOf(moodle_recordset::class, $rs);
$records = $this->convert_recordset_to_array($rs);
$resetrecords = reset($records);
$totalparticipants = $resetrecords->fullcount ?? 0;

$this->assertCount($count, $records);
$this->assertEquals($count, $search->get_total_participants_count());
$this->assertEquals($count, $totalparticipants);

foreach ($expectedusers as $expecteduser) {
$this->assertArrayHasKey($users[$expecteduser]->id, $records);
Expand Down Expand Up @@ -2340,9 +2351,11 @@ public function test_groups_filter_separate_groups(array $usersdata, array $grou
$rs = $search->get_participants();
$this->assertInstanceOf(moodle_recordset::class, $rs);
$records = $this->convert_recordset_to_array($rs);
$resetrecords = reset($records);
$totalparticipants = $resetrecords->fullcount ?? 0;

$this->assertCount($count, $records);
$this->assertEquals($count, $search->get_total_participants_count());
$this->assertEquals($count, $totalparticipants);

foreach ($expectedusers as $expecteduser) {
$this->assertArrayHasKey($users[$expecteduser]->id, $records);
Expand Down Expand Up @@ -2701,9 +2714,11 @@ public function test_accesssince_filter(array $usersdata, array $accesssince, in
$rs = $search->get_participants();
$this->assertInstanceOf(moodle_recordset::class, $rs);
$records = $this->convert_recordset_to_array($rs);
$resetrecords = reset($records);
$totalparticipants = $resetrecords->fullcount ?? 0;

$this->assertCount($count, $records);
$this->assertEquals($count, $search->get_total_participants_count());
$this->assertEquals($count, $totalparticipants);

foreach ($expectedusers as $expecteduser) {
$this->assertArrayHasKey($users[$expecteduser]->id, $records);
Expand Down Expand Up @@ -3106,9 +3121,11 @@ public function test_filterset_joins(array $usersdata, array $filterdata, array
$rs = $search->get_participants();
$this->assertInstanceOf(moodle_recordset::class, $rs);
$records = $this->convert_recordset_to_array($rs);
$resetrecords = reset($records);
$totalparticipants = $resetrecords->fullcount ?? 0;

$this->assertCount($count, $records);
$this->assertEquals($count, $search->get_total_participants_count());
$this->assertEquals($count, $totalparticipants);

foreach ($expectedusers as $expecteduser) {
$this->assertArrayHasKey($users[$expecteduser]->id, $records);
Expand Down

0 comments on commit 9354b80

Please sign in to comment.