MDL-61899 tool_dataprivacy: Add lawful bases fields
[moodle.git] / admin / tool / dataprivacy / classes / purpose.php
blobc1631e47a7dad3e54c740e5a1fa684970dbb94f8
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 * Class for loading/storing data purposes from the DB.
20 * @package tool_dataprivacy
21 * @copyright 2018 David Monllao
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace tool_dataprivacy;
26 use stdClass;
28 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->dirroot . '/' . $CFG->admin . '/tool/dataprivacy/lib.php');
32 /**
33 * Class for loading/storing data purposes from the DB.
35 * @copyright 2018 David Monllao
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class purpose extends \core\persistent {
40 /**
41 * Database table.
43 const TABLE = 'tool_dataprivacy_purpose';
45 /** Items under GDPR Article 6.1. */
46 const GDPR_ART_6_1_ITEMS = ['a', 'b', 'c', 'd', 'e', 'f'];
48 /** Items under GDPR Article 9.2. */
49 const GDPR_ART_9_2_ITEMS = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
51 /**
52 * Extended constructor to fetch from the cache if available.
54 * @param int $id If set, this is the id of an existing record, used to load the data.
55 * @param stdClass $record If set will be passed to {@link self::from_record()}.
57 public function __construct($id = 0, stdClass $record = null) {
58 global $CFG;
60 if ($id) {
61 $cache = \cache::make('tool_dataprivacy', 'purpose');
62 if ($data = $cache->get($id)) {
64 // Replicate self::read.
65 $this->from_record($data);
67 // Using validate() as self::$validated is private.
68 $this->validate();
70 // Now replicate the parent constructor.
71 if (!empty($record)) {
72 $this->from_record($record);
74 if ($CFG->debugdeveloper) {
75 $this->verify_protected_methods();
77 return;
80 parent::__construct($id, $record);
83 /**
84 * Return the definition of the properties of this model.
86 * @return array
88 protected static function define_properties() {
89 return array(
90 'name' => array(
91 'type' => PARAM_TEXT,
92 'description' => 'The purpose name.',
94 'description' => array(
95 'type' => PARAM_RAW,
96 'description' => 'The purpose description.',
97 'null' => NULL_ALLOWED,
98 'default' => '',
100 'descriptionformat' => array(
101 'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
102 'type' => PARAM_INT,
103 'default' => FORMAT_HTML
105 'lawfulbases' => array(
106 'type' => PARAM_TEXT,
107 'description' => 'Comma-separated IDs matching records in tool_dataprivacy_lawfulbasis.',
109 'sensitivedatareasons' => array(
110 'type' => PARAM_TEXT,
111 'description' => 'Comma-separated IDs matching records in tool_dataprivacy_sensitive',
112 'null' => NULL_ALLOWED,
113 'default' => ''
115 'retentionperiod' => array(
116 'type' => PARAM_ALPHANUM,
117 'description' => 'Retention period. ISO_8601 durations format (as in DateInterval format).',
118 'default' => '',
120 'protected' => array(
121 'type' => PARAM_INT,
122 'description' => 'Data retention with higher precedent over user\'s request to be forgotten.',
123 'default' => '0',
129 * Adds the new record to the cache.
131 * @return null
133 protected function after_create() {
134 $cache = \cache::make('tool_dataprivacy', 'purpose');
135 $cache->set($this->get('id'), $this->to_record());
139 * Updates the cache record.
141 * @param bool $result
142 * @return null
144 protected function after_update($result) {
145 $cache = \cache::make('tool_dataprivacy', 'purpose');
146 $cache->set($this->get('id'), $this->to_record());
150 * Removes unnecessary stuff from db.
152 * @return null
154 protected function before_delete() {
155 $cache = \cache::make('tool_dataprivacy', 'purpose');
156 $cache->delete($this->get('id'));
160 * Is this purpose used?.
162 * @return null
164 public function is_used() {
166 if (\tool_dataprivacy\contextlevel::is_purpose_used($this->get('id')) ||
167 \tool_dataprivacy\context_instance::is_purpose_used($this->get('id'))) {
168 return true;
171 $pluginconfig = get_config('tool_dataprivacy');
172 $levels = \context_helper::get_all_levels();
173 foreach ($levels as $level => $classname) {
175 list($purposevar, $unused) = \tool_dataprivacy\data_registry::var_names_from_context($classname);
176 if (!empty($pluginconfig->{$purposevar}) && $pluginconfig->{$purposevar} == $this->get('id')) {
177 return true;
181 return false;