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
 * Privacy api class.
19
 * @package block_dedication
20
 * @copyright 2022 University of Canterbury
21
 * @author Pramith Dayananda <pramithd@catalyst.net.nz>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace block_dedication\privacy;
26
 
27
use context_block;
28
use core_privacy\local\metadata\collection;
29
use core_privacy\local\request\{approved_contextlist, approved_userlist, contextlist, userlist};
30
 
31
/**
32
 * Class provider
33
 * @package block_dedication
34
 */
35
class provider implements
36
    \core_privacy\local\metadata\provider,
37
    \core_privacy\local\request\plugin\provider,
38
    \core_privacy\local\request\core_userlist_provider {
39
 
40
    /**
41
     * Returns metadata.
42
     *
43
     * @param collection $collection The initialised collection to add items to.
44
     * @return collection A listing of user data stored through this system.
45
     */
46
    public static function get_metadata(collection $collection): collection {
47
        $collection->add_database_table(
48
            'block_dedication',
49
            [
50
                'userid' => 'privacy:metadata:block_dedication:userid',
51
                'courseid' => 'privacy:metadata:block_dedication:courseid',
52
                'timespent' => 'privacy:metadata:block_dedication:timespent',
53
                'timestart' => 'privacy:metadata:block_dedication:timestart',
54
            ],
55
            'privacy:metadata'
56
        );
57
        return $collection;
58
    }
59
 
60
    /**
61
     * Get the list of users who have data within a context.
62
     *
63
     * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
64
     */
65
    public static function get_users_in_context(userlist $userlist) {
66
        $context = $userlist->get_context();
67
        if (!$context instanceof context_block) {
68
            return;
69
        }
70
        $sql = "SELECT DISTINCT(userid) FROM {block_dedication}";
71
        $userlist->add_from_sql('userid', $sql, []);
72
    }
73
 
74
    /**
75
     * Delete users' dedication items.
76
     *
77
     * @param approved_userlist $userlist
78
     * @return void
79
     */
80
    public static function delete_data_for_users(approved_userlist $userlist) {
81
        global $DB;
82
        $userids = $userlist->get_userids();
83
        list($insql, $inparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
84
        $DB->delete_records_select('block_dedication', "userid $insql", $inparams);
85
    }
86
 
87
    /**
88
     * Get the list of contexts that contain user information for the specified user.
89
     *
90
     * In the case of attendance, that is any attendance where a student has had their
91
     * attendance taken or has taken attendance for someone else.
92
     *
93
     * @param int $userid The user to search.
94
     * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
95
     */
96
    public static function get_contexts_for_userid(int $userid): contextlist {
97
        return (new contextlist)->add_from_sql(
98
            "SELECT cx.id
99
            FROM {block_dedication} bd
100
            JOIN {context} cx ON cx.contextlevel = :blocklevel AND cx.instanceid = bd.id
101
            WHERE bd.userid = :userid",
102
            [
103
                'blocklevel' => CONTEXT_BLOCK,
104
                'userid' => $userid,
105
            ]
106
        );
107
    }
108
 
109
    /**
110
     * Delete all personal data for all users in the specified context.
111
     *
112
     * @param context $context Context to delete data from.
113
     */
114
    public static function delete_data_for_all_users_in_context(\context $context) {
115
        global $DB;
116
        if ($context instanceof \context_course) {
117
            $DB->delete_records('block_dedication', ['courseid' => $context->instanceid]);
118
        }
119
    }
120
 
121
    /**
122
     * Delete all user data for the specified user, in the specified contexts.
123
     *
124
     * @param approved_contextlist $contextlist The approved contextlist to delete information for.
125
     */
126
    public static function delete_data_for_user(approved_contextlist $contextlist) {
127
        global $DB;
128
        $userid = $contextlist->get_user()->id;
129
        foreach ($contextlist as $context) {
130
            if ($context instanceof \context_user) {
131
                $DB->delete_records('block_dedication', ['userid' => $userid]);
132
            } else if ($context instanceof \context_course) {
133
                $DB->delete_records('block_dedication', ['userid' => $userid, 'courseid' => $context->instanceid]);
134
            }
135
        }
136
    }
137
 
138
    /**
139
     * Export all user data for the specified user, in the specified contexts, using the supplied exporter instance.
140
     *
141
     * @param approved_contextlist $contextlist The approved contexts to export information for.
142
     */
143
    public static function export_user_data(approved_contextlist $contextlist) {
144
        global $DB;
145
        $data = [];
146
 
147
        $userid = (int) $contextlist->get_user()->id;
148
        $results = $DB->get_records('block_dedication', array('userid' => $userid));
149
        foreach ($results as $result) {
150
            $data[] = (object) [
151
                'courseid' => $result->courseid,
152
                'timespent' => $result->timespent,
153
                'timestart' => $result->timestart,
154
            ];
155
        }
156
        if (!empty($data)) {
157
            $data = (object) [
158
                'block_dedication' => $data,
159
            ];
160
            \core_privacy\local\request\writer::with_context($contextlist->current())->export_data(
161
                [get_string('pluginname', 'local_ace')],
162
                $data
163
            );
164
        }
165
    }
166
 
167
    /**
168
     * Get the block instance record for the specified context.
169
     *
170
     * @param   context_block $context The context to fetch
171
     * @return  stdClass
172
     */
173
    protected static function get_instance_from_context(context_block $context) {
174
        global $DB;
175
        return $DB->get_record('block_instances', ['id' => $context->instanceid, 'blockname' => 'dedication']);
176
    }
177
}