From c003768e8432380c7522f5dc969a1a3ce275a078 Mon Sep 17 00:00:00 2001
From: Alexander Bias <alexander.bias@uni-ulm.de>
Date: Wed, 23 May 2018 13:41:59 +0200
Subject: [PATCH] Implement Privacy API

---
 CHANGES.md                               |   4 +
 classes/privacy/provider.php             | 146 +++++++++++++++++++++++
 lang/en/block_course_overview_campus.php |  16 +++
 version.php                              |   2 +-
 4 files changed, 167 insertions(+), 1 deletion(-)
 create mode 100644 classes/privacy/provider.php

diff --git a/CHANGES.md b/CHANGES.md
index 8347a4a..c8bc3f6 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -4,6 +4,10 @@ moodle-block_course_overview_campus
 Changes
 -------
 
+### Unreleased
+
+* 2018-05-16 - Implement Privacy API.
+
 ### v3.4-r1
 
 * 2018-03-30 - Check compatibility for Moodle 3.4, no functionality change.
diff --git a/classes/privacy/provider.php b/classes/privacy/provider.php
new file mode 100644
index 0000000..4459944
--- /dev/null
+++ b/classes/privacy/provider.php
@@ -0,0 +1,146 @@
+<?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/>.
+
+/**
+ * Block "course overview (campus)" - Privacy provider
+ *
+ * @package    block_course_overview_campus
+ * @copyright  2018 Alexander Bias, Ulm University <alexander.bias@uni-ulm.de>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace block_course_overview_campus\privacy;
+
+use \core_privacy\local\request\writer;
+use \core_privacy\local\metadata\collection;
+use \core_privacy\local\request\transform;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Privacy Subsystem implementing provider.
+ *
+ * @package    block_course_overview_campus
+ * @copyright  2018 Alexander Bias, Ulm University <alexander.bias@uni-ulm.de>
+ * @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 $collection 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 $collection) : collection {
+        $collection->add_user_preference('block_course_overview_campus-selectedterm',
+                'privacy:metadata:preference:selectedterm');
+        $collection->add_user_preference('block_course_overview_campus-selectedteacher',
+                'privacy:metadata:preference:selectedteacher');
+        $collection->add_user_preference('block_course_overview_campus-selectedcategory',
+                'privacy:metadata:preference:selectedcategory');
+        $collection->add_user_preference('block_course_overview_campus-selectedtoplevelcategory',
+                'privacy:metadata:preference:selectedtoplevelcategory');
+        $collection->add_user_preference('block_course_overview_campus-hidecourse-',
+                'privacy:metadata:preference:hidecourse');
+        $collection->add_user_preference('block_course_overview_campus-hidenews-',
+                'privacy:metadata:preference:hidenews');
+        $collection->add_user_preference('local_boostcoc-notshowncourses',
+                'privacy:metadata:preference:local_boostcoc-notshowncourses');
+        $collection->add_user_preference('local_boostcoc-activefilters',
+                'privacy:metadata:preference:local_boostcoc-activefilters');
+
+        return $collection;
+    }
+
+    /**
+     * 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) {
+            $descriptionidentifier = null;
+
+            // User preferences for filters.
+            if (strpos($name, 'block_course_overview_campus-selected') === 0) {
+                if ($name == 'block_course_overview_campus-selectedterm') {
+                    $descriptionidentifier = 'privacy:request:preference:selectedterm';
+                } else if ($name == 'block_course_overview_campus-selectedteacher') {
+                    $descriptionidentifier = 'privacy:request:preference:selectedteacher';
+                } else if ($name == 'block_course_overview_campus-selectedcategory') {
+                    $descriptionidentifier = 'privacy:request:preference:selectedcategory';
+                } else if ($name == 'block_course_overview_campus-selectedtoplevelcategory') {
+                    $descriptionidentifier = 'privacy:request:preference:selectedtoplevelcategory';
+                }
+
+                if ($descriptionidentifier !== null) {
+                    writer::export_user_preference(
+                            'block_course_overview_campus',
+                            $name,
+                            $value,
+                            get_string($descriptionidentifier, 'block_course_overview_campus', (object) [
+                                    'value' => $value,
+                            ])
+                    );
+                }
+
+                // User preferences for hiding stuff.
+            } else if (strpos($name, 'block_course_overview_campus-hide') === 0) {
+                if (strpos($name, 'block_course_overview_campus-hidecourse-') === 0) {
+                    $descriptionidentifier = 'privacy:request:preference:hidecourse';
+                    $item = substr($name, strlen('block_course_overview_campus-hidecourse-'));
+                } else if (strpos($name, 'block_course_overview_campus-hidenews-') === 0) {
+                    $descriptionidentifier = 'privacy:request:preference:hidenews';
+                    $item = substr($name, strlen('block_course_overview_campus-hidecourse-'));
+                }
+
+                if ($descriptionidentifier !== null) {
+                    writer::export_user_preference(
+                            'block_course_overview_campus',
+                            $name,
+                            $value,
+                            get_string($descriptionidentifier, 'block_course_overview_campus', (object) [
+                                    'item' => $item,
+                                    'value' => $value,
+                            ])
+                    );
+                }
+
+                // User preferences for local_boostcoc.
+            } else if (strpos($name, 'local_boostcoc-') === 0) {
+                if ($name == 'local_boostcoc-notshowncourses') {
+                    $descriptionidentifier = 'privacy:request:preference:local_boostcoc-notshowncourses';
+                } else if ($name == 'local_boostcoc-activefilters') {
+                    $descriptionidentifier = 'privacy:request:preference:local_boostcoc-activefilters';
+                }
+
+                if ($descriptionidentifier !== null) {
+                    writer::export_user_preference(
+                            'block_course_overview_campus',
+                            $name,
+                            $value,
+                            get_string($descriptionidentifier, 'block_course_overview_campus', (object) [
+                                    'value' => $value,
+                            ])
+                    );
+                }
+            }
+        }
+    }
+}
diff --git a/lang/en/block_course_overview_campus.php b/lang/en/block_course_overview_campus.php
index eddd760..bc0668d 100644
--- a/lang/en/block_course_overview_campus.php
+++ b/lang/en/block_course_overview_campus.php
@@ -68,6 +68,22 @@ $string['other'] = 'Other';
 $string['pluginname'] = 'Course overview on campus';
 $string['prioritizemyteachedcourses'] = 'Prioritize courses in which I teach';
 $string['prioritizemyteachedcourses_desc'] = 'Courses in which the user has a teacher role are listed first in course overview list';
+$string['privacy:metadata:preference:selectedterm'] = 'The current selection of the term filter.';
+$string['privacy:metadata:preference:selectedteacher'] = 'The current selection of the teacher filter.';
+$string['privacy:metadata:preference:selectedcategory'] = 'The current selection of the parent category filter.';
+$string['privacy:metadata:preference:selectedtoplevelcategory'] = 'The current selection of the top level category filter.';
+$string['privacy:metadata:preference:hidecourse'] = 'The show/hide status of a course in the course overview list.';
+$string['privacy:metadata:preference:hidenews'] = 'The show/hide status of a course\'s course news in the course overview list.';
+$string['privacy:metadata:preference:local_boostcoc-notshowncourses'] = 'The list of currently not shown courses to be used in the companion plugin \'Boost course overview campus\'.';
+$string['privacy:metadata:preference:local_boostcoc-activefilters'] = 'The list of currently active filters to be used in the companion plugin \'Boost course overview campus\'.';
+$string['privacy:request:preference:selectedterm'] = 'The current selection of the term filter is: {$a->value}.';
+$string['privacy:request:preference:selectedteacher'] = 'The current selection of the teacher filter is: {$a->value}.';
+$string['privacy:request:preference:selectedcategory'] = 'The current selection of the parent category filter is: {$a->value}.';
+$string['privacy:request:preference:selectedtoplevelcategory'] = 'The current selection of the top level category filter is: {$a->value}.';
+$string['privacy:request:preference:hidecourse'] = 'The show/hide status of the course {$a->item} in the course overview list is: {$a->value}.';
+$string['privacy:request:preference:hidenews'] = 'The show/hide status of course {$a->item}\'s course news in the course overview list is: {$a->value}.';
+$string['privacy:request:preference:local_boostcoc-notshowncourses'] = 'The list of currently not shown courses to be used in the companion plugin \'Boost course overview campus\' is: {$a->value}.';
+$string['privacy:request:preference:local_boostcoc-activefilters'] = 'The list of currently active filters to be used in the companion plugin \'Boost course overview campus\' is: {$a->value}.';
 $string['secondrowhideonphones'] = 'Second row: Hide on phones';
 $string['secondrowhideonphones_desc'] = 'Hide the second row on mobile phones to save space';
 $string['secondrowshowcategoryname'] = 'Second row: Show parent category name';
diff --git a/version.php b/version.php
index ac24b82..c5fe698 100644
--- a/version.php
+++ b/version.php
@@ -25,7 +25,7 @@
 defined('MOODLE_INTERNAL') || die();
 
 $plugin->component = 'block_course_overview_campus';
-$plugin->version = 2018033000;
+$plugin->version = 2018051600;
 $plugin->release = 'v3.4-r1';
 $plugin->requires = 2017111300;
 $plugin->maturity = MATURITY_STABLE;
-- 
GitLab