From c504ba4332ad9ea861307d86638d1f4847c091b0 Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Fri, 13 Apr 2018 12:06:30 +0800 Subject: [PATCH] MDL-61899 tool_dataprivacy: Helper class for the tool * Create helper functions for getting the string names of data request types and statuses. --- admin/tool/dataprivacy/classes/api.php | 21 +--- .../external/data_request_exporter.php | 24 +--- .../tool/dataprivacy/classes/local/helper.php | 110 ++++++++++++++++++ 3 files changed, 118 insertions(+), 37 deletions(-) create mode 100644 admin/tool/dataprivacy/classes/local/helper.php diff --git a/admin/tool/dataprivacy/classes/api.php b/admin/tool/dataprivacy/classes/api.php index b3e61a51744..bba717cbf31 100644 --- a/admin/tool/dataprivacy/classes/api.php +++ b/admin/tool/dataprivacy/classes/api.php @@ -36,14 +36,9 @@ use moodle_exception; use moodle_url; use required_capability_exception; use stdClass; +use tool_dataprivacy\local\helper; use tool_dataprivacy\task\initiate_data_request_task; use tool_dataprivacy\task\process_data_request_task; -use tool_dataprivacy\purpose; -use tool_dataprivacy\category; -use tool_dataprivacy\contextlevel; -use tool_dataprivacy\context_instance; -use tool_dataprivacy\data_registry; -use tool_dataprivacy\expired_context; defined('MOODLE_INTERNAL') || die(); @@ -400,19 +395,7 @@ class api { // Create message to send to the Data Protection Officer(s). $typetext = null; - switch ($request->get('type')) { - case self::DATAREQUEST_TYPE_EXPORT: - $typetext = get_string('requesttypeexport', 'tool_dataprivacy'); - break; - case self::DATAREQUEST_TYPE_DELETE: - $typetext = get_string('requesttypedelete', 'tool_dataprivacy'); - break; - case self::DATAREQUEST_TYPE_OTHERS: - $typetext = get_string('requesttypeothers', 'tool_dataprivacy'); - break; - default: - throw new moodle_exception('errorinvalidrequesttype', 'tool_dataprivacy'); - } + $typetext = helper::get_request_type_string($request->get('type')); $subject = get_string('datarequestemailsubject', 'tool_dataprivacy', $typetext); $requestedby = core_user::get_user($request->get('requestedby')); diff --git a/admin/tool/dataprivacy/classes/external/data_request_exporter.php b/admin/tool/dataprivacy/classes/external/data_request_exporter.php index d51c8b64637..7ba3266ebce 100644 --- a/admin/tool/dataprivacy/classes/external/data_request_exporter.php +++ b/admin/tool/dataprivacy/classes/external/data_request_exporter.php @@ -29,9 +29,11 @@ use core\external\persistent_exporter; use core_user; use core_user\external\user_summary_exporter; use dml_exception; +use moodle_exception; use renderer_base; use tool_dataprivacy\api; use tool_dataprivacy\data_request; +use tool_dataprivacy\local\helper; /** * Class for exporting user evidence with all competencies. @@ -110,6 +112,7 @@ class data_request_exporter extends persistent_exporter { * @return array * @throws coding_exception * @throws dml_exception + * @throws moodle_exception */ protected function get_other_values(renderer_base $output) { $values = []; @@ -135,52 +138,37 @@ class data_request_exporter extends persistent_exporter { $values['messagehtml'] = text_to_html($this->persistent->get('comments')); - if ($this->persistent->get('type') == api::DATAREQUEST_TYPE_EXPORT) { - $values['typename'] = get_string('requesttypeexport', 'tool_dataprivacy'); - $values['typenameshort'] = get_string('requesttypeexportshort', 'tool_dataprivacy'); - } else if ($this->persistent->get('type') == api::DATAREQUEST_TYPE_DELETE) { - $values['typename'] = get_string('requesttypedelete', 'tool_dataprivacy'); - $values['typenameshort'] = get_string('requesttypedeleteshort', 'tool_dataprivacy'); - } else { - $values['typename'] = get_string('requesttypeothers', 'tool_dataprivacy'); - $values['typenameshort'] = get_string('requesttypeothersshort', 'tool_dataprivacy'); - } + $values['typename'] = helper::get_request_type_string($this->persistent->get('type')); + $values['typenameshort'] = helper::get_shortened_request_type_string($this->persistent->get('type')); $values['canreview'] = false; + $values['statuslabel'] = helper::get_request_status_string($this->persistent->get('status')); switch ($this->persistent->get('status')) { case api::DATAREQUEST_STATUS_PENDING: $values['statuslabelclass'] = 'label-default'; - $values['statuslabel'] = get_string('statuspending', 'tool_dataprivacy'); break; case api::DATAREQUEST_STATUS_PREPROCESSING: $values['statuslabelclass'] = 'label-default'; - $values['statuslabel'] = get_string('statuspreprocessing', 'tool_dataprivacy'); break; case api::DATAREQUEST_STATUS_AWAITING_APPROVAL: $values['statuslabelclass'] = 'label-info'; - $values['statuslabel'] = get_string('statusawaitingapproval', 'tool_dataprivacy'); // DPO can review the request once it's ready. $values['canreview'] = true; break; case api::DATAREQUEST_STATUS_APPROVED: $values['statuslabelclass'] = 'label-info'; - $values['statuslabel'] = get_string('statusapproved', 'tool_dataprivacy'); break; case api::DATAREQUEST_STATUS_PROCESSING: $values['statuslabelclass'] = 'label-info'; - $values['statuslabel'] = get_string('statusprocessing', 'tool_dataprivacy'); break; case api::DATAREQUEST_STATUS_COMPLETE: $values['statuslabelclass'] = 'label-success'; - $values['statuslabel'] = get_string('statuscomplete', 'tool_dataprivacy'); break; case api::DATAREQUEST_STATUS_CANCELLED: $values['statuslabelclass'] = 'label-warning'; - $values['statuslabel'] = get_string('statuscancelled', 'tool_dataprivacy'); break; case api::DATAREQUEST_STATUS_REJECTED: $values['statuslabelclass'] = 'label-important'; - $values['statuslabel'] = get_string('statusrejected', 'tool_dataprivacy'); break; } diff --git a/admin/tool/dataprivacy/classes/local/helper.php b/admin/tool/dataprivacy/classes/local/helper.php new file mode 100644 index 00000000000..bf5254b554c --- /dev/null +++ b/admin/tool/dataprivacy/classes/local/helper.php @@ -0,0 +1,110 @@ +. + +/** + * Collection of helper functions for the data privacy tool. + * + * @package tool_dataprivacy + * @copyright 2018 Jun Pataleta + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace tool_dataprivacy\local; + +use coding_exception; +use moodle_exception; +use tool_dataprivacy\api; + +/** + * Class containing helper functions for the data privacy tool. + * + * @copyright 2018 Jun Pataleta + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class helper { + + /** + * Retrieves the human-readable text value of a data request type. + * + * @param int $requesttype The request type. + * @return string + * @throws coding_exception + * @throws moodle_exception + */ + public static function get_request_type_string($requesttype) { + switch ($requesttype) { + case api::DATAREQUEST_TYPE_EXPORT: + return get_string('requesttypeexport', 'tool_dataprivacy'); + case api::DATAREQUEST_TYPE_DELETE: + return get_string('requesttypedelete', 'tool_dataprivacy'); + case api::DATAREQUEST_TYPE_OTHERS: + return get_string('requesttypeothers', 'tool_dataprivacy'); + default: + throw new moodle_exception('errorinvalidrequesttype', 'tool_dataprivacy'); + } + } + + /** + * Retrieves the human-readable shortened text value of a data request type. + * + * @param int $requesttype The request type. + * @return string + * @throws coding_exception + * @throws moodle_exception + */ + public static function get_shortened_request_type_string($requesttype) { + switch ($requesttype) { + case api::DATAREQUEST_TYPE_EXPORT: + return get_string('requesttypeexportshort', 'tool_dataprivacy'); + case api::DATAREQUEST_TYPE_DELETE: + return get_string('requesttypedeleteshort', 'tool_dataprivacy'); + case api::DATAREQUEST_TYPE_OTHERS: + return get_string('requesttypeothersshort', 'tool_dataprivacy'); + default: + throw new moodle_exception('errorinvalidrequesttype', 'tool_dataprivacy'); + } + } + + /** + * Retrieves the human-readable value of a data request status. + * + * @param int $status The request status. + * @return string + * @throws coding_exception + * @throws moodle_exception + */ + public static function get_request_status_string($status) { + switch ($status) { + case api::DATAREQUEST_STATUS_PENDING: + return get_string('statuspending', 'tool_dataprivacy'); + case api::DATAREQUEST_STATUS_PREPROCESSING: + return get_string('statuspreprocessing', 'tool_dataprivacy'); + case api::DATAREQUEST_STATUS_AWAITING_APPROVAL: + return get_string('statusawaitingapproval', 'tool_dataprivacy'); + case api::DATAREQUEST_STATUS_APPROVED: + return get_string('statusapproved', 'tool_dataprivacy'); + case api::DATAREQUEST_STATUS_PROCESSING: + return get_string('statusprocessing', 'tool_dataprivacy'); + case api::DATAREQUEST_STATUS_COMPLETE: + return get_string('statuscomplete', 'tool_dataprivacy'); + case api::DATAREQUEST_STATUS_CANCELLED: + return get_string('statuscancelled', 'tool_dataprivacy'); + case api::DATAREQUEST_STATUS_REJECTED: + return get_string('statusrejected', 'tool_dataprivacy'); + default: + throw new moodle_exception('errorinvalidrequeststatus', 'tool_dataprivacy'); + } + } +}