Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 311 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 Subsystem implementation for enrol_fee.
  19   *
  20   * @package    enrol_fee
  21   * @category   privacy
  22   * @copyright  2020 Shamim Rezaie <shamim@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace enrol_fee\privacy;
  27  
  28  use core_privacy\local\request\approved_contextlist;
  29  use core_privacy\local\request\approved_userlist;
  30  use core_privacy\local\request\transform;
  31  use core_privacy\local\request\userlist;
  32  use core_privacy\local\request\writer;
  33  use core_payment\helper as payment_helper;
  34  
  35  /**
  36   * Privacy Subsystem for enrol_fee implementing null_provider.
  37   *
  38   * @copyright  2020 Shamim Rezaie <shamim@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\null_provider,
  43      \core_payment\privacy\consumer_provider
  44  {
  45      /**
  46       * Get the language string identifier with the component's language
  47       * file to explain why this plugin stores no data.
  48       *
  49       * @return  string
  50       */
  51      public static function get_reason(): string {
  52          return 'privacy:metadata';
  53      }
  54  
  55      public static function get_contextid_for_payment(string $paymentarea, int $itemid): ?int {
  56          global $DB;
  57  
  58          $sql = "SELECT ctx.id
  59                    FROM {enrol} e
  60                    JOIN {context} ctx ON (e.courseid = ctx.instanceid AND ctx.contextlevel = :contextcourse)
  61                   WHERE e.id = :enrolid AND e.enrol = :enrolname";
  62          $params = [
  63              'contextcourse' => CONTEXT_COURSE,
  64              'enrolid' => $itemid,
  65              'enrolname' => 'fee',
  66          ];
  67          $contextid = $DB->get_field_sql($sql, $params);
  68  
  69          return $contextid ?: null;
  70      }
  71  
  72      public static function get_users_in_context(userlist $userlist) {
  73          $context = $userlist->get_context();
  74  
  75          if ($context instanceof \context_course) {
  76              $sql = "SELECT p.userid
  77                        FROM {payments} p
  78                        JOIN {enrol} e ON (p.component = :component AND p.itemid = e.id)
  79                       WHERE e.courseid = :courseid";
  80              $params = [
  81                  'component' => 'enrol_fee',
  82                  'courseid' => $context->instanceid,
  83              ];
  84              $userlist->add_from_sql('userid', $sql, $params);
  85          } else if ($context instanceof \context_system) {
  86              // If context is system, then the enrolment belongs to a deleted enrolment.
  87              $sql = "SELECT p.userid
  88                        FROM {payments} p
  89                   LEFT JOIN {enrol} e ON p.itemid = e.id
  90                       WHERE p.component = :component AND e.id IS NULL";
  91              $params = [
  92                  'component' => 'enrol_fee',
  93              ];
  94              $userlist->add_from_sql('userid', $sql, $params);
  95          }
  96      }
  97  
  98      /**
  99       * Export all user data for the specified user, in the specified contexts.
 100       *
 101       * @param approved_contextlist $contextlist The approved contexts to export information for.
 102       */
 103      public static function export_user_data(approved_contextlist $contextlist) {
 104          global $DB;
 105  
 106          $subcontext = [
 107              get_string('pluginname', 'enrol_fee'),
 108          ];
 109          foreach ($contextlist as $context) {
 110              if (!$context instanceof \context_course) {
 111                  continue;
 112              }
 113              $feeplugins = $DB->get_records('enrol', ['courseid' => $context->instanceid, 'enrol' => 'fee']);
 114  
 115              foreach ($feeplugins as $feeplugin) {
 116                  \core_payment\privacy\provider::export_payment_data_for_user_in_context(
 117                      $context,
 118                      $subcontext,
 119                      $contextlist->get_user()->id,
 120                      'enrol_fee',
 121                      'fee',
 122                      $feeplugin->id
 123                  );
 124              }
 125          }
 126  
 127          if (in_array(SYSCONTEXTID, $contextlist->get_contextids())) {
 128              // Orphaned payments.
 129              $sql = "SELECT p.*
 130                        FROM {payments} p
 131                   LEFT JOIN {enrol} e ON p.itemid = e.id
 132                       WHERE p.userid = :userid AND p.component = :component AND e.id IS NULL";
 133              $params = [
 134                  'component' => 'enrol_fee',
 135                  'userid' => $contextlist->get_user()->id,
 136              ];
 137  
 138              $orphanedpayments = $DB->get_recordset_sql($sql, $params);
 139              foreach ($orphanedpayments as $payment) {
 140                  \core_payment\privacy\provider::export_payment_data_for_user_in_context(
 141                      \context_system::instance(),
 142                      $subcontext,
 143                      $payment->userid,
 144                      $payment->component,
 145                      $payment->paymentarea,
 146                      $payment->itemid
 147                  );
 148              }
 149              $orphanedpayments->close();
 150          }
 151      }
 152  
 153      /**
 154       * Delete all data for all users in the specified context.
 155       *
 156       * @param context $context The specific context to delete data for.
 157       */
 158      public static function delete_data_for_all_users_in_context(\context $context) {
 159          if ($context instanceof \context_course) {
 160              $sql = "SELECT p.id
 161                        FROM {payments} p
 162                        JOIN {enrol} e ON (p.component = :component AND p.itemid = e.id)
 163                       WHERE e.courseid = :courseid";
 164              $params = [
 165                  'component' => 'enrol_fee',
 166                  'courseid' => $context->instanceid,
 167              ];
 168  
 169              \core_payment\privacy\provider::delete_data_for_payment_sql($sql, $params);
 170          } else if ($context instanceof \context_system) {
 171              // If context is system, then the enrolment belongs to a deleted enrolment.
 172              $sql = "SELECT p.id
 173                        FROM {payments} p
 174                   LEFT JOIN {enrol} e ON p.itemid = e.id
 175                       WHERE p.component = :component AND e.id IS NULL";
 176              $params = [
 177                  'component' => 'enrol_fee',
 178              ];
 179  
 180              \core_payment\privacy\provider::delete_data_for_payment_sql($sql, $params);
 181          }
 182      }
 183  
 184      /**
 185       * Delete all user data for the specified user, in the specified contexts.
 186       *
 187       * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
 188       */
 189      public static function delete_data_for_user(approved_contextlist $contextlist) {
 190          global $DB;
 191  
 192          if (empty($contextlist->count())) {
 193              return;
 194          }
 195  
 196          $contexts = $contextlist->get_contexts();
 197  
 198          $courseids = [];
 199          foreach ($contexts as $context) {
 200              if ($context instanceof \context_course) {
 201                  $courseids[] = $context->instanceid;
 202              }
 203          }
 204  
 205          [$insql, $inparams] = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED);
 206  
 207          $sql = "SELECT p.id
 208                    FROM {payments} p
 209                    JOIN {enrol} e ON (p.component = :component AND p.itemid = e.id)
 210                   WHERE p.userid = :userid AND e.courseid $insql";
 211          $params = $inparams + [
 212              'component' => 'enrol_fee',
 213              'userid' => $contextlist->get_user()->id,
 214          ];
 215  
 216          \core_payment\privacy\provider::delete_data_for_payment_sql($sql, $params);
 217  
 218          if (in_array(SYSCONTEXTID, $contextlist->get_contextids())) {
 219              // Orphaned payments.
 220              $sql = "SELECT p.id
 221                        FROM {payments} p
 222                   LEFT JOIN {enrol} e ON p.itemid = e.id
 223                       WHERE p.component = :component AND p.userid = :userid AND e.id IS NULL";
 224              $params = [
 225                  'component' => 'enrol_fee',
 226                  'userid' => $contextlist->get_user()->id,
 227              ];
 228  
 229              \core_payment\privacy\provider::delete_data_for_payment_sql($sql, $params);
 230          }
 231      }
 232  
 233      /**
 234       * Delete multiple users within a single context.
 235       *
 236       * @param approved_userlist $userlist The approved context and user information to delete information for.
 237       */
 238      public static function delete_data_for_users(approved_userlist $userlist) {
 239          global $DB;
 240  
 241          $context = $userlist->get_context();
 242  
 243          if ($context instanceof \context_course) {
 244              [$usersql, $userparams] = $DB->get_in_or_equal($userlist->get_userids(), SQL_PARAMS_NAMED);
 245              $sql = "SELECT p.id
 246                        FROM {payments} p
 247                        JOIN {enrol} e ON (p.component = :component AND p.itemid = e.id)
 248                       WHERE e.courseid = :courseid AND p.userid $usersql";
 249              $params = $userparams + [
 250                  'component' => 'enrol_fee',
 251                  'courseid' => $context->instanceid,
 252              ];
 253  
 254              \core_payment\privacy\provider::delete_data_for_payment_sql($sql, $params);
 255          } else if ($context instanceof \context_system) {
 256              // Orphaned payments.
 257              [$usersql, $userparams] = $DB->get_in_or_equal($userlist->get_userids(), SQL_PARAMS_NAMED);
 258              $sql = "SELECT p.id
 259                        FROM {payments} p
 260                   LEFT JOIN {enrol} e ON p.itemid = e.id
 261                       WHERE p.component = :component AND p.userid $usersql AND e.id IS NULL";
 262              $params = $userparams + [
 263                  'component' => 'enrol_fee',
 264              ];
 265  
 266              \core_payment\privacy\provider::delete_data_for_payment_sql($sql, $params);
 267          }
 268      }
 269  }