2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
;
28 defined('MOODLE_INTERNAL') ||
die();
30 require_once($CFG->dirroot
. '/' . $CFG->admin
. '/tool/dataprivacy/lib.php');
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
{
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'];
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) {
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.
70 // Now replicate the parent constructor.
71 if (!empty($record)) {
72 $this->from_record($record);
74 if ($CFG->debugdeveloper
) {
75 $this->verify_protected_methods();
80 parent
::__construct($id, $record);
84 * Return the definition of the properties of this model.
88 protected static function define_properties() {
92 'description' => 'The purpose name.',
94 'description' => array(
96 'description' => 'The purpose description.',
97 'null' => NULL_ALLOWED
,
100 'descriptionformat' => array(
101 'choices' => array(FORMAT_HTML
, FORMAT_MOODLE
, FORMAT_PLAIN
, FORMAT_MARKDOWN
),
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
,
115 'retentionperiod' => array(
116 'type' => PARAM_ALPHANUM
,
117 'description' => 'Retention period. ISO_8601 durations format (as in DateInterval format).',
120 'protected' => array(
122 'description' => 'Data retention with higher precedent over user\'s request to be forgotten.',
129 * Adds the new record to the cache.
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
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.
154 protected function before_delete() {
155 $cache = \cache
::make('tool_dataprivacy', 'purpose');
156 $cache->delete($this->get('id'));
160 * Is this purpose used?.
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'))) {
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')) {