Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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   * Privacy class for requesting user data.
  19   *
  20   * @package    profilefield_datetime
  21   * @copyright  2018 Mihail Geshoski <mihail@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace profilefield_datetime\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\userlist;
  34  use core_privacy\local\request\approved_userlist;
  35  
  36  /**
  37   * Privacy class for requesting user data.
  38   *
  39   * @copyright  2018 Mihail Geshoski <mihail@moodle.com>
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class provider implements
  43          \core_privacy\local\metadata\provider,
  44          \core_privacy\local\request\core_userlist_provider,
  45          \core_privacy\local\request\plugin\provider {
  46  
  47      /**
  48       * Returns meta data about this system.
  49       *
  50       * @param   collection $collection The initialised collection to add items to.
  51       * @return  collection A listing of user data stored through this system.
  52       */
  53      public static function get_metadata(collection $collection) : collection {
  54          return $collection->add_database_table('user_info_data', [
  55              'userid' => 'privacy:metadata:profilefield_datetime:userid',
  56              'fieldid' => 'privacy:metadata:profilefield_datetime:fieldid',
  57              'data' => 'privacy:metadata:profilefield_datetime:data',
  58              'dataformat' => 'privacy:metadata:profilefield_datetime:dataformat'
  59          ], 'privacy:metadata:profilefield_datetime:tableexplanation');
  60      }
  61  
  62      /**
  63       * Get the list of contexts that contain user information for the specified user.
  64       *
  65       * @param   int         $userid     The user to search.
  66       * @return  contextlist $contextlist  The contextlist containing the list of contexts used in this plugin.
  67       */
  68      public static function get_contexts_for_userid(int $userid) : contextlist {
  69          $sql = "SELECT ctx.id
  70                    FROM {user_info_data} uda
  71                    JOIN {user_info_field} uif ON uda.fieldid = uif.id
  72                    JOIN {context} ctx ON ctx.instanceid = uda.userid
  73                         AND ctx.contextlevel = :contextlevel
  74                   WHERE uda.userid = :userid
  75                         AND uif.datatype = :datatype";
  76          $params = [
  77              'userid' => $userid,
  78              'contextlevel' => CONTEXT_USER,
  79              'datatype' => 'datetime'
  80          ];
  81          $contextlist = new contextlist();
  82          $contextlist->add_from_sql($sql, $params);
  83  
  84          return $contextlist;
  85      }
  86  
  87      /**
  88       * Get the list of users within a specific 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          $context = $userlist->get_context();
  94  
  95          if (!$context instanceof \context_user) {
  96              return;
  97          }
  98  
  99          $sql = "SELECT uda.userid
 100                    FROM {user_info_data} uda
 101                    JOIN {user_info_field} uif
 102                         ON uda.fieldid = uif.id
 103                   WHERE uda.userid = :userid
 104                         AND uif.datatype = :datatype";
 105  
 106          $params = [
 107              'userid' => $context->instanceid,
 108              'datatype' => 'datetime'
 109          ];
 110  
 111          $userlist->add_from_sql('userid', $sql, $params);
 112      }
 113  
 114      /**
 115       * Export all user data for the specified user, in the specified contexts.
 116       *
 117       * @param approved_contextlist $contextlist The approved contexts to export information for.
 118       */
 119      public static function export_user_data(approved_contextlist $contextlist) {
 120          $user = $contextlist->get_user();
 121          foreach ($contextlist->get_contexts() as $context) {
 122              // Check if the context is a user context.
 123              if ($context->contextlevel == CONTEXT_USER && $context->instanceid == $user->id) {
 124                  $results = static::get_records($user->id);
 125                  foreach ($results as $result) {
 126                      $data = (object) [
 127                          'name' => $result->name,
 128                          'description' => $result->description,
 129                          'data' => transform::date($result->data)
 130                      ];
 131                      \core_privacy\local\request\writer::with_context($context)->export_data([
 132                          get_string('pluginname', 'profilefield_datetime')], $data);
 133                  }
 134              }
 135          }
 136      }
 137  
 138      /**
 139       * Delete all user data which matches the specified context.
 140       *
 141       * @param   context $context A user context.
 142       */
 143      public static function delete_data_for_all_users_in_context(\context $context) {
 144          // Delete data only for user context.
 145          if ($context->contextlevel == CONTEXT_USER) {
 146              static::delete_data($context->instanceid);
 147          }
 148      }
 149  
 150      /**
 151       * Delete multiple users within a single context.
 152       *
 153       * @param approved_userlist $userlist The approved context and user information to delete information for.
 154       */
 155      public static function delete_data_for_users(approved_userlist $userlist) {
 156          $context = $userlist->get_context();
 157  
 158          if ($context instanceof \context_user) {
 159              static::delete_data($context->instanceid);
 160          }
 161      }
 162  
 163      /**
 164       * Delete all user data for the specified user, in the specified contexts.
 165       *
 166       * @param   approved_contextlist    $contextlist    The approved contexts and user information to delete information for.
 167       */
 168      public static function delete_data_for_user(approved_contextlist $contextlist) {
 169          $user = $contextlist->get_user();
 170          foreach ($contextlist->get_contexts() as $context) {
 171              // Check if the context is a user context.
 172              if ($context->contextlevel == CONTEXT_USER && $context->instanceid == $user->id) {
 173                  static::delete_data($context->instanceid);
 174              }
 175          }
 176      }
 177  
 178      /**
 179       * Delete data related to a userid.
 180       *
 181       * @param  int $userid The user ID
 182       */
 183      protected static function delete_data($userid) {
 184          global $DB;
 185  
 186          $params = [
 187              'userid' => $userid,
 188              'datatype' => 'datetime'
 189          ];
 190  
 191          $DB->delete_records_select('user_info_data', "fieldid IN (
 192                  SELECT id FROM {user_info_field} WHERE datatype = :datatype)
 193                  AND userid = :userid", $params);
 194      }
 195  
 196      /**
 197       * Get records related to this plugin and user.
 198       *
 199       * @param  int $userid The user ID
 200       * @return array An array of records.
 201       */
 202      protected static function get_records($userid) {
 203          global $DB;
 204  
 205          $sql = "SELECT *
 206                    FROM {user_info_data} uda
 207                    JOIN {user_info_field} uif ON uda.fieldid = uif.id
 208                   WHERE uda.userid = :userid
 209                         AND uif.datatype = :datatype";
 210          $params = [
 211              'userid' => $userid,
 212              'datatype' => 'datetime'
 213          ];
 214  
 215          return $DB->get_records_sql($sql, $params);
 216      }
 217  }