MDL-61899 tool_dataprivacy: Refined patch fixing cibot complains
[moodle.git] / admin / tool / dataprivacy / classes / purpose.php
blob461740d48b8eef84ac75b0186dff9e426845be26
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 /**
46 * Extended constructor to fetch from the cache if available.
48 * @param int $id If set, this is the id of an existing record, used to load the data.
49 * @param stdClass $record If set will be passed to {@link self::from_record()}.
51 public function __construct($id = 0, stdClass $record = null) {
52 global $CFG;
54 if ($id) {
55 $cache = \cache::make('tool_dataprivacy', 'purpose');
56 if ($data = $cache->get($id)) {
58 // Replicate self::read.
59 $this->from_record($data);
61 // Using validate() as self::$validated is private.
62 $this->validate();
64 // Now replicate the parent constructor.
65 if (!empty($record)) {
66 $this->from_record($record);
68 if ($CFG->debugdeveloper) {
69 $this->verify_protected_methods();
71 return;
74 parent::__construct($id, $record);
77 /**
78 * Return the definition of the properties of this model.
80 * @return array
82 protected static function define_properties() {
83 return array(
84 'name' => array(
85 'type' => PARAM_TEXT,
86 'description' => 'The purpose name.',
88 'description' => array(
89 'type' => PARAM_RAW,
90 'description' => 'The purpose description.',
91 'null' => NULL_ALLOWED,
92 'default' => '',
94 'descriptionformat' => array(
95 'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
96 'type' => PARAM_INT,
97 'default' => FORMAT_HTML
99 'retentionperiod' => array(
100 'type' => PARAM_ALPHANUM,
101 'description' => 'Retention period. ISO_8601 durations format (as in DateInterval format).',
102 'default' => '',
104 'protected' => array(
105 'type' => PARAM_INT,
106 'description' => 'Data retention with higher precedent over user\'s request to be forgotten.',
107 'default' => '0',
113 * Adds the new record to the cache.
115 * @return null
117 protected function after_create() {
118 $cache = \cache::make('tool_dataprivacy', 'purpose');
119 $cache->set($this->get('id'), $this->to_record());
123 * Updates the cache record.
125 * @param bool $result
126 * @return null
128 protected function after_update($result) {
129 $cache = \cache::make('tool_dataprivacy', 'purpose');
130 $cache->set($this->get('id'), $this->to_record());
134 * Removes unnecessary stuff from db.
136 * @return null
138 protected function before_delete() {
139 $cache = \cache::make('tool_dataprivacy', 'purpose');
140 $cache->delete($this->get('id'));
144 * Is this purpose used?.
146 * @return null
148 public function is_used() {
150 if (\tool_dataprivacy\contextlevel::is_purpose_used($this->get('id')) ||
151 \tool_dataprivacy\context_instance::is_purpose_used($this->get('id'))) {
152 return true;
155 $pluginconfig = get_config('tool_dataprivacy');
156 $levels = \context_helper::get_all_levels();
157 foreach ($levels as $level => $classname) {
159 list($purposevar, $unused) = \tool_dataprivacy\data_registry::var_names_from_context($classname);
160 if (!empty($pluginconfig->{$purposevar}) && $pluginconfig->{$purposevar} == $this->get('id')) {
161 return true;
165 return false;