MDL-62904 tool_dataprivacy: Add data request links in profile page
[moodle.git] / admin / tool / dataprivacy / createdatarequest.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 * Prints the contact form to the site's Data Protection Officer
19 *
20 * @copyright 2018 onwards Jun Pataleta
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22 * @package tool_dataprivacy
23 */
24
25require_once('../../../config.php');
26require_once('lib.php');
5efc1f9e
DM
27require_once('createdatarequest_form.php');
28
29$manage = optional_param('manage', 0, PARAM_INT);
500dc0b1 30$requesttype = optional_param('type', \tool_dataprivacy\api::DATAREQUEST_TYPE_EXPORT, PARAM_INT);
5efc1f9e 31
500dc0b1 32$url = new moodle_url('/admin/tool/dataprivacy/createdatarequest.php', ['manage' => $manage, 'type' => $requesttype]);
5efc1f9e
DM
33
34$PAGE->set_url($url);
35
36require_login();
37if (isguestuser()) {
38 print_error('noguest');
39}
40
41// Return URL and context.
42if ($manage) {
43 // For the case where DPO creates data requests on behalf of another user.
44 $returnurl = new moodle_url($CFG->wwwroot . '/admin/tool/dataprivacy/datarequests.php');
45 $context = context_system::instance();
46 // Make sure the user has the proper capability.
47 require_capability('tool/dataprivacy:managedatarequests', $context);
48} else {
49 // For the case where a user makes request for themselves (or for their children if they are the parent).
50 $returnurl = new moodle_url($CFG->wwwroot . '/admin/tool/dataprivacy/mydatarequests.php');
51 $context = context_user::instance($USER->id);
52}
53$PAGE->set_context($context);
54
55// If contactdataprotectionofficer is disabled, send the user back to the profile page, or the privacy policy page.
97009b73
JD
56// That is, unless you have sufficient capabilities to perform this on behalf of a user.
57if (!$manage && !\tool_dataprivacy\api::can_contact_dpo()) {
df751f00 58 redirect($returnurl, get_string('contactdpoviaprivacypolicy', 'tool_dataprivacy'), 0, \core\output\notification::NOTIFY_ERROR);
5efc1f9e
DM
59}
60
f946d875 61$mform = new tool_dataprivacy_data_request_form($url->out(false), ['manage' => !empty($manage)]);
500dc0b1 62$mform->set_data(['type' => $requesttype]);
5efc1f9e
DM
63
64// Data request cancelled.
65if ($mform->is_cancelled()) {
66 redirect($returnurl);
67}
68
69// Data request submitted.
70if ($data = $mform->get_data()) {
cbae8dcd
AN
71 if ($data->userid != $USER->id) {
72 if (!\tool_dataprivacy\api::can_manage_data_requests($USER->id)) {
73 // If not a DPO, only users with the capability to make data requests for the user should be allowed.
74 // (e.g. users with the Parent role, etc).
75 \tool_dataprivacy\api::require_can_create_data_request_for_user($data->userid);
76 }
77 }
78
5efc1f9e
DM
79 \tool_dataprivacy\api::create_data_request($data->userid, $data->type, $data->comments);
80
f946d875
JP
81 if ($manage) {
82 $foruser = core_user::get_user($data->userid);
83 $redirectmessage = get_string('datarequestcreatedforuser', 'tool_dataprivacy', fullname($foruser));
84 } else {
85 $redirectmessage = get_string('requestsubmitted', 'tool_dataprivacy');
86 }
87 redirect($returnurl, $redirectmessage);
5efc1f9e
DM
88}
89
457047de
MG
90$title = get_string('createnewdatarequest', 'tool_dataprivacy');
91$PAGE->set_heading($SITE->fullname);
5efc1f9e
DM
92$PAGE->set_title($title);
93echo $OUTPUT->header();
94echo $OUTPUT->heading($title);
95
96echo $OUTPUT->box_start();
97$mform->display();
98echo $OUTPUT->box_end();
99
100echo $OUTPUT->footer();