MDL-62487 quiz manual grading: implement suggestions from int review

Add privacy provider bits, and tidy code.
This commit is contained in:
Tim Hunt 2020-05-15 13:11:43 +01:00
parent b20541805f
commit 89ee18c0e1
4 changed files with 159 additions and 36 deletions

View File

@ -15,32 +15,76 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Privacy Subsystem implementation for quiz_grading.
* Privacy subsystem implementation for quiz_grading.
*
* @package quiz_grading
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @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 <andrew@nicols.co.uk>
* @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'));
}
}
}

View File

@ -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';

View File

@ -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;
}

View File

@ -0,0 +1,78 @@
<?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 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);
}
}