Skip to content

Commit 597f103

Browse files
committed
issue #4: add privacy API
1 parent c11b4db commit 597f103

File tree

3 files changed

+226
-2
lines changed

3 files changed

+226
-2
lines changed

classes/privacy/provider.php

+218
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
<?php
2+
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
16+
17+
namespace tool_dynamic_cohorts\privacy;
18+
19+
use core_privacy\local\metadata\collection;
20+
use core_privacy\local\request\approved_contextlist;
21+
use core_privacy\local\request\approved_userlist;
22+
use core_privacy\local\request\contextlist;
23+
use core_privacy\local\request\userlist;
24+
use core_privacy\local\request\writer;
25+
26+
/**
27+
* Privacy Subsystem.
28+
*
29+
* @package tool_dynamic_cohorts
30+
* @copyright 2024 Catalyst IT
31+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32+
*/
33+
class provider implements \core_privacy\local\metadata\provider,
34+
\core_privacy\local\request\core_userlist_provider,
35+
\core_privacy\local\request\plugin\provider {
36+
37+
38+
/**
39+
* Return the fields which contain personal data.
40+
*
41+
* @param collection $collection a reference to the collection to use to store the metadata.
42+
* @return collection the updated collection of metadata items.
43+
*/
44+
public static function get_metadata(collection $collection) : collection {
45+
$collection->add_database_table(
46+
'tool_dynamic_cohorts',
47+
[
48+
'name' => 'privacy:metadata:tool_dynamic_cohorts:name',
49+
'usermodified' => 'privacy:metadata:tool_dynamic_cohorts:usermodified',
50+
51+
],
52+
'privacy:metadata:tool_dynamic_cohorts'
53+
);
54+
55+
$collection->add_database_table(
56+
'tool_dynamic_cohorts_c',
57+
[
58+
'ruleid' => 'privacy:metadata:tool_dynamic_cohorts:ruleid',
59+
'usermodified' => 'privacy:metadata:tool_dynamic_cohorts:usermodified',
60+
61+
],
62+
'privacy:metadata:tool_dynamic_cohorts_c'
63+
);
64+
65+
return $collection;
66+
}
67+
68+
/**
69+
* Get the list of contexts that contain user information for the specified user.
70+
*
71+
* @param int $userid the userid.
72+
* @return contextlist the list of contexts containing user info for the user.
73+
*/
74+
public static function get_contexts_for_userid(int $userid) : contextlist {
75+
global $DB;
76+
$contextlist = new contextlist();
77+
78+
if ($DB->record_exists('tool_dynamic_cohorts', ['usermodified' => $userid])) {
79+
$contextlist->add_system_context();
80+
}
81+
82+
if ($DB->record_exists('tool_dynamic_cohorts_c', ['usermodified' => $userid])) {
83+
$contextlist->add_system_context();
84+
}
85+
86+
return $contextlist;
87+
}
88+
89+
/**
90+
* Get the list of users who have data within a context.
91+
*
92+
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
93+
*/
94+
public static function get_users_in_context(userlist $userlist) {
95+
$context = $userlist->get_context();
96+
97+
if (!$context instanceof \context_system) {
98+
return;
99+
}
100+
101+
$sql = "SELECT usermodified FROM {tool_dynamic_cohorts}";
102+
$userlist->add_from_sql('usermodified', $sql, []);
103+
104+
$sql = "SELECT usermodified FROM {tool_dynamic_cohorts_c}";
105+
$userlist->add_from_sql('usermodified', $sql, []);
106+
}
107+
108+
/**
109+
* Export personal data for the given approved_contextlist. User and context information is contained within the contextlist.
110+
*
111+
* @param approved_contextlist $contextlist a list of contexts approved for export.
112+
*/
113+
public static function export_user_data(approved_contextlist $contextlist) {
114+
global $DB;
115+
116+
$user = $contextlist->get_user();
117+
118+
// Rules.
119+
$rules = [];
120+
$recordset = $DB->get_recordset('tool_dynamic_cohorts', ['usermodified' => $user->id], '', 'name');
121+
122+
foreach ($recordset as $record) {
123+
$rules[] = [
124+
'rulename' => format_string($record->name),
125+
];
126+
}
127+
$recordset->close();
128+
129+
if (count($rules) > 0) {
130+
$context = \context_system::instance();
131+
$contextpath = [get_string('pluginname', 'tool_dynamic_cohorts')];
132+
133+
writer::with_context($context)->export_data($contextpath, (object) ['rules' => $rules]);
134+
}
135+
136+
// Conditions.
137+
$conditions = [];
138+
$sql = 'SELECT c.*, r.name as rulename
139+
FROM {tool_dynamic_cohorts_c} c
140+
JOIN {tool_dynamic_cohorts} r ON (r.id = c.ruleid)
141+
WHERE c.usermodified = :userid
142+
ORDER BY r.id ASC';
143+
144+
$recordset = $DB->get_recordset_sql($sql, ['userid' => $user->id]);
145+
146+
foreach ($recordset as $record) {
147+
$conditions[] = [
148+
'rulename' => format_string($record->rulename),
149+
'classname' => format_string($record->classname),
150+
];
151+
}
152+
$recordset->close();
153+
154+
if (count($conditions) > 0) {
155+
$context = \context_system::instance();
156+
$contextpath = [get_string('pluginname', 'tool_dynamic_cohorts')];
157+
158+
writer::with_context($context)->export_data($contextpath, (object) ['conditions' => $conditions]);
159+
}
160+
}
161+
162+
/**
163+
* Delete all data for all users in the specified context.
164+
*
165+
* @param \context $context the context to delete in.
166+
*/
167+
public static function delete_data_for_all_users_in_context(\context $context) {
168+
global $DB;
169+
170+
if (!$context instanceof \context_system) {
171+
return;
172+
}
173+
174+
$DB->set_field('tool_dynamic_cohorts', 'usermodified', 0);
175+
$DB->set_field('tool_dynamic_cohorts_c', 'usermodified', 0);
176+
}
177+
178+
/**
179+
* Delete all user data for the specified user, in the specified contexts.
180+
*
181+
* @param approved_contextlist $contextlist a list of contexts approved for deletion.
182+
*/
183+
public static function delete_data_for_user(approved_contextlist $contextlist) {
184+
global $DB;
185+
186+
if (empty($contextlist->count())) {
187+
return;
188+
}
189+
190+
$userid = $contextlist->get_user()->id;
191+
foreach ($contextlist->get_contexts() as $context) {
192+
if (!$context instanceof \context_system) {
193+
continue;
194+
}
195+
196+
$DB->set_field('tool_dynamic_cohorts', 'usermodified', 0, ['usermodified' => $userid]);
197+
$DB->set_field('tool_dynamic_cohorts_c', 'usermodified', 0, ['usermodified' => $userid]);
198+
}
199+
}
200+
201+
/**
202+
* Delete multiple users within a single context.
203+
*
204+
* @param approved_userlist $userlist The approved context and user information to delete information for.
205+
*/
206+
public static function delete_data_for_users(approved_userlist $userlist) {
207+
global $DB;
208+
$context = $userlist->get_context();
209+
if (!$context instanceof \context_system) {
210+
return;
211+
}
212+
list($userinsql, $userinparams) = $DB->get_in_or_equal($userlist->get_userids(), SQL_PARAMS_NAMED);
213+
214+
$DB->set_field_select('tool_dynamic_cohorts', 'usermodified', 0, ' usermodified ' . $userinsql, $userinparams);
215+
$DB->set_field_select('tool_dynamic_cohorts_c', 'usermodified', 0, ' usermodified ' . $userinsql, $userinparams);
216+
}
217+
218+
}

lang/en/tool_dynamic_cohorts.php

+6
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,9 @@
2727

2828
$string['dynamic_cohorts:manage'] = 'Manage rules';
2929
$string['pluginname'] = 'Dynamic cohort rules';
30+
$string['privacy:metadata:tool_dynamic_cohorts'] = 'Information about rules created or updated by a user';
31+
$string['privacy:metadata:tool_dynamic_cohorts:name'] = 'Rule name';
32+
$string['privacy:metadata:tool_dynamic_cohorts:usermodified'] = 'The ID of the user who created or updated a rule';
33+
$string['privacy:metadata:tool_dynamic_cohorts_c'] = 'Information about conditions created or updated by a user';
34+
$string['privacy:metadata:tool_dynamic_cohorts_c:ruleid'] = 'ID of the rule';
35+
$string['privacy:metadata:tool_dynamic_cohorts_c:usermodified'] = 'The ID of the user who created or updated a condition';

version.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
defined('MOODLE_INTERNAL') || die();
2626

2727
$plugin->component = 'tool_dynamic_cohorts';
28-
$plugin->release = '0.1.0';
29-
$plugin->version = 2024030400;
28+
$plugin->release = 2024030401;
29+
$plugin->version = 2024030401;
3030
$plugin->requires = 2022112800;
3131
$plugin->supported = [401, 403];
3232
$plugin->maturity = MATURITY_ALPHA;

0 commit comments

Comments
 (0)