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.

Differences Between: [Versions 400 and 402] [Versions 400 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   * Privacy class for requesting user data.
  18   *
  19   * @package    block_rss_client
  20   * @copyright  2018 Mihail Geshoski <mihail@moodle.com>
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  namespace block_rss_client\privacy;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use \core_privacy\local\metadata\collection;
  29  use \core_privacy\local\request\contextlist;
  30  use \core_privacy\local\request\approved_contextlist;
  31  use \core_privacy\local\request\userlist;
  32  use \core_privacy\local\request\approved_userlist;
  33  
  34  /**
  35   * Privacy class for requesting user data.
  36   *
  37   * @package    block_rss_client
  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          $collection->add_database_table('block_rss_client', [
  54              'userid' => 'privacy:metadata:block_rss_client:userid',
  55              'title' => 'privacy:metadata:block_rss_client:title',
  56              'preferredtitle' => 'privacy:metadata:block_rss_client:preferredtitle',
  57              'description' => 'privacy:metadata:block_rss_client:description',
  58              'shared' => 'privacy:metadata:block_rss_client:shared',
  59              'url' => 'privacy:metadata:block_rss_client:url',
  60              'skiptime' => 'privacy:metadata:block_rss_client:skiptime',
  61              'skipuntil' => 'privacy:metadata:block_rss_client:skipuntil',
  62          ], 'privacy:metadata:block_rss_client:tableexplanation');
  63          return $collection;
  64      }
  65  
  66      /**
  67       * Get the list of contexts that contain user information for the specified user.
  68       *
  69       * @param   int         $userid     The user to search.
  70       * @return  contextlist $contextlist  The contextlist containing the list of contexts used in this plugin.
  71       */
  72      public static function get_contexts_for_userid(int $userid) : contextlist {
  73          $sql = "SELECT ctx.id
  74                  FROM {block_rss_client} brc
  75                  JOIN {user} u
  76                      ON brc.userid = u.id
  77                  JOIN {context} ctx
  78                      ON ctx.instanceid = u.id
  79                          AND ctx.contextlevel = :contextlevel
  80                  WHERE brc.userid = :userid";
  81  
  82          $params = ['userid' => $userid, 'contextlevel' => CONTEXT_USER];
  83  
  84          $contextlist = new contextlist();
  85          $contextlist->add_from_sql($sql, $params);
  86          return $contextlist;
  87      }
  88  
  89      /**
  90       * Get the list of users within a specific context.
  91       *
  92       * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
  93       */
  94      public static function get_users_in_context(userlist $userlist) {
  95          $context = $userlist->get_context();
  96  
  97          if (!$context instanceof \context_user) {
  98              return;
  99          }
 100  
 101          $sql = "SELECT userid
 102                    FROM {block_rss_client}
 103                   WHERE userid = ?";
 104          $params = [$context->instanceid];
 105          $userlist->add_from_sql('userid', $sql, $params);
 106      }
 107  
 108      /**
 109       * Export all user data for the specified user, in the specified contexts.
 110       *
 111       * @param approved_contextlist $contextlist The approved contexts to export information for.
 112       */
 113      public static function export_user_data(approved_contextlist $contextlist) {
 114          $rssdata = [];
 115          $results = static::get_records($contextlist->get_user()->id);
 116          foreach ($results as $result) {
 117              $rssdata[] = (object) [
 118                  'title' => $result->title,
 119                  'preferredtitle' => $result->preferredtitle,
 120                  'description' => $result->description,
 121                  'shared' => \core_privacy\local\request\transform::yesno($result->shared),
 122                  'url' => $result->url
 123              ];
 124          }
 125          if (!empty($rssdata)) {
 126              $data = (object) [
 127                  'feeds' => $rssdata,
 128              ];
 129              \core_privacy\local\request\writer::with_context($contextlist->current())->export_data([
 130                      get_string('pluginname', 'block_rss_client')], $data);
 131          }
 132      }
 133  
 134      /**
 135       * Delete all use data which matches the specified deletion_criteria.
 136       *
 137       * @param   context $context A user context.
 138       */
 139      public static function delete_data_for_all_users_in_context(\context $context) {
 140          if ($context instanceof \context_user) {
 141              static::delete_data($context->instanceid);
 142          }
 143      }
 144  
 145      /**
 146       * Delete multiple users within a single context.
 147       *
 148       * @param approved_userlist $userlist The approved context and user information to delete information for.
 149       */
 150      public static function delete_data_for_users(approved_userlist $userlist) {
 151          $context = $userlist->get_context();
 152  
 153          if ($context instanceof \context_user) {
 154              static::delete_data($context->instanceid);
 155          }
 156      }
 157  
 158      /**
 159       * Delete all user data for the specified user, in the specified contexts.
 160       *
 161       * @param   approved_contextlist    $contextlist    The approved contexts and user information to delete information for.
 162       */
 163      public static function delete_data_for_user(approved_contextlist $contextlist) {
 164          static::delete_data($contextlist->get_user()->id);
 165      }
 166  
 167      /**
 168       * Delete data related to a userid.
 169       *
 170       * @param  int $userid The user ID
 171       */
 172      protected static function delete_data($userid) {
 173          global $DB;
 174  
 175          $DB->delete_records('block_rss_client', ['userid' => $userid]);
 176      }
 177  
 178      /**
 179       * Get records related to this plugin and user.
 180       *
 181       * @param  int $userid The user ID
 182       * @return array An array of records.
 183       */
 184      protected static function get_records($userid) {
 185          global $DB;
 186  
 187          return $DB->get_records('block_rss_client', ['userid' => $userid]);
 188      }
 189  }