MDL-64506 templates: Move BS2 labels to BS4 badges
[moodle.git] / admin / tool / dataprivacy / classes / external / data_request_exporter.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 * Class for exporting user evidence with all competencies.
19 *
20 * @package tool_dataprivacy
21 * @copyright 2018 Jun Pataleta
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace tool_dataprivacy\external;
25defined('MOODLE_INTERNAL') || die();
26
27use coding_exception;
28use core\external\persistent_exporter;
29use core_user;
30use core_user\external\user_summary_exporter;
31use dml_exception;
c504ba43 32use moodle_exception;
5efc1f9e
DM
33use renderer_base;
34use tool_dataprivacy\api;
35use tool_dataprivacy\data_request;
c504ba43 36use tool_dataprivacy\local\helper;
5efc1f9e
DM
37
38/**
39 * Class for exporting user evidence with all competencies.
40 *
41 * @copyright 2018 Jun Pataleta
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 */
44class data_request_exporter extends persistent_exporter {
45
46 /**
47 * Class definition.
48 *
49 * @return string
50 */
51 protected static function define_class() {
52 return data_request::class;
53 }
54
55 /**
56 * Related objects definition.
57 *
58 * @return array
59 */
60 protected static function define_related() {
61 return [
62 'context' => 'context',
63 ];
64 }
65
66 /**
67 * Other properties definition.
68 *
69 * @return array
70 */
71 protected static function define_other_properties() {
72 return [
73 'foruser' => [
74 'type' => user_summary_exporter::read_properties_definition(),
75 ],
76 'requestedbyuser' => [
77 'type' => user_summary_exporter::read_properties_definition(),
78 'optional' => true
79 ],
80 'dpouser' => [
81 'type' => user_summary_exporter::read_properties_definition(),
82 'optional' => true
83 ],
84 'messagehtml' => [
85 'type' => PARAM_RAW,
86 'optional' => true
87 ],
88 'typename' => [
89 'type' => PARAM_TEXT,
90 ],
91 'typenameshort' => [
92 'type' => PARAM_TEXT,
93 ],
94 'statuslabel' => [
95 'type' => PARAM_TEXT,
96 ],
97 'statuslabelclass' => [
98 'type' => PARAM_TEXT,
99 ],
100 'canreview' => [
101 'type' => PARAM_BOOL,
102 'optional' => true,
103 'default' => false
104 ],
ef7f7cfd
JP
105 'approvedeny' => [
106 'type' => PARAM_BOOL,
107 'optional' => true,
108 'default' => false
109 ],
110 'canmarkcomplete' => [
111 'type' => PARAM_BOOL,
112 'optional' => true,
113 'default' => false
114 ],
5efc1f9e
DM
115 ];
116 }
117
118 /**
119 * Assign values to the defined other properties.
120 *
121 * @param renderer_base $output The output renderer object.
122 * @return array
123 * @throws coding_exception
124 * @throws dml_exception
c504ba43 125 * @throws moodle_exception
5efc1f9e
DM
126 */
127 protected function get_other_values(renderer_base $output) {
128 $values = [];
129
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);
134
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);
ba5b59c0
JP
140 } else {
141 $values['requestedbyuser'] = $values['foruser'];
5efc1f9e
DM
142 }
143
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);
149 }
150
151 $values['messagehtml'] = text_to_html($this->persistent->get('comments'));
152
ef7f7cfd
JP
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);
5efc1f9e
DM
156
157 $values['canreview'] = false;
ef7f7cfd 158 $values['approvedeny'] = false;
c504ba43 159 $values['statuslabel'] = helper::get_request_status_string($this->persistent->get('status'));
ef7f7cfd 160
5efc1f9e
DM
161 switch ($this->persistent->get('status')) {
162 case api::DATAREQUEST_STATUS_PENDING:
3f2c68f1 163 $values['statuslabelclass'] = 'badge-info';
ef7f7cfd
JP
164 // Request can be manually completed for general enquiry requests.
165 $values['canmarkcomplete'] = $requesttype == api::DATAREQUEST_TYPE_OTHERS;
5efc1f9e 166 break;
5efc1f9e 167 case api::DATAREQUEST_STATUS_AWAITING_APPROVAL:
3f2c68f1 168 $values['statuslabelclass'] = 'badge-info';
5efc1f9e
DM
169 // DPO can review the request once it's ready.
170 $values['canreview'] = true;
ef7f7cfd
JP
171 // Whether the DPO can approve or deny the request.
172 $values['approvedeny'] = in_array($requesttype, [api::DATAREQUEST_TYPE_EXPORT, api::DATAREQUEST_TYPE_DELETE]);
5efc1f9e
DM
173 break;
174 case api::DATAREQUEST_STATUS_APPROVED:
3f2c68f1 175 $values['statuslabelclass'] = 'badge-info';
5efc1f9e
DM
176 break;
177 case api::DATAREQUEST_STATUS_PROCESSING:
3f2c68f1 178 $values['statuslabelclass'] = 'badge-info';
5efc1f9e
DM
179 break;
180 case api::DATAREQUEST_STATUS_COMPLETE:
693f690c
MH
181 case api::DATAREQUEST_STATUS_DOWNLOAD_READY:
182 case api::DATAREQUEST_STATUS_DELETED:
3f2c68f1 183 $values['statuslabelclass'] = 'badge-success';
5efc1f9e
DM
184 break;
185 case api::DATAREQUEST_STATUS_CANCELLED:
3f2c68f1 186 $values['statuslabelclass'] = 'badge-warning';
5efc1f9e
DM
187 break;
188 case api::DATAREQUEST_STATUS_REJECTED:
3f2c68f1 189 $values['statuslabelclass'] = 'badge-danger';
5efc1f9e 190 break;
693f690c 191 case api::DATAREQUEST_STATUS_EXPIRED:
3f2c68f1 192 $values['statuslabelclass'] = 'badge-secondary';
693f690c 193 break;
5efc1f9e
DM
194 }
195
196 return $values;
197 }
198}