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