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 context level data 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
;
25 defined('MOODLE_INTERNAL') ||
die();
28 * Class for loading/storing context level data from the DB.
30 * @copyright 2018 David Monllao
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class contextlevel
extends \core\persistent
{
38 const TABLE
= 'tool_dataprivacy_ctxlevel';
41 * Return the definition of the properties of this model.
45 protected static function define_properties() {
47 'contextlevel' => array(
49 'description' => 'The context level.',
53 'description' => 'The purpose id.',
55 'categoryid' => array(
57 'description' => 'The category id.',
63 * Returns an instance by contextlevel.
65 * @param mixed $contextlevel
66 * @param mixed $exception
69 public static function get_record_by_contextlevel($contextlevel, $exception = true) {
72 $cache = \cache
::make('tool_dataprivacy', 'contextlevel');
73 if ($data = $cache->get($contextlevel)) {
74 return new static(0, $data);
77 if (!$record = $DB->get_record(self
::TABLE
, array('contextlevel' => $contextlevel))) {
81 throw new \
dml_missing_record_exception(self
::TABLE
);
85 return new static(0, $record);
89 * Is the provided purpose used by any contextlevel?
91 * @param int $purposeid
94 public static function is_purpose_used($purposeid) {
96 return $DB->record_exists(self
::TABLE
, array('purposeid' => $purposeid));
100 * Is the provided category used by any contextlevel?
102 * @param int $categoryid
105 public static function is_category_used($categoryid) {
107 return $DB->record_exists(self
::TABLE
, array('categoryid' => $categoryid));
111 * Adds the new record to the cache.
115 protected function after_create() {
116 $cache = \cache
::make('tool_dataprivacy', 'contextlevel');
117 $cache->set($this->get('contextlevel'), $this->to_record());
121 * Updates the cache record.
123 * @param bool $result
126 protected function after_update($result) {
127 $cache = \cache
::make('tool_dataprivacy', 'contextlevel');
128 $cache->set($this->get('contextlevel'), $this->to_record());
132 * Removes unnecessary stuff from db.
136 protected function before_delete() {
137 $cache = \cache
::make('tool_dataprivacy', 'contextlevel');
138 $cache->delete($this->get('contextlevel'));