Skip to content

Commit

Permalink
MDL-75381 gradereport_grader: ensure valid paging preference value.
Browse files Browse the repository at this point in the history
Set type of the report paging setting to integer, to ensure usage
of it is predictable. Unsupported operated type errors were thrown
on PHP8.0 when it's value contained a string or was empty.
  • Loading branch information
paulholden committed Nov 29, 2022
1 parent 57c1e97 commit 33c1fb4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
4 changes: 2 additions & 2 deletions grade/report/grader/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1994,7 +1994,7 @@ public function get_sort_arrows(array $extrafields = array()) {
*
* @return int The maximum number of students to display per page
*/
public function get_students_per_page() {
return $this->get_pref('studentsperpage');
public function get_students_per_page(): int {
return (int) $this->get_pref('studentsperpage');
}
}
2 changes: 1 addition & 1 deletion grade/report/grader/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

/// Add settings for this module to the $settings object (it's already defined)
$settings->add(new admin_setting_configtext('grade_report_studentsperpage', get_string('studentsperpage', 'grades'),
get_string('studentsperpage_help', 'grades'), 100));
get_string('studentsperpage_help', 'grades'), 100, PARAM_INT));

$settings->add(new admin_setting_configcheckbox('grade_report_showonlyactiveenrol', get_string('showonlyactiveenrol', 'grades'),
get_string('showonlyactiveenrol_help', 'grades'), 1));
Expand Down
42 changes: 42 additions & 0 deletions grade/tests/report_graderlib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* Tests grade_report_grader (the grader report)
*
* @package core_grades
* @covers \grade_report_grader
* @category test
* @copyright 2012 Andrew Davis
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
Expand Down Expand Up @@ -518,6 +519,47 @@ public function test_get_right_rows() {
$this->assertCount(3, $result);
}

/**
* Test loading report users when per page preferences are set
*/
public function test_load_users_paging_preference(): void {
$this->resetAfterTest();

$course = $this->getDataGenerator()->create_course();

// The report users will default to sorting by their lastname.
$user1 = $this->getDataGenerator()->create_and_enrol($course, 'student', ['lastname' => 'Apple']);
$user2 = $this->getDataGenerator()->create_and_enrol($course, 'student', ['lastname' => 'Banana']);
$user3 = $this->getDataGenerator()->create_and_enrol($course, 'student', ['lastname' => 'Carrot']);

// Set to empty string.
$report = $this->create_report($course);
$report->set_pref('studentsperpage', '');
$users = $report->load_users();
$this->assertEquals([$user1->id, $user2->id, $user3->id], array_column($users, 'id'));

// Set to valid value.
$report = $this->create_report($course);
$report->set_pref('studentsperpage', 2);
$users = $report->load_users();
$this->assertEquals([$user1->id, $user2->id], array_column($users, 'id'));
}

/**
* Test getting students per page report preference
*/
public function test_get_students_per_page(): void {
$this->resetAfterTest();

$course = $this->getDataGenerator()->create_course();

$report = $this->create_report($course);
$report->set_pref('studentsperpage', 10);

$perpage = $report->get_students_per_page();
$this->assertSame(10, $perpage);
}

private function create_grade_category($course) {
static $cnt = 0;
$cnt++;
Expand Down

0 comments on commit 33c1fb4

Please sign in to comment.