9 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Privacy API Provider
|
|
|
19 |
*
|
|
|
20 |
* @package block_openai_chat
|
|
|
21 |
* @copyright 2024 Bryce Yoder <me@bryceyoder.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace block_openai_chat\privacy;
|
|
|
26 |
|
|
|
27 |
use \core_privacy\local\metadata\collection;
|
|
|
28 |
use \core_privacy\local\request\writer;
|
|
|
29 |
use \core_privacy\local\request\contextlist;
|
|
|
30 |
use \core_privacy\local\request\approved_contextlist;
|
|
|
31 |
use \core_privacy\local\request\userlist;
|
|
|
32 |
use core_privacy\local\request\approved_userlist;
|
|
|
33 |
|
|
|
34 |
defined('MOODLE_INTERNAL') || die();
|
|
|
35 |
|
|
|
36 |
class provider implements
|
|
|
37 |
\core_privacy\local\metadata\provider,
|
|
|
38 |
\core_privacy\local\request\plugin\provider,
|
|
|
39 |
\core_privacy\local\request\core_userlist_provider {
|
|
|
40 |
|
|
|
41 |
public static function get_metadata(collection $collection): collection {
|
|
|
42 |
$collection->add_database_table(
|
|
|
43 |
'block_openai_chat_log',
|
|
|
44 |
[
|
|
|
45 |
'userid' => 'privacy:metadata:openai_chat_log:userid',
|
|
|
46 |
'usermessage' => 'privacy:metadata:openai_chat_log:usermessage',
|
|
|
47 |
'airesponse' => 'privacy:metadata:openai_chat_log:airesponse',
|
|
|
48 |
'timecreated' => 'privacy:metadata:openai_chat_log:timecreated'
|
|
|
49 |
],
|
|
|
50 |
'privacy:metadata:openai_chat_log'
|
|
|
51 |
);
|
|
|
52 |
|
|
|
53 |
return $collection;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
public static function get_contexts_for_userid(int $userid): contextlist {
|
|
|
57 |
$contextlist = new \core_privacy\local\request\contextlist();
|
|
|
58 |
$sql = "SELECT id FROM {context} WHERE contextlevel = 30 AND instanceid = :userid";
|
|
|
59 |
$contextlist->add_from_sql($sql, ['userid' => $userid]);
|
|
|
60 |
return $contextlist;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
public static function get_users_in_context(userlist $userlist) {
|
|
|
64 |
$context = $userlist->get_context();
|
|
|
65 |
if (!$context instanceof \context_user) {
|
|
|
66 |
return;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
if ($DB->record_exists('block_openai_chat_log', ['userid' => $context->instanceid])) {
|
|
|
70 |
$userlist->add_user($context->instanceid);
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
public static function export_user_data(approved_contextlist $contextlist) {
|
|
|
75 |
global $DB;
|
|
|
76 |
|
|
|
77 |
$context = $contextlist->current();
|
|
|
78 |
$user = $contextlist->get_user();
|
|
|
79 |
$userid = $user->id;
|
|
|
80 |
|
|
|
81 |
// Sent messages.
|
|
|
82 |
$sql = "SELECT id, userid, usermessage, airesponse, timecreated FROM {block_openai_chat_log} WHERE userid = :userid";
|
|
|
83 |
$records = $DB->get_records_sql($sql, ["userid" => $userid]);
|
|
|
84 |
|
|
|
85 |
if (!empty($records)) {
|
|
|
86 |
$messages = new \stdClass();
|
|
|
87 |
foreach ($records as $message) {
|
|
|
88 |
$messages->{$message->id} = [
|
|
|
89 |
"userid" => $message->userid,
|
|
|
90 |
"usermessage" => $message->usermessage,
|
|
|
91 |
"airesponse" => $message->airesponse,
|
|
|
92 |
"timecreated" => $message->timecreated
|
|
|
93 |
];
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
writer::with_context($context)->export_data(
|
|
|
97 |
[get_string('privacy:chatmessagespath', 'block_openai_chat')],
|
|
|
98 |
$messages
|
|
|
99 |
);
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
public static function delete_data_for_all_users_in_context(\context $context) {
|
|
|
104 |
global $DB;
|
|
|
105 |
// Only delete data for a user context.
|
|
|
106 |
if ($context->contextlevel == CONTEXT_USER) {
|
|
|
107 |
$DB->delete_records('block_openai_chat_log', ['userid' => $context->instanceid]);
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
public static function delete_data_for_user(approved_contextlist $contextlist) {
|
|
|
112 |
global $DB;
|
|
|
113 |
foreach ($contextlist as $context) {
|
|
|
114 |
// Let's be super certain that we have the right information for this user here.
|
|
|
115 |
if ($context->contextlevel == CONTEXT_USER && $contextlist->get_user()->id == $context->instanceid) {
|
|
|
116 |
$DB->delete_records('block_openai_chat_log', ['userid' => $context->instanceid]);
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
public static function delete_data_for_users(approved_userlist $userlist) {
|
|
|
122 |
global $DB;
|
|
|
123 |
$context = $userlist->get_context();
|
|
|
124 |
if ($context instanceof \context_user && in_array($context->instanceid, $userlist->get_userids())) {
|
|
|
125 |
$DB->delete_records('block_openai_chat_log', ['userid' => $context->instanceid]);
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
}
|