Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]

   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 exporting data purpose.
  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\external;
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use core\external\persistent_exporter;
  28  use renderer_base;
  29  use tool_dataprivacy\context_instance;
  30  use tool_dataprivacy\purpose;
  31  
  32  /**
  33   * Class for exporting field data.
  34   *
  35   * @copyright  2018 David Monllao
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class purpose_exporter extends persistent_exporter {
  39  
  40      /**
  41       * Defines the persistent class.
  42       *
  43       * @return string
  44       */
  45      protected static function define_class() {
  46          return purpose::class;
  47      }
  48  
  49      /**
  50       * Returns a list of objects that are related.
  51       *
  52       * @return array
  53       */
  54      protected static function define_related() {
  55          return array(
  56              'context' => 'context',
  57          );
  58      }
  59  
  60      /**
  61       * Return the list of additional properties.
  62       *
  63       * @return array
  64       */
  65      protected static function define_other_properties() {
  66          return [
  67              'formattedretentionperiod' => [
  68                  'type' => PARAM_TEXT
  69              ],
  70              'formattedlawfulbases' => [
  71                  'type' => name_description_exporter::read_properties_definition(),
  72                  'multiple' => true
  73              ],
  74              'formattedsensitivedatareasons' => [
  75                  'type' => name_description_exporter::read_properties_definition(),
  76                  'multiple' => true,
  77                  'optional' => true
  78              ],
  79              'roleoverrides' => [
  80                  'type' => PARAM_TEXT
  81              ],
  82          ];
  83      }
  84  
  85      /**
  86       * Return other properties.
  87       *
  88       * @param renderer_base $output
  89       * @return array
  90       * @throws coding_exception
  91       * @throws Exception
  92       */
  93      protected function get_other_values(renderer_base $output) {
  94          $values = [];
  95  
  96          $formattedbases = [];
  97          $lawfulbases = explode(',', $this->persistent->get('lawfulbases'));
  98          if (!empty($lawfulbases)) {
  99              foreach ($lawfulbases as $basis) {
 100                  if (empty(trim($basis))) {
 101                      continue;
 102                  }
 103                  $formattedbases[] = (object)[
 104                      'name' => get_string($basis . '_name', 'tool_dataprivacy'),
 105                      'description' => get_string($basis . '_description', 'tool_dataprivacy')
 106                  ];
 107              }
 108          }
 109          $values['formattedlawfulbases'] = $formattedbases;
 110  
 111          $formattedsensitivereasons = [];
 112          $sensitivereasons = explode(',', $this->persistent->get('sensitivedatareasons') ?? '');
 113          if (!empty($sensitivereasons)) {
 114              foreach ($sensitivereasons as $reason) {
 115                  if (empty(trim($reason))) {
 116                      continue;
 117                  }
 118                  $formattedsensitivereasons[] = (object)[
 119                      'name' => get_string($reason . '_name', 'tool_dataprivacy'),
 120                      'description' => get_string($reason . '_description', 'tool_dataprivacy')
 121                  ];
 122              }
 123          }
 124          $values['formattedsensitivedatareasons'] = $formattedsensitivereasons;
 125  
 126          $retentionperiod = $this->persistent->get('retentionperiod');
 127          if ($retentionperiod) {
 128              $formattedtime = \tool_dataprivacy\api::format_retention_period(new \DateInterval($retentionperiod));
 129          } else {
 130              $formattedtime = get_string('retentionperiodnotdefined', 'tool_dataprivacy');
 131          }
 132          $values['formattedretentionperiod'] = $formattedtime;
 133  
 134          $values['roleoverrides'] = !empty($this->persistent->get_purpose_overrides());
 135  
 136          return $values;
 137      }
 138  
 139      /**
 140       * Utility function that fetches a purpose name from the given ID.
 141       *
 142       * @param int $purposeid The purpose ID. Could be INHERIT (false, -1), NOT_SET (0), or the actual ID.
 143       * @return string The purpose name.
 144       */
 145      public static function get_name($purposeid) {
 146          global $PAGE;
 147          if ($purposeid === false || $purposeid == context_instance::INHERIT) {
 148              return get_string('inherit', 'tool_dataprivacy');
 149          } else if ($purposeid == context_instance::NOTSET) {
 150              return get_string('notset', 'tool_dataprivacy');
 151          } else {
 152              $purpose = new purpose($purposeid);
 153              $output = $PAGE->get_renderer('tool_dataprivacy');
 154              $exporter = new self($purpose, ['context' => \context_system::instance()]);
 155              $data = $exporter->export($output);
 156              return $data->name;
 157          }
 158      }
 159  }