Fully implemented privacy API
This commit is contained in:
parent
7b66bea57d
commit
e8f96845f0
1 changed files with 104 additions and 76 deletions
|
@ -34,6 +34,8 @@ use core_privacy\local\request\helper;
|
|||
use core_privacy\local\request\transform;
|
||||
use tool_dataprivacy\context_instance;
|
||||
use context;
|
||||
use context_system;
|
||||
use local_treestudyplan\contextinfo;
|
||||
use local_treestudyplan\studyline;
|
||||
use local_treestudyplan\studyplan;
|
||||
|
||||
|
@ -81,7 +83,7 @@ class provider implements \core_privacy\local\metadata\provider,
|
|||
$collection->add_database_table(
|
||||
'local_treestudyplan_teachers',
|
||||
[
|
||||
'teacher_id' => 'privacy:metadata:teachers:user_id',
|
||||
'teacher_id' => 'privacy:metadata:teachers:teacher_id',
|
||||
'studyplan_id' => 'privacy:metadata:teachers:studyplan_id',
|
||||
],
|
||||
'privacy:metadata:teachers'
|
||||
|
@ -120,10 +122,28 @@ class provider implements \core_privacy\local\metadata\provider,
|
|||
// Add contexts for linked studyplans.
|
||||
$sql = "SELECT s.context_id FROM {local_treestudyplan} s
|
||||
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]);
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
@ -137,110 +157,112 @@ class provider implements \core_privacy\local\metadata\provider,
|
|||
foreach ($contextlist->get_contexts() as $context) {
|
||||
$user = $contextlist->get_user();
|
||||
|
||||
if ($context instanceof \context_system) {
|
||||
if ($context->contextlevel == CONTEXT_SYSTEM) {
|
||||
// Export invitations.
|
||||
$sql = "SELECT * FROM {local_treestudyplan_invit} i
|
||||
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);
|
||||
static::export_invitation_data_for_user($r, $context);
|
||||
}
|
||||
|
||||
// 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
|
||||
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_student");
|
||||
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]);
|
||||
foreach ($ids as $id) {
|
||||
$plan = studyplan::find_by_id($id);
|
||||
static::export_studyplan_data_for_user($plan, $context, "Student (directly linked)");
|
||||
}
|
||||
|
||||
// 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
|
||||
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");
|
||||
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]);
|
||||
foreach ($ids as $id) {
|
||||
$plan = studyplan::find_by_id($id);
|
||||
static::export_studyplan_data_for_user($plan, $context, "Coach");
|
||||
}
|
||||
|
||||
// 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
|
||||
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");
|
||||
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]);
|
||||
foreach ($ids as $id) {
|
||||
$plan = studyplan::find_by_id($id);
|
||||
static::export_studyplan_data_for_user($plan, $context, "Teaching");
|
||||
}
|
||||
|
||||
// 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)
|
||||
$sql = "SELECT e.* 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} s 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]);
|
||||
$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");
|
||||
static::export_line_enrolment_data_for_user($r, $context, "Enrolled in a line");
|
||||
}
|
||||
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) {
|
||||
// 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
|
||||
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_student");
|
||||
$ids = $DB->get_fieldset_sql($sql, ["userid" => $user->id, "contextid" => $context->id]);
|
||||
foreach ($ids as $id) {
|
||||
$plan = studyplan::find_by_id($id);
|
||||
static::export_studyplan_data_for_user($plan, $context, "Student (directly linked)");
|
||||
}
|
||||
|
||||
// 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
|
||||
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");
|
||||
$ids = $DB->get_fieldset_sql($sql, ["userid" => $user->id, "contextid" => $context->id]);
|
||||
foreach ($ids as $id) {
|
||||
$plan = studyplan::find_by_id($id);
|
||||
static::export_studyplan_data_for_user($plan, $context, "Coach");
|
||||
}
|
||||
|
||||
// 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
|
||||
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");
|
||||
$ids = $DB->get_fieldset_sql($sql, ["userid" => $user->id, "contextid" => $context->id]);
|
||||
foreach ($ids as $id) {
|
||||
$plan = studyplan::find_by_id($id);
|
||||
static::export_studyplan_data_for_user($plan, $context, "Teaching");
|
||||
}
|
||||
|
||||
// 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_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)
|
||||
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) {
|
||||
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) {
|
||||
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.
|
||||
* @param stdClass $invit The invitation record.
|
||||
* @param context $context The relevant context.
|
||||
*/
|
||||
protected static function export_invitation_data_for_user($invit) {
|
||||
$context = \context_system::instance();
|
||||
$subcontext = ["invitations"];
|
||||
|
||||
protected static function export_invitation_data_for_user($invit, context $context) {
|
||||
$subcontext = ["Studyplan", "Invitations"];
|
||||
$data = new \stdClass;
|
||||
$data->recipient = $invit->name;
|
||||
$data->email = $invit->email;
|
||||
|
@ -261,33 +282,41 @@ class provider implements \core_privacy\local\metadata\provider,
|
|||
|
||||
/**
|
||||
* 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) {
|
||||
$context = \context_system::instance();
|
||||
protected static function export_studyplan_data_for_user(studyplan $studyplan, $context, $subcontext) {
|
||||
$data = new \stdClass;
|
||||
$data->fullname = $studyplan->name();
|
||||
$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.
|
||||
* @param stdClass $invit The invitation record.
|
||||
* @param context $context The relevant context.
|
||||
*/
|
||||
protected static function export_line_enrolment_data_for_user($enrol,$subcontext) {
|
||||
$context = \context_system::instance();
|
||||
|
||||
protected static function export_line_enrolment_data_for_user($enrol, $context, $subcontext) {
|
||||
$data = new \stdClass;
|
||||
$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);
|
||||
$data->studyplan = $studyplan->name();
|
||||
$data->page = $page->fullname();
|
||||
$data->line = $line->name();
|
||||
$data->enrolled = ($enrol->enrolled) ? "True" : "False";
|
||||
$data->enrolledsince = (new \DateTime($enrol->timeenrolled))->format("X-m-d\\TH:i:sP");
|
||||
|
||||
$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;
|
||||
// Find studyplans in context.
|
||||
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]);
|
||||
|
||||
// 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 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;
|
||||
if ($context->contextlevel == CONTEXT_SYSTEM || $context->contextlevel == CONTEXT_COURSECAT) {
|
||||
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]);
|
||||
|
||||
// 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)";
|
||||
$planids = $DB->get_fieldset_sql($sql);
|
||||
|
||||
|
@ -350,7 +379,7 @@ class provider implements \core_privacy\local\metadata\provider,
|
|||
|
||||
} else { // if ($context->contextlevel == CONTEXT_COURSECAT) {
|
||||
// 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)";
|
||||
$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) {
|
||||
// Remove all associated users to the studyplan.
|
||||
$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_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]);
|
||||
|
||||
// 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) {
|
||||
$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();
|
||||
|
||||
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();
|
||||
|
||||
foreach ($users as $userid) {
|
||||
static::delete_user_in_context($context,$userid);
|
||||
static::delete_userdata_in_context($context, $userid);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue