MDL-62660 tool_dataprivacy: Add ability to expire data requests
[moodle.git] / admin / tool / dataprivacy / classes / external / data_request_exporter.php
blobb7d483ca64d512d7c35e1293da0735ca61c0481c
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.
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/>.
17 /**
18 * Class for exporting user evidence with all competencies.
20 * @package tool_dataprivacy
21 * @copyright 2018 Jun Pataleta
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace tool_dataprivacy\external;
25 defined('MOODLE_INTERNAL') || die();
27 use coding_exception;
28 use core\external\persistent_exporter;
29 use core_user;
30 use core_user\external\user_summary_exporter;
31 use dml_exception;
32 use moodle_exception;
33 use renderer_base;
34 use tool_dataprivacy\api;
35 use tool_dataprivacy\data_request;
36 use tool_dataprivacy\local\helper;
38 /**
39 * Class for exporting user evidence with all competencies.
41 * @copyright 2018 Jun Pataleta
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 class data_request_exporter extends persistent_exporter {
46 /**
47 * Class definition.
49 * @return string
51 protected static function define_class() {
52 return data_request::class;
55 /**
56 * Related objects definition.
58 * @return array
60 protected static function define_related() {
61 return [
62 'context' => 'context',
66 /**
67 * Other properties definition.
69 * @return array
71 protected static function define_other_properties() {
72 return [
73 'foruser' => [
74 'type' => user_summary_exporter::read_properties_definition(),
76 'requestedbyuser' => [
77 'type' => user_summary_exporter::read_properties_definition(),
78 'optional' => true
80 'dpouser' => [
81 'type' => user_summary_exporter::read_properties_definition(),
82 'optional' => true
84 'messagehtml' => [
85 'type' => PARAM_RAW,
86 'optional' => true
88 'typename' => [
89 'type' => PARAM_TEXT,
91 'typenameshort' => [
92 'type' => PARAM_TEXT,
94 'statuslabel' => [
95 'type' => PARAM_TEXT,
97 'statuslabelclass' => [
98 'type' => PARAM_TEXT,
100 'canreview' => [
101 'type' => PARAM_BOOL,
102 'optional' => true,
103 'default' => false
105 'approvedeny' => [
106 'type' => PARAM_BOOL,
107 'optional' => true,
108 'default' => false
110 'canmarkcomplete' => [
111 'type' => PARAM_BOOL,
112 'optional' => true,
113 'default' => false
119 * Assign values to the defined other properties.
121 * @param renderer_base $output The output renderer object.
122 * @return array
123 * @throws coding_exception
124 * @throws dml_exception
125 * @throws moodle_exception
127 protected function get_other_values(renderer_base $output) {
128 $values = [];
130 $foruserid = $this->persistent->get('userid');
131 $user = core_user::get_user($foruserid, '*', MUST_EXIST);
132 $userexporter = new user_summary_exporter($user);
133 $values['foruser'] = $userexporter->export($output);
135 $requestedbyid = $this->persistent->get('requestedby');
136 if ($requestedbyid != $foruserid) {
137 $user = core_user::get_user($requestedbyid, '*', MUST_EXIST);
138 $userexporter = new user_summary_exporter($user);
139 $values['requestedbyuser'] = $userexporter->export($output);
140 } else {
141 $values['requestedbyuser'] = $values['foruser'];
144 if (!empty($this->persistent->get('dpo'))) {
145 $dpoid = $this->persistent->get('dpo');
146 $user = core_user::get_user($dpoid, '*', MUST_EXIST);
147 $userexporter = new user_summary_exporter($user);
148 $values['dpouser'] = $userexporter->export($output);
151 $values['messagehtml'] = text_to_html($this->persistent->get('comments'));
153 $requesttype = $this->persistent->get('type');
154 $values['typename'] = helper::get_request_type_string($requesttype);
155 $values['typenameshort'] = helper::get_shortened_request_type_string($requesttype);
157 $values['canreview'] = false;
158 $values['approvedeny'] = false;
159 $values['statuslabel'] = helper::get_request_status_string($this->persistent->get('status'));
161 switch ($this->persistent->get('status')) {
162 case api::DATAREQUEST_STATUS_PENDING:
163 $values['statuslabelclass'] = 'label-info';
164 // Request can be manually completed for general enquiry requests.
165 $values['canmarkcomplete'] = $requesttype == api::DATAREQUEST_TYPE_OTHERS;
166 break;
167 case api::DATAREQUEST_STATUS_PREPROCESSING:
168 $values['statuslabelclass'] = 'label-default';
169 break;
170 case api::DATAREQUEST_STATUS_AWAITING_APPROVAL:
171 $values['statuslabelclass'] = 'label-info';
172 // DPO can review the request once it's ready.
173 $values['canreview'] = true;
174 // Whether the DPO can approve or deny the request.
175 $values['approvedeny'] = in_array($requesttype, [api::DATAREQUEST_TYPE_EXPORT, api::DATAREQUEST_TYPE_DELETE]);
176 break;
177 case api::DATAREQUEST_STATUS_APPROVED:
178 $values['statuslabelclass'] = 'label-info';
179 break;
180 case api::DATAREQUEST_STATUS_PROCESSING:
181 $values['statuslabelclass'] = 'label-info';
182 break;
183 case api::DATAREQUEST_STATUS_COMPLETE:
184 case api::DATAREQUEST_STATUS_DOWNLOAD_READY:
185 case api::DATAREQUEST_STATUS_DELETED:
186 $values['statuslabelclass'] = 'label-success';
187 break;
188 case api::DATAREQUEST_STATUS_CANCELLED:
189 $values['statuslabelclass'] = 'label-warning';
190 break;
191 case api::DATAREQUEST_STATUS_REJECTED:
192 $values['statuslabelclass'] = 'label-important';
193 break;
194 case api::DATAREQUEST_STATUS_EXPIRED:
195 $values['statuslabelclass'] = 'label-default';
196 break;
199 return $values;