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
/**
18
 * This file manages the privacy
19
 *
20
 * @package    mod_custommailing
21
 * @author     jeanfrancois@cblue.be
22
 * @copyright  2021 CBlue SPRL
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace mod_custommailing\privacy;
27
 
28
use core_privacy\local\metadata\collection;
29
use core_privacy\local\request\approved_contextlist;
30
use core_privacy\local\request\approved_userlist;
31
use core_privacy\local\request\contextlist;
32
use core_privacy\local\request\userlist;
33
use core_privacy\local\request\transform;
34
use context;
35
use context_module;
36
use core_privacy\local\request\writer;
37
use mod_custommailing\Mailing;
38
use mod_custommailing\MailingLog;
39
 
40
defined('MOODLE_INTERNAL') || die();
41
 
42
/**
43
 * Class provider
44
 * This plugin does not store any personal user data.
45
 *
46
 * @package mod_custommailing\privacy
47
 */
48
class provider implements
49
    \core_privacy\local\metadata\provider,
50
    \core_privacy\local\request\core_userlist_provider,
51
    \core_privacy\local\request\plugin\provider
52
{
53
 
54
    /**
55
     * @param collection $collection
56
     * @return collection
57
     */
58
    public static function get_metadata(collection $collection) : collection {
59
        $collection->add_database_table(
60
            'custommailing_logs',
61
            [
62
                'custommailingmailingid' => 'privacy:metadata:custommailingmailingid',
63
                'emailtouserid' => 'privacy:metadata:emailtouserid',
64
                'emailstatus' => 'privacy:metadata:emailstatus',
65
                'timecreated' => 'privacy:metadata:timecreated',
66
                'timemodified' => 'privacy:metadata:timemodified'
67
            ],
68
            'privacy:metadata:custommailing_logs'
69
        );
70
 
71
        return $collection;
72
    }
73
 
74
    /**
75
     * Get the list of contexts that contain user information for the specified user.
76
     *
77
     * @param int $userid The user to search.
78
     * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
79
     */
80
    public static function get_contexts_for_userid(int $userid) : contextlist {
81
        return (new contextlist)->add_from_sql(
82
            "SELECT ctx.id
83
                 FROM {context} ctx
84
                 JOIN {course_modules} cm ON cm.id = ctx.instanceid AND ctx.contextlevel = :contextlevel
85
                 JOIN {modules} m ON m.id = cm.module AND m.name = :modname
86
                 JOIN {custommailing} c ON c.id = cm.instance
87
                 JOIN {custommailing_mailing} cmm ON cmm.custommailingid = c.id
88
                 JOIN {custommailing_logs} cml ON cml.custommailingmailingid = cmm.id
89
                 WHERE cml.emailtouserid = :userid",
90
            [
91
                'modname' => 'custommailing',
92
                'contextlevel' => CONTEXT_MODULE,
93
                'userid' => $userid
94
            ]
95
        );
96
    }
97
 
98
    /**
99
     * Get the list of users who have data within a context.
100
     *
101
     * @param   userlist    $userlist   The userlist containing the list of users who have data in this context/plugin combination.
102
     */
103
    public static function get_users_in_context(userlist $userlist) {
104
        $context = $userlist->get_context();
105
        if (!is_a($context, \context_module::class)) {
106
            return;
107
        }
108
        $sql = "SELECT cml.emailtouserid as userid
109
                    FROM {custommailing_logs} cml
110
                    JOIN {custommailing_mailing} cmm ON cmm.id = cml.custommailingmailingid
111
                    JOIN {custommailing} c ON c.id = cmm.custommailingid
112
                    JOIN {course_modules} cm ON cm.instance = c.id
113
                    JOIN {modules} m ON m.id = cm.module AND m.name = 'custommailing'
114
                 WHERE cm.id = :instanceid";
115
 
116
        $params = [
117
            'modulename' => 'custommailing',
118
            'instanceid'    => $context->instanceid,
119
        ];
120
 
121
        $userlist->add_from_sql('userid', $sql, $params);
122
    }
123
 
124
    /**
125
     * Delete all data for all users in the specified context.
126
     *
127
     * @param context $context
128
     * @throws \coding_exception
129
     * @throws \dml_exception
130
     */
131
    public static function delete_data_for_all_users_in_context(context $context) {
132
 
133
        if (!$context instanceof context_module) {
134
            return;
135
        }
136
 
137
        if (!$cm = get_coursemodule_from_id('custommailing', $context->instanceid)) {
138
            return;
139
        }
140
 
141
        Mailing::deleteAll($cm->instance);
142
    }
143
 
144
    /**
145
     * Delete all user data for the specified user, in the specified contexts.
146
     *
147
     * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
148
     * @throws \coding_exception
149
     * @throws \dml_exception
150
     */
151
    public static function delete_data_for_user(approved_contextlist $contextlist) {
152
 
153
        if (empty($contextlist->count())) {
154
            return;
155
        }
156
        $userid = (int) $contextlist->get_user()->id;
157
 
158
        foreach ($contextlist as $context) {
159
            if (!$context instanceof context_module) {
160
                continue;
161
            }
162
            if (!$cm = get_coursemodule_from_id('custommailing', $context->instanceid)) {
163
                continue;
164
            }
165
            MailingLog::deleteByUser($userid);
166
        }
167
    }
168
 
169
    /**
170
     * Delete multiple users within a single context.
171
     *
172
     * @param   approved_userlist       $userlist The approved context and user information to delete information for.
173
     * @throws \dml_exception
174
     */
175
    public static function delete_data_for_users(approved_userlist $userlist) {
176
 
177
        $context = $userlist->get_context();
178
        if (!is_a($context, \context_module::class)) {
179
            return;
180
        }
181
        if (!$cm = get_coursemodule_from_id('custommailing', $context->instanceid)) {
182
            return;
183
        }
184
        $userids = $userlist->get_userids();
185
        foreach ($userids as $userid) {
186
            MailingLog::deleteByUser($userid);
187
        }
188
 
189
    }
190
 
191
    /**
192
     * Export all user data for the specified user, in the specified contexts.
193
     *
194
     * @param approved_contextlist $contextlist The approved contexts to export information for.
195
     */
196
    public static function export_user_data(approved_contextlist $contextlist) {
197
       global $DB;
198
 
199
        $user = $contextlist->get_user();
200
 
201
        list($contextsql, $contextparams) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED);
202
        $contextparams['userid'] = $user->id;
203
 
204
        $sql = "SELECT cml.*,ctx.id as contextid
205
                FROM {custommailing_logs} cml
206
                    JOIN {custommailing_mailing} cmm ON cmm.id = cml.custommailingmailingid
207
                    JOIN {custommailing} c ON c.id = cmm.custommailingid
208
                    JOIN {course_modules} cm ON cm.instance = c.id
209
                    JOIN {modules} m ON m.id = cm.module AND m.name = 'custommailing'
210
                    JOIN {context} ctx ON ctx.instanceid = cm.id
211
                WHERE cml.emailtouserid = :userid AND ctx.id {$contextsql}";
212
 
213
        $logs = $DB->get_records_sql($sql, $contextparams);
214
 
215
        foreach ($logs as $log) {
216
            $logdata = (object) [
217
                'custommailingmailingid' => $log->custommailingmailingid,
218
                'emailtouserid' => $log->emailtouserid,
219
                'emailstatus' => $log->emailstatus,
220
                'timecreated' => transform::datetime($log->timecreated),
221
                'timemodified' => transform::datetime($log->timemodified)
222
            ];
223
            $context = context::instance_by_id($log->contextid);
224
            writer::with_context($context)->export_data(
225
                [get_string('modulename', 'custommailing') . ' ' . $log->custommailingmailingid],
226
                $logdata
227
            );
228
        }
229
    }
230
}