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.
   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   * Data provider.
  19   *
  20   * @package    tool_messageinbound
  21   * @copyright  2018 Frédéric Massart
  22   * @author     Frédéric Massart <fred@branchup.tech>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace tool_messageinbound\privacy;
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use context;
  30  use context_user;
  31  use core_privacy\local\metadata\collection;
  32  use core_privacy\local\request\approved_contextlist;
  33  use core_privacy\local\request\approved_userlist;
  34  use core_privacy\local\request\transform;
  35  use core_privacy\local\request\userlist;
  36  use core_privacy\local\request\writer;
  37  
  38  /**
  39   * Data provider class.
  40   *
  41   * @package    tool_messageinbound
  42   * @copyright  2018 Frédéric Massart
  43   * @author     Frédéric Massart <fred@branchup.tech>
  44   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   */
  46  class provider implements
  47      \core_privacy\local\metadata\provider,
  48      \core_privacy\local\request\core_userlist_provider,
  49      \core_privacy\local\request\plugin\provider {
  50  
  51      /**
  52       * Returns metadata.
  53       *
  54       * @param collection $collection The initialised collection to add items to.
  55       * @return collection A listing of user data stored through this system.
  56       */
  57      public static function get_metadata(collection $collection) : collection {
  58  
  59          $collection->add_database_table('messageinbound_messagelist', [
  60              'messageid' => 'privacy:metadata:messagelist:messageid',
  61              'userid' => 'privacy:metadata:messagelist:userid',
  62              'address' => 'privacy:metadata:messagelist:address',
  63              'timecreated' => 'privacy:metadata:messagelist:timecreated',
  64          ], 'privacy:metadata:messagelist');
  65  
  66          // Arguably the keys are handled by \core\message\inbound\address_manager and thus could/should be handled by core.
  67          $collection->add_subsystem_link('core_userkey', [], 'privacy:metadata:coreuserkey');
  68  
  69          return $collection;
  70      }
  71  
  72      /**
  73       * Get the list of contexts that contain user information for the specified user.
  74       *
  75       * @param int $userid The user to search.
  76       * @return \contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
  77       */
  78      public static function get_contexts_for_userid(int $userid) : \core_privacy\local\request\contextlist {
  79          $contextlist = new \core_privacy\local\request\contextlist();
  80  
  81          // Always add the user context so we're sure we're not dodging user keys, besides it's not costly to do so.
  82          $contextlist->add_user_context($userid);
  83  
  84          return $contextlist;
  85      }
  86  
  87      /**
  88       * Get the list of users who have data within a context.
  89       *
  90       * @param   userlist    $userlist   The userlist containing the list of users who have data in this context/plugin combination.
  91       */
  92      public static function get_users_in_context(userlist $userlist) {
  93          global $DB;
  94  
  95          $context = $userlist->get_context();
  96  
  97          if (!is_a($context, \context_user::class)) {
  98              return;
  99          }
 100  
 101          // Add user if any messagelist data exists.
 102          if ($DB->record_exists('messageinbound_messagelist', ['userid' => $context->instanceid])) {
 103              // Only using user context, so instance ID will be the only user ID.
 104              $userlist->add_user($context->instanceid);
 105          }
 106  
 107          // Add users based on userkey (since we also delete those).
 108          \core_userkey\privacy\provider::get_user_contexts_with_script($userlist, $context, 'messageinbound_handler');
 109      }
 110  
 111      /**
 112       * Export all user data for the specified user, in the specified contexts.
 113       *
 114       * @param approved_contextlist $contextlist The approved contexts to export information for.
 115       */
 116      public static function export_user_data(approved_contextlist $contextlist) {
 117          global $DB;
 118          if (!static::approved_contextlist_contains_my_context($contextlist)) {
 119              // We only care about the user's user context.
 120              return;
 121          }
 122  
 123          $userid = $contextlist->get_user()->id;
 124          $context = context_user::instance($userid);
 125          $path = [get_string('messageinbound', 'tool_messageinbound')];
 126  
 127          // Export user keys.
 128          \core_userkey\privacy\provider::export_userkeys($context, $path, 'messageinbound_handler');
 129  
 130          // Export the message list.
 131          $data = [];
 132          $recordset = $DB->get_recordset('messageinbound_messagelist', ['userid' => $userid], 'timecreated, id');
 133          foreach ($recordset as $record) {
 134              $data[] = [
 135                  'received_at' => $record->address,
 136                  'timecreated' => transform::datetime($record->timecreated),
 137              ];
 138          }
 139          $recordset->close();
 140          writer::with_context($context)->export_data($path, (object) ['messages_pending_validation' => $data]);
 141      }
 142  
 143      /**
 144       * Delete all data for all users in the specified context.
 145       *
 146       * @param context $context The specific context to delete data for.
 147       */
 148      public static function delete_data_for_all_users_in_context(context $context) {
 149          global $DB;
 150          if ($context->contextlevel != CONTEXT_USER) {
 151              return;
 152          }
 153  
 154          static::delete_user_data($context->instanceid);
 155      }
 156  
 157      /**
 158       * Delete all user data for the specified user, in the specified contexts.
 159       *
 160       * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
 161       */
 162      public static function delete_data_for_user(approved_contextlist $contextlist) {
 163          global $DB;
 164          if (!static::approved_contextlist_contains_my_context($contextlist)) {
 165              // We only care about the user's user context.
 166              return;
 167          }
 168  
 169          static::delete_user_data($contextlist->get_user()->id);
 170      }
 171  
 172      /**
 173       * Delete multiple users within a single context.
 174       *
 175       * @param   approved_userlist       $userlist The approved context and user information to delete information for.
 176       */
 177      public static function delete_data_for_users(approved_userlist $userlist) {
 178          $context = $userlist->get_context();
 179          $userids = $userlist->get_userids();
 180  
 181          // Since this falls within a user context, only that user should be valid.
 182          if ($context->contextlevel != CONTEXT_USER || count($userids) != 1 || $context->instanceid != $userids[0]) {
 183              return;
 184          }
 185  
 186          static::delete_user_data($userids[0]);
 187      }
 188  
 189      /**
 190       * Delete a user's data.
 191       *
 192       * @param int $userid The user ID.
 193       * @return void
 194       */
 195      protected static function delete_user_data($userid) {
 196          global $DB;
 197          $DB->delete_records_select('messageinbound_messagelist', 'userid = :userid', ['userid' => $userid]);
 198          \core_userkey\privacy\provider::delete_userkeys('messageinbound_handler', $userid);
 199      }
 200  
 201      /**
 202       * Return whether the contextlist contains our own context.
 203       *
 204       * @param approved_contextlist $contextlist The contextlist
 205       * @return bool
 206       */
 207      protected static function approved_contextlist_contains_my_context(approved_contextlist $contextlist) {
 208          $userid = $contextlist->get_user()->id;
 209          foreach ($contextlist->get_contexts() as $context) {
 210              if ($context->contextlevel == CONTEXT_USER && $context->instanceid == $userid) {
 211                  return true;
 212              }
 213          }
 214          return false;
 215      }
 216  
 217  }