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.
   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 Subsystem implementation for block_comments.
  19   *
  20   * @package    block_comments
  21   * @category   privacy
  22   * @copyright  2018 Shamim Rezaie <shamim@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace block_comments\privacy;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  use core_privacy\local\metadata\collection;
  31  use core_privacy\local\request\approved_contextlist;
  32  use core_privacy\local\request\contextlist;
  33  use core_privacy\local\request\userlist;
  34  use core_privacy\local\request\approved_userlist;
  35  
  36  /**
  37   * Privacy Subsystem implementation for block_comments.
  38   *
  39   * @copyright  2018 Shamim Rezaie <shamim@moodle.com>
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class provider implements
  43          // The block_comments block stores user provided data.
  44          \core_privacy\local\metadata\provider,
  45          \core_privacy\local\request\core_userlist_provider,
  46          // The block_comments block provides data directly to core.
  47          \core_privacy\local\request\plugin\provider {
  48  
  49      /**
  50       * Returns meta data about this system.
  51       *
  52       * @param collection $collection
  53       * @return collection
  54       */
  55      public static function get_metadata(collection $collection) : collection {
  56          return $collection->add_subsystem_link('core_comment', [], 'privacy:metadata:core_comment');
  57      }
  58  
  59      /**
  60       * Get the list of contexts that contain user information for the specified user.
  61       *
  62       * @param int $userid
  63       * @return contextlist
  64       */
  65      public static function get_contexts_for_userid(int $userid) : contextlist {
  66          $contextlist = new contextlist();
  67  
  68          $sql = "SELECT contextid
  69                    FROM {comments}
  70                   WHERE component = :component
  71                     AND userid = :userid";
  72          $params = [
  73              'component' => 'block_comments',
  74              'userid' => $userid
  75          ];
  76  
  77          $contextlist->add_from_sql($sql, $params);
  78  
  79          return $contextlist;
  80      }
  81  
  82      /**
  83       * Get the list of users within a specific context.
  84       *
  85       * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
  86       */
  87      public static function get_users_in_context(userlist $userlist) {
  88          $context = $userlist->get_context();
  89  
  90          $params = [
  91              'contextid' => $context->id,
  92              'component' => 'block_comments',
  93          ];
  94  
  95          $sql = "SELECT userid as userid
  96                    FROM {comments}
  97                   WHERE component = :component
  98                         AND contextid = :contextid";
  99  
 100          $userlist->add_from_sql('userid', $sql, $params);
 101      }
 102  
 103      /**
 104       * Export all user data for the specified user, in the specified contexts.
 105       *
 106       * @param approved_contextlist $contextlist
 107       */
 108      public static function export_user_data(approved_contextlist $contextlist) {
 109          $contexts = $contextlist->get_contexts();
 110          foreach ($contexts as $context) {
 111              \core_comment\privacy\provider::export_comments(
 112                      $context,
 113                      'block_comments',
 114                      'page_comments',
 115                      0,
 116                      []
 117              );
 118          }
 119      }
 120  
 121      /**
 122       * Delete all data for all users in the specified context.
 123       *
 124       * @param \context $context
 125       */
 126      public static function delete_data_for_all_users_in_context(\context $context) {
 127          \core_comment\privacy\provider::delete_comments_for_all_users($context, 'block_comments');
 128      }
 129  
 130      /**
 131       * Delete multiple users within a single context.
 132       *
 133       * @param approved_userlist $userlist The approved context and user information to delete information for.
 134       */
 135      public static function delete_data_for_users(approved_userlist $userlist) {
 136          \core_comment\privacy\provider::delete_comments_for_users($userlist, 'block_comments');
 137      }
 138  
 139      /**
 140       * Delete all user data for the specified user, in the specified contexts.
 141       *
 142       * @param approved_contextlist $contextlist
 143       */
 144      public static function delete_data_for_user(approved_contextlist $contextlist) {
 145          \core_comment\privacy\provider::delete_comments_for_user($contextlist, 'block_comments');
 146      }
 147  }