MDL-61899 tool_dataprivacy: Refined patch fixing cibot complains
[moodle.git] / admin / tool / dataprivacy / classes / contextlevel.php
blob97af9622079ffafa68dd94e2e62f998128b28241
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 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();
27 /**
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 {
35 /**
36 * Database table.
38 const TABLE = 'tool_dataprivacy_ctxlevel';
40 /**
41 * Return the definition of the properties of this model.
43 * @return array
45 protected static function define_properties() {
46 return array(
47 'contextlevel' => array(
48 'type' => PARAM_INT,
49 'description' => 'The context level.',
51 'purposeid' => array(
52 'type' => PARAM_INT,
53 'description' => 'The purpose id.',
55 'categoryid' => array(
56 'type' => PARAM_INT,
57 'description' => 'The category id.',
62 /**
63 * Returns an instance by contextlevel.
65 * @param mixed $contextlevel
66 * @param mixed $exception
67 * @return null
69 public static function get_record_by_contextlevel($contextlevel, $exception = true) {
70 global $DB;
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))) {
78 if (!$exception) {
79 return false;
80 } else {
81 throw new \dml_missing_record_exception(self::TABLE);
85 return new static(0, $record);
88 /**
89 * Is the provided purpose used by any contextlevel?
91 * @param int $purposeid
92 * @return bool
94 public static function is_purpose_used($purposeid) {
95 global $DB;
96 return $DB->record_exists(self::TABLE, array('purposeid' => $purposeid));
99 /**
100 * Is the provided category used by any contextlevel?
102 * @param int $categoryid
103 * @return bool
105 public static function is_category_used($categoryid) {
106 global $DB;
107 return $DB->record_exists(self::TABLE, array('categoryid' => $categoryid));
111 * Adds the new record to the cache.
113 * @return null
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
124 * @return null
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.
134 * @return null
136 protected function before_delete() {
137 $cache = \cache::make('tool_dataprivacy', 'contextlevel');
138 $cache->delete($this->get('contextlevel'));