Search moodle.org's
Developer Documentation

See Release Notes

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