From 2ce4a2d94ba323c2dcc593246691c89d8e511039 Mon Sep 17 00:00:00 2001 From: PMKuipers Date: Sat, 28 Dec 2024 00:11:34 +0100 Subject: [PATCH] Partially added further code to privacy provider --- classes/privacy/provider.php | 137 +++++++++++++++++++++++++++++++---- 1 file changed, 121 insertions(+), 16 deletions(-) diff --git a/classes/privacy/provider.php b/classes/privacy/provider.php index c40b2cd..14bccca 100644 --- a/classes/privacy/provider.php +++ b/classes/privacy/provider.php @@ -34,6 +34,7 @@ use core_privacy\local\request\helper; use core_privacy\local\request\transform; use tool_dataprivacy\context_instance; use context; +use local_treestudyplan\studyline; /** * Privacy provider @@ -138,31 +139,107 @@ class provider implements \core_privacy\local\metadata\provider, if ($context instanceof \context_system) { // Export invitations. $sql = "SELECT * FROM {local_treestudyplan_invit} i - WHERE ( aiuser_id = :userid ) + WHERE ( i.user_id = :userid ) "; $records = $DB->get_records_sql($sql, ["userid" => $user->id]); foreach ($records as $r) { static::export_invitation_data_for_user($r); } - // Export empty associations. + // Export studyplan student associations. $sql = "SELECT * FROM {local_treestudyplan} s 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 = 0) + WHERE a.user_id = :userid AND (s.context_id IS NULL or s.context_id <= 1) "; - $records = $DB->get_records_sql($sql, ["userid" => $user->id, "contextid" => $context->id]); + $records = $DB->get_records_sql($sql, ["userid" => $user->id]); foreach ($records as $r) { - static::export_studyplan_data_for_user($r); + static::export_studyplan_data_for_user($r,"studyplan_student"); } + + // Export studyplan coaching associations. + $sql = "SELECT * FROM {local_treestudyplan} s + 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) + "; + $records = $DB->get_records_sql($sql, ["userid" => $user->id]); + foreach ($records as $r) { + static::export_studyplan_data_for_user($r,"studyplan_coach"); + } + + // Export studyplan teaching associations. + $sql = "SELECT * FROM {local_treestudyplan} s + 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) + "; + $records = $DB->get_records_sql($sql, ["userid" => $user->id]); + foreach ($records as $r) { + static::export_studyplan_data_for_user($r,"studyplan_teacher"); + } + + // Export studyline enrolled associations. + $sql = "SELECT lu.* FROM {local_treestudyplan_lineuser} e + 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} p ON p.studyplan_id = s.id + WHERE (e.user_id = :userid OR e.enrolledby = :euserid) + AND (s.context_id IS NULL or s.context_id <= 1) + "; + $records = $DB->get_records_sql($sql, ["userid" => $user->id,"euserid" => $user->id]); + foreach ($records as $r) { + if ($r->user_id == $user->id) { + static::export_line_enrolment_data_for_user($r,"lineuser_enrolled"); + } + if ($r->enrolledby == $user->id) { + static::export_line_enrolment_data_for_user($r,"linesuer_enroller"); + } + } + } else if ($context->contextlevel == CONTEXT_COURSECAT) { - // Export studyplan associations. + // Export studyplan student associations. $sql = "SELECT * FROM {local_treestudyplan} s INNER JOIN {local_treestudyplan_user} a ON a.studyplan_id = s.id WHERE ( a.user_id = :userid AND s.context_id = :contextid)"; $records = $DB->get_records_sql($sql, ["userid" => $user->id, "contextid" => $context->id]); foreach ($records as $r) { - static::export_studyplan_data_for_user($r); + static::export_studyplan_data_for_user($r,"studyplan_student"); } + + // Export studyplan coaching associations. + $sql = "SELECT * FROM {local_treestudyplan} s + INNER JOIN {local_treestudyplan_coach} a ON a.studyplan_id = s.id + WHERE ( a.user_id = :userid AND s.context_id = :contextid)"; + $records = $DB->get_records_sql($sql, ["userid" => $user->id, "contextid" => $context->id]); + foreach ($records as $r) { + static::export_studyplan_data_for_user($r,"studyplan_coach"); + } + + // Export studyplan teaching associations. + $sql = "SELECT * FROM {local_treestudyplan} s + INNER JOIN {local_treestudyplan_teachers} a ON a.studyplan_id = s.id + WHERE ( a.teacher_id = :userid AND s.context_id = :contextid)"; + $records = $DB->get_records_sql($sql, ["userid" => $user->id, "contextid" => $context->id]); + foreach ($records as $r) { + static::export_studyplan_data_for_user($r,"studyplan_teacher"); + } + + // Export studyline enrolled associations. + $sql = "SELECT lu.* FROM {local_treestudyplan_lineuser} e + 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} p ON p.studyplan_id = s.id + WHERE (e.user_id = :userid OR e.enrolledby = :euserid) + AND (s.context_id = :contextid) + "; + $records = $DB->get_records_sql($sql, ["userid" => $user->id,"euserid" => $user->id]); + foreach ($records as $r) { + if ($r->user_id == $user->id) { + static::export_line_enrolment_data_for_user($r,"lineuser_enrolled"); + } + if ($r->enrolledby == $user->id) { + static::export_line_enrolment_data_for_user($r,"linesuer_enroller"); + } + } + } } } @@ -179,22 +256,37 @@ class provider implements \core_privacy\local\metadata\provider, $data->recipient = $invit->name; $data->email = $invit->email; writer::with_context($context)->export_data($subcontext, $data); - } /** * Export studyplan data for (current) user * @param stdClass $studyplan The studyplan */ - protected static function export_studyplan_data_for_user($studyplan) { + protected static function export_studyplan_data_for_user($studyplan,$subcontext) { + $context = \context_system::instance(); + $data = new \stdClass; + $data->fullname = $studyplan->name(); + $data->shortname = $studyplan->shortname(); + writer::with_context($context)->export_data([$subcontext], $data); + } + + /** + * Export the supplied personal data for a study line enrollment. + * @param stdClass $invit The invitation record. + */ + protected static function export_line_enrolment_data_for_user($enrol,$subcontext) { $context = \context_system::instance(); - $subcontext = ["studyplans"]; $data = new \stdClass; - $data->fullname = $studyplan->fullname; - $data->shortname = $studyplan->shortname; - writer::with_context($context)->export_data($subcontext, $data); - + $line = studyline::find_by_id($enrol->line_id); + $studyplan = $line->studyplan(); + $page = $line->page(); + $data->planname = $studyplan->name(); + $data->pagename = $page->fullname(); + $data->linename = $line->name(); + $data->enrolled = ($enrol->enrolled)?"True":"False"; + $data->enrolledsince = new \DateTime($enrol->timeenrolled); + writer::with_context($context)->export_data([$subcontext], $data); } /** @@ -205,7 +297,7 @@ class provider implements \core_privacy\local\metadata\provider, public static function delete_data_for_all_users_in_context(context $context) { global $DB; // Find studyplans in context. - if ($context->contextlevel == CONTEXT_COURSECAT) { + 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 ( a.user_id = :userid AND s.context_id = :contextid)"; $planids = $DB->get_fieldset_sql($sql, ["contextid" => $context->id]); @@ -213,6 +305,10 @@ class provider implements \core_privacy\local\metadata\provider, $DB->delete_records("local_treestudyplan_user", ["studyplan_id" => $planid]); } } + + // TODO: REMOVE INVITATIONS FOR THE RELEVANT STUDYPLANS AS WELL + // TODO: REMOVE LINE ENROLLMENTS FOR THESE STUDYPLANS + // TODO: REMOVE COACHES FOR THESE STUDYPLANS } /** @@ -243,6 +339,11 @@ class provider implements \core_privacy\local\metadata\provider, } } } + + // TODO: REMOVE LINE ENROLLMENTS + // TODO: Replace ENROLLEDBY by id 2 (ADMIN user) + // TODO: REMOVE COACHES + // TODO: REMOVE TEACHING CACHE } /** @@ -261,7 +362,7 @@ class provider implements \core_privacy\local\metadata\provider, // Also add "contextless studyplans, they are considered in system context". $sql = "SELECT a.user_id as userid FROM {local_treestudyplan_user} a INNER JOIN {local_treestudyplan} s ON a.studyplan_id = s.id - WHERE ( a.context_id is NULL or a.context_id = 0) + WHERE ( a.context_id is NULL or a.context_id < 1) "; $userlist->add_from_sql('userid', $sql, []); @@ -274,6 +375,8 @@ class provider implements \core_privacy\local\metadata\provider, "; $userlist->add_from_sql('userid', $sql, ["contextid" => $context->id]); + // TODO: FIX THIS WITH ALL OF THE ABOVE TODOS + } /** @@ -314,6 +417,8 @@ class provider implements \core_privacy\local\metadata\provider, $sql = "user_id {$userinsql} and studyplan_id {$planinsql}"; $DB->delete_records_select('local_treestudyplan_user', $sql, $params); } + + // TODO: FIX THIS WITH ALL OF THE ABOVE TODOS } }