Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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.
13
//
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/>.
16
 
17
/**
18
 * This file contains the form add/update context instance data.
19
 *
20
 * @package   tool_dataprivacy
21
 * @copyright 2018 David Monllao
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace tool_dataprivacy\form;
26
defined('MOODLE_INTERNAL') || die();
27
 
28
use tool_dataprivacy\api;
29
use tool_dataprivacy\data_registry;
30
use tool_dataprivacy\purpose;
31
 
32
/**
33
 * Context instance data form.
34
 *
35
 * @package   tool_dataprivacy
36
 * @copyright 2018 David Monllao
37
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class context_instance extends \core\form\persistent {
40
 
41
    /**
42
     * @var The persistent class.
43
     */
44
    protected static $persistentclass = 'tool_dataprivacy\\context_instance';
45
 
46
    /**
47
     * Define the form - called by parent constructor
48
     */
49
    public function definition() {
50
        $this->_form->setDisableShortforms();
51
 
52
        $this->_form->addElement('header', 'contextname', $this->_customdata['contextname']);
53
 
54
        $subjectscope = implode(', ', $this->_customdata['subjectscope']);
55
        if (empty($subjectscope)) {
56
            $subjectscope = get_string('noassignedroles', 'tool_dataprivacy');
57
        }
58
        $this->_form->addElement('static', 'subjectscope', get_string('subjectscope', 'tool_dataprivacy'), $subjectscope);
59
        $this->_form->addHelpButton('subjectscope', 'subjectscope', 'tool_dataprivacy');
60
 
61
        $this->add_purpose_category($this->_customdata['context']->contextlevel);
62
 
63
        $this->_form->addElement('hidden', 'contextid');
64
        $this->_form->setType('contextid', PARAM_INT);
65
 
66
        parent::add_action_buttons(false, get_string('savechanges'));
67
    }
68
 
69
    /**
70
     * Adds purpose and category selectors.
71
     *
72
     * @param int $contextlevel Apply this context level defaults. False for no defaults.
73
     * @return null
74
     */
75
    protected function add_purpose_category($contextlevel = false) {
76
 
77
        $mform = $this->_form;
78
 
79
        $addcategorytext = $this->get_add_element_content(get_string('addcategory', 'tool_dataprivacy'));
80
        $categoryselect = $mform->createElement('select', 'categoryid', null, $this->_customdata['categories']);
81
        $addcategory = $mform->createElement('button', 'addcategory', $addcategorytext, ['data-add-element' => 'category']);
82
        $mform->addElement('group', 'categorygroup', get_string('category', 'tool_dataprivacy'),
83
            [$categoryselect, $addcategory], null, false);
84
        $mform->addHelpButton('categorygroup', 'category', 'tool_dataprivacy');
85
        $mform->setType('categoryid', PARAM_INT);
86
        $mform->setDefault('categoryid', 0);
87
 
88
        $addpurposetext = $this->get_add_element_content(get_string('addpurpose', 'tool_dataprivacy'));
89
        $purposeselect = $mform->createElement('select', 'purposeid', null, $this->_customdata['purposes']);
90
        $addpurpose = $mform->createElement('button', 'addpurpose', $addpurposetext, ['data-add-element' => 'purpose']);
91
        $mform->addElement('group', 'purposegroup', get_string('purpose', 'tool_dataprivacy'),
92
            [$purposeselect, $addpurpose], null, false);
93
        $mform->addHelpButton('purposegroup', 'purpose', 'tool_dataprivacy');
94
        $mform->setType('purposeid', PARAM_INT);
95
        $mform->setDefault('purposeid', 0);
96
 
97
        if (!empty($this->_customdata['currentretentionperiod'])) {
98
            $mform->addElement('static', 'retention_current', get_string('retentionperiod', 'tool_dataprivacy'),
99
                $this->_customdata['currentretentionperiod']);
100
            $mform->addHelpButton('retention_current', 'retentionperiod', 'tool_dataprivacy');
101
        }
102
    }
103
 
104
    /**
105
     * Returns the 'add' label.
106
     *
107
     * It depends on the theme in use.
108
     *
109
     * @param string $label
110
     * @return \renderable|string
111
     */
112
    private function get_add_element_content($label) {
113
        global $PAGE, $OUTPUT;
114
 
115
        $bs4 = false;
116
 
117
        $theme = $PAGE->theme;
118
        if ($theme->name === 'boost') {
119
            $bs4 = true;
120
        } else {
121
            foreach ($theme->parents as $basetheme) {
122
                if ($basetheme === 'boost') {
123
                    $bs4 = true;
124
                }
125
            }
126
        }
127
 
128
        if (!$bs4) {
129
            return $label;
130
        }
131
        return $OUTPUT->pix_icon('e/insert', $label);
132
    }
133
 
134
    /**
135
     * Returns the customdata array for the provided context instance.
136
     *
137
     * @param \context $context
138
     * @return array
139
     */
140
    public static function get_context_instance_customdata(\context $context) {
141
 
142
        $persistent = \tool_dataprivacy\context_instance::get_record_by_contextid($context->id, false);
143
        if (!$persistent) {
144
            $persistent = new \tool_dataprivacy\context_instance();
145
            $persistent->set('contextid', $context->id);
146
        }
147
 
148
        $purposes = [];
149
        foreach (api::get_purposes() as $purpose) {
150
            $purposes[$purpose->get('id')] = $purpose;
151
        }
152
        $purposeoptions = \tool_dataprivacy\output\data_registry_page::purpose_options($purposes);
153
        $categoryoptions = \tool_dataprivacy\output\data_registry_page::category_options(api::get_categories());
154
 
155
        $customdata = [
156
            'context' => $context,
157
            'subjectscope' => data_registry::get_subject_scope($context),
158
            'contextname' => $context->get_context_name(),
159
            'persistent' => $persistent,
160
            'purposes' => $purposeoptions,
161
            'categories' => $categoryoptions,
162
        ];
163
 
164
        $effectivepurpose = api::get_effective_context_purpose($context);
165
        if ($effectivepurpose) {
166
 
167
            $customdata['currentretentionperiod'] = self::get_retention_display_text($effectivepurpose, $context->contextlevel,
168
                $context);
169
 
170
            $customdata['purposeretentionperiods'] = [];
171
            foreach (array_keys($purposeoptions) as $optionvalue) {
172
 
173
                if (isset($purposes[$optionvalue])) {
174
                    $purpose = $purposes[$optionvalue];
175
                } else {
176
                    // Get the effective purpose if $optionvalue would be the selected value.
177
                    $purpose = api::get_effective_context_purpose($context, $optionvalue);
178
                }
179
 
180
                $retentionperiod = self::get_retention_display_text(
181
                    $purpose,
182
                    $context->contextlevel,
183
                    $context
184
                );
185
                $customdata['purposeretentionperiods'][$optionvalue] = $retentionperiod;
186
            }
187
        }
188
 
189
        return $customdata;
190
    }
191
 
192
    /**
193
     * Returns the purpose display text.
194
     *
195
     * @param purpose $effectivepurpose
196
     * @param int $retentioncontextlevel
197
     * @param \context $context The context, just for displaying (filters) purposes.
198
     * @return string
199
     */
200
    protected static function get_retention_display_text(purpose $effectivepurpose, $retentioncontextlevel, \context $context) {
201
        global $PAGE;
202
 
203
        $renderer = $PAGE->get_renderer('tool_dataprivacy');
204
 
205
        $exporter = new \tool_dataprivacy\external\purpose_exporter($effectivepurpose, ['context' => $context]);
206
        $exportedpurpose = $exporter->export($renderer);
207
 
208
        switch ($retentioncontextlevel) {
209
            case CONTEXT_COURSE:
210
            case CONTEXT_MODULE:
211
            case CONTEXT_BLOCK:
212
                $str = get_string('effectiveretentionperiodcourse', 'tool_dataprivacy',
213
                    $exportedpurpose->formattedretentionperiod);
214
                break;
215
            case CONTEXT_USER:
216
                $str = get_string('effectiveretentionperioduser', 'tool_dataprivacy',
217
                    $exportedpurpose->formattedretentionperiod);
218
                break;
219
            default:
220
                $str = $exportedpurpose->formattedretentionperiod;
221
        }
222
 
223
        return $str;
224
    }
225
 
226
}