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';
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) {
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.
64 // Now replicate the parent constructor.
65 if (!empty($record)) {
66 $this->from_record($record);
68 if ($CFG->debugdeveloper
) {
69 $this->verify_protected_methods();
74 parent
::__construct($id, $record);
78 * Return the definition of the properties of this model.
82 protected static function define_properties() {
86 'description' => 'The purpose name.',
88 'description' => array(
90 'description' => 'The purpose description.',
91 'null' => NULL_ALLOWED
,
94 'descriptionformat' => array(
95 'choices' => array(FORMAT_HTML
, FORMAT_MOODLE
, FORMAT_PLAIN
, FORMAT_MARKDOWN
),
97 'default' => FORMAT_HTML
99 'retentionperiod' => array(
100 'type' => PARAM_ALPHANUM
,
101 'description' => 'Retention period. ISO_8601 durations format (as in DateInterval format).',
104 'protected' => array(
106 'description' => 'Data retention with higher precedent over user\'s request to be forgotten.',
113 * Adds the new record to the cache.
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
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.
138 protected function before_delete() {
139 $cache = \cache
::make('tool_dataprivacy', 'purpose');
140 $cache->delete($this->get('id'));
144 * Is this purpose used?.
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'))) {
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')) {