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] [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   * Collection of helper functions for the data privacy tool.
  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\local;
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use coding_exception;
  28  use moodle_exception;
  29  use tool_dataprivacy\api;
  30  use tool_dataprivacy\data_request;
  31  
  32  /**
  33   * Class containing helper functions for the data privacy tool.
  34   *
  35   * @copyright  2018 Jun Pataleta
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class helper {
  39      /** The default number of results to be shown per page. */
  40      const DEFAULT_PAGE_SIZE = 20;
  41  
  42      /** Filter constant associated with the request type filter. */
  43      const FILTER_TYPE = 1;
  44  
  45      /** Filter constant associated with the request status filter. */
  46      const FILTER_STATUS = 2;
  47  
  48      /** Filter constant associated with the request creation filter. */
  49      const FILTER_CREATION = 3;
  50  
  51      /** The request filters preference key. */
  52      const PREF_REQUEST_FILTERS = 'tool_dataprivacy_request-filters';
  53  
  54      /** The number of data request records per page preference key. */
  55      const PREF_REQUEST_PERPAGE = 'tool_dataprivacy_request-perpage';
  56  
  57      /**
  58       * Retrieves the human-readable text value of a data request type.
  59       *
  60       * @param int $requesttype The request type.
  61       * @return string
  62       * @throws coding_exception
  63       * @throws moodle_exception
  64       */
  65      public static function get_request_type_string($requesttype) {
  66          $types = self::get_request_types();
  67          if (!isset($types[$requesttype])) {
  68              throw new moodle_exception('errorinvalidrequesttype', 'tool_dataprivacy');
  69          }
  70          return $types[$requesttype];
  71      }
  72  
  73      /**
  74       * Retrieves the human-readable shortened text value of a data request type.
  75       *
  76       * @param int $requesttype The request type.
  77       * @return string
  78       * @throws coding_exception
  79       * @throws moodle_exception
  80       */
  81      public static function get_shortened_request_type_string($requesttype) {
  82          $types = self::get_request_types_short();
  83          if (!isset($types[$requesttype])) {
  84              throw new moodle_exception('errorinvalidrequesttype', 'tool_dataprivacy');
  85          }
  86          return $types[$requesttype];
  87      }
  88  
  89      /**
  90       * Returns the key value-pairs of request type code and their string value.
  91       *
  92       * @return array
  93       */
  94      public static function get_request_types() {
  95          return [
  96              api::DATAREQUEST_TYPE_EXPORT => get_string('requesttypeexport', 'tool_dataprivacy'),
  97              api::DATAREQUEST_TYPE_DELETE => get_string('requesttypedelete', 'tool_dataprivacy'),
  98              api::DATAREQUEST_TYPE_OTHERS => get_string('requesttypeothers', 'tool_dataprivacy'),
  99          ];
 100      }
 101  
 102      /**
 103       * Returns the key value-pairs of request type code and their shortened string value.
 104       *
 105       * @return array
 106       */
 107      public static function get_request_types_short() {
 108          return [
 109              api::DATAREQUEST_TYPE_EXPORT => get_string('requesttypeexportshort', 'tool_dataprivacy'),
 110              api::DATAREQUEST_TYPE_DELETE => get_string('requesttypedeleteshort', 'tool_dataprivacy'),
 111              api::DATAREQUEST_TYPE_OTHERS => get_string('requesttypeothersshort', 'tool_dataprivacy'),
 112          ];
 113      }
 114  
 115      /**
 116       * Retrieves the human-readable value of a data request status.
 117       *
 118       * @param int $status The request status.
 119       * @return string
 120       * @throws moodle_exception
 121       */
 122      public static function get_request_status_string($status) {
 123          $statuses = self::get_request_statuses();
 124          if (!isset($statuses[$status])) {
 125              throw new moodle_exception('errorinvalidrequeststatus', 'tool_dataprivacy');
 126          }
 127  
 128          return $statuses[$status];
 129      }
 130  
 131      /**
 132       * Returns the key value-pairs of request status code and string value.
 133       *
 134       * @return array
 135       */
 136      public static function get_request_statuses() {
 137          return [
 138              api::DATAREQUEST_STATUS_PENDING => get_string('statuspending', 'tool_dataprivacy'),
 139              api::DATAREQUEST_STATUS_PREPROCESSING => get_string('statuspreprocessing', 'tool_dataprivacy'),
 140              api::DATAREQUEST_STATUS_AWAITING_APPROVAL => get_string('statusawaitingapproval', 'tool_dataprivacy'),
 141              api::DATAREQUEST_STATUS_APPROVED => get_string('statusapproved', 'tool_dataprivacy'),
 142              api::DATAREQUEST_STATUS_PROCESSING => get_string('statusprocessing', 'tool_dataprivacy'),
 143              api::DATAREQUEST_STATUS_COMPLETE => get_string('statuscomplete', 'tool_dataprivacy'),
 144              api::DATAREQUEST_STATUS_DOWNLOAD_READY => get_string('statusready', 'tool_dataprivacy'),
 145              api::DATAREQUEST_STATUS_EXPIRED => get_string('statusexpired', 'tool_dataprivacy'),
 146              api::DATAREQUEST_STATUS_CANCELLED => get_string('statuscancelled', 'tool_dataprivacy'),
 147              api::DATAREQUEST_STATUS_REJECTED => get_string('statusrejected', 'tool_dataprivacy'),
 148              api::DATAREQUEST_STATUS_DELETED => get_string('statusdeleted', 'tool_dataprivacy'),
 149          ];
 150      }
 151  
 152      /**
 153       * Retrieves the human-readable value of a data request creation method.
 154       *
 155       * @param int $creation The request creation method.
 156       * @return string
 157       * @throws moodle_exception
 158       */
 159      public static function get_request_creation_method_string($creation) {
 160          $creationmethods = self::get_request_creation_methods();
 161          if (!isset($creationmethods[$creation])) {
 162              throw new moodle_exception('errorinvalidrequestcreationmethod', 'tool_dataprivacy');
 163          }
 164  
 165          return $creationmethods[$creation];
 166      }
 167  
 168      /**
 169       * Returns the key value-pairs of request creation method code and string value.
 170       *
 171       * @return array
 172       */
 173      public static function get_request_creation_methods() {
 174          return [
 175              data_request::DATAREQUEST_CREATION_MANUAL => get_string('creationmanual', 'tool_dataprivacy'),
 176              data_request::DATAREQUEST_CREATION_AUTO => get_string('creationauto', 'tool_dataprivacy'),
 177          ];
 178      }
 179  
 180      /**
 181       * Get the users that a user can make data request for.
 182       *
 183       * E.g. User having a parent role and has the 'tool/dataprivacy:makedatarequestsforchildren' capability.
 184       * @param int $userid The user's ID.
 185       * @return array
 186       */
 187      public static function get_children_of_user($userid) {
 188          global $DB;
 189  
 190          // Get users that the user has role assignments to.
 191          $userfieldsapi = \core_user\fields::for_name();
 192          $allusernames = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
 193          $sql = "SELECT u.id, $allusernames
 194                    FROM {role_assignments} ra, {context} c, {user} u
 195                   WHERE ra.userid = :userid
 196                         AND ra.contextid = c.id
 197                         AND c.instanceid = u.id
 198                         AND c.contextlevel = :contextlevel";
 199          $params = [
 200              'userid' => $userid,
 201              'contextlevel' => CONTEXT_USER
 202          ];
 203  
 204          // The final list of users that we will return.
 205          $finalresults = [];
 206  
 207          // Our prospective list of users.
 208          if ($candidates = $DB->get_records_sql($sql, $params)) {
 209              foreach ($candidates as $key => $child) {
 210                  $childcontext = \context_user::instance($child->id);
 211                  if (has_capability('tool/dataprivacy:makedatarequestsforchildren', $childcontext, $userid)) {
 212                      $finalresults[$key] = $child;
 213                  }
 214              }
 215          }
 216          return $finalresults;
 217      }
 218  
 219      /**
 220       * Get options for the data requests filter.
 221       *
 222       * @return array
 223       * @throws coding_exception
 224       */
 225      public static function get_request_filter_options() {
 226          $filters = [
 227              self::FILTER_TYPE => (object)[
 228                  'name' => get_string('requesttype', 'tool_dataprivacy'),
 229                  'options' => self::get_request_types_short()
 230              ],
 231              self::FILTER_STATUS => (object)[
 232                  'name' => get_string('requeststatus', 'tool_dataprivacy'),
 233                  'options' => self::get_request_statuses()
 234              ],
 235              self::FILTER_CREATION => (object)[
 236                  'name' => get_string('requestcreation', 'tool_dataprivacy'),
 237                  'options' => self::get_request_creation_methods()
 238              ],
 239          ];
 240          $options = [];
 241          foreach ($filters as $category => $filtercategory) {
 242              foreach ($filtercategory->options as $key => $name) {
 243                  $option = (object)[
 244                      'category' => $filtercategory->name,
 245                      'name' => $name
 246                  ];
 247                  $options["{$category}:{$key}"] = get_string('filteroption', 'tool_dataprivacy', $option);
 248              }
 249          }
 250          return $options;
 251      }
 252  }