Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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    message_airnotifier
  21   * @copyright  2018 Adrian Greeve <adrian@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace message_airnotifier\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   * @package    message_airnotifier
  40   * @copyright  2018 Adrian Greeve <adrian@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\plugin\provider {
  47  
  48      /**
  49       * Returns meta data about this system.
  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->link_subsystem('core_user', 'privacy:metadata:usersubsystem');
  56          $collection->add_database_table('message_airnotifier_devices', [
  57                  'userdeviceid' => 'privacy:metadata:userdeviceid',
  58                  'enabled' => 'privacy:metadata:enabled'
  59              ], 'privacy:metadata:tableexplanation');
  60          $collection->link_external_location('External airnotifier site.', [
  61                  'userid' => 'privacy:metadata:userid',
  62                  'username' => 'privacy:metadata:username',
  63                  'userfromid' => 'privacy:metadata:userfromid',
  64                  'userfromfullname' => 'privacy:metadata:userfromfullname',
  65                  'date' => 'privacy:metadata:date',
  66                  'subject' => 'privacy:metadata:subject',
  67                  'notification' => 'privacy:metadata:notification',
  68                  'smallmessage' => 'privacy:metadata:smallmessage',
  69                  'fullmessage' => 'privacy:metadata:fullmessage'
  70          ], 'privacy:metadata:externalpurpose');
  71          // This system is unaware of user preferences such as message_provider_moodle_instantmessage_loggedin.
  72          return $collection;
  73      }
  74  
  75      /**
  76       * Get the list of contexts that contain user information for the specified user.
  77       *
  78       * @param   int         $userid     The user to search.
  79       * @return  contextlist $contextlist  The contextlist containing the list of contexts used in this plugin.
  80       */
  81      public static function get_contexts_for_userid(int $userid) : contextlist {
  82          $sql = "SELECT ctx.id
  83                    FROM {message_airnotifier_devices} mad
  84                    JOIN {user_devices} ud ON ud.id = mad.userdeviceid
  85                    JOIN {user} u ON ud.userid = u.id
  86                    JOIN {context} ctx ON ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel
  87                   WHERE ud.userid = :userid";
  88  
  89          $params = ['userid' => $userid, 'contextlevel' => CONTEXT_USER];
  90  
  91          $contextlist = new contextlist();
  92          $contextlist->add_from_sql($sql, $params);
  93          return $contextlist;
  94      }
  95  
  96      /**
  97       * Get the list of users within a specific context.
  98       *
  99       * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
 100       */
 101      public static function get_users_in_context(userlist $userlist) {
 102          $context = $userlist->get_context();
 103  
 104          if (!$context instanceof \context_user) {
 105              return;
 106          }
 107  
 108          $sql = "SELECT ud.userid
 109                    FROM {message_airnotifier_devices} mad
 110                    JOIN {user_devices} ud ON ud.id = mad.userdeviceid
 111                   WHERE ud.userid = ?";
 112          $params = [$context->instanceid];
 113          $userlist->add_from_sql('userid', $sql, $params);
 114      }
 115  
 116      /**
 117       * Export all user data for the specified user, in the specified contexts.
 118       *
 119       * @param approved_contextlist $contextlist The approved contexts to export information for.
 120       */
 121      public static function export_user_data(approved_contextlist $contextlist) {
 122          $results = static::get_records($contextlist->get_user()->id);
 123          $context = $contextlist->current();
 124          foreach ($results as $result) {
 125              $data = (object)[
 126                  'appid' => $result->appid,
 127                  'pushid' => $result->pushid,
 128                  'name' => $result->name,
 129                  'model' => $result->model,
 130                  'platform' => $result->platform,
 131                  'version' => $result->version,
 132                  'timecreated' => transform::datetime($result->timecreated),
 133                  'timemodified' => transform::datetime($result->timemodified),
 134                  'enabled' => transform::yesno($result->enable)
 135              ];
 136              \core_privacy\local\request\writer::with_context($context)->export_data([
 137                      get_string('privacy:subcontext', 'message_airnotifier'),
 138                      $result->model . '_' . $result->pushid
 139                  ], $data);
 140          }
 141      }
 142  
 143      /**
 144       * Delete all use data which matches the specified deletion_criteria.
 145       *
 146       * @param context $context A context.
 147       */
 148      public static function delete_data_for_all_users_in_context(\context $context) {
 149  
 150          if (!$context instanceof \context_user) {
 151              return;
 152          }
 153  
 154          static::delete_data($context->instanceid);
 155      }
 156  
 157      /**
 158       * Delete multiple users within a single context.
 159       *
 160       * @param approved_userlist $userlist The approved context and user information to delete information for.
 161       */
 162      public static function delete_data_for_users(approved_userlist $userlist) {
 163          $context = $userlist->get_context();
 164  
 165          if ($context instanceof \context_user) {
 166              static::delete_data($context->instanceid);
 167          }
 168      }
 169  
 170      /**
 171       * Delete all user data for the specified user, in the specified contexts.
 172       *
 173       * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
 174       */
 175      public static function delete_data_for_user(approved_contextlist $contextlist) {
 176          static::delete_data($contextlist->get_user()->id);
 177      }
 178  
 179      /**
 180       * Delete data related to a userid.
 181       *
 182       * @param int $userid The user ID
 183       */
 184      protected static function delete_data(int $userid) {
 185          global $DB;
 186  
 187          foreach (static::get_records($userid) as $record) {
 188              $DB->delete_records('message_airnotifier_devices', ['id' => $record->id]);
 189          }
 190      }
 191  
 192      /**
 193       * Get records related to this plugin and user.
 194       *
 195       * @param  int $userid The user ID
 196       * @return array An array of records.
 197       */
 198      protected static function get_records(int $userid) : array {
 199          global $DB;
 200          $sql = "SELECT mad.id, mad.enable, ud.appid, ud.name, ud.model, ud.platform, ud.version, ud.timecreated, ud.timemodified,
 201                          ud.pushid
 202                  FROM {message_airnotifier_devices} mad
 203                  JOIN {user_devices} ud ON mad.userdeviceid = ud.id
 204                  WHERE ud.userid = :userid";
 205          $params = ['userid' => $userid];
 206          return $DB->get_records_sql($sql, $params);
 207      }
 208  }