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 data purposes 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  
  26  use stdClass;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  require_once($CFG->dirroot . '/' . $CFG->admin . '/tool/dataprivacy/lib.php');
  31  
  32  /**
  33   * Class for loading/storing data purposes from the DB.
  34   *
  35   * @copyright  2018 David Monllao
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class purpose extends \core\persistent {
  39  
  40      /**
  41       * Database table.
  42       */
  43      const TABLE = 'tool_dataprivacy_purpose';
  44  
  45      /** Items under GDPR Article 6.1. */
  46      const GDPR_ART_6_1_ITEMS = ['a', 'b', 'c', 'd', 'e', 'f'];
  47  
  48      /** Items under GDPR Article 9.2. */
  49      const GDPR_ART_9_2_ITEMS = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
  50  
  51      /**
  52       * Extended constructor to fetch from the cache if available.
  53       *
  54       * @param int $id If set, this is the id of an existing record, used to load the data.
  55       * @param stdClass $record If set will be passed to {@link self::from_record()}.
  56       */
  57      public function __construct($id = 0, stdClass $record = null) {
  58          global $CFG;
  59  
  60          if ($id) {
  61              $cache = \cache::make('tool_dataprivacy', 'purpose');
  62              if ($data = $cache->get($id)) {
  63  
  64                  // Replicate self::read.
  65                  $this->from_record($data);
  66  
  67                  // Validate the purpose record.
  68                  $this->validate();
  69  
  70                  // Now replicate the parent constructor.
  71                  if (!empty($record)) {
  72                      $this->from_record($record);
  73                  }
  74                  if ($CFG->debugdeveloper) {
  75                      $this->verify_protected_methods();
  76                  }
  77                  return;
  78              }
  79          }
  80          parent::__construct($id, $record);
  81      }
  82  
  83      /**
  84       * Return the definition of the properties of this model.
  85       *
  86       * @return array
  87       */
  88      protected static function define_properties() {
  89          return array(
  90              'name' => array(
  91                  'type' => PARAM_TEXT,
  92                  'description' => 'The purpose name.',
  93              ),
  94              'description' => array(
  95                  'type' => PARAM_RAW,
  96                  'description' => 'The purpose description.',
  97                  'null' => NULL_ALLOWED,
  98                  'default' => '',
  99              ),
 100              'descriptionformat' => array(
 101                  'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
 102                  'type' => PARAM_INT,
 103                  'default' => FORMAT_HTML
 104              ),
 105              'lawfulbases' => array(
 106                  'type' => PARAM_TEXT,
 107                  'description' => 'Comma-separated IDs matching records in tool_dataprivacy_lawfulbasis.',
 108              ),
 109              'sensitivedatareasons' => array(
 110                  'type' => PARAM_TEXT,
 111                  'description' => 'Comma-separated IDs matching records in tool_dataprivacy_sensitive',
 112                  'null' => NULL_ALLOWED,
 113                  'default' => ''
 114              ),
 115              'retentionperiod' => array(
 116                  'type' => PARAM_ALPHANUM,
 117                  'description' => 'Retention period. ISO_8601 durations format (as in DateInterval format).',
 118                  'default' => '',
 119              ),
 120              'protected' => array(
 121                  'type' => PARAM_INT,
 122                  'description' => 'Data retention with higher precedent over user\'s request to be forgotten.',
 123                  'default' => '0',
 124              ),
 125          );
 126      }
 127  
 128      /**
 129       * Adds the new record to the cache.
 130       *
 131       * @return null
 132       */
 133      protected function after_create() {
 134          $cache = \cache::make('tool_dataprivacy', 'purpose');
 135          $cache->set($this->get('id'), $this->to_record());
 136      }
 137  
 138      /**
 139       * Updates the cache record.
 140       *
 141       * @param bool $result
 142       * @return null
 143       */
 144      protected function after_update($result) {
 145          $cache = \cache::make('tool_dataprivacy', 'purpose');
 146          $cache->set($this->get('id'), $this->to_record());
 147      }
 148  
 149      /**
 150       * Removes unnecessary stuff from db.
 151       *
 152       * @return null
 153       */
 154      protected function before_delete() {
 155          $cache = \cache::make('tool_dataprivacy', 'purpose');
 156          $cache->delete($this->get('id'));
 157      }
 158  
 159      /**
 160       * Is this purpose used?.
 161       *
 162       * @return null
 163       */
 164      public function is_used() {
 165          if (\tool_dataprivacy\contextlevel::is_purpose_used($this->get('id')) ||
 166                  \tool_dataprivacy\context_instance::is_purpose_used($this->get('id'))) {
 167              return true;
 168          }
 169  
 170          $pluginconfig = get_config('tool_dataprivacy');
 171          $levels = \context_helper::get_all_levels();
 172          foreach ($levels as $level => $classname) {
 173  
 174              list($purposevar, $unused) = \tool_dataprivacy\data_registry::var_names_from_context($classname);
 175              if (!empty($pluginconfig->{$purposevar}) && $pluginconfig->{$purposevar} == $this->get('id')) {
 176                  return true;
 177              }
 178          }
 179  
 180          return false;
 181      }
 182  
 183      /**
 184       * Get a list of the role purpose overrides for this purpose.
 185       *
 186       * @return  array
 187       */
 188      public function get_purpose_overrides() : array {
 189          return purpose_override::get_overrides_for_purpose($this);
 190      }
 191  }