MDL-61899 tool_dataprivacy: Create name-description exporter
[moodle.git] / admin / tool / dataprivacy / classes / external / purpose_exporter.php
blobf91e2f28b8d555fb73af74726295ea7653b17972
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 renderer_base;
32 use tool_dataprivacy\purpose;
34 /**
35 * Class for exporting field data.
37 * @copyright 2018 David Monllao
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class purpose_exporter extends persistent_exporter {
42 /**
43 * Defines the persistent class.
45 * @return string
47 protected static function define_class() {
48 return purpose::class;
51 /**
52 * Returns a list of objects that are related.
54 * @return array
56 protected static function define_related() {
57 return array(
58 'context' => 'context',
62 /**
63 * Return the list of additional properties.
65 * @return array
67 protected static function define_other_properties() {
68 return [
69 'formattedretentionperiod' => [
70 'type' => PARAM_TEXT
72 'formattedlawfulbases' => [
73 'type' => name_description_exporter::read_properties_definition(),
74 'multiple' => true
76 'formattedsensitivedatareasons' => [
77 'type' => name_description_exporter::read_properties_definition(),
78 'multiple' => true,
79 'optional' => true
84 /**
85 * Return other properties.
87 * @param renderer_base $output
88 * @return array
89 * @throws coding_exception
90 * @throws Exception
92 protected function get_other_values(renderer_base $output) {
93 $values = [];
95 $formattedbases = [];
96 $lawfulbases = explode(',', $this->persistent->get('lawfulbases'));
97 if (!empty($lawfulbases)) {
98 foreach ($lawfulbases as $basis) {
99 if (empty(trim($basis))) {
100 continue;
102 $formattedbases[] = (object)[
103 'name' => get_string($basis . '_name', 'tool_dataprivacy'),
104 'description' => get_string($basis . '_description', 'tool_dataprivacy')
108 $values['formattedlawfulbases'] = $formattedbases;
110 $formattedsensitivereasons = [];
111 $sensitivereasons = explode(',', $this->persistent->get('sensitivedatareasons'));
112 if (!empty($sensitivereasons)) {
113 foreach ($sensitivereasons as $reason) {
114 if (empty(trim($reason))) {
115 continue;
117 $formattedsensitivereasons[] = (object)[
118 'name' => get_string($reason . '_name', 'tool_dataprivacy'),
119 'description' => get_string($reason . '_description', 'tool_dataprivacy')
123 $values['formattedsensitivedatareasons'] = $formattedsensitivereasons;
125 $retentionperiod = $this->persistent->get('retentionperiod');
126 if ($retentionperiod) {
127 $interval = new DateInterval($retentionperiod);
129 // It is one or another.
130 if ($interval->y) {
131 $formattedtime = get_string('numyears', 'moodle', $interval->format('%y'));
132 } else if ($interval->m) {
133 $formattedtime = get_string('nummonths', 'moodle', $interval->format('%m'));
134 } else if ($interval->d) {
135 $formattedtime = get_string('numdays', 'moodle', $interval->format('%d'));
136 } else {
137 $formattedtime = get_string('retentionperiodzero', 'tool_dataprivacy');
139 } else {
140 $formattedtime = get_string('retentionperiodnotdefined', 'tool_dataprivacy');
142 $values['formattedretentionperiod'] = $formattedtime;
144 return $values;