MDL-61899 tool_dataprivacy: Add lawful bases fields
[moodle.git] / admin / tool / dataprivacy / classes / external / purpose_exporter.php
blobe2f73c3b9a57d2747e06f0dacebadc106b94b3e5
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 data purpose.
20 * @package tool_dataprivacy
21 * @copyright 2018 David Monllao
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 DateInterval;
30 use Exception;
31 use external_single_structure;
32 use external_value;
33 use renderer_base;
34 use tool_dataprivacy\purpose;
36 /**
37 * Class for exporting field data.
39 * @copyright 2018 David Monllao
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class purpose_exporter extends persistent_exporter {
44 /**
45 * Defines the persistent class.
47 * @return string
49 protected static function define_class() {
50 return purpose::class;
53 /**
54 * Returns a list of objects that are related.
56 * @return array
58 protected static function define_related() {
59 return array(
60 'context' => 'context',
64 /**
65 * Return the list of additional properties.
67 * @return array
69 protected static function define_other_properties() {
70 $namedescriptiontype = [
71 'name' => [
72 'type' => PARAM_TEXT,
74 'description' => [
75 'type' => PARAM_TEXT,
78 return [
79 'formattedretentionperiod' => [
80 'type' => PARAM_TEXT
82 'formattedlawfulbases' => [
83 'type' => $namedescriptiontype,
84 'multiple' => true
86 'formattedsensitivedatareasons' => [
87 'type' => $namedescriptiontype,
88 'multiple' => true,
89 'optional' => true
94 /**
95 * Return other properties.
97 * @param renderer_base $output
98 * @return array
99 * @throws coding_exception
100 * @throws Exception
102 protected function get_other_values(renderer_base $output) {
103 $values = [];
105 $formattedbases = [];
106 $lawfulbases = explode(',', $this->persistent->get('lawfulbases'));
107 if (!empty($lawfulbases)) {
108 foreach ($lawfulbases as $basis) {
109 if (empty(trim($basis))) {
110 continue;
112 $formattedbases[] = (object)[
113 'name' => get_string($basis . '_name', 'tool_dataprivacy'),
114 'description' => get_string($basis . '_description', 'tool_dataprivacy')
118 $values['formattedlawfulbases'] = $formattedbases;
120 $formattedsensitivereasons = [];
121 $sensitivereasons = explode(',', $this->persistent->get('sensitivedatareasons'));
122 if (!empty($sensitivereasons)) {
123 foreach ($sensitivereasons as $reason) {
124 if (empty(trim($reason))) {
125 continue;
127 $formattedsensitivereasons[] = (object)[
128 'name' => get_string($reason . '_name', 'tool_dataprivacy'),
129 'description' => get_string($reason . '_description', 'tool_dataprivacy')
133 $values['formattedsensitivedatareasons'] = $formattedsensitivereasons;
135 $retentionperiod = $this->persistent->get('retentionperiod');
136 if ($retentionperiod) {
137 $interval = new DateInterval($retentionperiod);
139 // It is one or another.
140 if ($interval->y) {
141 $formattedtime = get_string('numyears', 'moodle', $interval->format('%y'));
142 } else if ($interval->m) {
143 $formattedtime = get_string('nummonths', 'moodle', $interval->format('%m'));
144 } else if ($interval->d) {
145 $formattedtime = get_string('numdays', 'moodle', $interval->format('%d'));
146 } else {
147 $formattedtime = get_string('retentionperiodzero', 'tool_dataprivacy');
149 } else {
150 $formattedtime = get_string('retentionperiodnotdefined', 'tool_dataprivacy');
152 $values['formattedretentionperiod'] = $formattedtime;
154 return $values;