Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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