Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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 gradepenalty_duedate\privacy;
18
 
19
use context;
20
use core_privacy\local\metadata\collection;
21
use core_privacy\local\request\approved_contextlist;
22
use core_privacy\local\request\approved_userlist;
23
use core_privacy\local\request\contextlist;
24
use core_privacy\local\request\userlist;
25
use core_privacy\local\request\writer;
26
 
27
/**
28
 * Privacy Subsystem for gradepenalty_duedate implementing null_provider.
29
 *
30
 * @package   gradepenalty_duedate
31
 * @copyright 2024 Catalyst IT Australia Pty Ltd
32
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class provider implements
35
    \core_privacy\local\metadata\provider,
36
    \core_privacy\local\request\core_userlist_provider,
37
    \core_privacy\local\request\plugin\provider {
38
 
39
    /**
40
     * Get the list of contexts that contain user information for the specified user.
41
     *
42
     * @param int $userid The user to search.
43
     * @return contextlist A list of contexts used in this plugin.
44
     */
45
    public static function get_contexts_for_userid(int $userid): contextlist {
46
        $sql = "SELECT ctx.id
47
                  FROM {gradepenalty_duedate_rule} gdr
48
                  JOIN {context} ctx ON ctx.instanceid = gdr.usermodified AND ctx.contextlevel = :contextlevel
49
                 WHERE gdr.usermodified = :userid";
50
 
51
        $params = ['userid' => $userid, 'contextlevel' => CONTEXT_USER];
52
        $contextlist = new contextlist();
53
        $contextlist->add_from_sql($sql, $params);
54
        return $contextlist;
55
    }
56
 
57
    /**
58
     * Export all user data for the specified user, in the specified contexts.
59
     *
60
     * @param approved_contextlist $contextlist The approved contexts to export information for.
61
     */
62
    public static function export_user_data(approved_contextlist $contextlist): void {
63
        global $DB;
64
 
65
        $user = $contextlist->get_user();
66
 
67
        // Get all the rules edited by the user.
68
        $rules = $DB->get_records('gradepenalty_duedate_rule', ['usermodified' => $user->id]);
69
        $data = (object) $rules;
70
 
71
        // Write out the data.
72
        writer::with_context(\context_user::instance($user->id))->export_data([
73
            get_string('privacy:metadata:gradepenalty_duedate_rule', 'gradepenalty_duedate'),
74
            'rules',
75
        ], $data);
76
    }
77
 
78
    /**
79
     * Delete all data for all users in the specified context.
80
     *
81
     * @param context $context The specific context to delete data for.
82
     */
83
    public static function delete_data_for_all_users_in_context(context $context): void {
84
        if ($context->contextlevel != CONTEXT_USER) {
85
            return;
86
        }
87
        static::delete_user_data($context->instanceid);
88
    }
89
 
90
    /**
91
     * Delete user data in the list of given contexts.
92
     *
93
     * @param approved_contextlist $contextlist the list of contexts.
94
     */
95
    public static function delete_data_for_user(approved_contextlist $contextlist): void {
96
        if (empty($contextlist->count())) {
97
            return;
98
        }
99
        $userid = $contextlist->get_user()->id;
100
        foreach ($contextlist->get_contexts() as $context) {
101
            if ($context->contextlevel != CONTEXT_USER) {
102
                continue;
103
            }
104
            if ($context->instanceid == $userid) {
105
                static::delete_user_data($context->instanceid);
106
            }
107
        }
108
    }
109
 
110
    /**
111
     * Get the list of users within a specific context.
112
     *
113
     * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
114
     */
115
    public static function get_users_in_context(userlist $userlist): void {
116
        $context = $userlist->get_context();
117
 
118
        if (!$context instanceof \context_user) {
119
            return;
120
        }
121
 
122
        $sql = "SELECT DISTINCT u.id
123
                  FROM {gradepenalty_duedate_rule} gdr
124
                  JOIN {user} u ON u.id = gdr.usermodified
125
                 WHERE gdr.usermodified = :userid";
126
        $params = ['userid' => $context->instanceid];
127
        $userlist->add_from_sql('id', $sql, $params);
128
    }
129
 
130
    /**
131
     * Delete multiple users within a single context.
132
     *
133
     * @param approved_userlist $userlist The approved context and user information to delete information for.
134
     */
135
    public static function delete_data_for_users(approved_userlist $userlist): void {
136
        $context = $userlist->get_context();
137
 
138
        if ($context instanceof \context_user) {
139
            static::delete_user_data($context->instanceid);
140
        }
141
    }
142
 
143
    /**
144
     * Retrieve the user metadata stored by plugin.
145
     *
146
     * @param collection $collection Collection of metadata.
147
     * @return collection Collection of metadata.
148
     */
149
    public static function get_metadata(collection $collection): collection {
150
        $collection->add_database_table(
151
            'gradepenalty_duedate_rule',
152
            [
153
                'usermodified' => 'privacy:metadata:gradepenalty_duedate_rule:usermodified',
154
              ],
155
            'privacy:metadata:gradepenalty_duedate_rule'
156
        );
157
        return $collection;
158
    }
159
 
160
    /**
161
     * Set the usermodified to 0 instead of deleting the data.
162
     *
163
     * @param  int $userid The id of the user.
164
     */
165
    protected static function delete_user_data(int $userid): void {
166
        global $DB;
167
 
168
        // Set the usermodified to 0.
169
        $DB->set_field_select('gradepenalty_duedate_rule', 'usermodified', 0,
170
            "usermodified = :userid", ['userid' => $userid]);
171
    }
172
}