MDL-61868 tool_policy: Implement privacy API
authorSara Arjona <sara@moodle.com>
Thu, 5 Apr 2018 19:31:24 +0000 (21:31 +0200)
committerMarina Glancy <marina@moodle.com>
Mon, 16 Apr 2018 03:30:14 +0000 (11:30 +0800)
Part of MDL-61864

admin/tool/policy/classes/privacy/provider.php [new file with mode: 0644]
admin/tool/policy/lang/en/tool_policy.php
admin/tool/policy/tests/privacy_provider_test.php [new file with mode: 0644]

diff --git a/admin/tool/policy/classes/privacy/provider.php b/admin/tool/policy/classes/privacy/provider.php
new file mode 100644 (file)
index 0000000..d541cb7
--- /dev/null
@@ -0,0 +1,104 @@
+<?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 tool_policy.
+ *
+ * @package    tool_policy
+ * @copyright  2018 Sara Arjona <sara@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace tool_policy\privacy;
+
+use core_privacy\local\metadata\collection;
+use core_privacy\local\request\approved_contextlist;
+use core_privacy\local\request\contextlist;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Implementation of the privacy subsystem plugin provider for the policy tool.
+ *
+ * @copyright  2018 Sara Arjona <sara@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class provider implements
+        // This tool stores user data.
+        \core_privacy\local\metadata\provider,
+
+        // This tool may provide access to and deletion of user data.
+        \core_privacy\local\request\plugin\provider {
+
+    /**
+     * Return the fields which contain personal data.
+     *
+     * @param collection $items A reference to the collection to use to store the metadata.
+     * @return collection The updated collection of metadata items.
+     */
+    public static function get_metadata(collection $items) : collection {
+        $items->add_database_table(
+            'tool_policy_acceptances',
+            [
+                'policyversionid' => 'privacy:metadata:acceptances:policyversionid',
+                'userid' => 'privacy:metadata:acceptances:userid',
+                'status' => 'privacy:metadata:acceptances:status',
+                'lang' => 'privacy:metadata:acceptances:lang',
+                'usermodified' => 'privacy:metadata:acceptances:usermodified',
+                'timecreated' => 'privacy:metadata:acceptances:timecreated',
+                'timemodified' => 'privacy:metadata:acceptances:timemodified',
+                'note' => 'privacy:metadata:acceptances:note',
+            ],
+            'privacy:metadata:acceptances'
+        );
+
+        return $items;
+    }
+
+    /**
+     * Get the list of contexts that contain user information for the specified user.
+     *
+     * @param int $userid The userid.
+     * @return contextlist The list of contexts containing user info for the user.
+     */
+    public static function get_contexts_for_userid(int $userid) : contextlist {
+        return new contextlist();
+    }
+
+    /**
+     * Export personal data for the given approved_contextlist. User and context information is contained within the contextlist.
+     *
+     * @param approved_contextlist $contextlist A list of contexts approved for export.
+     */
+    public static function export_user_data(approved_contextlist $contextlist) {
+    }
+
+    /**
+     * Delete all data for all users in the specified context.
+     *
+     * @param \context $context The context to delete in.
+     */
+    public static function delete_data_for_all_users_in_context(\context $context) {
+    }
+
+    /**
+     * Delete all user data for the specified user, in the specified contexts.
+     *
+     * @param approved_contextlist $contextlist A list of contexts approved for deletion.
+     */
+    public static function delete_data_for_user(approved_contextlist $contextlist) {
+    }
+}
index 74d54c8..f8639d0 100644 (file)
@@ -116,6 +116,15 @@ $string['policydoctype99'] = 'Other policy';
 $string['policyversionacceptedinbehalf'] = 'This policy version has been agreed to by another user on behalf of you.';
 $string['policyversionacceptedinotherlang'] = 'This policy version has been agreed to in a different language.';
 $string['previousversions'] = '{$a} previous versions';
+$string['privacy:metadata:acceptances'] = 'Information from policies acceptances made by the users of this site.';
+$string['privacy:metadata:acceptances:policyversionid'] = 'The ID of the accepted version policy.';
+$string['privacy:metadata:acceptances:userid'] = 'The ID of the user who has accepted the policy.';
+$string['privacy:metadata:acceptances:status'] = 'The status of the acceptance: 0 if not accepted; 1 otherwise.';
+$string['privacy:metadata:acceptances:lang'] = 'The current lang displayed when the policy is accepted.';
+$string['privacy:metadata:acceptances:usermodified'] = 'The ID of the user accepting the policy, if made on behalf of another user.';
+$string['privacy:metadata:acceptances:timecreated'] = 'The timestamp indicating when the acceptance was made by the user.';
+$string['privacy:metadata:acceptances:timemodified'] = 'The timestamp indicating when the acceptance was modified by the user.';
+$string['privacy:metadata:acceptances:note'] = 'Any comments added by the user who accepts policies on behalf of another.';
 $string['privacysettings'] = 'Privacy settings';
 $string['readpolicy'] = 'Please read our {$a}';
 $string['refertofullpolicytext'] = 'Please refer to the full {$a} text if you would like to review.';
diff --git a/admin/tool/policy/tests/privacy_provider_test.php b/admin/tool/policy/tests/privacy_provider_test.php
new file mode 100644 (file)
index 0000000..b37dfee
--- /dev/null
@@ -0,0 +1,83 @@
+<?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    tool_policy
+ * @category   test
+ * @copyright  2018 Sara Arjona <sara@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+use core_privacy\local\metadata\collection;
+use tool_policy\privacy\provider;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Privacy provider tests class.
+ *
+ * @copyright  2018 Sara Arjona <sara@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class tool_policy_privacy_provider_testcase extends \core_privacy\tests\provider_testcase {
+    /** @var stdClass The user object. */
+    protected $user;
+
+    /**
+     * Setup function. Will create a user.
+     */
+    protected function setUp() {
+        $this->resetAfterTest();
+
+        $generator = $this->getDataGenerator();
+        $this->user = $generator->create_user();
+    }
+
+    /**
+     * Test for provider::get_metadata().
+     */
+    public function test_get_metadata() {
+        $collection = new collection('tool_policy');
+        $newcollection = provider::get_metadata($collection);
+        $itemcollection = $newcollection->get_collection();
+        $this->assertCount(1, $itemcollection);
+
+        $table = reset($itemcollection);
+        $this->assertEquals('tool_policy_acceptances', $table->get_name());
+
+        $privacyfields = $table->get_privacy_fields();
+        $this->assertArrayHasKey('policyversionid', $privacyfields);
+        $this->assertArrayHasKey('userid', $privacyfields);
+        $this->assertArrayHasKey('status', $privacyfields);
+        $this->assertArrayHasKey('lang', $privacyfields);
+        $this->assertArrayHasKey('usermodified', $privacyfields);
+        $this->assertArrayHasKey('timecreated', $privacyfields);
+        $this->assertArrayHasKey('timemodified', $privacyfields);
+        $this->assertArrayHasKey('note', $privacyfields);
+
+        $this->assertEquals('privacy:metadata:acceptances', $table->get_summary());
+    }
+
+    /**
+     * Test getting the context for the user ID related to this plugin.
+     */
+    public function test_get_contexts_for_userid() {
+        $contextlist = \tool_policy\privacy\provider::get_contexts_for_userid($this->user->id);
+        $this->assertEmpty($contextlist);
+    }
+}