MDL-62487 quiz manual grading: implement suggestions from int review
authorTim Hunt <T.J.Hunt@open.ac.uk>
Fri, 15 May 2020 12:11:43 +0000 (13:11 +0100)
committerTim Hunt <T.J.Hunt@open.ac.uk>
Fri, 15 May 2020 12:48:21 +0000 (13:48 +0100)
Add privacy provider bits, and tidy code.

mod/quiz/report/grading/classes/privacy/provider.php
mod/quiz/report/grading/lang/en/quiz_grading.php
mod/quiz/report/grading/report.php
mod/quiz/report/grading/tests/privacy_provider_test.php [new file with mode: 0644]

index bc5cae8..96594c0 100644 (file)
 // 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 {
+
+    /**
+     * Returns meta data about this system.
+     *
+     * @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_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;
+    }
 
     /**
-     * Get the language string identifier with the component's language
-     * file to explain why this plugin stores no data.
+     * Export all user preferences for the plugin.
      *
-     * @return  string
+     * @param   int         $userid The userid of the user whose data is to be exported.
      */
-    public static function get_reason() : string {
-        return 'privacy:metadata';
+    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'));
+        }
     }
 }
index cd939b0..aa1224f 100644 (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';
index 0bbcecb..88ef37e 100644 (file)
@@ -153,30 +153,30 @@ class quiz_grading_report extends quiz_default_report {
             return true;
         }
 
+        if (!$slot) {
+            $this->display_index($includeauto);
+            return true;
+        }
+
+        // Display the grading UI for one question.
+
+        // Make sure there is something to do.
         $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'));
+        $statecounts = $this->get_question_state_summary([$slot]);
+        foreach ($statecounts as $record) {
+            if ($record->questionid == $questionid) {
+                $counts = $record;
+                break;
             }
         }
 
-        // 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);
+        // 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 (file)
index 0000000..96c3f5e
--- /dev/null
@@ -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);
+    }
+}