Fully implemented privacy API

This commit is contained in:
PMKuipers 2024-12-29 16:09:33 +01:00
parent 7b66bea57d
commit e8f96845f0

View file

@ -34,6 +34,8 @@ use core_privacy\local\request\helper;
use core_privacy\local\request\transform; use core_privacy\local\request\transform;
use tool_dataprivacy\context_instance; use tool_dataprivacy\context_instance;
use context; use context;
use context_system;
use local_treestudyplan\contextinfo;
use local_treestudyplan\studyline; use local_treestudyplan\studyline;
use local_treestudyplan\studyplan; use local_treestudyplan\studyplan;
@ -81,7 +83,7 @@ class provider implements \core_privacy\local\metadata\provider,
$collection->add_database_table( $collection->add_database_table(
'local_treestudyplan_teachers', 'local_treestudyplan_teachers',
[ [
'teacher_id' => 'privacy:metadata:teachers:user_id', 'teacher_id' => 'privacy:metadata:teachers:teacher_id',
'studyplan_id' => 'privacy:metadata:teachers:studyplan_id', 'studyplan_id' => 'privacy:metadata:teachers:studyplan_id',
], ],
'privacy:metadata:teachers' 'privacy:metadata:teachers'
@ -120,10 +122,28 @@ class provider implements \core_privacy\local\metadata\provider,
// Add contexts for linked studyplans. // Add contexts for linked studyplans.
$sql = "SELECT s.context_id FROM {local_treestudyplan} s $sql = "SELECT s.context_id FROM {local_treestudyplan} s
INNER JOIN {local_treestudyplan_user} a ON a.studyplan_id = s.id INNER JOIN {local_treestudyplan_user} a ON a.studyplan_id = s.id
WHERE ( a.user_id = :userid ) WHERE ( a.user_id = :userid )";
";
$contextlist->add_from_sql($sql, ['userid' => $userid]); $contextlist->add_from_sql($sql, ['userid' => $userid]);
// Add contexts for coaching studyplans.
$sql = "SELECT s.context_id FROM {local_treestudyplan} s
INNER JOIN {local_treestudyplan_coach} a ON a.studyplan_id = s.id
WHERE ( a.user_id = :userid )";
$contextlist->add_from_sql($sql, ['userid' => $userid]);
// Add contexts for teaching studyplans.
$sql = "SELECT s.context_id FROM {local_treestudyplan} s
INNER JOIN {local_treestudyplan_teachers} a ON a.studyplan_id = s.id
WHERE ( a.teacher_id = :userid )";
$contextlist->add_from_sql($sql, ['userid' => $userid]);
$sql = "SELECT s.context_id FROM {local_treestudyplan} s
INNER JOIN {local_treestudyplan_page} p ON p.studyplan_id = s.id
INNER JOIN {local_treestudyplan_line} l ON l.page_id = p.id
INNER JOIN {local_treestudyplan_lineuser} e ON e.line_id = l.id
WHERE (e.user_id = :userid OR e.enrolledby = :euserid)";
$contextlist->add_from_sql($sql, ['userid' => $userid, 'euserid' => $userid]);
return $contextlist; return $contextlist;
} }
@ -137,110 +157,112 @@ class provider implements \core_privacy\local\metadata\provider,
foreach ($contextlist->get_contexts() as $context) { foreach ($contextlist->get_contexts() as $context) {
$user = $contextlist->get_user(); $user = $contextlist->get_user();
if ($context instanceof \context_system) { if ($context->contextlevel == CONTEXT_SYSTEM) {
// Export invitations. // Export invitations.
$sql = "SELECT * FROM {local_treestudyplan_invit} i $sql = "SELECT * FROM {local_treestudyplan_invit} i
WHERE ( i.user_id = :userid ) WHERE ( i.user_id = :userid )
"; ";
$records = $DB->get_records_sql($sql, ["userid" => $user->id]); $records = $DB->get_records_sql($sql, ["userid" => $user->id]);
foreach ($records as $r) { foreach ($records as $r) {
static::export_invitation_data_for_user($r); static::export_invitation_data_for_user($r, $context);
} }
// Export studyplan student associations. // Export studyplan student associations.
$sql = "SELECT * FROM {local_treestudyplan} s $sql = "SELECT s.id as id FROM {local_treestudyplan} s
INNER JOIN {local_treestudyplan_user} a ON a.studyplan_id = s.id INNER JOIN {local_treestudyplan_user} a ON a.studyplan_id = s.id
WHERE a.user_id = :userid AND (s.context_id IS NULL or s.context_id <= 1) WHERE a.user_id = :userid AND (s.context_id IS NULL or s.context_id <= 1)";
"; $ids = $DB->get_fieldset_sql($sql, ["userid" => $user->id]);
$records = $DB->get_records_sql($sql, ["userid" => $user->id]); foreach ($ids as $id) {
foreach ($records as $r) { $plan = studyplan::find_by_id($id);
static::export_studyplan_data_for_user($r,"studyplan_student"); static::export_studyplan_data_for_user($plan, $context, "Student (directly linked)");
} }
// Export studyplan coaching associations. // Export studyplan coaching associations.
$sql = "SELECT * FROM {local_treestudyplan} s $sql = "SELECT s.id FROM {local_treestudyplan} s
INNER JOIN {local_treestudyplan_coach} a ON a.studyplan_id = s.id INNER JOIN {local_treestudyplan_coach} a ON a.studyplan_id = s.id
WHERE a.user_id = :userid AND (s.context_id IS NULL or s.context_id <= 1) WHERE a.user_id = :userid AND (s.context_id IS NULL or s.context_id <= 1)";
"; $ids = $DB->get_fieldset_sql($sql, ["userid" => $user->id]);
$records = $DB->get_records_sql($sql, ["userid" => $user->id]); foreach ($ids as $id) {
foreach ($records as $r) { $plan = studyplan::find_by_id($id);
static::export_studyplan_data_for_user($r,"studyplan_coach"); static::export_studyplan_data_for_user($plan, $context, "Coach");
} }
// Export studyplan teaching associations. // Export studyplan teaching associations.
$sql = "SELECT * FROM {local_treestudyplan} s $sql = "SELECT s.id FROM {local_treestudyplan} s
INNER JOIN {local_treestudyplan_teachers} a ON a.studyplan_id = s.id INNER JOIN {local_treestudyplan_teachers} a ON a.studyplan_id = s.id
WHERE a.teacher_id = :userid AND (s.context_id IS NULL or s.context_id <= 1) WHERE a.teacher_id = :userid AND (s.context_id IS NULL or s.context_id <= 1)";
"; $ids = $DB->get_fieldset_sql($sql, ["userid" => $user->id]);
$records = $DB->get_records_sql($sql, ["userid" => $user->id]); foreach ($ids as $id) {
foreach ($records as $r) { $plan = studyplan::find_by_id($id);
static::export_studyplan_data_for_user($r,"studyplan_teacher"); static::export_studyplan_data_for_user($plan, $context, "Teaching");
} }
// Export studyline enrolled associations. // Export studyline enrolled associations.
$sql = "SELECT lu.* FROM {local_treestudyplan_lineuser} e $sql = "SELECT e.* FROM {local_treestudyplan_lineuser} e
INNER JOIN {local_treestudyplan_line} l ON e.line_id = l.id INNER JOIN {local_treestudyplan_line} l ON e.line_id = l.id
INNER JOIN {local_treestudyplan_page} p ON l.page_id = p.id INNER JOIN {local_treestudyplan_page} p ON l.page_id = p.id
INNER JOIN {local_treestudyplan} p ON p.studyplan_id = s.id INNER JOIN {local_treestudyplan} s ON p.studyplan_id = s.id
WHERE (e.user_id = :userid OR e.enrolledby = :euserid) WHERE (e.user_id = :userid OR e.enrolledby = :euserid)
AND (s.context_id IS NULL or s.context_id <= 1) AND (s.context_id IS NULL or s.context_id <= 1)
"; ";
$records = $DB->get_records_sql($sql, ["userid" => $user->id,"euserid" => $user->id]); $records = $DB->get_records_sql($sql, ["userid" => $user->id, "euserid" => $user->id]);
foreach ($records as $r) { foreach ($records as $r) {
if ($r->user_id == $user->id) { if ($r->user_id == $user->id) {
static::export_line_enrolment_data_for_user($r,"lineuser_enrolled"); static::export_line_enrolment_data_for_user($r, $context, "Enrolled in a line");
} }
if ($r->enrolledby == $user->id) { if ($r->enrolledby == $user->id) {
static::export_line_enrolment_data_for_user($r,"linesuer_enroller"); static::export_line_enrolment_data_for_user($r, $context, "Enrolled another user in a line");
} }
} }
} else if ($context->contextlevel == CONTEXT_COURSECAT) { } else if ($context->contextlevel == CONTEXT_COURSECAT) {
// Export studyplan student associations. // Export studyplan student associations.
$sql = "SELECT * FROM {local_treestudyplan} s $sql = "SELECT s.id FROM {local_treestudyplan} s
INNER JOIN {local_treestudyplan_user} a ON a.studyplan_id = s.id INNER JOIN {local_treestudyplan_user} a ON a.studyplan_id = s.id
WHERE ( a.user_id = :userid AND s.context_id = :contextid)"; WHERE ( a.user_id = :userid AND s.context_id = :contextid)";
$records = $DB->get_records_sql($sql, ["userid" => $user->id, "contextid" => $context->id]); $ids = $DB->get_fieldset_sql($sql, ["userid" => $user->id, "contextid" => $context->id]);
foreach ($records as $r) { foreach ($ids as $id) {
static::export_studyplan_data_for_user($r,"studyplan_student"); $plan = studyplan::find_by_id($id);
static::export_studyplan_data_for_user($plan, $context, "Student (directly linked)");
} }
// Export studyplan coaching associations. // Export studyplan coaching associations.
$sql = "SELECT * FROM {local_treestudyplan} s $sql = "SELECT s.id FROM {local_treestudyplan} s
INNER JOIN {local_treestudyplan_coach} a ON a.studyplan_id = s.id INNER JOIN {local_treestudyplan_coach} a ON a.studyplan_id = s.id
WHERE ( a.user_id = :userid AND s.context_id = :contextid)"; WHERE ( a.user_id = :userid AND s.context_id = :contextid)";
$records = $DB->get_records_sql($sql, ["userid" => $user->id, "contextid" => $context->id]); $ids = $DB->get_fieldset_sql($sql, ["userid" => $user->id, "contextid" => $context->id]);
foreach ($records as $r) { foreach ($ids as $id) {
static::export_studyplan_data_for_user($r,"studyplan_coach"); $plan = studyplan::find_by_id($id);
static::export_studyplan_data_for_user($plan, $context, "Coach");
} }
// Export studyplan teaching associations. // Export studyplan teaching associations.
$sql = "SELECT * FROM {local_treestudyplan} s $sql = "SELECT s.id FROM {local_treestudyplan} s
INNER JOIN {local_treestudyplan_teachers} a ON a.studyplan_id = s.id INNER JOIN {local_treestudyplan_teachers} a ON a.studyplan_id = s.id
WHERE ( a.teacher_id = :userid AND s.context_id = :contextid)"; WHERE ( a.teacher_id = :userid AND s.context_id = :contextid)";
$records = $DB->get_records_sql($sql, ["userid" => $user->id, "contextid" => $context->id]); $ids = $DB->get_fieldset_sql($sql, ["userid" => $user->id, "contextid" => $context->id]);
foreach ($records as $r) { foreach ($ids as $id) {
static::export_studyplan_data_for_user($r,"studyplan_teacher"); $plan = studyplan::find_by_id($id);
static::export_studyplan_data_for_user($plan, $context, "Teaching");
} }
// Export studyline enrolled associations. // Export studyline enrolled associations.
$sql = "SELECT lu.* FROM {local_treestudyplan_lineuser} e $sql = "SELECT e.* FROM {local_treestudyplan_lineuser} e
INNER JOIN {local_treestudyplan_line} l ON e.line_id = l.id INNER JOIN {local_treestudyplan_line} l ON e.line_id = l.id
INNER JOIN {local_treestudyplan_page} p ON l.page_id = p.id INNER JOIN {local_treestudyplan_page} p ON l.page_id = p.id
INNER JOIN {local_treestudyplan} p ON p.studyplan_id = s.id INNER JOIN {local_treestudyplan} s ON p.studyplan_id = s.id
WHERE (e.user_id = :userid OR e.enrolledby = :euserid) WHERE (e.user_id = :userid OR e.enrolledby = :euserid)
AND (s.context_id = :contextid) AND (s.context_id = :contextid)
"; ";
$records = $DB->get_records_sql($sql, ["userid" => $user->id,"euserid" => $user->id]); $records = $DB->get_records_sql($sql, ["userid" => $user->id, "euserid" => $user->id, "contextid" => $context->id]);
foreach ($records as $r) { foreach ($records as $r) {
if ($r->user_id == $user->id) { if ($r->user_id == $user->id) {
static::export_line_enrolment_data_for_user($r,"lineuser_enrolled"); static::export_line_enrolment_data_for_user($r, $context, "Enrolled in a studyplan line");
} }
if ($r->enrolledby == $user->id) { if ($r->enrolledby == $user->id) {
static::export_line_enrolment_data_for_user($r,"linesuer_enroller"); static::export_line_enrolment_data_for_user($r, $context, "Enrolled another user in a studyplan line");
} }
} }
} }
} }
} }
@ -248,11 +270,10 @@ class provider implements \core_privacy\local\metadata\provider,
/** /**
* Export the supplied personal data for an invitation. * Export the supplied personal data for an invitation.
* @param stdClass $invit The invitation record. * @param stdClass $invit The invitation record.
* @param context $context The relevant context.
*/ */
protected static function export_invitation_data_for_user($invit) { protected static function export_invitation_data_for_user($invit, context $context) {
$context = \context_system::instance(); $subcontext = ["Studyplan", "Invitations"];
$subcontext = ["invitations"];
$data = new \stdClass; $data = new \stdClass;
$data->recipient = $invit->name; $data->recipient = $invit->name;
$data->email = $invit->email; $data->email = $invit->email;
@ -261,33 +282,41 @@ class provider implements \core_privacy\local\metadata\provider,
/** /**
* Export studyplan data for (current) user * Export studyplan data for (current) user
* @param stdClass $studyplan The studyplan * @param studyplan $studyplan The studyplan
* @param context $context The relevant context.
*/ */
protected static function export_studyplan_data_for_user($studyplan,$subcontext) { protected static function export_studyplan_data_for_user(studyplan $studyplan, $context, $subcontext) {
$context = \context_system::instance();
$data = new \stdClass; $data = new \stdClass;
$data->fullname = $studyplan->name(); $data->fullname = $studyplan->name();
$data->shortname = $studyplan->shortname(); $data->shortname = $studyplan->shortname();
writer::with_context($context)->export_data([$subcontext], $data); $data->idnumber = $studyplan->idnumber();
$data->context = (new contextinfo($context))->path();
$path = ($context->contextlevel == CONTEXT_SYSTEM) ? [] : (new contextinfo($context))->path();
$subcontextpath = array_merge(["Studyplan",$subcontext], $path, [$studyplan->name()]);
writer::with_context(context_system::instance())->export_data($subcontextpath, $data);
} }
/** /**
* Export the supplied personal data for a study line enrollment. * Export the supplied personal data for a study line enrollment.
* @param stdClass $invit The invitation record. * @param stdClass $invit The invitation record.
* @param context $context The relevant context.
*/ */
protected static function export_line_enrolment_data_for_user($enrol,$subcontext) { protected static function export_line_enrolment_data_for_user($enrol, $context, $subcontext) {
$context = \context_system::instance();
$data = new \stdClass; $data = new \stdClass;
$line = studyline::find_by_id($enrol->line_id); $line = studyline::find_by_id($enrol->line_id);
$studyplan = $line->studyplan(); $studyplan = $line->studyplan();
$page = $line->page(); $page = $line->page();
$data->planname = $studyplan->name(); $data->studyplan = $studyplan->name();
$data->pagename = $page->fullname(); $data->page = $page->fullname();
$data->linename = $line->name(); $data->line = $line->name();
$data->enrolled = ($enrol->enrolled)?"True":"False"; $data->enrolled = ($enrol->enrolled) ? "True" : "False";
$data->enrolledsince = new \DateTime($enrol->timeenrolled); $data->enrolledsince = (new \DateTime($enrol->timeenrolled))->format("X-m-d\\TH:i:sP");
writer::with_context($context)->export_data([$subcontext], $data);
$path = ($context->contextlevel == CONTEXT_SYSTEM) ? [] : (new contextinfo($context))->path();
$subcontextpath = array_merge(["Studyplan",$subcontext], $path, [$studyplan->name()]);
writer::with_context(context_system::instance())->export_data($subcontextpath, $data);
} }
/** /**
@ -299,7 +328,7 @@ class provider implements \core_privacy\local\metadata\provider,
global $DB; global $DB;
// Find studyplans in context. // Find studyplans in context.
if ($context->contextlevel == CONTEXT_COURSECAT) { // The system context (probably) never be triggered like this, so limit code to Categories. if ($context->contextlevel == CONTEXT_COURSECAT) { // The system context (probably) never be triggered like this, so limit code to Categories.
$sql = "SELECT s.id FROM {local_treestudyplan} WHERE (s.context_id = :contextid)"; $sql = "SELECT s.id FROM {local_treestudyplan} s WHERE (s.context_id = :contextid)";
$planids = $DB->get_fieldset_sql($sql, ["contextid" => $context->id]); $planids = $DB->get_fieldset_sql($sql, ["contextid" => $context->id]);
// Remove all associated users to the studyplan. // Remove all associated users to the studyplan.
@ -329,7 +358,7 @@ class provider implements \core_privacy\local\metadata\provider,
* @param context $context The context in which to remove the user * @param context $context The context in which to remove the user
* @param int $userid The userid of the user to remove * @param int $userid The userid of the user to remove
*/ */
private static function delete_user_in_context(\context $context, int $userid) { public static function delete_userdata_in_context(\context $context, int $userid) {
global $DB; global $DB;
if ($context->contextlevel == CONTEXT_SYSTEM || $context->contextlevel == CONTEXT_COURSECAT) { if ($context->contextlevel == CONTEXT_SYSTEM || $context->contextlevel == CONTEXT_COURSECAT) {
if ($context->contextlevel == CONTEXT_SYSTEM) { if ($context->contextlevel == CONTEXT_SYSTEM) {
@ -337,7 +366,7 @@ class provider implements \core_privacy\local\metadata\provider,
$DB->delete_records("local_treestudyplan_invit", ["user_id" => $userid]); $DB->delete_records("local_treestudyplan_invit", ["user_id" => $userid]);
// Retrieve all studyplans in system context. // Retrieve all studyplans in system context.
$sql = "SELECT s.id FROM {local_treestudyplan} INNER JOIN {local_treestudyplan_user} a ON a.studyplan_id = s.id $sql = "SELECT s.id FROM {local_treestudyplan} s INNER JOIN {local_treestudyplan_user} a ON a.studyplan_id = s.id
WHERE (s.context_id <= 1)"; WHERE (s.context_id <= 1)";
$planids = $DB->get_fieldset_sql($sql); $planids = $DB->get_fieldset_sql($sql);
@ -350,7 +379,7 @@ class provider implements \core_privacy\local\metadata\provider,
} else { // if ($context->contextlevel == CONTEXT_COURSECAT) { } else { // if ($context->contextlevel == CONTEXT_COURSECAT) {
// Retrieve all studyplans in this category. // Retrieve all studyplans in this category.
$sql = "SELECT s.id FROM {local_treestudyplan} INNER JOIN {local_treestudyplan_user} a ON a.studyplan_id = s.id $sql = "SELECT s.id FROM {local_treestudyplan} s INNER JOIN {local_treestudyplan_user} a ON a.studyplan_id = s.id
WHERE (s.context_id = :contextid)"; WHERE (s.context_id = :contextid)";
$planids = $DB->get_fieldset_sql($sql, ["contextid" => $context->id]); $planids = $DB->get_fieldset_sql($sql, ["contextid" => $context->id]);
@ -365,7 +394,6 @@ class provider implements \core_privacy\local\metadata\provider,
foreach ($planids as $planid) { foreach ($planids as $planid) {
// Remove all associated users to the studyplan. // Remove all associated users to the studyplan.
$DB->delete_records("local_treestudyplan_user", ["studyplan_id" => $planid, "user_id" => $userid]); $DB->delete_records("local_treestudyplan_user", ["studyplan_id" => $planid, "user_id" => $userid]);
$DB->delete_records("local_treestudyplan_cohort", ["studyplan_id" => $planid, "user_id" => $userid]);
$DB->delete_records("local_treestudyplan_teachers", ["studyplan_id" => $planid, "teacher_id" => $userid]); $DB->delete_records("local_treestudyplan_teachers", ["studyplan_id" => $planid, "teacher_id" => $userid]);
$DB->delete_records("local_treestudyplan_coach", ["studyplan_id" => $planid, "user_id" => $userid]); $DB->delete_records("local_treestudyplan_coach", ["studyplan_id" => $planid, "user_id" => $userid]);
} }
@ -374,10 +402,10 @@ class provider implements \core_privacy\local\metadata\provider,
$DB->delete_records("local_treestudyplan_lineuser", ["line_id" => $lineid, "user_id" => $userid]); $DB->delete_records("local_treestudyplan_lineuser", ["line_id" => $lineid, "user_id" => $userid]);
// Replace all enrolledby references with the admin user (2). // Replace all enrolledby references with the admin user (2).
$records = $DB->get_records("local_treestudyplan_lineuser",["line_id" => $lineid, "enrolledby" => $userid]); $records = $DB->get_records("local_treestudyplan_lineuser", ["line_id" => $lineid, "enrolledby" => $userid]);
foreach ($records as $r) { foreach ($records as $r) {
$r->enrolledby = 2; // Replace by admin user. $r->enrolledby = 2; // Replace by admin user.
$DB->update_record("local_treestudyplan_lineuser",$r); $DB->update_record("local_treestudyplan_lineuser", $r);
} }
} }
} }
@ -393,7 +421,7 @@ class provider implements \core_privacy\local\metadata\provider,
$user = $contextlist->get_user(); $user = $contextlist->get_user();
foreach ($contextlist->get_contexts() as $context) { foreach ($contextlist->get_contexts() as $context) {
static::delete_user_in_context($context, $user->id); static::delete_userdata_in_context($context, $user->id);
} }
} }
@ -480,7 +508,7 @@ class provider implements \core_privacy\local\metadata\provider,
$users = $userlist->get_userids(); $users = $userlist->get_userids();
foreach ($users as $userid) { foreach ($users as $userid) {
static::delete_user_in_context($context,$userid); static::delete_userdata_in_context($context, $userid);
} }
} }