MDL-61899 tool_dataprivacy: Subject access requests tool
[moodle.git] / admin / tool / dataprivacy / classes / form / purpose.php
blob52c1d1f9d835db21535a50d43af4664b5bd1b163
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;
30 /**
31 * Data purpose form.
33 * @package tool_dataprivacy
34 * @copyright 2018 David Monllao
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class purpose extends persistent {
39 /**
40 * @var The persistent class.
42 protected static $persistentclass = 'tool_dataprivacy\\purpose';
44 /**
45 * Define the form - called by parent constructor
47 public function definition() {
48 $mform = $this->_form;
50 $mform->addElement('text', 'name', get_string('name'), 'maxlength="100"');
51 $mform->setType('name', PARAM_TEXT);
52 $mform->addRule('name', get_string('required'), 'required', null, 'server');
53 $mform->addRule('name', get_string('maximumchars', '', 100), 'maxlength', 100, 'server');
55 $mform->addElement('editor', 'description', get_string('description'), null, ['autosave' => false]);
56 $mform->setType('description', PARAM_CLEANHTML);
58 $number = $mform->createElement('text', 'retentionperiodnumber', null, ['size' => 8]);
59 $unitoptions = [
60 'Y' => get_string('years'),
61 'M' => strtolower(get_string('months')),
62 'D' => strtolower(get_string('days'))
64 $unit = $mform->createElement('select', 'retentionperiodunit', '', $unitoptions);
65 $mform->addGroup(['number' => $number, 'unit' => $unit], 'retentionperiod', get_string('retentionperiod', 'tool_dataprivacy'), null, false);
66 $mform->setType('retentionperiodnumber', PARAM_INT);
68 $this->_form->addElement('advcheckbox', 'protected', get_string('protected', 'tool_dataprivacy'),
69 get_string('protectedlabel', 'tool_dataprivacy'));
71 if (!empty($this->_customdata['showbuttons'])) {
72 if (!$this->get_persistent()->get('id')) {
73 $savetext = get_string('add');
74 } else {
75 $savetext = get_string('savechanges');
77 $this->add_action_buttons(true, $savetext);
81 /**
82 * Converts fields.
84 * @param \stdClass $data
85 * @return \stdClass
87 protected static function convert_fields(\stdClass $data) {
88 $data = parent::convert_fields($data);
90 // A single value.
91 $data->retentionperiod = 'P' . $data->retentionperiodnumber . $data->retentionperiodunit;
92 unset($data->retentionperiodnumber);
93 unset($data->retentionperiodunit);
94 return $data;
97 /**
98 * Get the default data.
100 * @return \stdClass
102 protected function get_default_data() {
103 $data = parent::get_default_data();
105 // Convert the single properties into number and unit.
106 $strlen = strlen($data->retentionperiod);
107 $data->retentionperiodnumber = substr($data->retentionperiod, 1, $strlen - 2);
108 $data->retentionperiodunit = substr($data->retentionperiod, $strlen - 1);
109 unset($data->retentionperiod);
111 return $data;