Merge branch 'MDL-66230' of https://github.com/paulholden/moodle
[moodle.git] / admin / tool / usertours / classes / privacy / provider.php
CommitLineData
d3c60717
AN
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Privacy Subsystem implementation for tool_usertours.
19 *
20 * @package tool_usertours
21 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace tool_usertours\privacy;
26
27use \core_privacy\local\request\writer;
28use \core_privacy\local\metadata\collection;
29use \core_privacy\local\request\transform;
30
31defined('MOODLE_INTERNAL') || die();
32
33/**
34 * Implementation of the privacy subsystem plugin provider for the user tours feature.
35 *
36 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class provider implements
40 // This plugin has data.
41 \core_privacy\local\metadata\provider,
42
43 // This plugin has some sitewide user preferences to export.
44 \core_privacy\local\request\user_preference_provider
45{
46
47 /**
48 * Returns meta data about this system.
49 *
50 * @param collection $itemcollection The initialised item collection to add items to.
51 * @return collection A listing of user data stored through this system.
52 */
53 public static function get_metadata(collection $items) : collection {
54 // There are several user preferences.
55 $items->add_user_preference(\tool_usertours\tour::TOUR_REQUESTED_BY_USER, 'privacy:metadata:preference:requested');
56 $items->add_user_preference(\tool_usertours\tour::TOUR_LAST_COMPLETED_BY_USER, 'privacy:metadata:preference:completed');
57
58 return $items;
59 }
60
61 /**
62 * Store all user preferences for the plugin.
63 *
64 * @param int $userid The userid of the user whose data is to be exported.
65 */
66 public static function export_user_preferences(int $userid) {
67 $preferences = get_user_preferences();
68 foreach ($preferences as $name => $value) {
69 $descriptionidentifier = null;
70 $tourid = null;
71 if (strpos($name, \tool_usertours\tour::TOUR_REQUESTED_BY_USER) === 0) {
72 $descriptionidentifier = 'privacy:request:preference:requested';
73 $tourid = substr($name, strlen(\tool_usertours\tour::TOUR_REQUESTED_BY_USER));
74 } else if (strpos($name, \tool_usertours\tour::TOUR_LAST_COMPLETED_BY_USER) === 0) {
75 $descriptionidentifier = 'privacy:request:preference:completed';
76 $tourid = substr($name, strlen(\tool_usertours\tour::TOUR_LAST_COMPLETED_BY_USER));
77 }
78
79 if ($descriptionidentifier !== null) {
a1ab8ec2
PH
80 try {
81 $tour = \tool_usertours\tour::instance($tourid);
82 $time = transform::datetime($value);
d3c60717 83
a1ab8ec2
PH
84 writer::export_user_preference(
85 'tool_usertours',
86 $name,
87 $time,
88 get_string($descriptionidentifier, 'tool_usertours', (object) [
89 'name' => $tour->get_name(),
90 'time' => $time,
91 ])
92 );
93 } catch (\dml_missing_record_exception $ex) {
94 // The tour related to this user preference no longer exists.
95 }
d3c60717
AN
96 }
97 }
98 }
99}