This repository has been archived on 2025-01-01. You can view files and clone it, but cannot push or open issues or pull requests.
moodle-local_treestudyplan/classes/privacy/provider.php

424 lines
18 KiB
PHP

<?php
// This file is part of the Studyplan plugin for Moodle
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
/**
* Privacy information metadata
* @package local_treestudyplan
* @copyright 2023 P.M. Kuipers
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_treestudyplan\privacy;
use core_privacy\local\metadata\collection;
use core_privacy\local\request\userlist;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\approved_userlist;
use core_privacy\local\request\deletion_criteria;
use core_privacy\local\request\writer;
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
*/
class provider implements \core_privacy\local\metadata\provider,
\core_privacy\local\request\plugin\provider,
\core_privacy\local\request\core_userlist_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
/**
* Get metadata about collected personal data
* @param collection $collection
* @return collection
*/
public static function get_metadata(collection $collection): collection {
$collection->add_database_table(
'local_treestudyplan_invit',
[
'user_id' => 'privacy:metadata:invit:user_id',
'name' => 'privacy:metadata:invit:name',
'email' => 'privacy:metadata:invit:email',
'date' => 'privacy:metadata:invit:date',
],
'privacy:metadata:invit'
);
$collection->add_database_table(
'local_treestudyplan_user',
[
'user_id' => 'privacy:metadata:user:user_id',
'studyplan_id' => 'privacy:metadata:user:studyplan_id',
],
'privacy:metadata:user'
);
$collection->add_database_table(
'local_treestudyplan_teachers',
[
'teacher_id' => 'privacy:metadata:teachers:user_id',
'studyplan_id' => 'privacy:metadata:teachers:studyplan_id',
],
'privacy:metadata:teachers'
);
$collection->add_database_table(
'local_treestudyplan_lineuser',
[
'user_id' => 'privacy:metadata:lineuser:user_id',
'line_id' => 'privacy:metadata:lineuser:line_id',
'timeenrolled' => 'privacy:metadata:lineuser:timeenrolled',
'enrolled' => 'privacy:metadata:lineuser:enrolledby',
'enrolledby' => 'privacy:metadata:lineuser:enrolledby',
],
'privacy:metadata:lineuser'
);
$collection->add_database_table(
'local_treestudyplan_coach',
[
'user_id' => 'privacy:metadata:coach:user_id',
'studyplan_id' => 'privacy:metadata:coach:studyplan_id',
],
'privacy:metadata:coach'
);
return $collection;
}
/**
* Get the list of contexts that contain user information for the specified user.
* @param int $userid The user to search.
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid(int $userid): contextlist {
$contextlist = new \core_privacy\local\request\contextlist();
$contextlist->add_system_context(); // For invitations.
// 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 )
";
$contextlist->add_from_sql($sql, ['userid' => $userid]);
return $contextlist;
}
/**
* Export all user data for the specified user, in the specified contexts.
* @param approved_contextlist $contextlist The approved contexts to export information for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
global $DB;
foreach ($contextlist->get_contexts() as $context) {
$user = $contextlist->get_user();
if ($context instanceof \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);
}
// 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 <= 1)
";
$records = $DB->get_records_sql($sql, ["userid" => $user->id]);
foreach ($records as $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 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,"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");
}
}
}
}
}
/**
* Export the supplied personal data for an invitation.
* @param stdClass $invit The invitation record.
*/
protected static function export_invitation_data_for_user($invit) {
$context = \context_system::instance();
$subcontext = ["invitations"];
$data = new \stdClass;
$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,$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();
$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);
}
/**
* Delete all data for all users in the specified context.
* Used when a context is past it's data retention period
* @param context $context The specific context to delete data for.
*/
public static function delete_data_for_all_users_in_context(context $context) {
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 ( a.user_id = :userid AND s.context_id = :contextid)";
$planids = $DB->get_fieldset_sql($sql, ["contextid" => $context->id]);
foreach ($planids as $planid) {
$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
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
global $DB;
$user = $contextlist->get_user();
foreach ($contextlist->get_contexts() as $context) {
if ($context->contextlevel == CONTEXT_SYSTEM) {
// Delete all associations for this user if the system context is included.
$DB->delete_records("local_treestudyplan_user", ["user_id" => $user->id]);
// Also delete all invitations for this user.
$DB->delete_records("local_treestudyplan_invit", ["user_id" => $user->id]);
} else if ($context->contextlevel == CONTEXT_COURSECAT) {
$sql = "SELECT s.id FROM {local_treestudyplan} INNER JOIN {local_treestudyplan_user} a ON a.studyplan_id = s.id
WHERE ( a.user_id = :userid AND s.context_id = :contextid)";
$planids = $DB->get_fieldset_sql($sql, ["contextid" => $context->id, "userid" => $user->id]);
foreach ($planids as $planid) {
$DB->delete_records("local_treestudyplan_user", ["studyplan_id" => $planid, "user_id" => $user->id]);
}
}
}
// TODO: REMOVE LINE ENROLLMENTS
// TODO: Replace ENROLLEDBY by id 2 (ADMIN user)
// TODO: REMOVE COACHES
// TODO: REMOVE TEACHING CACHE
}
/**
* Get the list of users who have data within a context.
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
*/
public static function get_users_in_context(userlist $userlist) {
$context = $userlist->get_context();
if ($context instanceof \context_system) {
// Add all invitations.
$sql = "SELECT i.user_id as userid
FROM {local_treestudyplan_invit} i;";
$userlist->add_from_sql('userid', $sql, []);
// 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 < 1)
";
$userlist->add_from_sql('userid', $sql, []);
}
// Add the links to all study plans in this 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 = :contextid )
";
$userlist->add_from_sql('userid', $sql, ["contextid" => $context->id]);
// TODO: FIX THIS WITH ALL OF THE ABOVE TODOS
}
/**
* Delete multiple users within a single context.
* @param approved_userlist $userlist The approved context and user information to delete information for.
*/
public static function delete_data_for_users(approved_userlist $userlist) {
global $DB;
$context = $userlist->get_context();
$users = $userlist->get_userids();
list($userinsql, $userinparams) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, 'user');
$planids = [];
if ($context->contextlevel == CONTEXT_SYSTEM) {
// Determine the relevant plan_ids for this context.
$sql = "SELECT s.id FROM {local_treestudyplan}
WHERE ( s.context_id IS NULL OR s.context_id == 0 OR s.context_id = :contextid)) ";
$planids = $DB->get_fieldset_sql($sql, ["contextid" => $context->id ]);
// If plan ids not empty, they will be processed later.
// Also delete all invitations for these users.
$sql = "user_id {$userinsql}";
$DB->delete_records_select("local_treestudyplan_invit", $sql, $userinparams);
} else if ($context->contextlevel == CONTEXT_COURSECAT) {
$sql = "SELECT s.id FROM {local_treestudyplan}
WHERE (s.context_id = :contextid)";
$planids = $DB->get_fieldset_sql($sql, ["contextid" => $context->id ]);
// If plan ids not empty, they will be processed later.
}
// Now delete the studyplan associations if relevant.
if (count($planids) > 0 && count($users) > 0) {
list($planinsql, $planinputparams) = $DB->get_in_or_equal($planids, SQL_PARAMS_NAMED, 'plan');
$params = $userinparams + $planinputparams;
$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
}
}