Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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