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   * Privacy Subsystem implementation for tool_mobile.
  18   *
  19   * @package    tool_mobile
  20   * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  namespace tool_mobile\privacy;
  24  defined('MOODLE_INTERNAL') || die();
  25  use core_privacy\local\request\writer;
  26  use core_privacy\local\metadata\collection;
  27  use core_privacy\local\request\contextlist;
  28  use core_privacy\local\request\approved_contextlist;
  29  use core_privacy\local\request\approved_userlist;
  30  use core_privacy\local\request\transform;
  31  use core_privacy\local\request\userlist;
  32  
  33  /**
  34   * Privacy provider for tool_mobile.
  35   *
  36   * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class provider implements
  40      \core_privacy\local\metadata\provider,
  41      \core_privacy\local\request\core_userlist_provider,
  42      \core_privacy\local\request\user_preference_provider,
  43      \core_privacy\local\request\plugin\provider {
  44      /**
  45       * Returns meta data about this system.
  46       *
  47       * @param   collection     $collection The initialised item collection to add items to.
  48       * @return  collection     A listing of user data stored through this system.
  49       */
  50      public static function get_metadata(collection $collection) : collection {
  51          // There is a one user preference.
  52          $collection->add_user_preference('tool_mobile_autologin_request_last',
  53              'privacy:metadata:preference:tool_mobile_autologin_request_last');
  54          $collection->add_subsystem_link('core_userkey', [], 'privacy:metadata:core_userkey');
  55  
  56          return $collection;
  57      }
  58      /**
  59       * Get the list of contexts that contain user information for the specified user.
  60       *
  61       * @param int $userid The user to search.
  62       * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
  63       */
  64      public static function get_contexts_for_userid(int $userid) : contextlist {
  65          $sql = "SELECT ctx.id
  66                    FROM {user_private_key} k
  67                    JOIN {user} u ON k.userid = u.id
  68                    JOIN {context} ctx ON ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel
  69                   WHERE k.userid = :userid AND k.script = 'tool_mobile'";
  70          $params = ['userid' => $userid, 'contextlevel' => CONTEXT_USER];
  71          $contextlist = new contextlist();
  72          $contextlist->add_from_sql($sql, $params);
  73  
  74          return $contextlist;
  75      }
  76  
  77      /**
  78       * Get the list of users who have data within a context.
  79       *
  80       * @param   userlist    $userlist   The userlist containing the list of users who have data in this context/plugin combination.
  81       */
  82      public static function get_users_in_context(userlist $userlist) {
  83          $context = $userlist->get_context();
  84  
  85          if (!is_a($context, \context_user::class)) {
  86              return;
  87          }
  88  
  89          // Add users based on userkey.
  90          \core_userkey\privacy\provider::get_user_contexts_with_script($userlist, $context, 'tool_mobile');
  91      }
  92  
  93      /**
  94       * Export all user data for the specified user, in the specified contexts.
  95       *
  96       * @param approved_contextlist $contextlist The approved contexts to export information for.
  97       */
  98      public static function export_user_data(approved_contextlist $contextlist) {
  99          // If the user has data, then only the CONTEXT_USER should be present so get the first context.
 100          $contexts = $contextlist->get_contexts();
 101          if (count($contexts) == 0) {
 102              return;
 103          }
 104          $context = reset($contexts);
 105          // Sanity check that context is at the user context level, then get the userid.
 106          if ($context->contextlevel !== CONTEXT_USER) {
 107              return;
 108          }
 109          // Export associated userkeys.
 110          \core_userkey\privacy\provider::export_userkeys($context, [], 'tool_mobile');
 111      }
 112      /**
 113       * Export all user preferences for the plugin.
 114       *
 115       * @param   int         $userid The userid of the user whose data is to be exported.
 116       */
 117      public static function export_user_preferences(int $userid) {
 118          $autologinrequestlast = get_user_preferences('tool_mobile_autologin_request_last', null, $userid);
 119          if ($autologinrequestlast !== null) {
 120              $time = transform::datetime($autologinrequestlast);
 121              writer::export_user_preference('tool_mobile',
 122                  'tool_mobile_autologin_request_last',
 123                  $time,
 124                  get_string('privacy:metadata:preference:tool_mobile_autologin_request_last', 'tool_mobile')
 125              );
 126          }
 127      }
 128      /**
 129       * Delete all use data which matches the specified deletion_criteria.
 130       *
 131       * @param context $context A user context.
 132       */
 133      public static function delete_data_for_all_users_in_context(\context $context) {
 134          // Sanity check that context is at the user context level, then get the userid.
 135          if ($context->contextlevel !== CONTEXT_USER) {
 136              return;
 137          }
 138          $userid = $context->instanceid;
 139          // Delete all the userkeys.
 140          \core_userkey\privacy\provider::delete_userkeys('tool_mobile', $userid);
 141      }
 142      /**
 143       * Delete all user data for the specified user, in the specified contexts.
 144       *
 145       * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
 146       */
 147      public static function delete_data_for_user(approved_contextlist $contextlist) {
 148          // If the user has data, then only the user context should be present so get the first context.
 149          $contexts = $contextlist->get_contexts();
 150          if (count($contexts) == 0) {
 151              return;
 152          }
 153          $context = reset($contexts);
 154          // Sanity check that context is at the user context level, then get the userid.
 155          if ($context->contextlevel !== CONTEXT_USER) {
 156              return;
 157          }
 158          $userid = $context->instanceid;
 159          // Delete all the userkeys.
 160          \core_userkey\privacy\provider::delete_userkeys('tool_mobile', $userid);
 161      }
 162  
 163      /**
 164       * Delete multiple users within a single context.
 165       *
 166       * @param   approved_userlist       $userlist The approved context and user information to delete information for.
 167       */
 168      public static function delete_data_for_users(approved_userlist $userlist) {
 169          global $DB;
 170          $context = $userlist->get_context();
 171          $userids = $userlist->get_userids();
 172          $userid = reset($userids);
 173  
 174          // Only deleting data for the user ID in that user's user context should be valid.
 175          if ($context->contextlevel !== CONTEXT_USER || count($userids) != 1 || $userid != $context->instanceid) {
 176              return;
 177          }
 178  
 179          // Delete all the userkeys.
 180          \core_userkey\privacy\provider::delete_userkeys('tool_mobile', $userid);
 181      }
 182  }