Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace tool_usertours\privacy;
18
 
19
use core_privacy\local\request\writer;
20
use core_privacy\local\metadata\collection;
21
use core_privacy\local\request\transform;
22
 
23
/**
24
 * Implementation of the privacy subsystem plugin provider for the user tours feature.
25
 *
26
 * @package    tool_usertours
27
 * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class provider implements
31
    // This plugin has data.
32
    \core_privacy\local\metadata\provider,
33
 
34
    // This plugin has some sitewide user preferences to export.
35
    \core_privacy\local\request\user_preference_provider {
36
    /**
37
     * Returns meta data about this system.
38
     *
39
     * @param   collection     $items The initialised item collection to add items to.
40
     * @return  collection     A listing of user data stored through this system.
41
     */
42
    public static function get_metadata(collection $items): collection {
43
        // There are several user preferences.
44
        $items->add_user_preference(\tool_usertours\tour::TOUR_REQUESTED_BY_USER, 'privacy:metadata:preference:requested');
45
        $items->add_user_preference(\tool_usertours\tour::TOUR_LAST_COMPLETED_BY_USER, 'privacy:metadata:preference:completed');
46
 
47
        return $items;
48
    }
49
 
50
    /**
51
     * Store all user preferences for the plugin.
52
     *
53
     * @param   int         $userid The userid of the user whose data is to be exported.
54
     */
55
    public static function export_user_preferences(int $userid) {
56
        $preferences = get_user_preferences(null, null, $userid);
57
        foreach ($preferences as $name => $value) {
58
            $descriptionidentifier = null;
59
            $tourid = null;
60
            if (strpos($name, \tool_usertours\tour::TOUR_REQUESTED_BY_USER) === 0) {
61
                $descriptionidentifier = 'privacy:request:preference:requested';
62
                $tourid = substr($name, strlen(\tool_usertours\tour::TOUR_REQUESTED_BY_USER));
63
            } else if (strpos($name, \tool_usertours\tour::TOUR_LAST_COMPLETED_BY_USER) === 0) {
64
                $descriptionidentifier = 'privacy:request:preference:completed';
65
                $tourid = substr($name, strlen(\tool_usertours\tour::TOUR_LAST_COMPLETED_BY_USER));
66
            }
67
 
68
            if ($descriptionidentifier !== null) {
69
                try {
70
                    $tour = \tool_usertours\tour::instance($tourid);
71
                    $time = transform::datetime($value);
72
 
73
                    writer::export_user_preference(
74
                        'tool_usertours',
75
                        $name,
76
                        $time,
77
                        get_string($descriptionidentifier, 'tool_usertours', (object) [
78
                            'name' => $tour->get_name(),
79
                            'time' => $time,
80
                        ])
81
                    );
82
                } catch (\dml_missing_record_exception $ex) {
83
                    // The tour related to this user preference no longer exists.
84
                }
85
            }
86
        }
87
    }
88
}