Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 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 user evidence with all competencies.
  19   *
  20   * @package    tool_dataprivacy
  21   * @copyright  2018 Jun Pataleta
  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 core_user;
  29  use core_user\external\user_summary_exporter;
  30  use renderer_base;
  31  use tool_dataprivacy\api;
  32  use tool_dataprivacy\data_request;
  33  use tool_dataprivacy\local\helper;
  34  
  35  /**
  36   * Class for exporting user evidence with all competencies.
  37   *
  38   * @copyright  2018 Jun Pataleta
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class data_request_exporter extends persistent_exporter {
  42  
  43      /**
  44       * Class definition.
  45       *
  46       * @return string
  47       */
  48      protected static function define_class() {
  49          return data_request::class;
  50      }
  51  
  52      /**
  53       * Related objects definition.
  54       *
  55       * @return array
  56       */
  57      protected static function define_related() {
  58          return [
  59              'context' => 'context',
  60          ];
  61      }
  62  
  63      /**
  64       * Other properties definition.
  65       *
  66       * @return array
  67       */
  68      protected static function define_other_properties() {
  69          return [
  70              'foruser' => [
  71                  'type' => user_summary_exporter::read_properties_definition(),
  72              ],
  73              'requestedbyuser' => [
  74                  'type' => user_summary_exporter::read_properties_definition(),
  75                  'optional' => true
  76              ],
  77              'dpouser' => [
  78                  'type' => user_summary_exporter::read_properties_definition(),
  79                  'optional' => true
  80              ],
  81              'messagehtml' => [
  82                  'type' => PARAM_RAW,
  83                  'optional' => true
  84              ],
  85              'typename' => [
  86                  'type' => PARAM_TEXT,
  87              ],
  88              'typenameshort' => [
  89                  'type' => PARAM_TEXT,
  90              ],
  91              'statuslabel' => [
  92                  'type' => PARAM_TEXT,
  93              ],
  94              'statuslabelclass' => [
  95                  'type' => PARAM_TEXT,
  96              ],
  97              'canreview' => [
  98                  'type' => PARAM_BOOL,
  99                  'optional' => true,
 100                  'default' => false
 101              ],
 102              'approvedeny' => [
 103                  'type' => PARAM_BOOL,
 104                  'optional' => true,
 105                  'default' => false
 106              ],
 107              'canmarkcomplete' => [
 108                  'type' => PARAM_BOOL,
 109                  'optional' => true,
 110                  'default' => false
 111              ],
 112          ];
 113      }
 114  
 115      /**
 116       * Assign values to the defined other properties.
 117       *
 118       * @param renderer_base $output The output renderer object.
 119       * @return array
 120       * @throws coding_exception
 121       * @throws dml_exception
 122       * @throws moodle_exception
 123       */
 124      protected function get_other_values(renderer_base $output) {
 125          $values = [];
 126  
 127          $foruserid = $this->persistent->get('userid');
 128          $user = core_user::get_user($foruserid, '*', MUST_EXIST);
 129          $userexporter = new user_summary_exporter($user);
 130          $values['foruser'] = $userexporter->export($output);
 131  
 132          $requestedbyid = $this->persistent->get('requestedby');
 133          if ($requestedbyid != $foruserid) {
 134              $user = core_user::get_user($requestedbyid, '*', MUST_EXIST);
 135              $userexporter = new user_summary_exporter($user);
 136              $values['requestedbyuser'] = $userexporter->export($output);
 137          } else {
 138              $values['requestedbyuser'] = $values['foruser'];
 139          }
 140  
 141          if (!empty($this->persistent->get('dpo'))) {
 142              $dpoid = $this->persistent->get('dpo');
 143              $user = core_user::get_user($dpoid, '*', MUST_EXIST);
 144              $userexporter = new user_summary_exporter($user);
 145              $values['dpouser'] = $userexporter->export($output);
 146          }
 147  
 148          $values['messagehtml'] = text_to_html($this->persistent->get('comments'));
 149  
 150          $requesttype = $this->persistent->get('type');
 151          $values['typename'] = helper::get_request_type_string($requesttype);
 152          $values['typenameshort'] = helper::get_shortened_request_type_string($requesttype);
 153  
 154          $values['canreview'] = false;
 155          $values['approvedeny'] = false;
 156          $values['statuslabel'] = helper::get_request_status_string($this->persistent->get('status'));
 157  
 158          switch ($this->persistent->get('status')) {
 159              case api::DATAREQUEST_STATUS_PENDING:
 160                  $values['statuslabelclass'] = 'badge-info';
 161                  // Request can be manually completed for general enquiry requests.
 162                  $values['canmarkcomplete'] = $requesttype == api::DATAREQUEST_TYPE_OTHERS;
 163                  break;
 164              case api::DATAREQUEST_STATUS_AWAITING_APPROVAL:
 165                  $values['statuslabelclass'] = 'badge-info';
 166                  // DPO can review the request once it's ready.
 167                  $values['canreview'] = true;
 168                  // Whether the DPO can approve or deny the request.
 169                  $values['approvedeny'] = in_array($requesttype, [api::DATAREQUEST_TYPE_EXPORT, api::DATAREQUEST_TYPE_DELETE]);
 170                  // If the request's type is delete, check if user have permission to approve/deny it.
 171                  if ($requesttype == api::DATAREQUEST_TYPE_DELETE) {
 172                      $values['approvedeny'] = api::can_create_data_deletion_request_for_other();
 173                  }
 174                  break;
 175              case api::DATAREQUEST_STATUS_APPROVED:
 176                  $values['statuslabelclass'] = 'badge-info';
 177                  break;
 178              case api::DATAREQUEST_STATUS_PROCESSING:
 179                  $values['statuslabelclass'] = 'badge-info';
 180                  break;
 181              case api::DATAREQUEST_STATUS_COMPLETE:
 182              case api::DATAREQUEST_STATUS_DOWNLOAD_READY:
 183              case api::DATAREQUEST_STATUS_DELETED:
 184                  $values['statuslabelclass'] = 'badge-success';
 185                  break;
 186              case api::DATAREQUEST_STATUS_CANCELLED:
 187                  $values['statuslabelclass'] = 'badge-warning';
 188                  break;
 189              case api::DATAREQUEST_STATUS_REJECTED:
 190                  $values['statuslabelclass'] = 'badge-danger';
 191                  break;
 192              case api::DATAREQUEST_STATUS_EXPIRED:
 193                  $values['statuslabelclass'] = 'badge-secondary';
 194                  break;
 195          }
 196  
 197          return $values;
 198      }
 199  }