MDL-61899 tool_dataprivacy: Refined patch fixing cibot complains
[moodle.git] / admin / tool / dataprivacy / classes / form / context_instance.php
blob707b290a84af9375a92441d4284a08f7dad53d71
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 context instance data.
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 tool_dataprivacy\api;
29 use tool_dataprivacy\data_registry;
31 /**
32 * Context instance data form.
34 * @package tool_dataprivacy
35 * @copyright 2018 David Monllao
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class context_instance extends \core\form\persistent {
40 /**
41 * @var The persistent class.
43 protected static $persistentclass = 'tool_dataprivacy\\context_instance';
45 /**
46 * Define the form - called by parent constructor
48 public function definition() {
49 $this->_form->setDisableShortforms();
51 $this->_form->addElement('header', 'contextname', $this->_customdata['contextname']);
53 $subjectscope = implode(', ', $this->_customdata['subjectscope']);
54 if (empty($subjectscope)) {
55 $subjectscope = get_string('noassignedroles', 'tool_dataprivacy');
57 $this->_form->addElement('static', 'subjectscope', get_string('subjectscope', 'tool_dataprivacy'), $subjectscope);
59 $this->add_purpose_category($this->_customdata['context']->contextlevel);
61 $this->_form->addElement('hidden', 'contextid');
62 $this->_form->setType('contextid', PARAM_INT);
64 parent::add_action_buttons(false, get_string('savechanges'));
67 /**
68 * Adds purpose and category selectors.
70 * @param int $contextlevel Apply this context level defaults. False for no defaults.
71 * @return null
73 protected function add_purpose_category($contextlevel = false) {
75 $mform = $this->_form;
77 $addcategorytext = $this->get_add_element_content(get_string('addcategory', 'tool_dataprivacy'));
78 $categoryselect = $mform->createElement('select', 'categoryid', null, $this->_customdata['categories']);
79 $addcategory = $mform->createElement('button', 'addcategory', $addcategorytext, ['data-add-element' => 'category']);
80 $mform->addElement('group', 'categorygroup', get_string('category', 'tool_dataprivacy'),
81 [$categoryselect, $addcategory], null, false);
82 $mform->setType('categoryid', PARAM_INT);
83 $mform->setDefault('categoryid', 0);
85 $addpurposetext = $this->get_add_element_content(get_string('addpurpose', 'tool_dataprivacy'));
86 $purposeselect = $mform->createElement('select', 'purposeid', null, $this->_customdata['purposes']);
87 $addpurpose = $mform->createElement('button', 'addpurpose', $addpurposetext, ['data-add-element' => 'purpose']);
88 $mform->addElement('group', 'purposegroup', get_string('purpose', 'tool_dataprivacy'),
89 [$purposeselect, $addpurpose], null, false);
90 $mform->setType('purposeid', PARAM_INT);
91 $mform->setDefault('purposeid', 0);
93 if (!empty($this->_customdata['currentretentionperiod'])) {
94 $mform->addElement('static', 'retention_current', get_string('retentionperiod', 'tool_dataprivacy'),
95 $this->_customdata['currentretentionperiod']);
99 /**
100 * Returns the 'add' label.
102 * It depends on the theme in use.
104 * @param string $label
105 * @return \renderable|string
107 private function get_add_element_content($label) {
108 global $PAGE, $OUTPUT;
110 $bs4 = false;
112 $theme = $PAGE->theme;
113 if ($theme->name === 'boost') {
114 $bs4 = true;
115 } else {
116 foreach ($theme->parents as $basetheme) {
117 if ($basetheme === 'boost') {
118 $bs4 = true;
123 if (!$bs4) {
124 return $label;
126 return $OUTPUT->pix_icon('e/insert', $label);
130 * Returns the customdata array for the provided context instance.
132 * @param \context $context
133 * @return array
135 public static function get_context_instance_customdata(\context $context) {
137 $persistent = \tool_dataprivacy\context_instance::get_record_by_contextid($context->id, false);
138 if (!$persistent) {
139 $persistent = new \tool_dataprivacy\context_instance();
140 $persistent->set('contextid', $context->id);
143 $purposeoptions = \tool_dataprivacy\output\data_registry_page::purpose_options(
144 api::get_purposes()
146 $categoryoptions = \tool_dataprivacy\output\data_registry_page::category_options(
147 api::get_categories()
150 $customdata = [
151 'context' => $context,
152 'subjectscope' => data_registry::get_subject_scope($context),
153 'contextname' => $context->get_context_name(),
154 'persistent' => $persistent,
155 'purposes' => $purposeoptions,
156 'categories' => $categoryoptions,
159 $effectivepurpose = api::get_effective_context_purpose($context);
160 if ($effectivepurpose) {
162 $customdata['currentretentionperiod'] = self::get_retention_display_text($effectivepurpose, $context->contextlevel,
163 $context);
165 $customdata['purposeretentionperiods'] = [];
166 foreach ($purposeoptions as $optionvalue => $unused) {
167 // Get the effective purpose if $optionvalue would be the selected value.
168 $purpose = api::get_effective_context_purpose($context, $optionvalue);
170 $retentionperiod = self::get_retention_display_text(
171 $purpose,
172 $context->contextlevel,
173 $context
175 $customdata['purposeretentionperiods'][$optionvalue] = $retentionperiod;
179 return $customdata;
183 * Returns the purpose display text.
185 * @param \tool_dataprivacy\purpose $effectivepurpose
186 * @param int $retentioncontextlevel
187 * @param \context $context The context, just for displaying (filters) purposes.
188 * @return string
190 protected static function get_retention_display_text(\tool_dataprivacy\purpose $effectivepurpose, $retentioncontextlevel, \context $context) {
191 global $PAGE;
193 $renderer = $PAGE->get_renderer('tool_dataprivacy');
195 $exporter = new \tool_dataprivacy\external\purpose_exporter($effectivepurpose, ['context' => $context]);
196 $exportedpurpose = $exporter->export($renderer);
198 switch ($retentioncontextlevel) {
199 case CONTEXT_COURSE:
200 case CONTEXT_MODULE:
201 case CONTEXT_BLOCK:
202 $str = get_string('effectiveretentionperiodcourse', 'tool_dataprivacy',
203 $exportedpurpose->formattedretentionperiod);
204 break;
205 case CONTEXT_USER:
206 $str = get_string('effectiveretentionperioduser', 'tool_dataprivacy',
207 $exportedpurpose->formattedretentionperiod);
208 break;
209 default:
210 $str = $exportedpurpose->formattedretentionperiod;
213 return $str;