diff --git a/admin/tool/dataprivacy/classes/data_request.php b/admin/tool/dataprivacy/classes/data_request.php index 6a94fad41a1..02edc7c64e5 100644 --- a/admin/tool/dataprivacy/classes/data_request.php +++ b/admin/tool/dataprivacy/classes/data_request.php @@ -26,10 +26,11 @@ namespace tool_dataprivacy; defined('MOODLE_INTERNAL') || die(); +use lang_string; use core\persistent; /** - * Class for loading/storing competencies from the DB. + * Class for loading/storing data requests from the DB. * * @copyright 2018 Jun Pataleta * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later @@ -62,6 +63,7 @@ class data_request extends persistent { ], 'comments' => [ 'type' => PARAM_TEXT, + 'message' => new lang_string('errorinvalidrequestcomments', 'tool_dataprivacy'), 'default' => '' ], 'commentsformat' => [ @@ -75,7 +77,10 @@ class data_request extends persistent { 'default' => FORMAT_PLAIN ], 'userid' => [ - 'default' => 0, + 'default' => function() { + global $USER; + return $USER->id; + }, 'type' => PARAM_INT ], 'requestedby' => [ diff --git a/admin/tool/dataprivacy/createdatarequest.php b/admin/tool/dataprivacy/createdatarequest.php index c29f1975515..a81df118777 100644 --- a/admin/tool/dataprivacy/createdatarequest.php +++ b/admin/tool/dataprivacy/createdatarequest.php @@ -67,8 +67,8 @@ if (!$manage && !\tool_dataprivacy\api::can_contact_dpo()) { redirect($returnurl, get_string('contactdpoviaprivacypolicy', 'tool_dataprivacy'), 0, \core\output\notification::NOTIFY_ERROR); } -$mform = new tool_dataprivacy_data_request_form($url->out(false), ['manage' => !empty($manage)]); -$mform->set_data(['type' => $requesttype]); +$mform = new tool_dataprivacy_data_request_form($url->out(false), ['manage' => !empty($manage), + 'persistent' => new \tool_dataprivacy\data_request(0, (object) ['type' => $requesttype])]); // Data request cancelled. if ($mform->is_cancelled()) { diff --git a/admin/tool/dataprivacy/createdatarequest_form.php b/admin/tool/dataprivacy/createdatarequest_form.php index c91213c67db..c708a8857ef 100644 --- a/admin/tool/dataprivacy/createdatarequest_form.php +++ b/admin/tool/dataprivacy/createdatarequest_form.php @@ -23,6 +23,7 @@ */ use tool_dataprivacy\api; +use tool_dataprivacy\data_request; use tool_dataprivacy\local\helper; defined('MOODLE_INTERNAL') || die(); @@ -36,7 +37,10 @@ require_once($CFG->libdir.'/formslib.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @package tool_dataprivacy */ -class tool_dataprivacy_data_request_form extends moodleform { +class tool_dataprivacy_data_request_form extends \core\form\persistent { + + /** @var string Name of the persistent class. */ + protected static $persistentclass = data_request::class; /** @var bool Flag to indicate whether this form is being rendered for managing data requests or for regular requests. */ protected $manage = false; @@ -96,14 +100,13 @@ class tool_dataprivacy_data_request_form extends moodleform { api::DATAREQUEST_TYPE_EXPORT => get_string('requesttypeexport', 'tool_dataprivacy'), api::DATAREQUEST_TYPE_DELETE => get_string('requesttypedelete', 'tool_dataprivacy') ]; + $mform->addElement('select', 'type', get_string('requesttype', 'tool_dataprivacy'), $options); - $mform->setType('type', PARAM_INT); $mform->addHelpButton('type', 'requesttype', 'tool_dataprivacy'); // Request comments text area. $textareaoptions = ['cols' => 60, 'rows' => 10]; $mform->addElement('textarea', 'comments', get_string('requestcomments', 'tool_dataprivacy'), $textareaoptions); - $mform->setType('type', PARAM_ALPHANUM); $mform->addHelpButton('comments', 'requestcomments', 'tool_dataprivacy'); // Action buttons. @@ -129,34 +132,49 @@ class tool_dataprivacy_data_request_form extends moodleform { } } + /** + * Get the default data. Unset the default userid if managing data requests + * + * @return stdClass + */ + protected function get_default_data() { + $data = parent::get_default_data(); + if ($this->manage) { + unset($data->userid); + } + + return $data; + } + /** * Form validation. * - * @param array $data + * @param stdClass $data * @param array $files + * @param array $errors * @return array * @throws coding_exception * @throws dml_exception */ - public function validation($data, $files) { + public function extra_validation($data, $files, array &$errors) { global $USER; - $errors = []; $validrequesttypes = [ api::DATAREQUEST_TYPE_EXPORT, api::DATAREQUEST_TYPE_DELETE ]; - if (!in_array($data['type'], $validrequesttypes)) { + if (!in_array($data->type, $validrequesttypes)) { $errors['type'] = get_string('errorinvalidrequesttype', 'tool_dataprivacy'); } - if (api::has_ongoing_request($data['userid'], $data['type'])) { + $userid = $data->userid; + + if (api::has_ongoing_request($userid, $data->type)) { $errors['type'] = get_string('errorrequestalreadyexists', 'tool_dataprivacy'); } // Check if current user can create data deletion request. - $userid = $data['userid']; - if ($data['type'] == api::DATAREQUEST_TYPE_DELETE) { + if ($data->type == api::DATAREQUEST_TYPE_DELETE) { if ($userid == $USER->id) { if (!api::can_create_data_deletion_request_for_self()) { $errors['type'] = get_string('errorcannotrequestdeleteforself', 'tool_dataprivacy'); diff --git a/admin/tool/dataprivacy/lang/en/tool_dataprivacy.php b/admin/tool/dataprivacy/lang/en/tool_dataprivacy.php index b5f3e605c9b..63d33fe50b6 100644 --- a/admin/tool/dataprivacy/lang/en/tool_dataprivacy.php +++ b/admin/tool/dataprivacy/lang/en/tool_dataprivacy.php @@ -135,6 +135,7 @@ $string['effectiveretentionperioduser'] = '{$a} (since the last time the user ac $string['emailsalutation'] = 'Dear {$a},'; $string['errorcannotrequestdeleteforself'] = 'You don\'t have permission to create deletion request for yourself.'; $string['errorcannotrequestdeleteforother'] = 'You don\'t have permission to create deletion request for this user.'; +$string['errorinvalidrequestcomments'] = 'Please ensure your comment contains plain text only.'; $string['errorinvalidrequestcreationmethod'] = 'Invalid request creation method!'; $string['errorinvalidrequeststatus'] = 'Invalid request status!'; $string['errorinvalidrequesttype'] = 'Invalid request type!';