moodle_local_treestudyplan/classes/privacy/provider.php

295 lines
12 KiB
PHP
Raw Permalink Normal View History

2023-07-18 22:14:52 +02:00
<?php
2023-08-24 23:02:41 +02:00
// 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/>.
/**
2023-08-27 15:12:54 +02:00
* Privacy information metadata
2023-08-24 23:02:41 +02:00
* @package local_treestudyplan
* @copyright 2023 P.M. Kuipers
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
2023-07-18 22:14:52 +02:00
namespace local_treestudyplan\privacy;
2023-08-25 12:04:27 +02:00
2023-08-27 15:12:54 +02:00
use \core_privacy\local\metadata\collection;
2023-07-18 22:14:52 +02:00
use \core_privacy\local\request\userlist;
2023-08-27 15:12:54 +02:00
use \core_privacy\local\request\contextlist;
2023-07-18 22:14:52 +02:00
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;
2023-08-27 15:12:54 +02:00
use \tool_dataprivacy\context_instance;
use \context;
2023-07-18 22:14:52 +02:00
2023-08-27 15:12:54 +02:00
/**
* Privacy provider
*/
2023-08-24 23:02:41 +02:00
class provider implements \core_privacy\local\metadata\provider,
2023-07-18 22:14:52 +02:00
\core_privacy\local\request\plugin\provider,
2023-08-25 17:33:20 +02:00
\core_privacy\local\request\core_userlist_provider {
/**
2023-07-18 22:14:52 +02:00
* 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';
}
2023-08-27 15:12:54 +02:00
/**
* Get metadata about collected personal data
* @param collection $collection
* @return collection
*/
2023-07-18 22:14:52 +02:00
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'
);
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();
2023-08-24 23:02:41 +02:00
$contextlist->add_system_context(); // For invitations.
2023-07-18 22:14:52 +02:00
2023-08-25 09:44:34 +02:00
// Add contexts for linked studyplans.
2023-07-18 22:14:52 +02:00
$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]);
2023-08-24 23:02:41 +02:00
2023-07-18 22:14:52 +02:00
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.
*/
2023-08-24 23:02:41 +02:00
public static function export_user_data(approved_contextlist $contextlist) {
2023-07-18 22:14:52 +02:00
global $DB;
foreach ($contextlist->get_contexts() as $context) {
$user = $contextlist->get_user();
2023-08-24 23:02:41 +02:00
if ($context instanceof \context_system) {
// Export invitations.
2023-07-18 22:14:52 +02:00
$sql = "SELECT * FROM {local_treestudyplan_invit} i
WHERE ( aiuser_id = :userid )
";
2023-08-24 23:02:41 +02:00
$records = $DB->get_records_sql($sql, ["userid" => $user->id]);
foreach ($records as $r) {
2023-07-18 22:14:52 +02:00
static::export_invitation_data_for_user($r);
}
2023-08-24 23:02:41 +02:00
// Export empty associations.
2023-07-18 22:14:52 +02:00
$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)
";
2023-08-24 23:02:41 +02:00
$records = $DB->get_records_sql($sql, ["userid" => $user->id, "contextid" => $context->id]);
foreach ($records as $r) {
2023-07-18 22:14:52 +02:00
static::export_studyplan_data_for_user($r);
2023-08-24 23:02:41 +02:00
}
} else if ($context->contextlevel == CONTEXT_COURSECAT) {
2023-08-25 17:33:20 +02:00
// Export studyplan associations.
2023-07-18 22:14:52 +02:00
$sql = "SELECT * FROM {local_treestudyplan} s
INNER JOIN {local_treestudyplan_user} a ON a.studyplan_id = s.id
2023-08-25 17:33:20 +02:00
WHERE ( a.user_id = :userid AND s.context_id = :contextid)";
2023-08-24 23:02:41 +02:00
$records = $DB->get_records_sql($sql, ["userid" => $user->id, "contextid" => $context->id]);
foreach ($records as $r) {
2023-07-18 22:14:52 +02:00
static::export_studyplan_data_for_user($r);
}
}
}
}
/**
* Export the supplied personal data for an invitation.
2023-08-27 15:12:54 +02:00
* @param stdClass $invit The invitation record.
2023-07-18 22:14:52 +02:00
*/
protected static function export_invitation_data_for_user($invit) {
$context = \context_system::instance();
$subcontext = ["invitations"];
writer::with_context($context)
->export_data($subcontext, (object)["recipient" => $invit->name, "email" => $invit->email]);
}
/**
2023-08-27 15:12:54 +02:00
* Export studyplan data for (current) user
* @param stdClass $studyplan The studyplan
2023-07-18 22:14:52 +02:00
*/
protected static function export_studyplan_data_for_user($studyplan) {
$context = \context_system::instance();
$subcontext = ["studyplans"];
writer::with_context($context)
->export_data($subcontext, (object)["fullname" => $studyplan->fullname, "shortname" => $studyplan->shortname]);
}
/**
* Delete all data for all users in the specified context.
* Used when a context is past it's data retention period
2023-08-27 15:12:54 +02:00
* @param context $context The specific context to delete data for.
2023-07-18 22:14:52 +02:00
*/
2023-08-27 15:12:54 +02:00
public static function delete_data_for_all_users_in_context(context $context) {
2023-07-18 22:14:52 +02:00
global $DB;
2023-08-25 09:44:34 +02:00
// Find studyplans in context.
2023-08-24 23:02:41 +02:00
if ($context->contextlevel == CONTEXT_COURSECAT) {
2023-07-18 22:14:52 +02:00
$sql = "SELECT s.id FROM {local_treestudyplan} WHERE ( a.user_id = :userid AND s.context_id = :contextid)";
2023-08-25 10:41:56 +02:00
$planids = $DB->get_fieldset_sql($sql, ["contextid" => $context->id]);
2023-08-24 23:02:41 +02:00
2023-08-25 09:33:42 +02:00
foreach ($planids as $planid) {
$DB->delete_records("local_treestudyplan_user", ["studyplan_id" => $planid]);
2023-07-18 22:14:52 +02:00
}
}
}
/**
* 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.
*/
2023-08-24 23:02:41 +02:00
public static function delete_data_for_user(approved_contextlist $contextlist) {
2023-07-18 22:14:52 +02:00
global $DB;
$user = $contextlist->get_user();
foreach ($contextlist->get_contexts() as $context) {
2023-08-24 23:02:41 +02:00
if ($context->contextlevel == CONTEXT_SYSTEM) {
2023-08-25 17:33:20 +02:00
$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 IS NULL OR s.context_id == 0 OR s.context_id = :contextid))";
2023-08-25 10:41:56 +02:00
$planids = $DB->get_fieldset_sql($sql, ["contextid" => $context->id, "userid" => $user->id]);
2023-08-24 23:02:41 +02:00
2023-08-25 09:33:42 +02:00
foreach ($planids as $planid) {
$DB->delete_records("local_treestudyplan_user", ["studyplan_id" => $planid, "user_id" => $user->id]);
2023-07-18 22:14:52 +02:00
}
2023-08-24 23:02:41 +02:00
// Also delete all invitations for this user.
$DB->delete_records("local_treestudyplan_invit", ["user_id" => $user->id]);
2023-07-18 22:14:52 +02:00
2023-08-24 23:02:41 +02:00
} else if ($context->contextlevel == CONTEXT_COURSECAT) {
2023-07-18 22:14:52 +02:00
$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)";
2023-08-25 10:41:56 +02:00
$planids = $DB->get_fieldset_sql($sql, ["contextid" => $context->id, "userid" => $user->id]);
2023-08-24 23:02:41 +02:00
2023-08-25 09:33:42 +02:00
foreach ($planids as $planid) {
$DB->delete_records("local_treestudyplan_user", ["studyplan_id" => $planid, "user_id" => $user->id]);
2023-07-18 22:14:52 +02:00
}
}
2023-08-24 23:02:41 +02:00
}
2023-07-18 22:14:52 +02:00
}
/**
* 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.
*/
2023-08-24 23:02:41 +02:00
public static function get_users_in_context(userlist $userlist) {
2023-07-18 22:14:52 +02:00
$context = $userlist->get_context();
if ($context instanceof \context_system) {
2023-08-24 23:02:41 +02:00
// Add all invitations.
2023-07-18 22:14:52 +02:00
$sql = "SELECT i.user_id as userid
FROM {local_treestudyplan_invit} i;";
$userlist->add_from_sql('userid', $sql, []);
2023-08-25 09:44:34 +02:00
// Also add "contextless studyplans, they are considered in system context".
2023-07-18 22:14:52 +02:00
$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)
";
$userlist->add_from_sql('userid', $sql, []);
}
2023-08-25 09:44:34 +02:00
// Add the links to all study plans in this context.
2023-07-18 22:14:52 +02:00
$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]);
}
/**
* Delete multiple users within a single context.
* @param approved_userlist $userlist The approved context and user information to delete information for.
*/
2023-08-24 23:02:41 +02:00
public static function delete_data_for_users(approved_userlist $userlist) {
2023-07-18 22:14:52 +02:00
global $DB;
$context = $userlist->get_context();
$users = $userlist->get_userids();
2023-08-24 23:02:41 +02:00
list($userinsql, $userinparams) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, 'user');
2023-07-18 22:14:52 +02:00
2023-08-25 09:33:42 +02:00
$planids = [];
2023-08-24 23:02:41 +02:00
if ($context->contextlevel == CONTEXT_SYSTEM) {
// Determine the relevant plan_ids for this context.
$sql = "SELECT s.id FROM {local_treestudyplan}
2023-07-18 22:14:52 +02:00
WHERE ( s.context_id IS NULL OR s.context_id == 0 OR s.context_id = :contextid)) ";
2023-08-25 10:41:56 +02:00
$planids = $DB->get_fieldset_sql($sql, ["contextid" => $context->id, ]);
2023-08-24 23:02:41 +02:00
// If plan ids not empty, they will be processed later.
// Also delete all invitations for these users.
2023-07-18 22:14:52 +02:00
$sql = "user_id {$userinsql}";
2023-08-24 23:02:41 +02:00
$DB->delete_records_select("local_treestudyplan_invit", $sql, $userinparams);
2023-07-18 22:14:52 +02:00
2023-08-24 23:02:41 +02:00
} else if ($context->contextlevel == CONTEXT_COURSECAT) {
$sql = "SELECT s.id FROM {local_treestudyplan}
2023-07-18 22:14:52 +02:00
WHERE (s.context_id = :contextid)";
2023-08-25 10:41:56 +02:00
$planids = $DB->get_fieldset_sql($sql, ["contextid" => $context->id, ]);
2023-08-24 23:02:41 +02:00
// If plan ids not empty, they will be processed later.
2023-07-18 22:14:52 +02:00
}
2023-08-24 23:02:41 +02:00
// Now delete the studyplan associations if relevant.
2023-08-25 17:33:20 +02:00
if (count($planids) > 0 && count($users) > 0) {
2023-07-18 22:14:52 +02:00
2023-08-25 09:33:42 +02:00
list($planinsql, $planinputparams) = $DB->get_in_or_equal($planids, SQL_PARAMS_NAMED, 'plan');
2023-08-25 17:33:20 +02:00
$params = $userinparams + $planinputparams;
2023-07-18 22:14:52 +02:00
$sql = "user_id {$userinsql} and studyplan_id {$planinsql}";
$DB->delete_records_select('local_treestudyplan_user', $sql, $params);
}
}
2023-08-25 11:52:05 +02:00
}