MDL-61899 tool_dataprivacy: Refined patch fixing cibot complains
[moodle.git] / admin / tool / dataprivacy / classes / form / purpose.php
blob1bed873e7f276fa8bd3ecd11501e345f926b7bb9
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',
66 get_string('retentionperiod', 'tool_dataprivacy'), null, false);
67 $mform->setType('retentionperiodnumber', PARAM_INT);
69 $this->_form->addElement('advcheckbox', 'protected', get_string('protected', 'tool_dataprivacy'),
70 get_string('protectedlabel', 'tool_dataprivacy'));
72 if (!empty($this->_customdata['showbuttons'])) {
73 if (!$this->get_persistent()->get('id')) {
74 $savetext = get_string('add');
75 } else {
76 $savetext = get_string('savechanges');
78 $this->add_action_buttons(true, $savetext);
82 /**
83 * Converts fields.
85 * @param \stdClass $data
86 * @return \stdClass
88 protected static function convert_fields(\stdClass $data) {
89 $data = parent::convert_fields($data);
91 // A single value.
92 $data->retentionperiod = 'P' . $data->retentionperiodnumber . $data->retentionperiodunit;
93 unset($data->retentionperiodnumber);
94 unset($data->retentionperiodunit);
95 return $data;
98 /**
99 * Get the default data.
101 * @return \stdClass
103 protected function get_default_data() {
104 $data = parent::get_default_data();
106 // Convert the single properties into number and unit.
107 $strlen = strlen($data->retentionperiod);
108 $data->retentionperiodnumber = substr($data->retentionperiod, 1, $strlen - 2);
109 $data->retentionperiodunit = substr($data->retentionperiod, $strlen - 1);
110 unset($data->retentionperiod);
112 return $data;