Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Class for loading/storing context level data from the DB.
  19   *
  20   * @package    tool_dataprivacy
  21   * @copyright  2018 David Monllao
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace tool_dataprivacy;
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Class for loading/storing context level data from the DB.
  29   *
  30   * @copyright  2018 David Monllao
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class contextlevel extends \core\persistent {
  34  
  35      /**
  36       * Database table.
  37       */
  38      const TABLE = 'tool_dataprivacy_ctxlevel';
  39  
  40      /**
  41       * Return the definition of the properties of this model.
  42       *
  43       * @return array
  44       */
  45      protected static function define_properties() {
  46          return array(
  47              'contextlevel' => array(
  48                  'type' => PARAM_INT,
  49                  'description' => 'The context level.',
  50              ),
  51              'purposeid' => array(
  52                  'type' => PARAM_INT,
  53                  'description' => 'The purpose id.',
  54              ),
  55              'categoryid' => array(
  56                  'type' => PARAM_INT,
  57                  'description' => 'The category id.',
  58              ),
  59          );
  60      }
  61  
  62      /**
  63       * Returns an instance by contextlevel.
  64       *
  65       * @param mixed $contextlevel
  66       * @param mixed $exception
  67       * @return null
  68       */
  69      public static function get_record_by_contextlevel($contextlevel, $exception = true) {
  70          global $DB;
  71  
  72          $cache = \cache::make('tool_dataprivacy', 'contextlevel');
  73          if ($data = $cache->get($contextlevel)) {
  74              return new static(0, $data);
  75          }
  76  
  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);
  82              }
  83          }
  84  
  85          return new static(0, $record);
  86      }
  87  
  88      /**
  89       * Is the provided purpose used by any contextlevel?
  90       *
  91       * @param int $purposeid
  92       * @return bool
  93       */
  94      public static function is_purpose_used($purposeid) {
  95          global $DB;
  96          return $DB->record_exists(self::TABLE, array('purposeid' => $purposeid));
  97      }
  98  
  99      /**
 100       * Is the provided category used by any contextlevel?
 101       *
 102       * @param int $categoryid
 103       * @return bool
 104       */
 105      public static function is_category_used($categoryid) {
 106          global $DB;
 107          return $DB->record_exists(self::TABLE, array('categoryid' => $categoryid));
 108      }
 109  
 110      /**
 111       * Adds the new record to the cache.
 112       *
 113       * @return null
 114       */
 115      protected function after_create() {
 116          $cache = \cache::make('tool_dataprivacy', 'contextlevel');
 117          $cache->set($this->get('contextlevel'), $this->to_record());
 118      }
 119  
 120      /**
 121       * Updates the cache record.
 122       *
 123       * @param bool $result
 124       * @return null
 125       */
 126      protected function after_update($result) {
 127          $cache = \cache::make('tool_dataprivacy', 'contextlevel');
 128          $cache->set($this->get('contextlevel'), $this->to_record());
 129      }
 130  
 131      /**
 132       * Removes unnecessary stuff from db.
 133       *
 134       * @return null
 135       */
 136      protected function before_delete() {
 137          $cache = \cache::make('tool_dataprivacy', 'contextlevel');
 138          $cache->delete($this->get('contextlevel'));
 139      }
 140  }