Skip to content

Commit

Permalink
MDL-62980 tool_dataprivacy: show persistent errors inside form.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Jul 30, 2020
1 parent d8b0be3 commit d688483
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
9 changes: 7 additions & 2 deletions admin/tool/dataprivacy/classes/data_request.php
Expand Up @@ -26,10 +26,11 @@

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
Expand Down Expand Up @@ -62,6 +63,7 @@ protected static function define_properties() {
],
'comments' => [
'type' => PARAM_TEXT,
'message' => new lang_string('errorinvalidrequestcomments', 'tool_dataprivacy'),
'default' => ''
],
'commentsformat' => [
Expand All @@ -75,7 +77,10 @@ protected static function define_properties() {
'default' => FORMAT_PLAIN
],
'userid' => [
'default' => 0,
'default' => function() {
global $USER;
return $USER->id;
},
'type' => PARAM_INT
],
'requestedby' => [
Expand Down
4 changes: 2 additions & 2 deletions admin/tool/dataprivacy/createdatarequest.php
Expand Up @@ -67,8 +67,8 @@
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()) {
Expand Down
38 changes: 28 additions & 10 deletions admin/tool/dataprivacy/createdatarequest_form.php
Expand Up @@ -23,6 +23,7 @@
*/

use tool_dataprivacy\api;
use tool_dataprivacy\data_request;
use tool_dataprivacy\local\helper;

defined('MOODLE_INTERNAL') || die();
Expand All @@ -36,7 +37,10 @@
* @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;
Expand Down Expand Up @@ -96,14 +100,13 @@ public function definition() {
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.
Expand All @@ -129,34 +132,49 @@ public function definition() {
}
}

/**
* 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');
Expand Down
1 change: 1 addition & 0 deletions admin/tool/dataprivacy/lang/en/tool_dataprivacy.php
Expand Up @@ -135,6 +135,7 @@
$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!';
Expand Down

0 comments on commit d688483

Please sign in to comment.