MDL-61899 tool_dataprivacy: Add lawful bases fields
[moodle.git] / admin / tool / dataprivacy / classes / form / purpose.php
blob3e42e3a488d32442eb087d74e71584eba2164f8d
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 * This file contains the form add/update a 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
25 namespace tool_dataprivacy\form;
26 defined('MOODLE_INTERNAL') || die();
28 use core\form\persistent;
29 use tool_dataprivacy\purpose as purpose_persistent;
31 /**
32 * Data purpose form.
34 * @package tool_dataprivacy
35 * @copyright 2018 David Monllao
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class purpose extends persistent {
40 /**
41 * @var string The persistent class.
43 protected static $persistentclass = 'tool_dataprivacy\\purpose';
45 /**
46 * Define the form - called by parent constructor
48 public function definition() {
49 global $DB;
51 $mform = $this->_form;
53 $mform->addElement('text', 'name', get_string('name'), 'maxlength="100"');
54 $mform->setType('name', PARAM_TEXT);
55 $mform->addRule('name', get_string('required'), 'required', null, 'server');
56 $mform->addRule('name', get_string('maximumchars', '', 100), 'maxlength', 100, 'server');
58 $mform->addElement('editor', 'description', get_string('description'), null, ['autosave' => false]);
59 $mform->setType('description', PARAM_CLEANHTML);
61 // Field for selecting lawful bases (from GDPR Article 6.1).
62 $lawfulbases = [];
63 foreach (purpose_persistent::GDPR_ART_6_1_ITEMS as $article) {
64 $key = 'gdpr_art_6_1_' . $article;
65 $lawfulbases[$key] = get_string($key . '_name', 'tool_dataprivacy');
67 $options = array(
68 'multiple' => true,
70 $mform->addElement('autocomplete', 'lawfulbases', get_string('lawfulbases', 'tool_dataprivacy'), $lawfulbases, $options);
71 $mform->addRule('lawfulbases', get_string('required'), 'required', null, 'server');
72 $mform->addHelpButton('lawfulbases', 'lawfulbases', 'tool_dataprivacy');
74 // Optional field for selecting reasons for collecting sensitive personal data (from GDPR Article 9.2).
75 $sensitivereasons = [];
76 foreach (purpose_persistent::GDPR_ART_9_2_ITEMS as $article) {
77 $key = 'gdpr_art_9_2_' . $article;
78 $sensitivereasons[$key] = get_string($key . '_name', 'tool_dataprivacy');
80 $mform->addElement('autocomplete', 'sensitivedatareasons', get_string('sensitivedatareasons', 'tool_dataprivacy'),
81 $sensitivereasons, $options);
82 $mform->addHelpButton('sensitivedatareasons', 'sensitivedatareasons', 'tool_dataprivacy');
84 $number = $mform->createElement('text', 'retentionperiodnumber', null, ['size' => 8]);
85 $unitoptions = [
86 'Y' => get_string('years'),
87 'M' => strtolower(get_string('months')),
88 'D' => strtolower(get_string('days'))
90 $unit = $mform->createElement('select', 'retentionperiodunit', '', $unitoptions);
91 $mform->addGroup(['number' => $number, 'unit' => $unit], 'retentionperiod',
92 get_string('retentionperiod', 'tool_dataprivacy'), null, false);
93 $mform->setType('retentionperiodnumber', PARAM_INT);
95 $this->_form->addElement('advcheckbox', 'protected', get_string('protected', 'tool_dataprivacy'),
96 get_string('protectedlabel', 'tool_dataprivacy'));
98 if (!empty($this->_customdata['showbuttons'])) {
99 if (!$this->get_persistent()->get('id')) {
100 $savetext = get_string('add');
101 } else {
102 $savetext = get_string('savechanges');
104 $this->add_action_buttons(true, $savetext);
109 * Converts fields.
111 * @param \stdClass $data
112 * @return \stdClass
114 protected static function convert_fields(\stdClass $data) {
115 $data = parent::convert_fields($data);
117 if (is_array($data->lawfulbases)) {
118 $data->lawfulbases = implode(',', $data->lawfulbases);
120 if (is_array($data->sensitivedatareasons)) {
121 $data->sensitivedatareasons = implode(',', $data->sensitivedatareasons);
124 // A single value.
125 $data->retentionperiod = 'P' . $data->retentionperiodnumber . $data->retentionperiodunit;
126 unset($data->retentionperiodnumber);
127 unset($data->retentionperiodunit);
128 return $data;
132 * Get the default data.
134 * @return \stdClass
136 protected function get_default_data() {
137 $data = parent::get_default_data();
139 $data->lawfulbases = explode(',', $data->lawfulbases);
140 if (!empty($data->sensitivedatareasons)) {
141 $data->sensitivedatareasons = explode(',', $data->sensitivedatareasons);
144 // Convert the single properties into number and unit.
145 $strlen = strlen($data->retentionperiod);
146 $data->retentionperiodnumber = substr($data->retentionperiod, 1, $strlen - 2);
147 $data->retentionperiodunit = substr($data->retentionperiod, $strlen - 1);
148 unset($data->retentionperiod);
150 return $data;