diff --git a/mod/quiz/report/grading/classes/privacy/provider.php b/mod/quiz/report/grading/classes/privacy/provider.php index bc5cae8deb5..96594c0e44d 100644 --- a/mod/quiz/report/grading/classes/privacy/provider.php +++ b/mod/quiz/report/grading/classes/privacy/provider.php @@ -15,32 +15,76 @@ // along with Moodle. If not, see . /** - * Privacy Subsystem implementation for quiz_grading. + * Privacy subsystem implementation for quiz_grading. * - * @package quiz_grading - * @copyright 2018 Andrew Nicols - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package quiz_grading + * @copyright 2020 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace quiz_grading\privacy; +use core_privacy\local\metadata\collection; +use core_privacy\local\request\writer; + defined('MOODLE_INTERNAL') || die(); /** - * Privacy Subsystem for quiz_grading implementing null_provider. - * - * @copyright 2018 Andrew Nicols - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * Privacy subsystem for quiz_grading. */ -class provider implements \core_privacy\local\metadata\null_provider { +class provider implements + \core_privacy\local\metadata\provider, + \core_privacy\local\request\user_preference_provider { /** - * Get the language string identifier with the component's language - * file to explain why this plugin stores no data. + * Returns meta data about this system. * - * @return string + * @param collection $collection The initialised collection to add items to. + * @return collection A listing of user data stored through this system. */ - public static function get_reason() : string { - return 'privacy:metadata'; + public static function get_metadata(collection $collection) : collection { + $collection->add_user_preference('quiz_grading_pagesize', 'privacy:preference:pagesize'); + $collection->add_user_preference('quiz_grading_order', 'privacy:preference:order'); + + return $collection; + } + + /** + * Export 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) { + + // Page size. + $pagesize = get_user_preferences("quiz_grading_pagesize", null, $userid); + if ($pagesize !== null) { + writer::export_user_preference('quiz_grading', 'pagesize', $pagesize, + get_string('privacy:preference:pagesize', 'quiz_grading')); + } + + // Attempt order. + $order = get_user_preferences("quiz_grading_order", null, $userid); + if ($order !== null) { + switch ($order) { + case 'random': + $order = get_string('randomly', 'quiz_grading'); + break; + case 'date': + $order = get_string('bydate', 'quiz_grading'); + break; + case 'studentfirstname': + $order = get_string('studentfirstname', 'quiz_grading'); + break; + case 'studentlastname': + $order = get_string('studentlastname', 'quiz_grading'); + break; + case 'idnumber': + $order = get_string('bystudentidnumber', 'quiz_grading'); + break; + } + writer::export_user_preference('quiz_grading', 'order', $order, + get_string('privacy:preference:order', 'quiz_grading')); + } } } diff --git a/mod/quiz/report/grading/lang/en/quiz_grading.php b/mod/quiz/report/grading/lang/en/quiz_grading.php index cd939b01e2f..aa1224f51bb 100644 --- a/mod/quiz/report/grading/lang/en/quiz_grading.php +++ b/mod/quiz/report/grading/lang/en/quiz_grading.php @@ -67,7 +67,8 @@ $string['nothingfound'] = 'Nothing to display'; $string['options'] = 'Options'; $string['orderattempts'] = 'Order attempts'; $string['pluginname'] = 'Manual grading'; -$string['privacy:metadata'] = 'The Quiz Manual grading plugin does not store any personal data. It provides an interface for users to store data without storing any data itself.'; +$string['privacy:preference:order'] = 'What order to show the attempts that need grading.'; +$string['privacy:preference:pagesize'] = 'How many attempts to show on each page of the grading interface.'; $string['qno'] = 'Q #'; $string['questionname'] = 'Question name'; $string['questionsperpage'] = 'Questions per page'; diff --git a/mod/quiz/report/grading/report.php b/mod/quiz/report/grading/report.php index 0bbcecb5f0a..88ef37e2e91 100644 --- a/mod/quiz/report/grading/report.php +++ b/mod/quiz/report/grading/report.php @@ -153,30 +153,30 @@ class quiz_grading_report extends quiz_default_report { return true; } - $counts = null; - if ($slot && $hasquestions) { - // Make sure there is something to do. - $statecounts = $this->get_question_state_summary(array($slot)); - foreach ($statecounts as $record) { - if ($record->questionid == $questionid) { - $counts = $record; - break; - } - } - // If not, redirect back to the list. - if (!$counts || $counts->$grade == 0) { - redirect($this->list_questions_url(), get_string('alldoneredirecting', 'quiz_grading')); - } - } - - // What sort of page to display? if (!$slot) { $this->display_index($includeauto); - - } else { - echo $this->display_grading_interface($slot, $questionid, $grade, - $pagesize, $page, $shownames, $showidnumbers, $order, $counts); + return true; } + + // Display the grading UI for one question. + + // Make sure there is something to do. + $counts = null; + $statecounts = $this->get_question_state_summary([$slot]); + foreach ($statecounts as $record) { + if ($record->questionid == $questionid) { + $counts = $record; + break; + } + } + + // If not, redirect back to the list. + if (!$counts || $counts->$grade == 0) { + redirect($this->list_questions_url(), get_string('alldoneredirecting', 'quiz_grading')); + } + + $this->display_grading_interface($slot, $questionid, $grade, + $pagesize, $page, $shownames, $showidnumbers, $order, $counts); return true; } diff --git a/mod/quiz/report/grading/tests/privacy_provider_test.php b/mod/quiz/report/grading/tests/privacy_provider_test.php new file mode 100644 index 00000000000..96c3f5ee4ef --- /dev/null +++ b/mod/quiz/report/grading/tests/privacy_provider_test.php @@ -0,0 +1,78 @@ +. + +/** + * Privacy provider tests. + * + * @package quiz_grading + * @copyright 2020 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +use core_privacy\local\metadata\collection; +use quiz_grading\privacy\provider; +use core_privacy\local\request\writer; +use core_privacy\local\request\transform; + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/question/engine/questionattempt.php'); + +/** + * Privacy provider tests class. + */ +class quiz_grading_privacy_provider_testcase extends \core_privacy\tests\provider_testcase { + /** + * When no preference exists, there should be no export. + */ + public function test_preference_unset() { + global $USER; + + $this->resetAfterTest(); + $this->setAdminUser(); + + provider::export_user_preferences($USER->id); + + $this->assertFalse(writer::with_context(\context_system::instance())->has_any_data()); + } + + /** + * Preference does exist. + */ + public function test_preference_bool_true() { + global $USER; + + $this->resetAfterTest(); + $this->setAdminUser(); + + set_user_preference('quiz_grading_pagesize', 42); + set_user_preference('quiz_grading_order', 'random'); + + provider::export_user_preferences($USER->id); + + $writer = writer::with_context(\context_system::instance()); + $this->assertTrue($writer->has_any_data()); + + $preferences = $writer->get_user_preferences('quiz_grading'); + + $this->assertNotEmpty($preferences->pagesize); + $this->assertEquals(42, $preferences->pagesize->value); + + $this->assertNotEmpty($preferences->order); + $this->assertEquals('Randomly', $preferences->order->value); + } +}