Skip to content

Commit

Permalink
MDL-84149 gradereport_grader: better support for collapse preferences.
Browse files Browse the repository at this point in the history
The privacy provider is now resilient against missing courses, and also
supports retrieving preferences from multiple courses for the user.
  • Loading branch information
paulholden committed Jan 9, 2025
1 parent a843eab commit aad978c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
22 changes: 13 additions & 9 deletions grade/report/grader/classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,21 @@ public static function export_user_preferences(int $userid) {
break;
default:
if (strpos($name, 'grade_report_grader_collapsed_categories') === 0) {
$prefname = 'grade_report_grader_collapsed_categories';
$prefname = $name;
$courseid = substr($name, strlen('grade_report_grader_collapsed_categories'));
$transformedvalue = $value;
$course = get_course($courseid);
$prefdescription = get_string(
'privacy:request:preference:'.$prefname,
'gradereport_grader',
(object) [
'name' => $course->fullname,
]
);
try {
$course = get_course($courseid);
$prefdescription = get_string(
'privacy:request:preference:grade_report_grader_collapsed_categories',
'gradereport_grader',
(object) [
'name' => $course->fullname,
]
);
} catch (\dml_missing_record_exception $exception) {
continue 2;
}
}
}

Expand Down
36 changes: 29 additions & 7 deletions grade/report/grader/tests/privacy/provider_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,22 @@ public function test_export_user_preferences_single(): void {
* Ensure that export_user_preferences returns preferences.
*/
public function test_export_user_preferences_multiple(): void {
// Create a course and add a user preference.
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$course = $this->getDataGenerator()->create_course();
$collapsed = serialize(['aggregatesonly' => array(), 'gradesonly' => array()]);
set_user_preference('grade_report_grader_collapsed_categories'.$course->id, $collapsed, $user);

// Set preference for an invalid course.
set_user_preference('grade_report_grader_collapsed_categories42', serialize(['gradesonly' => []]));

// Set preferences for a couple of valid courses.
$courseone = $this->getDataGenerator()->create_course();
$courseonepreferencename = "grade_report_grader_collapsed_categories{$courseone->id}";
$courseonepreferencevalue = serialize(['aggregatesonly' => [], 'gradesonly' => []]);
set_user_preference($courseonepreferencename, $courseonepreferencevalue);

$coursetwo = $this->getDataGenerator()->create_course();
$coursetwopreferencename = "grade_report_grader_collapsed_categories{$coursetwo->id}";
$coursetwopreferencevalue = serialize(['gradesonly' => [], 'aggregatesonly' => []]);
set_user_preference($coursetwopreferencename, $coursetwopreferencevalue);

// Validate exported data.
provider::export_user_preferences($user->id);
Expand All @@ -104,10 +114,22 @@ public function test_export_user_preferences_multiple(): void {
$writer = writer::with_context($context);
$this->assertTrue($writer->has_any_data());
$prefs = $writer->get_user_preferences('gradereport_grader');
$this->assertCount(1, (array) $prefs);
$this->assertCount(2, (array) $prefs);

$this->assertEquals($courseonepreferencevalue, $prefs->{$courseonepreferencename}->value);
$this->assertEquals(
get_string('privacy:request:preference:grade_report_grader_collapsed_categories', 'gradereport_grader', [
'name' => $courseone->fullname,
]),
$prefs->{$courseonepreferencename}->description,
);

$this->assertEquals($coursetwopreferencevalue, $prefs->{$coursetwopreferencename}->value);
$this->assertEquals(
get_string('privacy:request:preference:grade_report_grader_collapsed_categories', 'gradereport_grader', ['name' => $course->fullname]),
$prefs->grade_report_grader_collapsed_categories->description
get_string('privacy:request:preference:grade_report_grader_collapsed_categories', 'gradereport_grader', [
'name' => $coursetwo->fullname,
]),
$prefs->{$coursetwopreferencename}->description,
);
}
}

0 comments on commit aad978c

Please sign in to comment.