MDL-61957 gradereport_grader: Implement privacy API

This commit is contained in:
Sara Arjona 2018-04-12 10:35:50 +02:00
parent 20bf0c45ff
commit 8a43e40247
3 changed files with 320 additions and 0 deletions

View File

@ -0,0 +1,187 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Privacy Subsystem implementation for gradereport_grader.
*
* @package gradereport_grader
* @copyright 2018 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace gradereport_grader\privacy;
defined('MOODLE_INTERNAL') || die();
use \core_privacy\local\metadata\collection;
use \core_privacy\local\request\transform;
use \core_privacy\local\request\writer;
require_once $CFG->libdir.'/grade/constants.php';
/**
* Privacy Subsystem for gradereport_grader implementing null_provider.
*
* @copyright 2018 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\user_preference_provider {
/**
* Returns meta data about this system.
*
* @param collection $itemcollection The initialised item collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $items) : collection {
// There are several user preferences (shared between different courses).
// Show/hide toggles preferences.
$items->add_user_preference('grade_report_showcalculations', 'privacy:metadata:preference:grade_report_showcalculations');
$items->add_user_preference('grade_report_showeyecons', 'privacy:metadata:preference:grade_report_showeyecons');
$items->add_user_preference('grade_report_showaverages', 'privacy:metadata:preference:grade_report_showaverages');
$items->add_user_preference('grade_report_showlocks', 'privacy:metadata:preference:grade_report_showlocks');
$items->add_user_preference('grade_report_showuserimage', 'privacy:metadata:preference:grade_report_showuserimage');
$items->add_user_preference('grade_report_showactivityicons', 'privacy:metadata:preference:grade_report_showactivityicons');
$items->add_user_preference('grade_report_showranges', 'privacy:metadata:preference:grade_report_showranges');
$items->add_user_preference('grade_report_showanalysisicon', 'privacy:metadata:preference:grade_report_showanalysisicon');
// Special rows preferences.
$items->add_user_preference('grade_report_rangesdisplaytype', 'privacy:metadata:preference:grade_report_rangesdisplaytype');
$items->add_user_preference('grade_report_rangesdecimalpoints', 'privacy:metadata:preference:grade_report_rangesdecimalpoints');
$items->add_user_preference('grade_report_averagesdisplaytype', 'privacy:metadata:preference:grade_report_averagesdisplaytype');
$items->add_user_preference('grade_report_averagesdecimalpoints', 'privacy:metadata:preference:grade_report_averagesdecimalpoints');
$items->add_user_preference('grade_report_meanselection', 'privacy:metadata:preference:grade_report_meanselection');
$items->add_user_preference('grade_report_shownumberofgrades', 'privacy:metadata:preference:grade_report_shownumberofgrades');
// General preferences.
$items->add_user_preference('grade_report_quickgrading', 'privacy:metadata:preference:grade_report_quickgrading');
$items->add_user_preference('grade_report_showquickfeedback', 'privacy:metadata:preference:grade_report_showquickfeedback');
$items->add_user_preference('grade_report_studentsperpage', 'privacy:metadata:preference:grade_report_studentsperpage');
$items->add_user_preference('grade_report_showonlyactiveenrol', 'privacy:metadata:preference:grade_report_showonlyactiveenrol');
$items->add_user_preference('grade_report_aggregationposition', 'privacy:metadata:preference:grade_report_aggregationposition');
$items->add_user_preference('grade_report_enableajax', 'privacy:metadata:preference:grade_report_enableajax');
// There is also one user preference which can be defined on each course.
$items->add_user_preference('grade_report_grader_collapsed_categories', 'privacy:metadata:preference:grade_report_grader_collapsed_categories');
return $items;
}
/**
* Store all user preferences for the plugin.
*
* @param int $userid The userid of the user whose data is to be exported.
*/
public static function export_user_preferences(int $userid) {
$preferences = get_user_preferences();
foreach ($preferences as $name => $value) {
$prefname = null;
$prefdescription = null;
$transformedvalue = null;
switch ($name) {
case 'grade_report_showcalculations':
case 'grade_report_showeyecons':
case 'grade_report_showaverages':
case 'grade_report_showlocks':
case 'grade_report_showuserimage':
case 'grade_report_showactivityicons':
case 'grade_report_showranges':
case 'grade_report_showanalysisicon':
case 'grade_report_shownumberofgrades':
case 'grade_report_quickgrading':
case 'grade_report_showonlyactiveenrol':
case 'grade_report_showquickfeedback':
case 'grade_report_enableajax':
$prefname = $name;
$transformedvalue = transform::yesno($value);
break;
case 'grade_report_meanselection':
$prefname = $name;
switch ($value) {
case GRADE_REPORT_MEAN_ALL:
$transformedvalue = get_string('meanall', 'grades');
break;
case GRADE_REPORT_MEAN_GRADED:
$transformedvalue = get_string('meangraded', 'grades');
break;
}
break;
case 'grade_report_rangesdecimalpoints':
case 'grade_report_averagesdecimalpoints':
case 'grade_report_studentsperpage':
$prefname = $name;
$transformedvalue = $value;
break;
case 'grade_report_rangesdisplaytype':
case 'grade_report_averagesdisplaytype':
$prefname = $name;
switch ($value) {
case GRADE_REPORT_PREFERENCE_INHERIT:
$transformedvalue = get_string('inherit', 'grades');
break;
case GRADE_DISPLAY_TYPE_REAL:
$transformedvalue = get_string('real', 'grades');
break;
case GRADE_DISPLAY_TYPE_PERCENTAGE:
$transformedvalue = get_string('percentage', 'grades');
break;
case GRADE_DISPLAY_TYPE_LETTER:
$transformedvalue = get_string('letter', 'grades');
break;
}
break;
case 'grade_report_aggregationposition':
$prefname = $name;
switch ($value) {
case GRADE_REPORT_AGGREGATION_POSITION_FIRST:
$transformedvalue = get_string('positionfirst', 'grades');
break;
case GRADE_REPORT_AGGREGATION_POSITION_LAST:
$transformedvalue = get_string('positionlast', 'grades');
break;
}
break;
default:
if (strpos($name, 'grade_report_grader_collapsed_categories') === 0) {
$prefname = 'grade_report_grader_collapsed_categories';
$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,
]
);
}
}
if ($prefname !== null) {
if ($prefdescription == null) {
$prefdescription = get_string('privacy:metadata:preference:'.$prefname, 'gradereport_grader');
}
writer::export_user_preference(
'gradereport_grader',
$prefname,
$transformedvalue,
$prefdescription
);
}
}
}
}

View File

@ -33,6 +33,28 @@ $string['grader:manage'] = 'Manage the grader report';
$string['grader:view'] = 'View the grader report';
$string['pluginname'] = 'Grader report';
$string['preferences'] = 'Grader report preferences';
$string['privacy:metadata:preference:grade_report_aggregationposition'] = 'Whether the category and course total columns are displayed first or last in the gradebook reports';
$string['privacy:metadata:preference:grade_report_averagesdecimalpoints'] = 'The number of decimal points to display for each average or whether the overall decimal points setting for the category or grade item is used (inherit).';
$string['privacy:metadata:preference:grade_report_averagesdisplaytype'] = 'Whether the average (mean) is displayed as real grades, percentages or letters, or whether the display type for the category or grade item is used (inherit).';
$string['privacy:metadata:preference:grade_report_enableajax'] = 'Whether to add a layer of AJAX functionality to the grader report, simplifying and speeding up common operations';
$string['privacy:metadata:preference:grade_report_grader_collapsed_categories'] = 'List of gradebook categories to be collapsed';
$string['privacy:metadata:preference:grade_report_meanselection'] = 'Whether cells with no grade should be included when calculating the average (mean) for each category or grade item';
$string['privacy:metadata:preference:grade_report_quickgrading'] = 'Whether to display a text input box for each grade, allowing many grades to be edited at the same time';
$string['privacy:metadata:preference:grade_report_rangesdecimalpoints'] = 'The number of decimal points to display for each range or whether the overall decimal points setting for the category or grade item is used (inherit)';
$string['privacy:metadata:preference:grade_report_rangesdisplaytype'] = 'Whether the range is displayed as real grades, percentages or letters, or whether the display type for the category or grade item is used (inherit)';
$string['privacy:metadata:preference:grade_report_showactivityicons'] = 'Whether to show the activity icons next to activity names';
$string['privacy:metadata:preference:grade_report_showanalysisicon'] = 'Whether to show grade analysis icon by default. If the activity module supports it, the grade analysis icon links to a page with more detailed explanation of the grade and how it was obtained.';
$string['privacy:metadata:preference:grade_report_showaverages'] = 'Whether to display an additional row with the average (mean) for each category and grade item';
$string['privacy:metadata:preference:grade_report_showcalculations'] = 'Whether a calculator icon for each grade item and category with a visual indicator that a grade item is calculated is displayed or not when editing is turned on';
$string['privacy:metadata:preference:grade_report_showeyecons'] = 'Whether to display the show/hide icon for each grade for controlling its visibility to the student';
$string['privacy:metadata:preference:grade_report_showlocks'] = 'Whether to display a lock/unlock icon for each grade for controlling if the grade can be automatically updated by the related activity when editing is turned on';
$string['privacy:metadata:preference:grade_report_shownumberofgrades'] = 'Whether to display in brackets after each average the number of grades used when calculating the average';
$string['privacy:metadata:preference:grade_report_showonlyactiveenrol'] = 'Whether to display only active enrolled users (not suspended) in gradebook report or all the enrolled users';
$string['privacy:metadata:preference:grade_report_showquickfeedback'] = 'Whether to display a feedback text input box with a dotted border for each grade, allowing the feedback for many grades to be edited at the same time';
$string['privacy:metadata:preference:grade_report_showranges'] = 'Whether to display an additional row with the range for each category and grade item';
$string['privacy:metadata:preference:grade_report_showuserimage'] = 'Whether to show the user\'s profile image next to the name';
$string['privacy:metadata:preference:grade_report_studentsperpage'] = 'The number of students displayed per page in the grader report';
$string['privacy:request:preference:grade_report_grader_collapsed_categories'] = 'You have some gradebook categories collapsed in the "{$a->name}" course';
$string['summarygrader'] = 'A table with the names of students in the first column, with assessable activities grouped by course and category across the top.';
$string['useractivitygrade'] = '{$a} grade';
$string['useractivityfeedback'] = '{$a} feedback';

View File

@ -0,0 +1,111 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for the gradereport_grader implementation of the privacy API.
*
* @package gradereport_grader
* @category test
* @copyright 2018 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use \core_privacy\local\metadata\collection;
use \core_privacy\local\request\writer;
use \core_privacy\local\request\approved_contextlist;
use \core_privacy\local\request\deletion_criteria;
use \gradereport_grader\privacy\provider;
/**
* Unit tests for the gradereport_grader implementation of the privacy API.
*
* @copyright 2018 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class gradereport_grader_privacy_testcase extends \core_privacy\tests\provider_testcase {
/**
* Basic setup for these tests.
*/
public function setUp() {
$this->resetAfterTest(true);
}
/**
* Ensure that export_user_preferences returns no data if the user has no data.
*/
public function test_export_user_preferences_not_defined() {
$user = \core_user::get_user_by_username('admin');
provider::export_user_preferences($user->id);
$writer = writer::with_context(\context_system::instance());
$this->assertFalse($writer->has_any_data());
}
/**
* Ensure that export_user_preferences returns single preferences.
* These preferences can be set on each course, but the value is shared in the whole site.
*/
public function test_export_user_preferences_single() {
// Add some user preferences.
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
set_user_preference('grade_report_showcalculations', 1, $user);
set_user_preference('grade_report_meanselection', GRADE_REPORT_MEAN_GRADED, $user);
set_user_preference('grade_report_studentsperpage', 50, $user);
// Validate exported data.
provider::export_user_preferences($user->id);
$context = context_user::instance($user->id);
$writer = writer::with_context($context);
$this->assertTrue($writer->has_any_data());
$prefs = $writer->get_user_preferences('gradereport_grader');
$this->assertCount(3, (array) $prefs);
$this->assertEquals(
get_string('privacy:metadata:preference:grade_report_showcalculations', 'gradereport_grader'),
$prefs->grade_report_showcalculations->description
);
$this->assertEquals(get_string('meangraded', 'grades'), $prefs->grade_report_meanselection->value);
$this->assertEquals(50, $prefs->grade_report_studentsperpage->value);
}
/**
* Ensure that export_user_preferences returns preferences.
*/
public function test_export_user_preferences_multiple() {
// 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);
// Validate exported data.
provider::export_user_preferences($user->id);
$context = context_user::instance($user->id);
$writer = writer::with_context($context);
$this->assertTrue($writer->has_any_data());
$prefs = $writer->get_user_preferences('gradereport_grader');
$this->assertCount(1, (array) $prefs);
$this->assertEquals(
get_string('privacy:request:preference:grade_report_grader_collapsed_categories', 'gradereport_grader', ['name' => $course->fullname]),
$prefs->grade_report_grader_collapsed_categories->description
);
}
}