Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is 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 instances 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 instances 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 context_instance extends \core\persistent {
  34  
  35      /**
  36       * Database table.
  37       */
  38      const TABLE = 'tool_dataprivacy_ctxinstance';
  39  
  40      /**
  41       * Not set value.
  42       */
  43      const NOTSET = 0;
  44  
  45      /**
  46       * Inherit value.
  47       */
  48      const INHERIT = -1;
  49  
  50      /**
  51       * Return the definition of the properties of this model.
  52       *
  53       * @return array
  54       */
  55      protected static function define_properties() {
  56          return array(
  57              'contextid' => array(
  58                  'type' => PARAM_INT,
  59                  'description' => 'The context id.',
  60              ),
  61              'purposeid' => array(
  62                  'type' => PARAM_INT,
  63                  'description' => 'The purpose id.',
  64                  'null' => NULL_ALLOWED,
  65              ),
  66              'categoryid' => array(
  67                  'type' => PARAM_INT,
  68                  'description' => 'The category id.',
  69                  'null' => NULL_ALLOWED,
  70              ),
  71          );
  72      }
  73  
  74      /**
  75       * Returns an instance by contextid.
  76       *
  77       * @param mixed $contextid
  78       * @param mixed $exception
  79       * @return null
  80       */
  81      public static function get_record_by_contextid($contextid, $exception = true) {
  82          global $DB;
  83  
  84          if (!$record = $DB->get_record(self::TABLE, array('contextid' => $contextid))) {
  85              if (!$exception) {
  86                  return false;
  87              } else {
  88                  throw new \dml_missing_record_exception(self::TABLE);
  89              }
  90          }
  91  
  92          return new static(0, $record);
  93      }
  94  
  95      /**
  96       * Is the provided purpose used by any context instance?
  97       *
  98       * @param int $purposeid
  99       * @return bool
 100       */
 101      public static function is_purpose_used($purposeid) {
 102          global $DB;
 103          return $DB->record_exists(self::TABLE, array('purposeid' => $purposeid));
 104      }
 105  
 106      /**
 107       * Is the provided category used by any context instance?
 108       *
 109       * @param int $categoryid
 110       * @return bool
 111       */
 112      public static function is_category_used($categoryid) {
 113          global $DB;
 114          return $DB->record_exists(self::TABLE, array('categoryid' => $categoryid));
 115      }
 116  }