| 1 | efrain | 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 purpose overrides from the DB.
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 20 |  * @package    tool_dataprivacy
 | 
        
           |  |  | 21 |  * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
 | 
        
           |  |  | 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 purpose overrides from the DB.
 | 
        
           |  |  | 34 |  *
 | 
        
           |  |  | 35 |  * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
 | 
        
           |  |  | 36 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 37 |  */
 | 
        
           |  |  | 38 | class purpose_override extends \core\persistent {
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 |     /**
 | 
        
           |  |  | 41 |      * Database table.
 | 
        
           |  |  | 42 |      */
 | 
        
           |  |  | 43 |     const TABLE = 'tool_dataprivacy_purposerole';
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 |     /**
 | 
        
           |  |  | 46 |      * Return the definition of the properties of this model.
 | 
        
           |  |  | 47 |      *
 | 
        
           |  |  | 48 |      * @return  array
 | 
        
           |  |  | 49 |      */
 | 
        
           |  |  | 50 |     protected static function define_properties() {
 | 
        
           |  |  | 51 |         return array(
 | 
        
           |  |  | 52 |             'purposeid' => array(
 | 
        
           |  |  | 53 |                 'type' => PARAM_INT,
 | 
        
           |  |  | 54 |                 'description' => 'The purpose that that this override relates to',
 | 
        
           |  |  | 55 |             ),
 | 
        
           |  |  | 56 |             'roleid' => array(
 | 
        
           |  |  | 57 |                 'type' => PARAM_INT,
 | 
        
           |  |  | 58 |                 'description' => 'The role that that this override relates to',
 | 
        
           |  |  | 59 |             ),
 | 
        
           |  |  | 60 |             'lawfulbases' => array(
 | 
        
           |  |  | 61 |                 'type' => PARAM_TEXT,
 | 
        
           |  |  | 62 |                 'description' => 'Comma-separated IDs matching records in tool_dataprivacy_lawfulbasis.',
 | 
        
           |  |  | 63 |                 'null' => NULL_ALLOWED,
 | 
        
           |  |  | 64 |                 'default' => null,
 | 
        
           |  |  | 65 |             ),
 | 
        
           |  |  | 66 |             'sensitivedatareasons' => array(
 | 
        
           |  |  | 67 |                 'type' => PARAM_TEXT,
 | 
        
           |  |  | 68 |                 'description' => 'Comma-separated IDs matching records in tool_dataprivacy_sensitive',
 | 
        
           |  |  | 69 |                 'null' => NULL_ALLOWED,
 | 
        
           |  |  | 70 |                 'default' => null,
 | 
        
           |  |  | 71 |             ),
 | 
        
           |  |  | 72 |             'retentionperiod' => array(
 | 
        
           |  |  | 73 |                 'type' => PARAM_ALPHANUM,
 | 
        
           |  |  | 74 |                 'description' => 'Retention period. ISO_8601 durations format (as in DateInterval format).',
 | 
        
           |  |  | 75 |                 'default' => '',
 | 
        
           |  |  | 76 |             ),
 | 
        
           |  |  | 77 |             'protected' => array(
 | 
        
           |  |  | 78 |                 'type' => PARAM_INT,
 | 
        
           |  |  | 79 |                 'description' => 'Data retention with higher precedent over user\'s request to be forgotten.',
 | 
        
           |  |  | 80 |                 'default' => '0',
 | 
        
           |  |  | 81 |             ),
 | 
        
           |  |  | 82 |         );
 | 
        
           |  |  | 83 |     }
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 |     /**
 | 
        
           |  |  | 86 |      * Get all role overrides for the purpose.
 | 
        
           |  |  | 87 |      *
 | 
        
           |  |  | 88 |      * @param   purpose $purpose
 | 
        
           |  |  | 89 |      * @return  array
 | 
        
           |  |  | 90 |      */
 | 
        
           |  |  | 91 |     public static function get_overrides_for_purpose(purpose $purpose): array {
 | 
        
           |  |  | 92 |         $cache = \cache::make('tool_dataprivacy', 'purpose_overrides');
 | 
        
           |  |  | 93 |   | 
        
           |  |  | 94 |         $overrides = [];
 | 
        
           |  |  | 95 |         $alldata = $cache->get($purpose->get('id'));
 | 
        
           |  |  | 96 |         if (false === $alldata) {
 | 
        
           |  |  | 97 |             $tocache = [];
 | 
        
           |  |  | 98 |             foreach (self::get_records(['purposeid' => $purpose->get('id')]) as $override) {
 | 
        
           |  |  | 99 |                 $tocache[] = $override->to_record();
 | 
        
           |  |  | 100 |                 $overrides[$override->get('roleid')] = $override;
 | 
        
           |  |  | 101 |             }
 | 
        
           |  |  | 102 |             $cache->set($purpose->get('id'), $tocache);
 | 
        
           |  |  | 103 |         } else {
 | 
        
           |  |  | 104 |             foreach ($alldata as $data) {
 | 
        
           |  |  | 105 |                 $override = new self(0, $data);
 | 
        
           |  |  | 106 |                 $overrides[$override->get('roleid')] = $override;
 | 
        
           |  |  | 107 |             }
 | 
        
           |  |  | 108 |         }
 | 
        
           |  |  | 109 |   | 
        
           |  |  | 110 |         return $overrides;
 | 
        
           |  |  | 111 |     }
 | 
        
           |  |  | 112 |   | 
        
           |  |  | 113 |     /**
 | 
        
           |  |  | 114 |      * Adds the new record to the cache.
 | 
        
           |  |  | 115 |      *
 | 
        
           |  |  | 116 |      * @return null
 | 
        
           |  |  | 117 |      */
 | 
        
           |  |  | 118 |     protected function after_create() {
 | 
        
           |  |  | 119 |         $cache = \cache::make('tool_dataprivacy', 'purpose_overrides');
 | 
        
           |  |  | 120 |         $cache->delete($this->get('purposeid'));
 | 
        
           |  |  | 121 |     }
 | 
        
           |  |  | 122 |   | 
        
           |  |  | 123 |     /**
 | 
        
           |  |  | 124 |      * Updates the cache record.
 | 
        
           |  |  | 125 |      *
 | 
        
           |  |  | 126 |      * @param bool $result
 | 
        
           |  |  | 127 |      * @return null
 | 
        
           |  |  | 128 |      */
 | 
        
           |  |  | 129 |     protected function after_update($result) {
 | 
        
           |  |  | 130 |         $cache = \cache::make('tool_dataprivacy', 'purpose_overrides');
 | 
        
           |  |  | 131 |         $cache->delete($this->get('purposeid'));
 | 
        
           |  |  | 132 |     }
 | 
        
           |  |  | 133 |   | 
        
           |  |  | 134 |     /**
 | 
        
           |  |  | 135 |      * Removes unnecessary stuff from db.
 | 
        
           |  |  | 136 |      *
 | 
        
           |  |  | 137 |      * @return null
 | 
        
           |  |  | 138 |      */
 | 
        
           |  |  | 139 |     protected function before_delete() {
 | 
        
           |  |  | 140 |         $cache = \cache::make('tool_dataprivacy', 'purpose_overrides');
 | 
        
           |  |  | 141 |         $cache->delete($this->get('purposeid'));
 | 
        
           |  |  | 142 |     }
 | 
        
           |  |  | 143 | }
 |