on-demand release 3.6dev+
[moodle.git] / admin / tool / dataprivacy / lib.php
CommitLineData
5efc1f9e
DM
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 * Data privacy plugin library
19 * @package tool_dataprivacy
20 * @copyright 2018 onwards Jun Pataleta
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24use core_user\output\myprofile\tree;
25
26defined('MOODLE_INTERNAL') || die();
27
28/**
29 * Add nodes to myprofile page.
30 *
31 * @param tree $tree Tree object
32 * @param stdClass $user User object
33 * @param bool $iscurrentuser
34 * @param stdClass $course Course object
35 * @return bool
36 * @throws coding_exception
37 * @throws dml_exception
38 * @throws moodle_exception
39 */
40function tool_dataprivacy_myprofile_navigation(tree $tree, $user, $iscurrentuser, $course) {
41 global $PAGE, $USER;
42
43 // Get the Privacy and policies category.
44 if (!array_key_exists('privacyandpolicies', $tree->__get('categories'))) {
45 // Create the category.
46 $categoryname = get_string('privacyandpolicies', 'admin');
47 $category = new core_user\output\myprofile\category('privacyandpolicies', $categoryname, 'contact');
48 $tree->add_category($category);
49 } else {
50 // Get the existing category.
51 $category = $tree->__get('categories')['privacyandpolicies'];
52 }
53
54 // Contact data protection officer link.
55 if (\tool_dataprivacy\api::can_contact_dpo() && $iscurrentuser) {
56 $renderer = $PAGE->get_renderer('tool_dataprivacy');
57 $content = $renderer->render_contact_dpo_link($USER->email);
58 $node = new core_user\output\myprofile\node('privacyandpolicies', 'contactdpo', null, null, null, $content);
59 $category->add_node($node);
60 $PAGE->requires->js_call_amd('tool_dataprivacy/myrequestactions', 'init');
61
62 $url = new moodle_url('/admin/tool/dataprivacy/mydatarequests.php');
63 $node = new core_user\output\myprofile\node('privacyandpolicies', 'datarequests',
64 get_string('datarequests', 'tool_dataprivacy'), null, $url);
65 $category->add_node($node);
66 }
67
edbdeb63
AG
68 $summaryurl = new moodle_url('/admin/tool/dataprivacy/summary.php');
69 $summarynode = new core_user\output\myprofile\node('privacyandpolicies', 'retentionsummary',
70 get_string('dataretentionsummary', 'tool_dataprivacy'), null, $summaryurl);
71 $category->add_node($summarynode);
72
5efc1f9e
DM
73 // Add the Privacy category to the tree if it's not empty and it doesn't exist.
74 $nodes = $category->nodes;
75 if (!empty($nodes)) {
76 if (!array_key_exists('privacyandpolicies', $tree->__get('categories'))) {
77 $tree->add_category($category);
78 }
79 return true;
80 }
81
82 return false;
83}
84
edbdeb63
AG
85/**
86 * Callback to add footer elements.
87 *
88 * @return string HTML footer content
89 */
90function tool_dataprivacy_standard_footer_html() {
91
92 $url = new moodle_url('/admin/tool/dataprivacy/summary.php');
93 $output = html_writer::link($url, get_string('dataretentionsummary', 'tool_dataprivacy'));
94 $output = html_writer::div($output, 'summaryfooter');
95
96 return $output;
97}
98
5efc1f9e
DM
99/**
100 * Fragment to add a new purpose.
101 *
102 * @param array $args The fragment arguments.
103 * @return string The rendered mform fragment.
104 */
105function tool_dataprivacy_output_fragment_addpurpose_form($args) {
106
107 $formdata = [];
108 if (!empty($args['jsonformdata'])) {
109 $serialiseddata = json_decode($args['jsonformdata']);
110 parse_str($serialiseddata, $formdata);
111 }
112
113 $persistent = new \tool_dataprivacy\purpose();
114 $mform = new \tool_dataprivacy\form\purpose(null, ['persistent' => $persistent],
115 'post', '', null, true, $formdata);
116
117 if (!empty($args['jsonformdata'])) {
118 // Show errors if data was received.
119 $mform->is_validated();
120 }
121
122 return $mform->render();
123}
124
125/**
126 * Fragment to add a new category.
127 *
128 * @param array $args The fragment arguments.
129 * @return string The rendered mform fragment.
130 */
131function tool_dataprivacy_output_fragment_addcategory_form($args) {
132
133 $formdata = [];
134 if (!empty($args['jsonformdata'])) {
135 $serialiseddata = json_decode($args['jsonformdata']);
136 parse_str($serialiseddata, $formdata);
137 }
138
139 $persistent = new \tool_dataprivacy\category();
140 $mform = new \tool_dataprivacy\form\category(null, ['persistent' => $persistent],
141 'post', '', null, true, $formdata);
142
143 if (!empty($args['jsonformdata'])) {
144 // Show errors if data was received.
145 $mform->is_validated();
146 }
147
148 return $mform->render();
149}
150
151/**
152 * Fragment to edit a context purpose and category.
153 *
154 * @param array $args The fragment arguments.
155 * @return string The rendered mform fragment.
156 */
157function tool_dataprivacy_output_fragment_context_form($args) {
158 global $PAGE;
159
160 $contextid = $args[0];
161
162 $context = \context_helper::instance_by_id($contextid);
163 $customdata = \tool_dataprivacy\form\context_instance::get_context_instance_customdata($context);
164
165 if (!empty($customdata['purposeretentionperiods'])) {
a8a69050
DM
166 $PAGE->requires->js_call_amd('tool_dataprivacy/effective_retention_period', 'init',
167 [$customdata['purposeretentionperiods']]);
5efc1f9e
DM
168 }
169 $mform = new \tool_dataprivacy\form\context_instance(null, $customdata);
170 return $mform->render();
171}
172
173/**
174 * Fragment to edit a contextlevel purpose and category.
175 *
176 * @param array $args The fragment arguments.
177 * @return string The rendered mform fragment.
178 */
179function tool_dataprivacy_output_fragment_contextlevel_form($args) {
180 global $PAGE;
181
182 $contextlevel = $args[0];
183 $customdata = \tool_dataprivacy\form\contextlevel::get_contextlevel_customdata($contextlevel);
184
185 if (!empty($customdata['purposeretentionperiods'])) {
a8a69050
DM
186 $PAGE->requires->js_call_amd('tool_dataprivacy/effective_retention_period', 'init',
187 [$customdata['purposeretentionperiods']]);
5efc1f9e
DM
188 }
189
190 $mform = new \tool_dataprivacy\form\contextlevel(null, $customdata);
191 return $mform->render();
192}
193
194/**
195 * Serves any files associated with the data privacy settings.
196 *
197 * @param stdClass $course Course object
198 * @param stdClass $cm Course module object
199 * @param context $context Context
200 * @param string $filearea File area for data privacy
201 * @param array $args Arguments
202 * @param bool $forcedownload If we are forcing the download
203 * @param array $options More options
204 * @return bool Returns false if we don't find a file.
205 */
206function tool_dataprivacy_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
207 if ($context->contextlevel == CONTEXT_USER) {
cb775057
JP
208 // Make sure the user is logged in.
209 require_login(null, false);
210
635c7b29 211 // Get the data request ID. This should be the first element of the $args array.
212 $itemid = $args[0];
213 // Fetch the data request object. An invalid ID will throw an exception.
214 $datarequest = new \tool_dataprivacy\data_request($itemid);
215
216 // Check if user is allowed to download it.
217 if (!\tool_dataprivacy\api::can_download_data_request_for_user($context->instanceid, $datarequest->get('requestedby'))) {
218 return false;
cb775057
JP
219 }
220
693f690c
MH
221 // Make the file unavailable if it has expired.
222 if (\tool_dataprivacy\data_request::is_expired($datarequest)) {
223 send_file_not_found();
224 }
225
cb775057 226 // All good. Serve the exported data.
5efc1f9e
DM
227 $fs = get_file_storage();
228 $relativepath = implode('/', $args);
229 $fullpath = "/$context->id/tool_dataprivacy/$filearea/$relativepath";
230 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
231 return false;
232 }
233 send_stored_file($file, 0, 0, $forcedownload, $options);
234 } else {
235 send_file_not_found();
236 }
237}