Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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   * Privacy class for requesting user data.
  19   *
  20   * @package    core_rss
  21   * @copyright  2018 Sara Arjona <sara@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_rss\privacy;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core_privacy\local\metadata\collection;
  30  use core_privacy\local\request\contextlist;
  31  use core_privacy\local\request\approved_contextlist;
  32  use core_privacy\local\request\transform;
  33  use core_privacy\local\request\writer;
  34  use core_privacy\local\request\userlist;
  35  use core_privacy\local\request\approved_userlist;
  36  
  37  /**
  38   * Privacy class for requesting user data.
  39   *
  40   * @copyright  2018 Sara Arjona <sara@moodle.com>
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class provider implements
  44          \core_privacy\local\metadata\provider,
  45          \core_privacy\local\request\core_userlist_provider,
  46          \core_privacy\local\request\subsystem\provider {
  47  
  48      /**
  49       * Return the fields which contain personal data.
  50       *
  51       * @param   collection $collection The initialised collection to add items to.
  52       * @return  collection A listing of user data stored through this system.
  53       */
  54      public static function get_metadata(collection $collection) : collection {
  55          $collection->add_subsystem_link('core_userkey', [], 'privacy:metadata:core_userkey');
  56  
  57          return $collection;
  58      }
  59  
  60      /**
  61       * Get the list of contexts that contain user information for the specified user.
  62       *
  63       * @param int $userid The user to search.
  64       * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
  65       */
  66      public static function get_contexts_for_userid(int $userid) : contextlist {
  67          $sql = "SELECT ctx.id
  68                    FROM {user_private_key} k
  69                    JOIN {user} u ON k.userid = u.id
  70                    JOIN {context} ctx ON ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel
  71                   WHERE k.userid = :userid AND k.script = 'rss'";
  72  
  73          $params = ['userid' => $userid, 'contextlevel' => CONTEXT_USER];
  74  
  75          $contextlist = new contextlist();
  76          $contextlist->add_from_sql($sql, $params);
  77          return $contextlist;
  78      }
  79  
  80      /**
  81       * Get the list of users within a specific context.
  82       *
  83       * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
  84       */
  85      public static function get_users_in_context(userlist $userlist) {
  86          $context = $userlist->get_context();
  87  
  88          if (!$context instanceof \context_user) {
  89              return;
  90          }
  91  
  92          \core_userkey\privacy\provider::get_user_contexts_with_script($userlist, $context, 'rss');
  93      }
  94  
  95      /**
  96       * Export all user data for the specified user, in the specified contexts.
  97       *
  98       * @param approved_contextlist $contextlist The approved contexts to export information for.
  99       */
 100      public static function export_user_data(approved_contextlist $contextlist) {
 101          // If the user has data, then only the CONTEXT_USER should be present so get the first context.
 102          $contexts = $contextlist->get_contexts();
 103          if (count($contexts) == 0) {
 104              return;
 105          }
 106          $context = reset($contexts);
 107  
 108          // Sanity check that context is at the user context level, then get the userid.
 109          if ($context->contextlevel !== CONTEXT_USER) {
 110              return;
 111          }
 112  
 113          // Export associated userkeys.
 114          \core_userkey\privacy\provider::export_userkeys($context, [], 'rss');
 115      }
 116  
 117      /**
 118       * Delete all use data which matches the specified deletion_criteria.
 119       *
 120       * @param context $context A user context.
 121       */
 122      public static function delete_data_for_all_users_in_context(\context $context) {
 123          // Sanity check that context is at the user context level, then get the userid.
 124          if ($context->contextlevel !== CONTEXT_USER) {
 125              return;
 126          }
 127          $userid = $context->instanceid;
 128  
 129          // Delete all the userkeys.
 130          \core_userkey\privacy\provider::delete_userkeys('rss', $userid);
 131  
 132      }
 133  
 134      /**
 135       * Delete multiple users within a single context.
 136       *
 137       * @param approved_userlist $userlist The approved context and user information to delete information for.
 138       */
 139      public static function delete_data_for_users(approved_userlist $userlist) {
 140  
 141          $context = $userlist->get_context();
 142  
 143          if ($context instanceof \context_user) {
 144              \core_userkey\privacy\provider::delete_userkeys('rss', $context->instanceid);
 145          }
 146      }
 147  
 148      /**
 149       * Delete all user data for the specified user, in the specified contexts.
 150       *
 151       * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
 152       */
 153      public static function delete_data_for_user(approved_contextlist $contextlist) {
 154          // If the user has data, then only the user context should be present so get the first context.
 155          $contexts = $contextlist->get_contexts();
 156          if (count($contexts) == 0) {
 157              return;
 158          }
 159          $context = reset($contexts);
 160  
 161          // Sanity check that context is at the user context level, then get the userid.
 162          if ($context->contextlevel !== CONTEXT_USER) {
 163              return;
 164          }
 165          $userid = $context->instanceid;
 166          // Delete all the userkeys.
 167          \core_userkey\privacy\provider::delete_userkeys('rss', $userid);
 168      }
 169  }